Image for post
Image for post
Design by Sachin Kumar

Configuring a Webserver on the Top of Docker Using Ansible

Let’s see how we can configure an Apache Web Server on the top of Docker in any managed node with help of the Redhat Automation tool Ansible.

Sachin Kumar
Nov 27, 2020 · 13 min read

Before configuring the Setup I want to introduce you to Ansible and Apache web server and docker so that you have a little bit of idea about these tools and you can easily understand why we are using the Redhat Automation tool to configure the webserver on the docker engine in the managed node and how Ansible will help to automate the configuration of the webserver in the docker. So without any delay let’s jump into the introduction part of both tools.

Redhat Ansible

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intra-service orchestration, and provisioning. Automation is crucial these days, with IT environments that are too complex and often need to scale too quickly for system administrators and developers to keep up if they had to do everything manually. Automation simplifies complex tasks, not just making developers’ jobs more manageable but allowing them to focus attention on other tasks that add value to an organization. In other words, it frees up time and increases efficiency. And Ansible, as noted above, is rapidly rising to the top in the world of automation tools. Let’s look at some of the reasons for Ansible’s popularity.

Apache Web Server

Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web. It’s widely thought that Apache gets its name from its development history and process of improvement through applied patches and modules but that was corrected back in 2000. It was revealed that the name originated from the respect of the Native American tribe for its resiliency and durability.

Docker

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Now hopefully you have an idea about the above-mentioned tools and why they are very useful in the DevOps World. So Now let’s see which steps we have to follow to configure this setup that is mentioned in the problem statement.

Problem Statement

Write an Ansible PlayBook that does the following operations in the managed nodes:

  1. Configure Docker.
  2. Start and enable Docker services.
  3. Pull the httpd(Apache) server image from the Docker Hub.
  4. Run the docker container and expose it to the public.
  5. Copy the html code in /var/www/html directory and start the webserver.

Note: I am setting up all the environments on the AWS Redhat Instance but you can set up this at any operating system i.e virtual machine (VM) Bare metal as well as a container.

Hopefully, now you have clear what we are going to perform. So let’s see one by one all the steps and what are the pre-requirements to set up the webserver in the docker container using ansible.

Prerequisite

We have to need the following tools and services to set up the webserver.

  1. Operating System i.e AWS instance, Container, Bare metal, or VM.
  2. Redhat Ansible
  3. Docker
  4. Apache WebServer

Now All are set so let’s see how to install the ansible on the AWS Redhat Instance and the same approach will work on VM etc.

Installation of Ansible

If you want to installl Ansible on the VM or instance then please launch your operating system. But I am using the AWS Redhat instance and also skipping the launching part of Instance. If you are not comfortable with the instance then you can see the mentioned Article.

Before Installing Ansible you have to install python first because this is the pre-requisite for Ansible installation. So, install the python using the below-mentioned command.

sudo dnf install python3 -y
Image for post
Image for post
python installation

Now we can install the Ansible Automation tool using the below-mentioned command on the controller node.

pip3 install ansible
Image for post
Image for post

After running the above command you can check it is installed or not using the below command.

ansible --version
Image for post
Image for post

Note: You are noticing that the config filehas none in the above screenshot so we have to replace none with an inventory. Now let’s see how we can replace it.

Now Ansible has installed in the controller node but we will manage the managed node with the help of the controller node which has the Ansible tool’s power so we have to access the managed nodes to install or anything whatever you want. For whom we have to install sshpass package. But before install this package we have to install its rpm file. So, run the below-mentioned command for rpm of sshpass.

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Image for post
Image for post

After installing rpm of sshpass you can install this package easily using the below command.

sudo dnf install sshpass -y
Image for post
Image for post

After it, we have also to upload the key pair of the managed node in the controller node because we are using the AWS instance and if you want to log in with the instance then you have private key pair of it. So, upload the key-pair of the managed node into the controller node using the below-mentioned command.

