Automate Kubernetes Cluster Using Ansible

Durgesh Kumar Patel
3 min readJan 12, 2022

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.

The task describes to launch a multi-tier application over kubernetes cluster using Ansible. The entire workflow is automated .

The first few steps, I already covered in my previous story i.e Launching EC2 instance on AWS and configuring them as Kubernetes Master and Slave node.

Now, the kubernetes cluster is up and running good. Here, we have configured Master node as a client of k8s cluster. So, we can launch wordpress and mysql pods using Master node.

Steps to create Wordpress and MySQL pods.

  • We need to launch a MySQL pod with the image “mysql:5.7
  • While launching mysql pod we have to provide following environment variable.
  • — env=MYSQL_ROOT_PASSWORD=
  • — env=MYSQL_DATABASE=
  • — env=MYSQL_USER=
  • — env=MYSQL_PASSWORD=
  • Above env variables are used to create a database. Next, we have to launch Wordpress pod.
  • Launch Wordpress pod with appropriate image “wordpress:5.1.1-php7.3-apache”.
  • Now, expose the wordpress pod with type NodePort.
  • Connect to the Wordpress site at proper port and provide the database details and the wordpress site will be launched.

Writing a playbook

  1. Create a role. Eg: wpSQL
ansible-galaxy init wpSQL

2. In tasks/main.yml write the code to launch both mysql and wordpress pods.

- name: delete everything
shell: kubectl delete all --all- name: creating mysql database pod
shell: kubectl run "{{ db_pod_name }}" --image=mysql:5.7 --env=MYSQL_ROOT_PASSWORD=secretpassword --env=MYSQL_DATABASE=db1 --env=MYSQL_USER=tirth --env=MYSQL_PASSWORD=secretpassword- name: creating wordpress pod
shell: kubectl run "{{ wp_pod_name }}" --image=wordpress:5.1.1-php7.3-apache
- name: waiting for IP to comeup
pause:
seconds: 60- name: getting mysql database IP
shell: kubectl get pods -o wide
register: podsIP- name: expose wordpress pod
shell: kubectl expose pods {{ wp_pod_name }} --type=NodePort --port=80- name: get service
shell: kubectl get svc
register: service- name: printing exposed service
debug:
var: service.stdout_lines- name: printing pods IP
debug:
var: podsIP.stdout_lines

Now, we need to run this role in the main playbook i.e setup_cluster.yml.

Here, you can add the MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD as variable. Define variables inside vars/main.yml file in wpSQL role.

Here, I have paused the playbook so that the pod is launched and we get the details of pods for further connectivity.

- hosts: ["tag_Name_k8s_Master"]
roles:
- name: configure wordpress and mysql database and expose wordpress
role: /root/k8s_wp_sql_play/wpSQL/

Host will be Master node as we have also configured as a client.

Now, we get the Exposed Port number and further details of the pods. Connect to the wordpress site using the port number.

Thanku..

--

--