Kubernetes Cluster Setup Using Vagrant and Kind

Introduction
Kind is a tool that provides us to create a Kubernetes cluster. Through its simple use, it minimizes the time spent during cluster installation. We will use Vagrant additionally to make cluster installation even faster.
So, we will try to create a Kubernetes environment by installing Docker and Kind on the server that we raised with Vagrant today. Let’s start…
1) Preparing Vagrantfile
Before we get started, let’s go into what Vagrant is. Vagrant is a very useful tool that is used to create the virtual machine and manage created virtual machines. We will create a virtual Ubuntu machine with Vagrant. I prepared Vagrantfile as follows and kept the server’s features at the minimum level. You can change these settings according to you.
IMAGE_NAME = "bento/ubuntu-16.04" Vagrant.configure("2") do |config| config.ssh.insert_key = false config.vm.provider "virtualbox" do |v| v.memory = 2048 v.cpus = 2 end config.vm.define "kind" do |master| master.vm.box = IMAGE_NAME master.vm.network "private_network", ip: "192.168.50.10" master.vm.hostname = "kind" end end
When we run this Vagrantfile, in the Ubuntu-16.04 version, it raises the Ubuntu server which uses a certain CPU and memory, and which is we set the IP address. We are going to the path where this Vagrantfile is, from our terminal and we write in order the code below;
vagrant up vagrant ssh kind
We raised Bento/Ubuntu-16.04 box with the ‘Vagrant up’ command and then we wrote the other code ‘Vagrant ssh kind’ to reach this virtual server. If you are login into the server, it means everything OK.
2) Docker Setup
You can set up the Docker quickly, suitable for your environment at this address. I will add the steps required for Ubuntu by reference to Docker’s own installation instructions, in order below for you. You can implement these step by step at the server that we raised with Vagrant.
$ sudo apt-get update -y $ sudo apt-get install -y \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - $ sudo apt-key fingerprint 0EBFCD88 $ sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" $ sudo apt-get update -y $ sudo apt-get install docker-ce docker-ce-cli containerd.io -y
After doing the installation steps, let’s start a test container to be deleted.
$ docker run --rm hello-world && docker rmi hello-world

If you got an output like above, Docker had installed correctly.
NOTE: If you didn’t get an output like above, the reason is the command that we wrote at the end of the setup steps. When you exit and log in again, your problem should be solved.
3) Kind Setup
You can go to this GitHub address to set up the Kind and you can set up a Kind version that is suitable for your system. Linux users can do what I do.
$ kind version
After installing Kind, let’s check the version with the ‘kind version’ command. If you got an output like “kind v0.9.0 go1.15.2 linux/amd64”, let’s move on.
4) Kubectl Setup
We need to install the Kubectl tool, which is necessary to talk to the Kubernetes API. Here, you can do the suitable installation for your system.
$ curl -LO "[https://storage.googleapis.com/kubernetes-release/release/$(curl](https://storage.googleapis.com/kubernetes-release/release/$(curl) -s [https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl](https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl)" $ chmod +x ./kubectl $ sudo mv ./kubectl /usr/local/bin/kubectl $ kubectl version
5) Kubernetes Cluster Setup with Kind
Before I start, I want to touch on an important issue. If you, like me, set up the server in an environment such as Vagrant, VirtualBox, Vmware and install Docker and Kind on this server, you need to make some settings (I’ll talk about it in a moment). But otherwise, if you are using Linux and your Docker is on Linux or if you are using windows and your Docker is Docker desktop, your job is quite easy. So what are these settings?
Kind works as a Docker container, and therefore, when we add a label (vagrant, etc.), we aren’t able to access the applications we have created on this node from outside (you can only access the container from inside the server or as exec). At this point, we need to have a config file that we need to prepare while raising the cluster, and we need to specify the ports we want in this config file. Since I will give nodePort to applications, this port range should be 30000-32767. So, I need to open a port to the outside in this range. Let’s continue our transactions without further ado, so you can understand better. Let me show you how to set up a cluster for local users first and then Vagrant, Vmware, etc. what needs to be done for those who work in these environments.
5.1) Cluster Setup for Who Don’t Use Virtual Server (Vagrant, Vmware, etc.)
If you don’t use an external server or Vagrant, you don’t need to create the file below. You can run Node with the command below directly.
$ kind create cluster --name NODE-ISMI
You can continue your operation by running the above command (- name- parameter is not mandatory). If you don’t add it, the Node name sets as default and Kind. At first, it may take some time to capture the image, although it depends on your internet speed (next cluster installations are processed very quickly). Then you should see a screen like the one below.

If you see a screen like this congrats, your Node is ready. When we write the command below to see our Node, we come across some information about our Node;
$ kubectl get node
Ok, we can test with an application anymore. There is a sample deployment.yaml file below, if you wish, you can test your node with this deployment (you can skip the config step).
5.2) Cluster Setup for Who Uses Virtual Server (Vagrant, Vmware, etc.)
As I mentioned above if you are using Vagrant, Vmware, etc., first you must create a file called kindconfig.yaml and set it up as mentioned below.
The transaction we are doing here is this; We tell our master node to open port 30080, so if there is an application running on port 30080 in the node, we will be able to access this application from outside. Let’s try it on a test deployment right now.
Now, we are creating a deployment and a service with this deployment.yaml file. We are basically running a nginx actually. As you can see in the Service section, I use NodePort as the type of this service and I gave the port 30080 as nodePort. If you remember, we used this port in kindconfig.yaml file as well. Now let’s raise our application for this;
$ kubectl apply -f deployment.yaml

If your pod’s STATUS value is Running like the above picture, it means it works successfully. Now, to test this, open any browser from your local computer and write to the address line 192.168.50.10:30080 (according to your own IP address) value and press enter.

If a screen like the one above welcomes you, your application and node are running successfully. When you are done, you need to use the following command to delete the cluster;
$ kind delete clusters [NODE-NAME]
Later, to start again, it is enough you enter the command above. The image is ready in Docker so it will take a very short time to set up the cluster.
NOTE: Don’t forget, if you use a virtual server when you delete and create a cluster again, you must use – config parameter. We opened one port but you can open more ports and you can reach the applications in this way. If you don’t want to use nodePort, there are many ways except this, one of them _kubetcl port-forward_. If you wish you can reach more information here.
Kind is a very useful tool because it could set up a cluster environment quickly and we could test quickly. But there are many problems and I believe that these problems will be eliminated as time passes. For more information, let’s take you to Kind’s documentation.
Take care of yourself.
Author: Doğukan Turan
Date Published: Apr 29, 2021
