WordPress and MySQL in Kubernetes Cluster on AWS

Sunil Sirvi
2 min readJun 19, 2022

Hello guys, in this blog I will show you how we can automate Kubernetes Cluster using ansible over Aws cloud. We will launch WordPress and MySql database.

Prerequisite :

  • Configure Kubernetes Multi-node Cluster on AWS

Now, create Ansible role in the same directory where the Master and Slave roles are present.

ansible-galaxy init <role-name>

Write down the below code inside the “main.yml” file which is inside the tasks directory of the WordPress-MySQL role :

---
# tasks file for Wordpress-MySQL
- name: "Pulling Wordpress image"
shell: "kubectl run mywp1 --image=wordpress:5.1.1-php7.3-apache"
register: Wordpress

- debug:
var: "Wordpress.stdout_lines"

- name: "Pulling MySQL"
shell: "kubectl run mydb1 --image=mysql:5.7 --env=MYSQL_ROOT_PASSWORD=redhat --env=MYSQL_DATABASE=wpdb --env=MYSQL_USER=wpuser --env=MYSQL_PASSWORD=redhat"
register: MySQL

- debug:
var: "MySQL.stdout_lines"

- name: "Exposing Wordpess"
shell: "kubectl expose pods mywp1 --type=NodePort --port=80"
register: exposed
ignore_errors: yes

- debug:
var: "exposed.stdout_lines"

- name: "Getting service"
shell: "kubectl get svc"
register: svc

- debug:
var: "svc.stdout_lines"

- name: "Pausing playbook for pods to launch and getting IP(s)"
pause:
seconds: 60

- name: "Getting database IP"
shell: "kubectl get pods -o wide"
register: Database_IP

- debug:
var: "Database_IP.stdout_lines"

Now for launching WordPress use below command :

kubectl run mywp1 — image=wordpress:5.1.1-php7.3-apache

The image refers to the image version “ wordpress:5.1.1-php7.3-apache ”. For launching the MySQL pod we have to set the username and password by following command :

kubectl run mydb1 — image=mysql:5.7 env=MYSQL_ROOT_PASSWORD=redhat env=MYSQL_DATABASE=wpd — env=MYSQL_USER=wpuser — env=MYSQL_PASSWORD=redhat

Expose WordPress Pod using below command :

kubectl expose pods mywp1 — type=NodePort — port=80

Pausing the playbook after launching the pods it takes time to launch, so we are pausing the playbook for 60 seconds so till then all the pods will be ready.

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

- hosts: Master
roles:
- name: "Configuring Master Node"
role: Master
- hosts: Slave
roles:
- name: "Configuring Slave Node"
- hosts: Master
roles:
- name: "Launching WordPress and MySQL"
role: Wordpress-MySQL

Here, In this “main.yml” 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.yml” playbook using the below command :

ansible-playbook main.yml

Now we can take the public IP of any node either master or slave with the exposed port and we will land on the WordPress login page, there we need to enter the password and username of the MySQL database and hit the run installation button, our WordPress application is ready.

GitHub repository link for reference :

____________Thank you for reading____________

--

--