Automate Kubernetes cluster using Ansible roles to launch a WordPress website and a MySQL database server for it.

In this blog, we are going to create a Kubernetes cluster like how we did in the previous blog and then further we are going to launch a WordPress website on worker node and a database server for this website.

Steps to do the task :

Starting steps will be the same as the previous blog, so we’ll continue the further process.

Link for the previous blog-

https://ishikamandloi-963.medium.com/creating-kubernetes-cluster-on-aws-ec2-instance-using-ansible-roles-eb78509aaf01

1. Create an Ansible role to launch 3 AWS EC2 instances.

2. Create Ansible Playbook to configure Docker, K8S Master, K8S Worker Nodes on the above created EC2 Instances using kubeadm.

Task file of Masternode -

Task file of Worker node -

3. Create one more ansible role WordPress-MySQL in the same workspace.

In the task file of WordPress-MySQL role -

  • Pulling WordPress image

“kubectl run mywordpress — image=wordpress:5.1.1-php7.3-apache”

  • Pulling MySQL image

“kubectl run mydatabase — image=mysql:5.7 — env=MYSQL_ROOT_PASSWORD=xyz — env=MYSQL_DATABASE=wpdb — env=MYSQL_USER=abc — env=MYSQL_PASSWORD=xyz”

  • Exposing WordPress pod at port 80

the command for exposing pod -

“kubectl expose pods mywordpress —type=NodePort — port=80”

  • Checking for services

“kubectl get services”

  • Pausing playbook for some time to get ips
  • Checking pod launched to get ips

“kubectl get pods -o wide”

The main file to run all three roles Master node, Worker node and WordPress — MySQL.

4. Running main file

Token created…

Worker node joined…

IP and Port of WordPress

Now we can launch the website …

Success!!

Thank you !! keep learning!!

GitHub URL-

https://github.com/ishika2020/Ansible-playbook-to-launch-wordpress-website-and-database-on-kubernetes-cluster-using-ec2-instances.git