Configure Kubernetes Cluster with Ansible and Deploy WordPress application

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

  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

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.

  • 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 seceretGenertor will create a secrete 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: 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

  • 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
        
No alt text provided for this image

Resources Created on Kubernetes

  • Two Deployments are created [MySQL and WordPress]
  • Wordpress has 3 pods and MySQL has 1 pod
  • Two services are created

No alt text provided for this image

  • Let's access the wordpress with Node IP and the port on which it was exposed - 31774

No alt text provided for this image


No alt text provided for this image

Wordpress Dashboard

No alt text provided for this image


Thank you

To view or add a comment, sign in

More articles by Siva Naik Kethavath 🌱

Insights from the community

Others also viewed

Explore topics