Automating Deploy WordPress with MySQL on Kubernetes cluster on AWS.

Using Ansible

Mukul Jeveriya
4 min readJan 17, 2022

--

Task Description* 📄

🔅 Automate *Kubernetes* Cluster Using *Ansible*

🔅 Launch ec2-instances on *AWS Cloud* eg. for master and slave.

🔅 Create roles that will configure master node and slave node seperately.

🔅 Launch a *wordpress* and *mysql* database connected to it in the respectine slaves.

🔅 Expose the *wordpress* pod and client able hit the *wordpress* ip with its respective port.

Let’s start………………….

Firstly we have to launch ec2-instance on AWS Cloud for setup Kubernetes Master Slave Cluster. For this Please go through my previous Blog. In which I deeply cover how to setup a K8s cluster using ansible.

So lets run our setup.yml file which will deploy wordpress in a single go.

ansible-playbook setup.yml

This will launch 1 master node and 2 slave node and setup all the k8s cluster and deploy wordpress website.

\

This is my setup.yml file.

- hosts: localhost
vars:
instance_Name_tag: "master"
no_of_instances: "1"
vars_prompt:
- name: choice
prompt: "---Do You Want a master node instance,true or false---"
private: no
roles:
- name: Ec2 role to launch a master server
role: "ec2"
when: choice|bool == true
- hosts: localhost
vars:
instance_Name_tag: "slave"
vars_prompt:
- name: choice
prompt: "---Do You Want a slave node instances,true or false---"
private: no
- name: no_of_instances
prompt: "---How many slave node instances you want in Numbers,eg.1.---"
private: no
roles:
- name: Ec2 role to launch a master server
role: "ec2"
when: choice|bool == true
tasks:
- name: EC2 role to launch a master server
role: "ec2"
when: choice|bool == true
tasks:
- name: waiting for instances ssh
command: sleep 30
when: choice|bool == true
- name: refresh inventory
meta: refresh_inventory
- hosts: tag_Name_master
roles:
- name: "role for master"
role: "k8s_cluster"
- hosts: tag_Name_slave
roles:
- name: "role for master"
role: "k8s_cluster"
- hosts: tag_Name_master
tasks:
- name: "copy token file from local host to slave host"
copy:
src: database.yml
dest: /root
- name: "copy token file from local host to slave host"
copy:
src: wordpress.yml
dest: /root
- shell: "kubectl apply -f /root/database.yaml"
register: mysql
- shell: "kubectl apply -f /root/wordpress.yml"
register: wordpress
- debug:
msg: "{{ mysql }}"
- debug:
msg: "{{ wordpress }}"

database.yml file

apiVersion: v1
kind: Secret
metadata:
name: mysecure
data:
rootpass: 212121
userpass: 212121
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysecure
key: rootpass
- name: MYSQL_USER
value: kavin
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysecure
key: userpass
- name: MYSQL_DATABASE
value: sqldb
ports:
- containerPort: 3306
name: mysql

wordpress.yml file

apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
tier: mysql
type: LoadBalancer
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: wordpress:latest
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
- name: WORDPRESS_DB_USER
value: kavin
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysecure
key: userpass
- name: WORDPRESS_DB_NAME
value: sqldb
ports:
- containerPort: 80
name: wordpress

After completing playbook we can see our pods in master node

Now Copy the public IP of any of the node & respective port number and Go to your Google-chrome browser and paste it.

Now WordPress and MySQL database connected to it in the respective slaves successfully.

Thanks for reading and i hope you will like the Blog!!!

--

--