Set up a multi node Kubernetes cluster locally - using KinD
Local Kubernetes playground using KIND (Kubernetes in Docker)
Background
Have you ever wanted a simple Kubernetes cluster to try things out ? Or just to get familiar with kubernetes components, commands and other stuff ? Basically, have you ever wanted a Kubernetes Playground ?
There are many platforms that offer you kubernetes clusters to play with. For example: Katacoda, Play with Kubernetes, Minkube etc. Or else you can go with GKE(google), EKS(amazon) etc.
But as far as I found, there are limitations with those options. Either these clusters/environments are temporary(Katacoda, Play with Kubernetes etc) or you get only a single node cluster(Minukube) or you will have to pay for what you consume(GKE, EKS etc).
What if, we can set up a highly available kuberenetes cluster locally for our development and testing purposes ? Which is permanent and also it doesn't cost you a single penny. Sounds great ? Furthermore , if the cluster setup process is simple and straight forward ? If you think that is awesome, then this article is for you.
This can be used as a playground for practicing CKA,CKAD and CKS certifications.
Steps
1. Install Docker
You must have docker installed and running. You can select and follow the documentation as per your OS.
2. Install kubectl
Installing kubectl on your local machine will let you access the cluster from your local machine.
Follow this documentation to install and setup kubectl.
3. Install Kind
You can follow the below documentation or run below commands that I have mentioned.
On Linux:
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.9.0/kind-linux-amd64
chmod +x ./kind
mv ./kind /some-dir-in-your-PATH/kind
On Mac:
brew install kind
3. Add kind to PATH variable
This is not mandatory, but once this is done you can run ‘kind’ commands from anywhere without pointing to the executable. Therefore lets just add the executable to the PATH. I added below two lines in my ~/.bash_profile file. Im using a Mac.
export KIND_PATH=/usr/local/Cellar/kind/0.9.0/bin
export PATH=$PATH:$KIND_PATH
4. Create the cluster
Now everything is setup for creating a kubernetes cluster. Just running the “kind create cluster” command will give you a kubernetes cluster. But with default settings. Those default settings are,
- Cluster name will be ‘kind’
- Cluster will have only one node (control plane node only)
I want to change the cluster name and also I want to have 1 master node and 2 worker nodes. Lets see how we can achieve that.
4.1 Create kind config file
cat > kind-config.yaml <<EOF
# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
EOF
4.2 Create cluster
kind create cluster --name k8s-playground --config kind-config.yaml
Now the cluster is ready and you can use kubectl commands to work on the cluster. You can run kubectl commands from your local machine.
Bonus
Following will be Bonus stuff. Just thought of adding here. Please let me know in the comments if you face any issue while following this approach to play with k8s.
Change number of nodes in the cluster.
You can have control-plane nodes and worker nodes as many as you want. Gist for a kind config file with 3 control planes and 3 workers are as below. Create the kind-config.yaml file from below content and create the cluster pointing to this config file you created.
kind create cluster --name k8s-playground --config kind-config.yaml
SSH into a node
As these nodes are running as docker containers, We will have to use the docker command which we normally use to attach to the containers.
First you need to get the container ids
docker ps
docker exec -it c04633be10fd /bin/sh
Setup Kubectl Auto Completion and add alias
This will save a lot of time in your day today work.
And also if you are preparing for any exams like CKA,CKAD and CKS. This will be really helpfull during the exam. Therefore please take sometime to setup auto completion and alias for kubectl depending on your OS.
Im using a MacBook Pro, Steps in the above link didnt work for me for some reason. Then I found and followed the below blog post. It works fine for me.
Delete cluster
To delete the default clusters called ‘kind’ in your local machine
kind delete cluster
To delete a specific cluster
kind delete cluster --name k8s-playground