Configure Kubernetes Cluster with Ansible and Deploy WordPress application

Jyoti Pawar
4 min readJun 12, 2022

Hello Connections,

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

πŸ”… Launch a WordPress and MySQL database connected to it in the respective Kubernetes Nodes.

πŸ”… Expose the WordPress pod and client able hit the WordPress IP with its respective port.

Here is the documentation to Configure the Multi-Node Kubernetes Cluster and deploy the WordPress application with Ansible Roles. There are three roles to do configuration

  1. To configure Kubernetes Master (kube-mater)
  2. To configure Kubernetes Nodes (kube-node)
  3. To configure WordPress on Kubernetes Cluster

Configure Kubernetes Cluster with Ansible :

see my article for this:

Configure WordPress Application on top of Kubernetes:

Our Kubernetes Cluster is working great. Now we just have to deploy the WordPress Application on top of Kubernetes

Let’s first initialize an ansible role for WordPress deployment, role is a quick way in ansible to manage multiple files based on the use of that particular file. The role is just the pre-defined directory structure to manage multiple ansible file

Initialize Ansible Role

ansible-galaxy init   <role-name>

The above command will create a directory structure to configure a particular use case, in our case, it’s WordPress Application

I am here integrating the Kubernetes manifests with Ansible and doing a simple step.

Ansible will copy the Kubernetes Manifest files to Kubernetes Control Plane and another task will apply those manifest files with kustomization.yaml file.

kustomization.yaml file is to run multiple Kubernetes manifest files from a single file.

wordpress-deploy role:

AnsibleConf-MultiNodeK8S/wordpress-deploy/
β”œβ”€β”€ defaults
β”‚ └── main.yml
β”œβ”€β”€ files
β”‚ └── wordpress
β”‚ β”œβ”€β”€ kustomization.yaml
β”‚ β”œβ”€β”€ mysql-deploy.yaml
β”‚ β”œβ”€β”€ mysql-svc.yaml
β”‚ β”œβ”€β”€ wordpress-deploy.yaml
β”‚ └── wordpress-svc.yaml
β”œβ”€β”€ handlers
β”‚ └── main.yml
β”œβ”€β”€ meta
β”‚ └── main.yml
β”œβ”€β”€ README.md
β”œβ”€β”€ tasks
β”‚ β”œβ”€β”€ main.md
β”‚ └── main.yml
β”œβ”€β”€ templates
β”œβ”€β”€ tests
β”‚ β”œβ”€β”€ inventory
β”‚ └── test.yml
└── vars
└── main.yml

These are the files present in wordpress-deploy role.

  • The task directory contains the tasks we have to configure /tasks/main.yaml
  • /files/wordpress/* β€” The manifest files to deploy the WordPress Appliction \
β”œβ”€β”€ files
β”‚ └── wordpress
β”‚ β”œβ”€β”€ kustomization.yaml
β”‚ β”œβ”€β”€ mysql-deploy.yaml
β”‚ β”œβ”€β”€ mysql-svc.yaml
β”‚ β”œβ”€β”€ wordpress-deploy.yaml
β”‚ └── wordpress-svc.yaml
  • The wordpress-deploy and mysql-deplo are the deployment files and the wordpress-svc and mysql-svc are the service for deployments created.

How the WordPress is configured with role :

  • The /tasks/main.yaml file has the tasks to copy the Kubernetes manifest files and apply those manifest files
---
# tasks file for wordpress-deploy
- name: copy wordpress manifest files to kube-master
copy:
src: wordpress
dest: /root
#when: false
- name: Deploy wordpress on Kubernetes
shell: kubectl apply -k /root/wordpress
#when: false

The files/wordpress/*

  • kustomization.yaml
secretGenerator:
- name: mysql-pass
literals:
- password=redhat
resources:
- mysql-deploy.yaml
- mysql-svc.yaml
- wordpress-deploy.yaml
- wordpress-svc.yaml

The secretGenertor will create a secret to use in the manifest files. This file is to create multiple resources with a single file

  • mysql-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mysql
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
- name: MYSQL_DATABASE
value: wpdb
- name: MYSQL_USER
value: siva
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
resources: {}
status: {}

This file contains the manifest to create mysql deployment

  • wordpress-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: wordpress
name: wordpress
spec:
replicas: 3
selector:
matchLabels:
app: wordpress
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: wordpress
spec:
containers:
- image: wordpress:5.1.1-php7.3-apache
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: mysql
- name: WORDPRESS_DB_USER
value: jsp
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
- name: WORDPRESS_DB_NAME
value: wpdb
resources: {}
status: {}

This file has the wordpress deployment manifest

  • mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: mysql
name: mysql
spec:
ports:
- port: 3306
protocol: TCP
targetPort: 3306
selector:
app: mysql
type: ClusterIP
status:
loadBalancer: {}

This file has the manifest to create a service with ClusterIP

  • wordpress-svc.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: wordpress
name: wordpress
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: wordpress
type: NodePort
status:
loadBalancer: {}

This file has the manifest to create svc with NodePort to expose the WordPress to clients

  • We can make the wordpress more stateful by adding resources like Storage Class and PVC. for this demonstration I am going without those resources.

Let’s apply the ansible playbook

The wp-deploy.yaml is the entrypoint to run the wordpress-deploy file.

  • wp-deploy.yaml
- hosts: tag_Name_KubeMaster
roles:
- role: wordpress-deploy

Resources Created on Kubernetes

  • Two Deployments are created [MySQL and WordPress]
  • Wordpress has 3 pods and MySQL has 1 pod
  • Two services are created
  • Let’s access the wordpress with Node IP and the port on which it was exposed β€” 31774

Wordpress Dashboard

Thank you !!

--

--

Jyoti Pawar

Devops || AWS || ML || Deep learning || Python || Flask || Ansible RH294 || OpenShift DO180