Automating K8s Cluster using Ansible

What is KUBERNETES?

Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.

What is ANSIBLE?

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.

KUBERNETES CLUSTER :

A Kubernetes cluster is a set of node machines for running containerized applications. If you’re running Kubernetes, you’re running a cluster.

Kubernetes clusters allow containers to run across multiple machines and environments: virtual, physical, cloud-based, and on-premises. Kubernetes containers are not restricted to a specific operating system, unlike virtual machines. Instead, they are able to share operating systems and run anywhere.

The master node controls the state of the cluster; for example, which applications are running and their corresponding container images. The master node is the origin for all task assignments. It coordinates processes such as:

  • Scheduling and scaling applications
  • Maintaining a cluster’s state
  • Implementing updates

The worker nodes are the components that run these applications. Worker nodes perform tasks assigned by the master node. They can either be virtual machines or physical computers, all operating as part of one system.

There must be a minimum of one master node and one worker node for a Kubernetes cluster to be operational. For production and staging, the cluster is distributed across multiple worker nodes. For testing, the components can all run on the same physical or virtual node.

A namespace is a way for a Kubernetes user to organize many different clusters within just one physical cluster. Namespaces enable users to divide cluster resources within the physical cluster among different teams via resource quotas. For this reason, they are ideal in situations involving complex projects or multiple teams.

LET’S JUMP TO OUR PRACTICAL

TASK DESCRIPTION:

Ansible Role to Configure K8S Multi Node Cluster over AWS Cloud.
🔅 Create Ansible Playbook to launch 3 AWS EC2 Instance
🔅 Create Ansible Playbook to configure Docker over those instances.
🔅 Create Playbook to configure K8S Master, K8S Worker Nodes on the above created EC2 Instances using kubeadm.
🔅 Also Upload all the YAML code over your GitHub Repository.

AND

🔅 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.

So, we are combining both the tasks.

Let’s begin,

I have included all the steps of configuring Kubernetes cluster in the below video.

Please go through the video and configure it accordingly.

🎥Video

Kudos!! We have configured Kubernetes cluster successfully!!

So, our first practical got completed.

NEXT

Let's proceed to the next one -->

After doing all the above steps now we have to Launch a WordPress and MySQL database connected to it in the respective slave and Expose the WordPress pod and client able hit the WordPress IP with its respective port.

Ok, Lets begin next part!!

Create a new role for wordpress using the below command:

ansible-galaxy init wordpress_configuration

We are going to write role for setup of Wordpress and MySQL

Go inside the file folder of wordpress role folder

To configure wordpress installation part inside wordpress.yml file

apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
nodePort: 30333
selector:
app: wordpress
tier: frontend
type: LoadBalancer
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: frontend
spec:
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wordpress-pv-claim
containers:
- image: wordpress:4.8-apache
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysqlsecret
key: password
ports:
- containerPort: 80
name: wordpress
volumes:
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html

Next, create a Kubernetes pvc_wordpress.yml file

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wordpress-pv-claim
labels:
app: wordpress
tier: frontend
spec:
storageClassName: ""
resources:
requests:
storage: 1Gi
accessModes:
- ReadWriteOnce
---

apiVersion: v1
kind: PersistentVolume
metadata:
name: wordpress-pv
spec:
storageClassName: ""
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /wordpressdata

Next, create a Kubernetes mysql.yml file

apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysqlsecret
key: password
- name: MYSQL_USER
value: abcdefgh
- name: MYSQL_DATABASE
value: 123456789
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql

Next, create a Kubernetes pvc_mysql.yml file

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: wordpress
tier: mysql
spec:
storageClassName: ""
resources:
requests:
storage: 1Gi
accessModes:
- ReadWriteOnce
---

apiVersion: v1
kind: PersistentVolume
metadata:
name: msql-pv
spec:
storageClassName: ""
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /mysqldata

Now, create a secrete file “secret.yml” which will contain the password of MySQL database

apiVersion: v1
kind: Secret
metadata:
name: ABCHEF
data:
password: xxxxxxxxxx

Edit the main.yml file inside the tasks folder in the wordpress_configuration role:

- name: Copying Wordpress and MySQL files Master Node
copy:
src: "{{ item }}"
dest: /root/
loop:
- mysql.yml
- pvc_mysql.yml
- pvc_wordpress.yml
- secret.yml
- wordpress.yml

- name: Creating directory over which MySQL container mounts the PersistentVolume at /var/lib/mysql.
file:
path: /mysqldata
state: directory

- name: Creating directory over which WordPress container mounts the PersistentVolume at /var/www/html.
file:
path: /wordpressdata
state: directory


- name: Setup of Wordpress and MySQL
shell: "kubectl create -f /root/{{ item }}"
loop:
- mysql.yml
- pvc_mysql.yml
- pvc_wordpress.yml
- wordpress.yml

Now it’s finally the time to edit final-run.yml file inside kubernetes_cluster folder which we have setup for k8s configuration.

- hosts: kubernetes_master
gather_facts: no
tasks:
- name: Running Wordpress and Mysql
include_role:
name: wordpress_configuration

use below commands to run your ansible playbook.

ansible-playbook final-run.yml --ask-vault-pass

Now once our pods are ready, then we can take the public ip of any node either master or slave. We will land to the Wordpress login page.

Here we moved over to the master IP on port 30333 and we can see the WordPress

Wordpress application is ready !!

Thank You!!

I hope you got an idea about the task

For more such articles

Stay Connected 😄

I’ll be grateful to have connections like you on Linkedln

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store