scp -i ./your_key_pair.pem -r ./keypair_directory/* ec2-user@pubic_ip_of_controller_node
Image for post
Image for post

After uploading the key pair, give permission for the key pair using the below command.

chmod 400 your_key_pair.pem
Image for post
Image for post

Now everything is set so we have to create a file of the information of the managed node which will help to log into the managed node. So make a file for it using the below command.

vim ip.txt
Image for post
Image for post

After running vim ip.txt, write the public IP of the managed node as well as username also as mention in the below screenshot.

Image for post
Image for post

There are other 3 fields or variables that can be seen in this file

ansible_user — This defines the target node username. In AWS EC2 Instance, the by default username for each EC2 Instances (except Windows) is “ec2-user”.

ansible_ssh_pass — This defines the private key file which is used for authentication when needed (Because EC2 Instance doesn’t support direct passwords, we need to use Private Key File instead of Password to connect Target Node (EC2 Instance) for Ansible. I’ve used “key.pem” which is located at the “/home/ec2-user/” custom directory (the directory can be changed according to the user)

ansible_connection = This defines the connection type between Managed nodes and the control nodes. I’ve used “ssh” as the Connection type.

After saving the above file with the help of escthen press :wqnow check it out our managed node is configured not using the below command.

ansible all --list-hosts
Image for post
Image for post

But you can check out in the above screenshot no host is configured. Now we have to create an inventory for configuring the managed node. But before it, we have to make a /etc/ansibledirectory for the inventory and go inside the ansible.cfg file using the below-mentioned command.

sudo vim /etc/ansible/ansible.cfg
Image for post
Image for post

After opening the ansible.cfg file, write the code inside the directory which is mentioned in the screenshot, and save it.

Image for post
Image for post

Now the inventory is ready for use so let’s check it working or not using the below command.

ansible all --list-hosts
Image for post
Image for post

Now you can see in the above screenshot, the controller node has one managed node now. Also Now let’s check config file has any inventory or not using ansible --version

Image for post
Image for post

Nowconfig file has/etc/ansible/ansible.cfgas mentioned in the above screenshot. Also, let’s check the managed node is pingable or not using the below command.

ansible all -m ping
Image for post
Image for post

The managed node is pingable now so the Ansible setup has configured on the AWS instance so we can go forward for the next part of this task.

Deploying web server on Docker using a Playbook

Hopefully, your ansible setup has been configured But in case you are facing any problem with the setup then you can ping me on LinkedIn which is mentioned at the bottom of this article.

So let’s see all the steps one by one how we can deploy a web server on the docker container through ansible.

Note: Aqua colored terminal is presenting my managed node here.

Before installing the docker software we have to create a docker repo so that we can easily install it in the Redhat Linux especially rhel 8. Also, you can see in the below screenshot that the managed node has no docker repo. So let’s how we can create it through an ansible-playbook.

Image for post
Image for post

Make a docker.ymlusing the editor and write the below-mentined code inside it.

Image for post
Image for post

After saving the above code, run the playbook using the below command.

ansible-playbbok -v docker.yml
Image for post
Image for post

Now you can see in the below screenshot docker repository has been created in the managed node.

Image for post
Image for post

Docker repo has been created so now we can go forward with the installation of docker. But let’s check that the managed node has docker software or not using the below-mentioned command.

rpm -q docker-ce
Image for post
Image for post

So you can in the above screenshot that the managed node has no docker. So write the below code in the same playbook docker.yml

Image for post
Image for post

After saving code, run ansible-playbook docker.ymlcommand again.

Image for post
Image for post

So the controller node is showing that docker software has been installed in the managed node as mentioned in the above screenshot. But let’s see it is installed or not in the managed node using the below-mentioned command.

rpm -q docker-ce
Image for post
Image for post

Now you can see docker software has been installed so let’s jump into step 3.

Now if you want to use docker services then you have to start its services. So let’s see how we will write the code for it in the playbook. But I am showing you here docker service is running or not in the managed node using the below command.

systemctl status docker
Image for post
Image for post

Now you can see in the above screenshot that the docker service is inactive. So open docker.ymlplaybook and write the mentioned code inside it.

Image for post

After saving code, run ansible-playbook docker.ymlcommand again.

Image for post
Image for post

So the controller node is showing that the docker service has been started in the managed node as mentioned in the above screenshot. But let’s see it is started or not inside the managed node using the below-mentioned command.

systemctl status docker
Image for post
Image for post

Docker services have been successfully started in the managed node as mentioned in the above screenshot

After starting the services now we need a docker image so for whom we have to write the code for it inside the docker.ymlplaybook. But let’s see the managed node has any docker image or not using the below command.

docker images
Image for post
Image for post

The managed node has no docker images as mentioned in the above screenshot. So open docker.ymlplaybook and write the mentioned code inside it.

Image for post
Image for post

After saving code, run ansible-playbook docker.ymlcommand again.

Image for post

So the controller node is showing that the docker image has been pulled in the managed node as mentioned in the above screenshot. But let’s see it is pulled or not in the managed node using the below-mentioned command.

docker images
Image for post
Image for post

httpd docker image has been successfully pulled from the docker hub in the managed node as mentioned in the above screenshot.

This part totally depends on you how you want to create the webpage. So this is totally your desire. But I am making a very simple html file as you can see in the below screenshot.

Image for post
Image for post

Now you have to copy this content into the managed node. So I want to copy index.html file in the /home directory. But before copying the content, You can see that the managed node has no html file in the below screenshot.

Image for post
Image for post

So open docker.ymlplaybook and write the mentioned code inside it. But you can decidesrcanddestlocation whatever you want to give.

Image for post
Image for post

After saving code, run ansible-playbook docker.ymlcommand again.

Image for post
Image for post

So the controller node is showing that the index.html file has been copied in the managed node as mentioned in the above screenshot. But you can also check in the managed node as I have shown in the below screenshot.

Image for post
Image for post

index.html file has been successfully copied from the controller node to the managed node as mentioned in the above screenshot.

Now we are at the last step and will see how we can launch a container for the webserver. But let’s check first that the managed node has any docker container or not using the below-mentioned command.

docker ps -a
Image for post
Image for post

The managed node has no docker container as mentioned in the above screenshot. So open docker.ymlplaybook and write the mentioned code inside it.

Image for post
Image for post

After saving code, run ansible-playbook docker.ymlcommand again.

Image for post
Image for post

So the controller node is showing that the docker container has been launched for the webserver in the managed node as mentioned in the above screenshot. But let’s see it is launched or not using the below-mentioned command.

docker ps
Image for post
Image for post

The Docker container has been successfully launched with the name webserver in the managed node as mentioned in the above screenshot. But you can also see all the things after going inside the docker container. So follow all the command which are mentioned in the below screenshot

Image for post
Image for post

Now the webserver has been deployed on the docker container with the help of the Ansible Automation tool. So let’s check the webpage is responding or not.

Image for post
Image for post

So the webpage is working well and showing the content properly whatever I write in the index.html file.

Conclusion

Hopefully, now you have an idea what is the use of ansible here how we can deploy the webserver on the docker container through Ansible as well as how we can install the Ansible tool on the AWS Redhat instance. If you have any problem with the playbook then you check in the Github repo.

Image for post
Image for post

I tried to explain as much as possible. Hope You learned Something from here. Feel free to check out my LinkedIn profile mentioned below and obviously feel free to comment. I write Cloud Computing, Machine Learning, Bigdata, DevOps and Web, etc. blogs so feel free to follow me on Medium.

Hackcoderr’s Hub

Upgrade your knowledge with Hackcoderr

Sachin Kumar

Written by

Jr. Researcher | Computer Science Engineering Student| Tech Enthusiast | Technical Content Writer

Hackcoderr’s Hub

A Medium Publication sharing the technical concepts, ideas and contents.

Sachin Kumar

Written by

Jr. Researcher | Computer Science Engineering Student| Tech Enthusiast | Technical Content Writer

Hackcoderr’s Hub

A Medium Publication sharing the technical concepts, ideas and contents.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store