☸️LAUNCHING A WORDPRESS APPLICATION WITH MYSQL DATABASE ON K8S CLUSTER OVER AWS☁️ USING ANSIBLE

rishabhsharma
3 min readMar 19, 2021

--

Hello everyone👋🏻, another very much interesting and important use case based automation article. In this article we will be Launching a WordPress Application with MYSQL Database on K8S Cluster over AWS☁️ using Ansible.

I have already configured k8s multi node cluster over AWS using Ansible. In this article we will setup WordPress Application on our K8S Cluster using Ansible.

To know how to configure k8s multi node cluster over AWS using ansible you refer to my article

https://hrishabhsharma183.medium.com/%EF%B8%8Fconfiguring-k8s-multi-node-cluster-over-aws-%EF%B8%8F-%EF%B8%8Fusing-ansible-8d0da3a4b6c

We will configure everything using Ansible Roles.

Ansible Role

ansible-galaxy init wordpress-mysql

After creating ansible roles we need to write the yaml code inside the respective files. we have vars folder to keep variables and tasks folder to write tasks.

---
# tasks file for wordpress-mysql
#task to launch mysql
- name: "Launching MySql"
shell: "kubectl run mydb --image=mysql:5.7
--env=MYSQL_ROOT_PASSWORD={{ password }} --env=MYSQL_DATABASE=wpdb --env=MYSQL_USER=hrishabh --env=MYSQL_PASSWORD={{ password }}"
register: MySQL
- debug:
var: "MySQL.stdout_lines"
#task to launch wordpress
- name: "Launching Wordpress"
shell: "kubectl run mywp --image=wordpress:5.1.1-php7.3-apache"
register: wordpress
- debug:
var: "wordpress.stdout_lines"

vars file for wordpress-mysql

---
# vars file for wordpress-mysql
password: "your password"

“Exposing WordPress to the public world”

# Exposing Wordpress
- name: "Exposing wordpress"
shell: "kubectl expose pods mywp --type=NodePort --port=80"
register: exposed
ignore_errors: yes
- debug:
var: "exposed.stdout_lines"

To get service:

- name: "get service"
shell: "kubectl get svc"
register: svc
- debug:
var: "svc.stdout_lines"

After launching the pods it takes time to launch so, I am pausing the playbook for 60 seconds so all the pods will be ready and we can get the complete information of the pods

- name: "Pausing playbook for 60 seconds"
pause:
seconds: 60
- name: "Getting Database IP"
shell: "kubectl get pods -o wide"
register: Database_IP
- debug:
var: "Database_IP.stdout_lines"

Creating main playbook “wp-mysql-setup.yml” to launch WordPress and MySQL on K8S Cluster over AWS ☁️

- hosts: ["tag_Name_K8S_Master"]
roles:
- name: "Launching Wordpress and Mysql"
role: "/k8s-multi-node-cluster/wordpress-mysql"

The command to execute the playbook “wp-mysql-setup.yml”

ansible-playbook wp-mysql-setup.yml

Now you can take the public of any of the nodes whether master or slave with the exposed port, you will get landed to the wordpress login page and then enter password and username of the mysql database and hit the run installation button.

Our WordPress Application is ready!

We can also check from our Master Node

Thanks for reading !!!😊✨ keep Learning!

--

--

rishabhsharma

AWS Certified ☁️ | PySpark | DevOps | Machine Learning 🧠 | Kubernetes ☸️ | SQL 🛢