Automate Kubernetes Cluster Using Ansible

Ritesh Singh
3 min readMar 13, 2021

Problem Statement

  • Launch ec2-instances on AWS Cloud eg. for master and slave.
  • Create roles that will configure the master node and slave node separately.
  • Launch a wordpress and MySQL database connected to it in the respective slaves.
  • Expose the wordpress pod and the client able to hit the wordpress IP with its respective port.

Solution

  • For Configuring and launching the k8smulti node cluster I am using my own ansible collection 😅.
  • For Wordpress and Mysql we need to create yml files for k8s and deploy them.

Step-1)Launch ec2-instances on AWS Cloud and Configuring k8s Multi-Node

  • Install collection
ansible-galaxy collection install rootritesh.k8s_cluster

Learn More:

  • Only we need to copy the files from local yml files for k8s of wordpress+mysql to the cluster so that we can execute that.

Step-2)Creating a wordpress.yml file.

  • Create a file wordpress.yml
  • This file divided into two parts.
  • First One for service for exposing the wordpress
  • Second: for the deployment of wordpress.
  • In this deployment, I have already included the env for hosts, password so that we don’t need to type the hosts at the time of configuration of Wordpress
  • For password, I have used Secret.

Step-3)Creating a db.yml file.

  • Create a db.yml file.
  • Same here also two parts.
  • First: for Service, I am using type ClusterIP for making db secure.
  • Second and finally: For deployment, in this deployment for the MySQL root password I am using secret.

Step-3)Creating a Kustomization.yml file

  • Create a file kustomization.yml
  • In this file, I have created secrets and included files for deployment.

Step-4)Running the files

  • Launching ec2 on AWS.
  • Calling the ec2 role from ansible collections.
  • Configuring k8s multi-node and copying files for deployment.
  • First I am calling role k8s_master for configuring k8s master and k8s_slave for configuring k8s slave.
  • After this, I am copying deployments files from local to remote to the k8s master-slave.
  • And finally, running the file using shell module, kubectl apply -k .(this command finds the kustomization file and run it).

Output 🚀

  • EC2 instance launched 😀.
  • K8s Multi-Node Configured 😄.
Wordpress Site
  • When you open Wordpress, you don’t need to add MySQL DB, pass, etc, because at the time of deployment in wordpress.yml, for these keywords, I have used labels.

Github Link:

--

--