Deploy Kubernetes Cluster via Ansible and Kind

kumail1232

Kumail Rizvi

Posted on February 15, 2023

Deploy Kubernetes Cluster via Ansible and Kind

Kubernetes has become the go-to solution for container orchestration, allowing developers to easily manage and scale their containerized applications. However, setting up and managing a Kubernetes cluster can be a complex task. This is where Ansible comes in – it is a powerful automation tool that can simplify the deployment and management of Kubernetes clusters. In this blog, we will explore how to use Ansible to deploy a Kubernetes cluster using KIND (Kubernetes IN Docker).

What is KIND?

KIND is a tool that allows you to run Kubernetes clusters inside Docker containers. It is lightweight and easy to set up, making it an ideal choice for testing and development environments. KIND also supports creating multi-node clusters, making it a good choice for more complex environments.

Setting up the environment

Before we can deploy a Kubernetes cluster using Ansible and KIND, we need to set up the environment. First, we need to install Docker and Ansible on our system. Then, we need to install KIND. We can do this by running the following command:

$ curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
$ chmod +x ./kind
$ sudo mv ./kind /usr/local/bin/kind
Enter fullscreen mode Exit fullscreen mode

Once we have KIND installed, we can use it to create a new Kubernetes cluster. We can create a cluster with a single node by running the following command:

$ kind create cluster --name my-cluster
Enter fullscreen mode Exit fullscreen mode

This will create a new Kubernetes cluster named "my-cluster" with a single node (you can use any name while creating a new cluster )

Deploying Kubernetes using Ansible

Now that we have our environment set up, we can use Ansible to deploy Kubernetes. Ansible allows us to automate the process of installing and configuring Kubernetes on our cluster.

First, we need to create an Ansible inventory file that specifies the hosts we want to configure. We can create a file named inventory.ini with the following contents:

[kubernetes-nodes]
localhost ansible_connection=local
Enter fullscreen mode Exit fullscreen mode

This specifies that we want to configure the localhost host as a Kubernetes node.

Next, we need to create an Ansible playbook that defines the tasks we want to perform. We can create a file named deploy.yml with the following contents:

- name: Install Kubernetes
  hosts: kubernetes-nodes
  tasks:
  - name: Install kubeadm, kubelet, and kubectl
    become: true
    apt:
      name: ['kubelet','kubeadm','kubectl']
      state: present

  - name: Initialize Kubernetes cluster
    become: true
    command: kubeadm init --pod-network-cidr=10.244.0.0/16

  - name: Copy kubeconfig to user's home directory
    become: true
    copy:
      src: /etc/kubernetes/admin.conf
      dest: /home/user/.kube/config
      remote_src: yes

  - name: Install Calico CNI
    become: true
    command: kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml
Enter fullscreen mode Exit fullscreen mode

NOTE
Here iam using CNI ( Container Network Interface ) as Calico , you can use any different CNI such as Flannel, Canal etc

This playbook installs the necessary packages (kubelet, kubeadm, and kubectl), initializes the Kubernetes cluster using kubeadm, copies the kubeconfig file to the user's home directory, and installs the Calico CNI.

We can then run the playbook using the following command:

$ ansible-playbook -i inventory.ini deploy.yml
Enter fullscreen mode Exit fullscreen mode

This will run the deploy.yml playbook on the localhost host, configuring it as a Kubernetes node and deploying the necessary components.

That's it and we are done. We created an Kubernetes cluster with the help of ansible and kind

Thank you and I hope you enjoy it.

References
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_intro.html
https://kind.sigs.k8s.io/
https://kubernetes.io/docs/concepts/overview/working-with-objects/kubernetes-objects/
https://www.suse.com/c/rancher_blog/comparing-kubernetes-cni-providers-flannel-calico-canal-and-weave/#:~:text=In%20this%20article%2C%20we'll,containers%20are%20created%20or%20destroyed.

💖 💪 🙅 🚩
kumail1232
Kumail Rizvi

Posted on February 15, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related