Configure Kubernetes Cluster with Ansible and Deploy WordPress application
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
Configure Kubernetes Cluster with Ansible
Here is my previous article to configure Kubernetes cluster with Ansible
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 I am 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.
├── files
│ └── wordpress
│ ├── kustomization.yaml
│ ├── mysql-deploy.yaml
│ ├── mysql-svc.yaml
│ ├── wordpress-deploy.yaml
│ └── wordpress-svc.yaml
How the WordPress is configured with role :
---
# 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/*
Recommended by LinkedIn
secretGenerator:
- name: mysql-pass
literals:
- password=redhat
resources:
- mysql-deploy.yaml
- mysql-svc.yaml
- wordpress-deploy.yaml
- wordpress-svc.yaml
The seceretGenertor will create a secrete to use in the manifest files. This file is to create multiple resources with a single file
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
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: siva
- 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
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
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
Let's apply the ansible playbook
The wp-deploy.yaml is the entrypoint to run the wordpress-deploy file.
- hosts: tag_Name_KubeMaster
roles:
- role: wordpress-deploy
Resources Created on Kubernetes
Wordpress Dashboard
Thank you