Automating Kubernetes Cluster and Configuring WordPress Application on it using Ansible Roles over the AWS cloud.

Hey!!! Back with another article in which you gonna find how to automate the Kubernetes cluster over the AWS cloud and also to configure the WordPress Application connected with MySQL database inside the Kubernetes cluster using Ansible.

Steps to do in this practical:

  • Configuring dynamic inventory in local system.
  • Create Role for Launching EC2 instances.
  • Create Role for Configuring K8S multi node cluster on Ec2 instances.
  • Create Role for deploy Wodpress pod inside K8S Cluster.
  • Create Role for deploy MySQL database pod inside K8S cluster.
  • Create the main playbook to run all roles.( or you can run each role separately , here I combine all roles in one playbook)
  • Run the main playbook.
  • Check our configuration is successfully done or not.

Here is another article about configuring K8S cluster in detail

so, lets start the practical Step by Step

Step 1:

Configuring the dynamic inventory in local system

  • Create the Inventory directory.
  • Download ec2.yml and ec2.ini from ansible official dynamic inventory GitHub link in /Inventory folder. both the file should be in same folder.
# wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py# wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
  • Change ec2.py file in executable mode.
  • If we are using python3 then, open the ec2.py file and change env python to python3 in the first line as the python code is written in python2 but we’ll be using python3. So, we need to change it as “#!/usr/bin/python3”. and comment line no 172 in it.
# chmod +x ec2.py
  • Open the ec2.ini file and put your aws access key and secret key in credentials part.
  • Set environment variable for authentication
$ export AWS_RGION='YOUR-AWS-REGION-NAME-HERE'
$ export AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXX
$ export AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXX
  • Installing boto and boto3.
# pip install boto
# pip install boto3

Step 2:

Configure Privilege in ansible.cfg file

Most of the EC2 instances allow us to login as an “ec2-user” user, this is why we have to mention the remote_user as “ec2-user”.

  • EC2 instances allow key-based authentication, hence, we must mention the path of the private key.
  • Important part is privilege. “root” powers are required if we want to configure anything in the instance. But “ec2-user” user is a general user with limited powers. Privilege Escalation is used to give “Sudo” powers to a general user.
  • visit to the directory where your private key located and change the mode of private key file.
# chmod 400 newsk.pem

Step 3:

Now Create Roles for launching ec2 instances on aws , configuring k8s_master and slave and deploying wp and mysql pods on k8s_cluster

# ansible-galaxy role init launch_ec2
# ansible-galaxy role init k8s_master
# ansible-galaxy role init k8s_slave
# ansible-galaxy role init mysql-wp

Step 4:

  • Create Role for Launching EC2 instances.

Step 5:

  • Create Role for Configuring K8S mutinode cluster on Ec2 instances.

Step 6:

  • Create Role for Configuring K8S multi node cluster on Ec2 instances.

Step 7:

  • Create Role for deploy Wodpress pod inside K8S Cluster.

Step 8:

Create main playbook to run all Roles.

Step 9:

Run the main playbook.

  • Here we can see that our playbook run successfully now we can check no our master node

Step 10:

Check our Setup created or not

  • Here my Kubernetes cluster created successfully.
  • Here we can see that cluster created and nodes are connected to master node.
  • pods wp and mydb1 run successfully.
  • Check our WordPress application running properly using node ip in which pod is running and port no which we exposed in playbook.

Github Repo 👇👇👇

Kubernetes configuration Article link 👇👇👇

Ansible Roles Article link 👇👇👇

!!! Task completed Successfully !!!! 😃😃😉

☘☘ Keep Sharing!!! , Keep Learning!!! ☘☘

🙏🙏Thanks for Reading 🙏🙏

Arth Learner

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store