Webserver configuration over Docker using Ansible
In this article, we are going to setup Apache server over a docker container using Ansible.
Our Plan :
Write an Ansible Playbook that does the following operations in the managed nodes:
- Configure Docker.
- Start and enable Docker services.
- Pull the httpd(Apache) 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 webserver.
So lets begin the same, before starting we have some prerequisite setup to be done, that is :
- Two VM (In my case RHEL-8)
- Ansible setup (on the node that is going to be our Controller node).
Implementation
Lets begin the implementation with setup of Ansible.
Step 1. Setup Ansible
a). Install Ansible using command
pip3 install ansible
Note: In my case, it is showing requirement already satisfied because in my system Ansible is already installed.
b). Create an inventory file
vi hosts.txt
you can give any name to this file.
c). Configure the Ansible
To configure ansible, create a directory named ansible in /etc directory then create the ansible configuration file i.e., ansible.cfg
mkdir /etc/ansible
cd /etc/ansible
vi ansible.cfg
d). Add host to the inventory file, in the format
192.167.12.21 ansible_user=username ansible_ssh_pass=your_password ansible_connection=ssh
Replace the IP, username and password as per your target node configurations and check the list of hosts using
After setting up the inventory, check the connectivity to the hosts using command
ansible all -m ping
Step 2. Configuring yum
To configure yum, we need to create the ansible playbook
On execution through ansible-playbook command, it will be successfully created.
ansible-playbook -v dockerSet.yml
Here i am using single level verbosity to get some extra information.
Step 3. Configure the docker repository
Step 4. Install docker software
Step 5. Start the docker service
Before starting the docker service, we need to install a python package named docker-py in the target node as python is the dependency for Ansible. By installing docker-py, the system will understand that docker is using the correct and same version of python as used by the system.
To install the python package, we will use pip module
Now, we will start the docker service
Step 6. Pull the docker image, here we require httpd
Step 7. Create a docker container with Apache webserver over it
Here i have linked the /webpages/ path to the main path or the default path for the server files of Apache web server.
Step 8. Copy an index.html file to the /root/ directory
Step 9. Add a firewall rule
Here we need to add a firewall rule so that the port 85 that we have used above can be accessible to public.
Step 10. Go for the web page
Hence, we have successfully setup a webserver on a docker container using ansible. Also the complete thing is being done with one single command.The final code will be :
- hosts: all
tasks:
- file:
path: "/dvd2"
state: directory
- mount:
path: "/dvd2"
src: "/dev/sr0"
fstype: "iso9660"
state: mounted
- yum_repository:
name: "yumrepo1"
description: "yum repo to access AppStream"
baseurl: "file:///dvd2/AppStream"
gpgcheck: no
- yum_repository:
name: "yumrepo2"
description: "yum repo to access BaseOS"
baseurl: "file:///dvd2/BaseOS"
gpgcheck: no
- yum_repository:
name: "dockerrepo"
description: "yum repo to install docker"
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/"
gpgcheck: no
- yum:
name: "docker-ce-3:18.09.1-3.el7.x86_64"
state: present
enablerepo: "dockerrepo"
- file:
path: "/webpages"
state: directory
- copy:
dest: /webpages/index.html
content: '<html><body><h1>Task10 Completed Successfully !!!</h1></body></html>'
- pip:
name: "docker"
- service:
name: "docker"
state: started
enabled: yes
- docker_image:
name: "httpd"
source: pull
- docker_container:
name: "os32"
image: "httpd"
state: started
detach: yes
ports: "85:80"
volumes: /webpages:/usr/local/apache2/htdocs/
- firewalld:
port: "85/tcp"
permanent: yes
state: enabled
immediate: yes
To execute this code, we use the command :
ansible-playbook -v dockerSet.yml
Finally!! with this code we can setup hundreds or thousands of nodes within few minutes.
I hope this will be helpful to you to explore more with Ansible!!!🤗🤗
💫Keep learning, keep sharing !!💫