How to Configuring Webserver on the Top of Docker setup with the help of Ansible Playbook !!
🤩So guys this is my new article which give you some high level idea about DEPLOY DOCKER USING ANSIBLE-PLAYBOOK
I Hope you will get knowledge from this article.
so ! why are you waiting ?
LOOK FOR WHAT’S AHEAD & IT WILL TAKE YOU FAR BEYOND !!
Hello everyone as we all know we all are moving towards the world of automation , where we will do all the works automatically with the help of multiple tools & technology like ( Artificial Intelligence , DevOps , Ansible etc ).
So basically , in this article we discuss about the Ansible , what is the ansible , why do we use the ansible tool, & how to deploy complete docker setup with the help of ansible & how it will be beneficial from the deployment point of view.
So first of all we have to know what is Ansible & how does it works.
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.
TASK DESCRIPTION
Write an Ansible PlayBook that does the following operations in the managed nodes:🔹 Configure Docker🔹 Start and enable Docker services🔹 Pull the httpd server image from the Docker Hub🔹 Run the docker container and expose it to the public🔹 Copy the html code in /var/www/html directory and start the web server .
So Now we know what we have to do to complete the this task , but we don’t know how do we complete this task , so i’ll give you proper & step by step instruction to complete this task.
So for doing this task we will gonna use RHEL 8 operating system. So to use the ansible tool we need a controller node & managed node, so for the both the node we gonna use RHEL 8.
→ Step 1 : install & configure the ansible.
So now the question is how do we install the ansible in our RHEL 8. So for the installation process we will use python module to install the ansible tool.
To install the ansible we use this command
pip3 install ansible
So after the installation we have to configure the ansible to use it & before that we have to define the managed node that means which system gonna we will use as a remote system. So for that we have to create a file inside the /root/ folder ( to define the hosts )with the ip.txt . So we use the command to create a host like.
vim /root/ip.txt
After creating the hosts file we will create the configuration file for ansible because without configuration any software we can not use it properly & we can not use our hosts to work on it.
To configure the ansible first of we create a directory inside the /etc/ansible folder , after that we have to create the file with the name of ansible.cfg to use this file as a configuration file & we have to write the inventory name ( that will be the host file where we write our remote system IP ) inside that file. Then we have to pass the SSH Key authentication , So we have to make false this option so that there will no problem while doing the authentication.
# create configuration file#command to create the directory
mkdir /etc/ansible#to create the file inside the ansible
vim /etc/ansible/ansible.cfg
After we have to install the SSH pass with help of yum command , we use the command like :
yum install sshpass
After that we have to check that our ansible command is working or not properly simply means which version is install . Use below mentioned command:
ansible --version
So with the help of this command we can find that our ansible file is successfully configured & command is working properly.
Now we can check how many hosts we have with the help of below mentioned command:
ansible all --list-hosts
After checking the hosts name we have to check that there is proper connection established between the controller node & managed node or not . So we can ping the managed node from our controller node to check the connectivity . Command will be like
ansible all -m ping
So after checking this connection we have to write the code for the entire deployment of the docker with the help of ansible.
→ Step 2 : Write the code for deployment of docker.
So to deploy the entire setup we can use the command line or ansible-playbook option , but through the command line it is difficult for us to deploy the entire setup , so we use the ansible-playbook option for this task.
What is Ansible-playbook ?
An Ansible playbook is an organized unit of scripts that defines work for a server configuration managed by the automation tool Ansible.
A playbook is a configuration file written in YAML that provides instructions for what needs to be done in order to bring a managed node into the desired state. Playbooks are meant to be simple, human-readable, and self-documenting. They are also idempotent, meaning that a playbook can be run on a system at any time without having a negative effect upon it. If a playbook is run on a system that’s already properly configured and in its desired state, then that system should still be properly configured after a playbook run.
So Now we write the code for the entire deployment. first we have to define the host name & then multiple task.
So first we configure the yum repository for installation of docker.
After create the repository for we have to install the docker , so write the code to install the docker on remote system.
As we all know whenever we install any software in any OS then we have to start it & if we want to enable it permanently then we have to enable the service. so we write the code to start the service.
After this we have to install the SDK for the docker with the help of pip3 command . so we write a task for it also.
After the successful installation of docker & we have to pull to docker image from the Docker hub i.e; the official repository for the docker images. We pull the HTTPD images because we have to host a webpage from the container & also in our task it defined that we have to use the httpd docker images . So pull the images we write the code .
After successfully pulling of the docker images we create a folder in our remote system to copy the file or code from our controller node , to mount that folder to the docker container . so we write the code for that also like .
- name: create a folder
file:
path: "/root/"
state: "directory"- name : copy the code
copy:
src: "/root/web.html"
dest: "/root"
- name: copy image for code
copy:
src: "/root/Downloads/ansible-docker.jpg"
dest: "/root"- name: launch docker container
docker_container:
name: httpd-webserver
image: "httpd"
state: "started"
exposed_ports:
- "80"
ports:
- "8888:80"
volumes:
- /root/:/usr/local/apache2/htdocs/
After create the folder & copy the file in to managed node , We have to run the docker container with the help of ansible playbook. While run the docker container we also exposed our container to outside our system , with the help of patting concepts . we also mount the our code or file to the docker container , so that we can use it from our container service. so awe mount our volume to /usr/local/apache2/htdocs folder because this folder is a default folder that will use to access by the webserver inside the docker container . So we write the code for it also .
→ Step 3: To run the code
After writing the entire code we have to run this code with the help of ansible-playbook command. So the command would be like :
ansible-playbook ansible_docker.yml (file name )
After that our next task is to check that the our folder is successfully mount or not to our docker cotainer. So we move to our remote system where we check these things.
Now it’s time to check that our webpage is running or not.
Now we can see that our web page is successfully open on my chrome web browser, with the remote ip & the port no that is given by the me in my code .
So this is all about this task . If you have any queries regarding the task you can connect with me on linkedin: Jatin lodhi
LINKEDIN PROFILE LINK :- https://www.linkedin.com/in/jatin-lodhi-THANKS FOR EVERYONE TO READ THIS ARTICLE !!🤗