Using-Ansible-Launching-WordPress-Mysql-application-on-k8s-cluster-AWSCLOUD(Provisioning)
So in this article, I am going to launch a WordPress and SQL application on a Kubernetes cluster and automating this whole process using ANSIBLE on AWS CLOUD (Provisioning)
We are also going to use the dynamic inventory over here.
Amazon Web Service :
We can define AWS (Amazon Web Services) as a secured cloud services platform that offers compute power, database storage, content delivery, and various other functionalities. To be more specific, it is a large bundle of cloud-based services.
Kubernetes:
Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.
Ansible :
Ansible is the simplest way to automate apps and IT infrastructure. Application Deployment + Configuration Management + Continuous Delivery.
MYSQL :
MySQL Database Service is a fully managed database service to deploy cloud-native applications.
Let's Start????:
We Are going to use dynamic inventory.
Why dynamic inventory?
For example, Aws default provides a dynamic IP so, after every restart, it's assigned to a new IP, or we launch a new O.S in a region called ap-south-1 and we need to use all the container as the database from that region and suppose there are 100 servers, Instead of putting it manually in the inventory there is a python script that works as a dynamic inventory which divides the instances with tags, region, subnets, etc.
First, we have to create a Virtual Private Network in AWS for the instances we launch
Amazon Virtual Private Cloud (Amazon VPC) is a service that lets you launch AWS resources in a logically isolated virtual network that you define. ... You can use both IPv4 and IPv6 for most resources in your virtual private cloud, helping to ensure secure and easy access to resources and applications.
Steps in this Role:-
1. Creating VPC
2. Creating VPC InternetGateway
3. Creating VPC Subnet
4. Creating Security groups
5. Creating routes
We are going to create the role for it :
ansible-galaxy init aws_vpc
Writting the variables in aws_vpc/vars/main.yml
Writting all the task in aws_vpc/tasks/main.yml
VPC Created :
Subnet Created :
Security Groups Created :
Now let's move to provision the ec2 instances and launch them in the above-created VPC
We are going to launch one MASTER and TWO slaves for the k8s cluster
Creating role
ansible-galaxy init provision-ec2
Writting the varaibles in provision-ec2/vars/main.yml
Writting all the task in provision-ec2/tasks/main.yml
After this writting a playbook to run both the roles aws_vpc and provision-ec2
As you can see above we have successfully launched and provisioned the ec2-instances
Now let's move to the dynamic inventory part
The dynamic inventory will separate the instances according to region, tags, public and private IPs, and many more
We used two scripts for the dynamic inventory ec2.py and ec2.ini
https://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.py ---- ec2.py
https://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.ini ---- ec2.ini
Pre-requisites for these scripts are installing boto and boto3 in the system where you are running the program.
To install boto module
pip3 install boto pip3 install boto3
To successfully make an API call to AWS, you will need to configure Boto (the Python interface to AWS). There are a variety of methods available, but the simplest is just to export two environment variables:
export AWS_ACCESS_KEY_ID='your access key' export AWS_SECRET_ACCESS_KEY='your secret key'
or The second option is to copy the script to /etc/ansible/hosts and chmod +x it. You will also need to copy the ec2.ini file to /etc/ansible/ec2.ini. Then you can run ansible as you would normally.
We have to only run ec2.py for getting the dynamic inventory.
The script separated the instances according to tags "tag_db_k8s_master" and "tag_db_k8s_slave" and made them a separated host group so we can use them in the playbook.
Instead of providing the aws_access_key and aws_secret_key in the playbook, we can configure the AWS CLI and the Ansible will by default use the credentials which we provide while configuring the AWS CLI.
Our dynamic inventory is ready to use.
Now let's Create the role for Kubernetes Cluster(Master)
ansible-galaxy init k8s-cluster
In the k8s-cluster/vars/main.yml file
Steps in the below playbook /k8s-cluster/tasks/main.yml
- Installing docker and iproute-tc
- Configuring the Yum repo for kubernetes
- Installing kubeadm,kubelet kubectl program
- Enabling the docker and Kubernetes
- Pulling the config images
- Configuring the docker daemon.json file
- Restarting the Docker service
- Configuring the Ip tables and refreshing sysctl
- Starting kubeadm service
- Creating .kube Directory
- Copying file config file
- Installing Addons e.g flannel
- Creating the token
Now let's Create the role for Kubernetes Cluster(Slave)
ansible-galaxy init k8s-slave
In /k8s-slave/vars/main.yml
Steps in the below playbook /k8s-slave/task/main.yml
- Installing docker and iproute-tc
- Configuring the Yum repo for kubernetes
- Installing kubeadm,kubelet kubectl program
- Enabling the docker and Kubernetes
- Pulling the config images
- Configuring the docker daemon.json file
- Restarting the Docker service
- Configuring the Ip tables and refreshing sysctl
- Connecting to Master
?? Cluster is created successfully ??
Now, Launching a WordPress and MySQL applications in the above cluster
Creating a role to launch the applications
ansible-galaxy init mysql-wordpress
Steps in the below playbook
- Launching WordPress application
- Launching MySQL application
- Exposing the WordPress application
Let's Create a playbook to run both the roles K8s-cluster(Master) and K8s(Slave) and the Applications
Here, we are using the hosts "tag_Name-K8s_master" and "tag_Name-K8s_slave" which are taken after running ec2.py
Note: We have prompted for a token before running the slave role to make a successful cluster.
Both the cluster and the applications are launched successfully
After this, we can connect to the WordPress application by using the IP of the master or any slave with a port assigned to it.
Lead DevOps Engineer | Technical Product Manager @ IBM | Technical Expertise
4 年thats the best
Full Stack Developer | MERN Stack | Actively looking for new opportunities
4 年Congrats Suyog Shinde
ARTH - School Of Technologies || Python || Amazon Web Service || Big Data || C || C ++ || DevOps (Kubernetes , Ansible , Docker , Git and GitHub , Jenkins )
4 年Keep it up Suyog Shinde
ML | DL | DevOps | Linux | AWS | Python |
4 年??well done Suyog Shinde
Aspiring Full Stack Developer || Java || Spring Boot || MySql || HTML || CSS || Bootstrap
4 年Good job Suyog Shinde