Using-Ansible-Launching-WordPress-Mysql-application-on-k8s-cluster-AWSCLOUD(Provisioning)

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
No alt text provided for this image

Writting the variables in aws_vpc/vars/main.yml

No alt text provided for this image

Writting all the task in aws_vpc/tasks/main.yml

No alt text provided for this image

VPC Created :

No alt text provided for this image

Subnet Created :

No alt text provided for this image

Security Groups Created :

No alt text provided for this image

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

No alt text provided for this image

Writting all the task in provision-ec2/tasks/main.yml

No alt text provided for this image

After this writting a playbook to run both the roles aws_vpc and provision-ec2

No alt text provided for this image
No alt text provided for this image

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.

No alt text provided for this image

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

No alt text provided for this image

Steps in the below playbook /k8s-cluster/tasks/main.yml

  1. Installing docker and iproute-tc
  2. Configuring the Yum repo for kubernetes
  3. Installing kubeadm,kubelet kubectl program
  4. Enabling the docker and Kubernetes
  5. Pulling the config images
  6. Configuring the docker daemon.json file
  7. Restarting the Docker service
  8. Configuring the Ip tables and refreshing sysctl
  9. Starting kubeadm service
  10. Creating .kube Directory
  11. Copying file config file
  12. Installing Addons e.g flannel
  13. Creating the token
No alt text provided for this image

Now let's Create the role for Kubernetes Cluster(Slave)

ansible-galaxy init k8s-slave


In /k8s-slave/vars/main.yml

No alt text provided for this image


Steps in the below playbook /k8s-slave/task/main.yml

  1. Installing docker and iproute-tc
  2. Configuring the Yum repo for kubernetes
  3. Installing kubeadm,kubelet kubectl program
  4. Enabling the docker and Kubernetes
  5. Pulling the config images
  6. Configuring the docker daemon.json file
  7. Restarting the Docker service
  8. Configuring the Ip tables and refreshing sysctl
  9. Connecting to Master
No alt text provided for this image

?? 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

  1. Launching WordPress application
  2. Launching MySQL application
  3. Exposing the WordPress application
No alt text provided for this image

Let's Create a playbook to run both the roles K8s-cluster(Master) and K8s(Slave) and the Applications

No alt text provided for this image

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.

The MySQL-WordPress playbook is written in such a way that it will show the IP and Ports while running

For example

No alt text provided for this image

Now, let's take the IP of any node and use it with the above port

No alt text provided for this image

Clients are ready to connect my WordPress website

No alt text provided for this image

??Successfully completed the project of launching a WordPress and SQL application on a Kubernetes cluster and automating this whole process using ANSIBLE on AWS CLOUD (Provisioning)??

No alt text provided for this image

Github repo link: https://github.com/SuyogShinde942/Using-Ansible-Launching-Wordpress-Mysql-on-k8s-cluster.git

HAPPY LEARNING ???






















Raafat Abaid

Lead DevOps Engineer | Technical Product Manager @ IBM | Technical Expertise

4 年

thats the best

  • 该图片无替代文字
回复
Srushti Patil

Full Stack Developer | MERN Stack | Actively looking for new opportunities

4 年

Congrats Suyog Shinde

Pritee Dharme

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

Ishika Mandloi

ML | DL | DevOps | Linux | AWS | Python |

4 年

??well done Suyog Shinde

Shweta Gharal

Aspiring Full Stack Developer || Java || Spring Boot || MySql || HTML || CSS || Bootstrap

4 年

Good job Suyog Shinde

要查看或添加评论,请登录

Suyog Shinde的更多文章

社区洞察

其他会员也浏览了