Creating Kubernetes Cluster Using Kind on local computer
Learning Kubernetes
Kubernetes is an open-source tool for automating deployment, scaling, and management of containerized applications.
Developers push the code multiple times a day. Packaging our applications as containers, enables our application to be updated multiple times without downtime. Kubernetes helps us to run the containerized applications.
We can create a Kubernetes cluster in our local computer by using tools such as minikube and kind. In this blog, we will look into the Kind.
Prerequisite:
- Docker
Tools :
kubectl - A Kubernetes command-line tool that allows us to run commands against Kubernetes clusters. We can deploy applications, inspect and monitor the cluster resources, and view logs using kubectl.
kind - It is a tool that lets us run the Kubernetes cluster on our local computer.
Install Kubectl
Install Kind
macOS
$brew install kind
Windows
$curl.exe -Lo kind-windows-amd64.exe https://kind.sigs.k8s.io/dl/v0.11.1/kind-windows-amd64
$Move-Item .\kind-windows-amd64.exe c:\some-dir-in-your-PATH\kind.exe
Create a Kubernetes cluster
Once kind is installed, create a kubernetes cluster using the command
$kind create cluster --name learning-kubernetes-dev
When the cluster is created, the cluster configuration file is stored at the location ${home}/.kube/config
We can create multiple kubernetes clusters. Let us define one more cluster for production
$kind create cluster --name learning-kubernetes-prod
To verify, if the cluster has been created, issue the below command.
$kind get clusters
learning-kubernetes-dev
learning-kubernetes-prod
To interact with a particular cluster, we need to specify the kind-<cluster-name>
as a context in kubectl as shown below.
➜ $kubectl cluster-info --context kind-learning-kubernetes-dev
Kubernetes control plane is running at https://127.0.0.1:52083
CoreDNS is running at https://127.0.0.1:52083/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
➜ $kubectl cluster-info --context kind-learning-kubernetes-prod
Kubernetes control plane is running at https://127.0.0.1:52449
CoreDNS is running at https://127.0.0.1:52449/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To verify which context is currently used, we can issue the below command.
$kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
kind-learning-kubernetes-dev kind-learning-kubernetes-dev kind-learning-kubernetes-dev
* kind-learning-kubernetes-prod kind-learning-kubernetes-prod kind-learning-kubernetes-prod
The indicates the current cluster. So the current cluster context is *kind-learning-kubernetes-prod
To switch between the clusters, we can issue the below command.
$kubectl config use-context kind-learning-kubernetes-dev
Switched to context "kind-learning-kubernetes-dev"
Let us verify again, this time, the current context is kind-learning-kubernetes-dev
$kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* kind-learning-kubernetes-dev kind-learning-kubernetes-dev kind-learning-kubernetes-dev
kind-learning-kubernetes-prod kind-learning-kubernetes-prod kind-learning-kubernetes-prod
Namespace
The virtual clusters which are created within the physical cluster are called namespaces. If we have multiple teams/projects, we can use the same cluster, but for each team/project, we can define the namespaces.
If we do not define any namespaces, then we get a default
namespace. We can use the below command to see the namespaces.
kubectl get namespaces
NAME STATUS AGE
default Active 23m
Create a namespace
We can define multiple namespaces in kubernetes clusters. In our learning-kubernetes-dev, we can have two namespaces. One is used by team-1 and the other is used by team-2.
Let's first switch the context to create a namespace in the learning-kubernetes-dev cluster
$kubectl config use-context kind-learning-kubernetes-dev
create a file called team1-namespace.json with the below content.
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "team-1",
"labels": {
"name": "team-1"
}
}
}
Use the below command to create a namespace.
$kubectl create -f team1-namespace.json
namespace/team-1 created
create a file called team2-namespace.json with the below content.
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"name": "team-2",
"labels": {
"name": "team-2"
}
}
}
Use the below command to create a namespace.
$kubectl create -f team2-namespace.json
namespace/team-2 created
Use the below command to verify the namespaces.
$kubectl get namespace
NAME STATUS AGE
default Active 44m
team-1 Active 2m13s
team-2 Active 18s
Create a Pod in a Namespace
A Kubernetes namespace provides the scope for Pods, Services, and Deployments in the cluster.
let's create a simple Deployment and Pods for both Team-1 and Team-2 namespace. Team-1 needs an NGINX server of 1 instance and Team-2 needs 2 instances.
$kubectl create deployment nginx-team-1 --image nginx -n team-1 --replicas=1
deployment.apps/nginx-team-1 created
$kubectl create deployment nginx-team-2 --image nginx -n team-2 --replicas=2
deployment.apps/nginx-team-2 created
Let's verify the number of pods created for team-1 and team-2
kubectl get pods -n team-1
NAME READY STATUS RESTARTS AGE
nginx-team-1-544dd7f5bd-qk5g6 1/1 Running 0 113s
kubectl get pods -n team-2
NAME READY STATUS RESTARTS AGE
nginx-team-2-55f89d7667-2wd9n 1/1 Running 0 87s
nginx-team-2-55f89d7667-8xd95 1/1 Running 0 87s
Conclusion
In this blog post, we saw how easy it is to create a Kubernetes cluster in your local computer. Once installed, we can play with Kubernetes and it helps us learn about pods, deployments. We saw how to create a namespace so that we can use one cluster with multiple teams deploying applications on the same cluster.
GitHub Repository: https://github.com/rahulmlokurte/devops-stuff/tree/main/kubernetes/kubernetes-kind
This is just the starting point. Check out the below documents to get into the depth of Kubernetes.