TASK 23

Saranya. S
3 min readJan 13, 2022

πŸ“Œ 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 slaves.

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

In our last article, we launched the k8S cluster over AWS using Ansible.
https://www.linkedin.com/posts/saranya-s23_arthtask-19-activity-6787208553674493952-t-vv

Now what we can do is create a role for automating the 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.

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

In order to launch the pod, we need a Kubernetes client on our computer configured as a client. Also, we need a WordPress image, maybe 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 an image of that also: mysql:5.7.

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

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 a shell module to execute the commands.

For MySQL pod we are required to pass environment variables:

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

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

We have to create service resources for this:
A Kubernetes Service is an abstraction layer that 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:

Our pod is exposed.

For this page we need a DB endpoint, thus to get the IP of the Mysql pod , we can use:

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

This will help to get the IP of the pod.

TASK COMPLETED !!!

--

--