Kubernetes With Ansible

Shashwot Risal
4 min readFeb 5, 2022

It’s critical to automate the management of each containerized application if you’re managing a big number of containers. In case you are wondering how, it is possible using the Ansible Kubernetes module. The Ansible Kubernetes module provides access to the whole set of Kubernetes APIs and allows you to create objects such as Kubernetes deployment and Kubernetes service.

Installations

UBUNTU

$ sudo apt update -y
$ sudo apt install ansible -y

CENTOS

$ sudo yum install epel-release -y
$ sudo yum update -y
$ sudo yum install ansible -y

PIP

$ python3 -m venv venv
$ source ./venv/bin/activate
(venv) $ pip3 install --upgrade pip
(venv) $ pip3 install ansible

Verify Installation

$ ansible --version

Setting Up the Inventory File

Ansible takes the default configuration from /etc/ansible/ansible.cfg . But we do not want to mess up with the default configurations in case things go wrong. So we create a new configuration with the following configs. Also, ansible takes the host inventory from /etc/ansible/hosts where the hosts are defined which we will configure to override the values as follows:

$ mkdir ansible-kubernetes$ cd ansible-kubernetes$ cat <<EOF > ansible.cfg
[defaults]
inventory = ./dev
EOF
$ cat <<EOF > dev
[test]
test-1 ansible_ssh_host=x.x.x.x ansible_ssh_user=ubuntu
EOF
Ansible configuration

You should have something similar to this, replace the x.x.x.x with the IP of the server where you perform your kubectl executions from and the user to perform action with. So with this we have defined our own ansible configuration for the host inventory.

Creating Kubernetes Manifests

Lets create the kubernetes manifest file for a deployment and a pod on a different namespace. We create a folder called k8s where we will store the kubernetes manifest files.

$ mkdir k8s$ cd k8s$ cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
Namespace: test
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: shashwot/nginx-more:latest
ports:
- containerPort: 80
EOF
$ cat <<EOF > pod.yaml
apiVersion: v1
kind: Pod
Namespace: test
metadata:
name: nginx
labels:
app: nginx
tier: frontend
spec:
containers:
- name: nginx
image: shashwot/nginx-more:latest
EOF

We should have two files namely deployment.yaml and pod.yaml with the above configurations.

Creating the ansible playbook

If you followed all prerequisites, you should already have a working inventory file. Now, lets create the ansible playbook for kubernetes object inside a folder called playbooks.

$ mkdir playbooks$ cd playbooks$ cat <<EOF > kubernetes.yaml
---
- hosts: '{{ host }}'
tasks:

- name: "Install kubernetes python package" #This will be installed on the remote host.
pip:
name: kubernetes
state: present
# Create a test namespace on the cluster without any manifest fies. This is an added advantage on ansible.
- name: "Create a k8s namespace"
k8s:
name: test
api_version: v1
kind: Namespace
state: present
# Copying the Pod.yaml and deployment.yaml in the remote node.
- name: "copying file with playbook"
copy:
src: ../k8s/pod.yaml
dest: /tmp/pod.yaml
- name: copying file with playbook
copy:
src: ../k8s/deployment.yaml
dest: /tmp/deployment.yaml
# Creating a Kubernetes pod in test using file stored on local.- name: "Create a pod"
k8s:
state: present
namespace: test
src: /tmp/pod.yaml
# Checking if the Kubernetes pods is running on the cluster.
- name: "Status of the pod"
k8s:
api_version: v1
kind: pod
name: nginx
namespace: test
register: web_service
# Creating a Kubernetes deployment in test using file stored locally
- name: "Create a deployment"
k8s:
state: present
namespace: test
src: /tmp/deployment.yaml
# CleanUP all the applied configurations
- name: "Ansible file module to delete multiple files"
file:
path: "{{ item }}"
state: absent # to delete the files
with_items:
- /tmp/deployment.yaml
- /tmp/pod.yaml
# Clear the namespace on the cluster
- name: "Delete a k8s namespace"
k8s:
name: test
api_version: v1
kind: Namespace
state: absent
EOF

Execution

With the above configutaions, our folder structure should look similar to this.

Folder Structure

Lets execute the ansible playbook to run in the cluster. Here, host is the name of remote machine on the dev file which we created earlier on.

$ ansible-playbook playbooks/kubernetes.yaml -e host=test-1
Output

Conclusion

In this way, we have used the Ansible Kubernetes module in a playbook to create and deploy Kubernetes objects. With this Ansible Kubernetes module, we can effectively work with Kubernetes objects within the cluster on remote hosts.

References

https://github.com/shashwot/ansible-kubernetes

--

--