Deploying Kubernetes Pod with Ansible on AWS

@Harsh
5 min readNov 29, 2023

--

Introduction

In this tutorial, we’ll explore how to set up a Kubernetes environment using Minikube and deploy a containerized application using Ansible. The goal is to have a clear, step-by-step guide for deploying a Kubernetes pod on AWS, with one instance serving as the Ansible controller node and another as the Minikube server.

Part 1: Setting Up the Ansible Controller Node

Step 1: Launching an AWS EC2 Instance

Begin by launching an AWS EC2 instance to serve as the Ansible controller node. This instance will be responsible for orchestrating and managing the deployment process.

Step 2: Installing Ansible

Connect to the Ansible controller node via ssh and install Ansible. Ansible will act as the automation tool to deploy and manage resources on the Minikube server.

su - root
yum install ansible-core

Additional steps :

Go to this file after installation :

But before this run this command on the terminal :

ansible-config init --disabled > ansible.cfg

Now open the ansible.cfg file and make the following changes there :

This option is by-default True there, make it False.

Part 2: Setting Up the Minikube Node

Step 1: Launching Another AWS EC2 Instance

Launch a second EC2 instance to serve as the Minikube server. This instance will host the Kubernetes cluster and the Docker containers.

We also need to do some ssh configuration there to enable ssh connection :

vi /etc/ssh/sshd_config

Change the permit root login to yes. It was set as no by-default.

Then at the last line, change password authentication to yes :

Step 2: Installing Minikube, kubectl, and Docker

Connect to the Minikube server and install Minikube, kubectl, and Docker. These tools are essential for setting up and managing a Kubernetes environment.

  1. Installing Minikube :

Follow this document to set up mini-kube: minikube_documentation

2. Installing kubectl :

Follow this documentation to install kubectl : kubectl_documents

3. Install Docker :

yum install docker

If the above command gives an error, then do the follwing steps:

cd /etc/yum.repos.d
vi docker.repo

Paste the follwing line in the file:

[docker-repo]
baseurl=https://docker.download.com/linux/centos/7/x86_64/stable
gpgcheck=0

Finally run the yum command :

yum install docker-ce --nobest

Start the docker services :

systemctl start docker

Step 3: Starting Minikube

minikube start

Minikube doesn’t start with root priviledges. So for starting minikube, you need to switch user.

So first create newUser and run the following command :

useradd newUser
passwd newUser
su - newUser
sudo groupadd docker
sudo usermod -aG docker $USER

Then start the minikube :

minikube start --driver=docker

Part 3: Ansible Playbook Explanation

Create an Ansible playbook named kubernetes.yml.

This Ansible playbook performs the following tasks:

  • Deploys a Kubernetes pod with an my-web-container using pre-created image on Minikube.
  • Checks the status of the deployed pod using kubectl get pods.
  • Waits for the pod to be running.
  • Prints the status information of the pod.

Get this entire code here: https://github.com/harsh2478/Ansible-kubernetes

Inventory :

Before running this playbook, update the inventory :

vi /etc/ansible/hosts

Installing module :

k8s module of ansible comes from community.kubernetes collection.

So we need to first install it :

ansible-galaxy collection install community.kubernetes 

Kubernetes also need some python library as it makes use of python. So make sure that python is installed on your target node.

And then install this library :

Running the Ansible Playbook

Execute the Ansible playbook on the Ansible controller node:

ansible-playbook kubernetes.yml

Part 4: Results

You will see the result something like this, but not in green colour, if everything goes right in a single run.

Conclusion

In this tutorial, we navigated the process of deploying a Kubernetes pod on AWS using Ansible. By configuring an Ansible controller node on one AWS EC2 instance and a Minikube server on another, we established a seamless deployment pipeline. The Ansible playbook, kubernetes.yml, orchestrated the setup, starting Minikube, deploying a pod with web-container. This streamlined approach showcased the power of Ansible in automating complex infrastructure tasks. As we verified the deployment on the Minikube server, we witnessed the synergy between Ansible and Kubernetes, empowering efficient, repeatable, and scalable containerized application deployments on the cloud. This tutorial equips you with valuable insights to confidently navigate and scale your Kubernetes deployments with Ansible.

Happy deploying!

--

--

@Harsh
@Harsh

Written by @Harsh

A devOps engineer from India

No responses yet