Wordpress and SQL pod on the top of K8S using ansible.

Vishugoyal
3 min readJan 17, 2022

Welcome again to the another article of automation.

Today we are going to launch wordpress and sql pod over K8S multi-node cluster using Ansible.

In our last article we launched k8S cluster over AWS using Ansible.
Now lets continue further integration using ansible.

Now what we can do is create a role for automating deployment of pod over K8s cluster.

Requisites:

  1. Get epel release software and install
  2. Install python3 and python3-pip
  3. Upgrade aws cli version.

In this demonstration ,I would be using shell module of ansible to do deployment .

You can try Ansible K8S object to deploy pods over Cluster:

We will need credentials for going to instance and do configurations thus ,I have stored all credentials in ansible-vault.

In order to launch pod we need kubernetes client on our computer configured as a client.In earlier integration ,we did the needful.

Also we need WordPress image,may be you create it yourself and deploy or we have wordpress:5.1.1-php7.3-apache .

WordPress uses SQL as a database,thus we require image of that also :

mysql:5.7.

Note: Your WordPress version should be compatible with SQL version.

For more details refer documentation.

To launch pod over K8S we use:

kubectl run <podname> --image=<image_name>

Lets see how we can do this using ansible:

We have used shell module to execute the commands.

For mysql pod we required to pass environment variables:
For more info refer to this

  1. MYSQL_ROOT_PASSWORD
  2. MYSQL_DATABASE
  3. MYSQL_USER
  4. MYSQL_PASSWORD

Once we have launched the pod ,we need to expose Wordpress pod in order to access it .

We have to create service resource for this:
A Kubernetes Service is an abstraction layer which defines a logical set of Pods and enables external traffic exposure, load balancing and service discovery for those Pods.

Although each Pod has a unique IP address, those IPs are not exposed outside the cluster without a Service. Services allow your applications to receive traffic. Services can be exposed in different ways by specifying a type in the ServiceSpec:

  • ClusterIP (default) — Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.
  • NodePort — Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using <NodeIP>:<NodePort>. Superset of ClusterIP.
  • LoadBalancer — Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service. Superset of NodePort.
  • ExternalName — Maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is set up. This type requires v1.7 or higher of kube-dns, or CoreDNS version 0.0.8 or higher.

We will be using NodePort type to expose the pod using service:

Tadaa!!! Our pod is exposed .

Now,

For this page we need DB end point,thus to get IP of Mysql pod ,we can use:

- name: "Fetching DB IP"
shell: "kubectl get pod sqldb -o wide"
register: dbip

This will help to get ip of the pod .

🎉🎉Finally,we have done our setup using Ansible over K8S …..

k8s_deploy role is available on Github .

Happy learning …..😁😁😁

--

--

Vishugoyal

I am pursiing B-tech. and also very fond of learning new technology under the guidance of Vimal Daga sir (World Record Holder).