Launching WordPress With MySQL Database in K8S Cluster On AWS Using Ansible!

Piyush Bagani - May 20 '21 - - Dev Community

Hello Guys!! In this blog, We will create a Kubernetes Cluster like how we did in one of my previous blogs and then further we will launch a WordPress application connected with MySQL database inside Kubernetes Cluster on AWS and automation using Ansible.

So Let's Get Started,

Prerequisites

Configure Kubernetes Multinode Cluster on AWS
follow this link for reference

In the above-linked blog, I have explained thoroughly how to Configure Kubernetes Multinode Cluster.

Further Do the following steps.

  1. Create an Ansible role WordPress-MySQL in the same workspace, where other roles Master and Slave are there.

    ansible-galaxy init role-name

In the main.yml file inside the tasks directory of the WordPress-MySQL role, write down the following code.
Alt Text

Now in the above tasks, we have launched WordPress and MySQL respectively.

To launch the WordPress we use this command
kubectl run mywp1 --image=wordpress:5.1.1-php7.3-apache
The image version is wordpress:5.1.1-php7.3-apache.

Also to launch the MySQL pod we have to set the user name and password. Here we can use the secret resource Kubernetes. But we can use this command to launch MySQL pod
"kubectl run mydb1 --image=mysql:5.7 --env=MYSQL_ROOT_PASSWORD=redhat --env=MYSQL_DATABASE=wpdb --env=MYSQL_USER=piyush --env=MYSQL_PASSWORD=redhat".

Exposing WordPress pod

To Expose WordPress Pod we use the following command
"kubectl expose pods mywp1 --type=NodePort --port=80"
Here, I am exposing to NodePort as the client is in the public world.

Pausing the playbook

After launching the pods it takes time to launch so, We are pausing the playbook for 60 seconds so all the pods will be ready.

  1. Creating the main playbook to run all three roles Master, Slave, and WordPress-MySQL.

Alt Text

Here, In this main Playbook, we have included all the roles Master, Slave, WordPress-MySQL which configures Kubernetes Master Node, Slave Node, and launches the WordPress and MySQL pods.

Finally, run the main playbook using the following command.

ansible-playbook main.yml
Enter fullscreen mode Exit fullscreen mode

Alt Text
Alt Text

And the playbook runs successfully.

Now we can take the public of any node either master or slave with the exposed port you will landed on the WordPress login page and then enter the password and username of the MySQL database and hit the run installation button. Our WordPress application is ready !!

SEE the below-pasted images

Alt Text
Alt Text
Alt Text
Alt Text

Finally We have Automated it.

Thanks for Reading, Keep Learning, Keep Sharing

You can check GitHub link to get more info about the playbooks.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .