In this article, We are going to configuring the Apache Web server and deploying the code on it on the docker container in the managed node by using ansible-playbook.
Before jumping to the practical, let's get familiar with 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.
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.
The above figure reflects the interest of using Ansible for configuration management of their managed nodes.
Let's get into the practical.
Note: I used Amazon 2 Linux EC2 instances for this practical. I used container image as httpd
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.
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
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
The above figure shows that the controller node has a reliable connection with the managed nodes.
- 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
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.
After the creation of directory, next step is to copy the file you want to deploy in webserver in the above mentioned directory.
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.
Then we have to install the docker module in python by using pip
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.
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.
You can check this scripts from my github
GitHub URL: https://github.com/vishnuswmech/Launching-the-Web-server-and-deploying-the-code-on-top-of-the-Docker-by-using-Ansible-Playbook.git
We are done with it!!. Thank you all for your reads. Stay tuned for upcoming articles!! Suggestions are always welcome!!