Launching WordPress With MySQL Database in K8S Cluster On AWS Using Ansible!
Tags: ansible, wordpress, kubernetes, mysql
Published: 2021-05-20 09:15:35

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](https://dev.to/piyushbagani15/configuring-kubernetes-multinode-cluster-over-aws-using-ansible-2fhe)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0f7flpj3lznjmcbwippm.png)

###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.


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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5biz1xnplblzqbwyvrl.PNG)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7x172j8z2dadx9wwckq.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqxb4u19pglhiyui700o.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zz6aclbcxk3wq11heoyd.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mc0yk24hlha63u26rc5m.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mc0yk24hlha63u26rc5m.PNG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17lygpn0c4mgoyapy7bu.PNG)

##Finally We have Automated it.
##Thanks for Reading, Keep Learning, Keep Sharing

You can check [GitHub link](https://github.com/PiyushBagani15/Launching-WordPress-With-MySQL-Database-in-K8S-Cluster-On-AWS-Using-Ansible) to get more info about the playbooks.