Launching the Web server and deploying the code on top of the Docker by using Ansible Playbook

Launching the Web server and deploying the code on top of the Docker by using Ansible Playbook

Before jumping to the practical, let's get familiar with Ansible. Ansible It is an open-source tool mainly used for configuration management, application deployment and in some cases also for software provisioning. Ansible is written in Python language and it uses ssh protocol.

It uses YAML file for configuration management of the system.

image.png

You can configure multiple systems by sitting in one system. The systems that need the configuration are called as Managed nodes and the system that configured the multiple managed nodes can be called as Controller Node. Typically,there will be only one controller node that will configure the "N" number of managed nodes.

Ansible has an agentless architecture, means the software that helps the configuration need to be installed only on a single Controller node only. This is the core competency and unique feature for Ansible that possess, this helps Ansible to have a market share of 74.67% in the configuration-management market.

image.png

The above figure reflects the interest of using Ansible for configuration management of their managed nodes.

Let's get into the practical. We are going to configured the Apache Web server and deployed the code on it on the docker container in the managed node by using ansible-playbook.

Note: I used Amazon 2 Linux EC2 instances for this practical. I used container image as httpd

Controller node setup

First step is to configure the Ansible software or package in the Controller node. Since Ansible is written in Python, so we can install ansible by using pip3 command.

yum install python3 -y

pip3 install ansible

Then you create an inventory file which contains all the meta data of the managed nodes which you want to configure. You can find the inventory file which I configured in below figure.

1 vim ip.JPG

After creating the inventory file, you have to update it in configuration file. The ansible's configuration file is located in /etc/ansible/ansible.cfg . If not there means, you can create ansible folder using mkdir command and ansible.cfg using touch command.

config file.JPG

The above figure shows the configuration file and how to update the inventory in it.

After completion, you can use the below command to verify there is a connection between managed node and controller node. ansible all -m ping

2 ping.JPG

The above figure shows that the controller node has a reliable connection with the managed nodes.

Practical

Steps involved

  • Configuring Docker repo
  • Creating directory for mount
  • Copy the file to that directory
  • Python3 installation
  • Installation of Docker module
  • Installation of IP tables
  • Docker Installation
  • Starting/Executing Docker service
  • Launch of Docker Container

1.Configuring Docker repo

For installation of Docker, first step is to creating one yum repo called Docker.repo. Using this repo, we can install docker in our OS. image.png

2. Creating directory for mount

Usually code is stored in file only and files are stored in directory. The deployment of code in docker container requires storage. We are providing storage by mounting docker storage to this directory. So that, in future if you want to deploy any webapp in container, you can store that program file in this baseOS directory.

image.png

3. Copy

After the creation of directory, next step is to copy the file you want to deploy in webserver in the above mentioned directory.

image.png

4. Python3 installation

Next step is to install the Python3 using yum module, since ansible is written in Python and so that we can install the python-docker module in future.

image.png

5. Installation of Docker module

Then we have to install the docker module in python by using pip

image.png

6. Installing IP tables

In some cases, this step is important, since IP tables are required to start the docker service in some systems. Because in some versions of docker, docker has no pre-installed IP tables. To avoid those conflicts, we are installing IPtables using yum module.

image.png

7. Docker Installation

Next step is to install the docker by using yum module. image.png

8. Start and make Docker service permanent

Then we have to start the service of Docker and enable the service for avoiding the service stop due to system reboot. image.png

9. Pull image from DockerHub

We are pulling httpd image from DockerHub by using docker pull command image.png

10. Launching Container using docker image

Finally we are going to launch one container named httpd140 with exposed port of 8040 and also we attached docker container httpd's document root to the baseOS or host's directory. Since, the file in document root can only deployed it to the webserver.

image.png

##Output

7 yml output 1.JPG

8 yml output 2.JPG

expose checking.JPG

You can check this scripts from my github GitHub URL: github.com/vishnuswmech/Launching-the-Web-s..

We are done with it!!. Thank you all for your reads. Stay tuned for upcoming articles!! Suggestions are always welcome!!