
Ansible Inside Kubernetes
Hello all, i hope you all are doing good in your journey. This article will helps you to run ansible inside kubernetes. But why we need to run ansible on top of kubernetes. To configure ansible we require controller node as well as managed node(target node). Depends on the workload and how many system we have to configure we require that much nodes. Getting these nodes on cloud is easy but it cost as the usage of the nodes increases. So we can use kubernetes resources to manage. Kubernetes will helps us to create controller pod and managed pod(target pod) in a single vm instance.
Take away from this article:
- Installing minikube on aws to create a single node.
- Installing Kubectl command
- Installing Ansible inside kubernetes pod.
- Configuring ssh keys.
- Configuring Ansible inside pod.
Launching Ubuntu:20.04 ec2-instance on AWS:

Here i am using Ubuntu Server 20.04. the minikube will be installed in this ec2-instance and uisng t2.medium (2 cpu’s and 4 GB Ram).


Installing Kubectl:
The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. Now we can install kubectl command using below command. Kubectl will helps us to control kubernetes cluster.

Installing kubectl.
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectlmaking kubectl in executable mode.
chmod +x ./kubectl moving kubectl to /usr/local/bin/ folder
sudo mv ./kubectl /usr/local/bin/kubectl
Installing docker:

Refer below command to install docker on ubuntu.
sudo apt-get update && \
> sudo apt-get install docker.io -y
Minikube Installation:
Minikube is a lightweight Kubernetes implementation that creates a VM on your local machine and deploys a simple cluster containing only one node. … The Minikube CLI provides basic bootstrapping operations for working with your cluster, including start, stop, status, and delete.
Installing minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/


Installing conntrack:
Conntrack is command line interface conntrack provides a more flexible interface to the connnection tracking system than /proc/net/ip_conntrack. With conntrack, you can show, delete and update the existing state entries; and you can also listen to flow events. conntrackd is the user-space connection tracking daemon.
sudo apt install conntrack -y

Starting Minikube:
minikube start --vm-driver=none

Creating and configuring ansible-master pod.
kubectl run ansible-master --image=centos:7 -- /bin/sleep 3650dkubectl get pods

Installing below software in ansible-master pod:
1. Installing openssh-clients and openssh-server
yum install openssh-clients openssh-server -y2. Generating SSH keys.
ssh-keygen -A3. For restarting ssh service.
/usr/sbin/sshd -D -e "$@" 4. Save this above command in /root/.bashrc file.5. Changing password for root user
passwd root6. Installing epel-release repo.
yum install epel-release -y7. Installing ansible.
yum install ansible -y






Adding the inventory location with host_key_checking=false with remote_user=root in /etc/ansible/ansible.cfg file.
[defaults]
inventory = /etc/ansible/hosts
host_key_checking = false
ask_pass = false
remote_user = root

172.17.0.4 is the ip of ansible-slave1 pod. you can check the ip address by installing net-tools software.
yum install net-tools -y will give you ifconfig command
Adding the ip address of ansible-slave1 pod with user and password inside /etc/ansible/hosts file.
172.17.0.4 ansible_ssh_connection=root ansible_ssh_pass=redhat

Creating and configuring ansible-slave pod.

Installing below software in ansible-master pod:
1. Installing openssh-clients and openssh-server
yum install openssh-clients openssh-server -y2. Generating SSH keys.
ssh-keygen -A3. For restarting ssh service.
/usr/sbin/sshd -D -e "$@"4. Save this above command in /root/.bashrc file.5. Changing password for root user
passwd root




Listing all the hosts in the inventory file.

Pinging the hosts.

- hosts: slave1
vars:
software:
- "httpd"
- "php"
tasks:
- name: "installing software"
yum:
name: "{{ item }}"
state: absent
loop: "{{ software }}"
Running playbook.

I hope you had liked the article. 🔥🔥😍😍