DEV Community

Cover image for Kubernetes - Getting Started
Surendu Suresh
Surendu Suresh

Posted on

Kubernetes - Getting Started

When you think about DevOps, Kubernetes is one of the most important tool you need to learn. It is easy to grasp and valuable tool to have under your belt. This article will give you some basic understanding of Kubernetes and a bit of hands-on experience.

Prerequisites

  • Basic knowledge of Linux operating system.
  • Basic knowledge of container technologies like Docker.
  • Basic knowledge of docker registry like DockerHub.

What is Kubernetes

Kubernetes is an open-source container orchestration tool used for deploying, managing and monitoring the containerized applications. It was developed by Google and later donated to Cloud Native Computing Foundation (CNCF) which is a sub-foundation of Linux Foundation.

Kubernetes supports different container runtimes, including containerd, Docker Engine, CRI-O, Mirantis Container Runtime etc.

What are Containers, Pods and Nodes

  • Container is a bundling of software that packages up code and all its dependencies.
  • Pod is a group of one or more containers, with shared storage, network resources and a specification for how to run the containers.
  • Node is a physical or virtual machine which runs the workload Kubernetes places in it.

You can think of Kubernetes as a ship carrying lot of containers on it. In reality, Kubernetes cluster, is a group of physical or virtual machines, which can run and manage multiple containers in the form of Pods.

So, let's look at the architecture of Kubernetes and it's building blocks.

Kubernetes Architecture

Control Plane Components

kube-apiserver

The API Server acts as a front end for the Kubernetes. It manages all the interactions between all other components. You can run multiple instances of kube-apiserver by deploying more instances.

etcd

etcd is a key value store used by Kubernetes to store all data related to the Kubernetes cluster. You can backup the cluster by taking backup of the etcd data.

kube-scheduler

kube-scheduler is responsible for watching for unassigned pods and scheduling it in one of the available nodes. The node is selected based on the resource requirements of the pod and the resource availability in the nodes among other things.

kube-controller-manager

This component runs all the controller processes. There are many controller processes, Node controller, Job controller, Replication Controller, etc.

cloud-controller-manager

This is an optional component which helps in embedding cloud specific control logic. It lets you link the cluster to your cloud provider.

DNS

Cluster DNS is a DNS server which stores a DNS record for Kubernetes services. Each new services and pods created in Kubernetes has an entry in the DNS.

Node Components

Node components runs on every node and responsible for managing the pods.

kubelet

kubelet is responsible for running the containers in the pod. It is responsible for restarting any crashed pods and making sure the desired amount of pods are running as per the specification.

kube-proxy

kube-proxy maintains networks rules on nodes. It enables communication between pods and from outside your cluster.

Container runtime

Container runtime is responsible for running the container inside the pod. Kubernetes supports various container runtimes which follows the Kubernetes Container Runtime Interface (CRI).

Playing with Kubernetes

Enough with the theory! Let's get started running a Kubernetes cluster on your laptop. Easiest way to get your hands on Kubernetes is to install Docker Desktop application.

Docker Desktop Installation For Windows

Above link will guide you through the steps to install Docker Desktop for Windows. Similar guides are available for Mac and Linux OS as well. For Windows, we need to enable WSL 2 (Windows Subsystem for Linux).

Once Docker Desktop is installed, go to Settings -> Kubernetes -> Enable Kubernetes as below. Wait for the Kubernetes to be up.

Docker Desktop

kubectl

kubectl is the Command Line Interface (cli) tool used for managing all the operations of Kubernetes cluster. It can be used for monitoring the nodes, pods, services etc. It can create, modify and delete pods, deployments, services etc.

Open Ubuntu for Windows or Terminal to run the below commands once Kubernetes is up.

For example:

  • To list all the nodes in the cluster
ubuntu: ~ > kubectl get nodes
NAME             STATUS   ROLES           AGE   VERSION
docker-desktop   Ready    control-plane   27d   v1.25.4
Enter fullscreen mode Exit fullscreen mode

Above shows, there is only one node in the cluster named docker-desktop. It is in ready status and version is 1.25.4.

  • To list all the pods in all namespaces
ubuntu: ~ > kubectl get pods --all-namespaces
NAMESPACE     NAME                                     READY   STATUS    RESTARTS         AGE
kube-system   coredns-565d847f94-f7k2x                 1/1     Running   4 (6d1h ago)     27d
kube-system   coredns-565d847f94-s78sp                 1/1     Running   4 (6d1h ago)     27d
kube-system   etcd-docker-desktop                      1/1     Running   4 (6d1h ago)     27d
kube-system   kube-apiserver-docker-desktop            1/1     Running   4 (6d1h ago)     27d
kube-system   kube-controller-manager-docker-desktop   1/1     Running   4 (6d1h ago)     27d
kube-system   kube-proxy-9ll9n                         1/1     Running   4 (6d1h ago)     27d
kube-system   kube-scheduler-docker-desktop            1/1     Running   4 (6d1h ago)     27d
kube-system   storage-provisioner                      1/1     Running   68 (6d1h ago)    27d
kube-system   vpnkit-controller                        1/1     Running   1040 (12m ago)   27d
Enter fullscreen mode Exit fullscreen mode

Hope you are seeing few familiar names above. All the controlplane components are in kube-system namespace. Namespace is a logical separator for the objects we create in Kubernetes.

  • Run an nginx server at port 80
ubuntu: ~ > kubectl run nginx --image=nginx --port=80
pod/nginx created
ubuntu: ~ > kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          10s
Enter fullscreen mode Exit fullscreen mode

kubectl run command creates a new pod with the docker image provided by --image argument.

  • To access the nginx container in the pod from outside.
ubuntu: ~ > kubectl port-forward nginx 30081:80
Forwarding from 127.0.0.1:30081 -> 80
Forwarding from [::1]:30081 -> 80
Handling connection for 30081
Handling connection for 30081
Enter fullscreen mode Exit fullscreen mode

This is for local testing only. After the above command is run, if you access http://localhost:30081, you will get the nginx default page as below.

Nginx Output

  • Delete the nginx pod
ubuntu: ~ > kubectl delete pod nginx
pod "nginx" deleted
ubuntu: ~ >
Enter fullscreen mode Exit fullscreen mode
  • Creating a deployment using nginx image
ubuntu: ~ > kubectl create deployment nginx --image=nginx --replicas=2
deployment.apps/nginx created
ubuntu: ~ > kubectl get all
NAME                        READY   STATUS    RESTARTS   AGE
pod/nginx-76d6c9b8c-vz677   1/1     Running   0          8s
pod/nginx-76d6c9b8c-zw999   1/1     Running   0          8s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   27d

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   2/2     2            2           8s

NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-76d6c9b8c   2         2         2       8s
ubuntu: ~ >
Enter fullscreen mode Exit fullscreen mode

Here we are creating a new deployment with the nginx image. Number of replicas are mentioned using --replicas argument. You can see there are 2 pods created by this deployment. Replicaset is another object created by the deployment to control the replication process.

  • Scaling up deployment
ubuntu: ~ > kubectl scale deployment nginx --replicas=3
deployment.apps/nginx scaled
ubuntu: ~ > kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
nginx-76d6c9b8c-56llc   1/1     Running   0          8s
nginx-76d6c9b8c-vz677   1/1     Running   0          3m11s
nginx-76d6c9b8c-zw999   1/1     Running   0          3m11s
Enter fullscreen mode Exit fullscreen mode

This command created an additional pod for nginx

  • Scaling down deployment
ubuntu: ~ > kubectl scale deployment nginx --replicas=1
deployment.apps/nginx scaled
ubuntu: ~ > k get pods
NAME                    READY   STATUS    RESTARTS   AGE
nginx-76d6c9b8c-zw999   1/1     Running   0          4m28s
Enter fullscreen mode Exit fullscreen mode

This command deleted two replicas of nginx and kept only one.

This is just a brief introduction to the world of Kubernetes. You can explore further on your own using the Docker Desktop setup or you can use one of the cloud providers, which is paid service.

Happy learning!!

Top comments (0)