Docker and Kubernetes Made Relatable - III
Tags: devops, devs, kubernetes, docker
Published: 2021-10-29 19:15:16

Hi everyone!
Welcome to the third part of this series, where we try to relate with kubernetes as a part of dev life.

In the previous articles, we have discussed about containers & images and how to run containers on docker. We discussed about the need for Kubernetes. In this article, we will take a closer look at K8s and why it is so essential.

## How Do We Guide All Developers To Work in Sync To Provide the Final Product?
### Kubernetes: The engineering manager

> Kubernetes is an open source container management and deployment platform. It orchestrates virtual machine clusters and schedules containers to operate on those virtual machines depending on their available computational resources and the container’s resource needs. 

They can be related with managers, who plan and coordinate developers to deliver projects and make sure they have all the resources required to do so.

We can use K8s to deploy our services, roll out new releases without downtime, and scale (or descale) those services. It is portable, extensible, self-healing, and highly available.

### What are pods?
> A Kubernetes pod is a group of one or more containers, tied together for the purposes of administration and networking.

In our workspace analogy, pods can be a pair of senior and junior dev, working together on their related tasks. It can also be a dev who is ramped up on the system and is comfortable with working on the tasks alone.

### Nodes and Clusters: Workstation that provides resources to the devs
Consider the example of the employee workspace above. Every employee would require resources like a monitor, chair, desk, etc., to function properly. Every employee needs to be assigned these resources by the manager.
A node can be thought of as the workstation that provides these resources and kubernetes, the manager that assigns these stations to the employees.
> In Kubernetes, a Node is a worker computer that can be either virtual or physical. A Node can have many pods, and Kubernetes handles pod scheduling among the cluster’s Nodes automatically. Node is the smallest unit for computing. To establish a cluster, nodes share their resources.

## Getting Started With Kubectl and Minikube
Kubectl is Kubernetes’ CLI used to interact with Kubernetes API to create and manage clusters. To install kubectl, run the following:
```
brew install kubectl
```

### What is Minikube?
> Minikube is a tool that lets you try out Kubernetes locally. It is a single node K8s cluster on your local machine for purpose of development or trying out K8s. The Minikube tool includes a set of built-in add-ons that can be enabled, disabled and opened in the local Kubernetes environment.

For installing minikube and starting it on MacOS, run the following commands:
```
brew install minikube
minikube start
```

### Deploying Your Service to Kubernetes
First, push your local docker image that you built into minikube cache by using the following command:
```
minikube cache add myapp:latest
```
### Deployment: The team goals and structure defined at the start of the year
> A deployment provides declarative updates for Pods and ReplicaSets.
In a Deployment, we define a desired state, and the Deployment Controller gradually converts the current state to the desired state. Deployments may be used to build new ReplicaSets or to delete current Deployments and replace them with new Deployments.
Take a look at the deployment file below:

{% gist https://gist.github.com/Sampriti-Mitra/ca444dc963541cd1cbb82d557058d99f %}

The above deployment file is like a declarative template for pods and replicasets. The above deployment named myapp in `{metadata.name}`, creates a replicaset to bring up two pods of myapp. Now the `{spec.containers.image}` gives the image to be pulled to run the container in the pod given by `{template.metadata.labels}` `app:myapp`. The deployment knows which pods to manage by the `{spec.selector.matchlabels}` `app:myapp`.

### What are ReplicaSets?
> The goal of a ReplicaSet is to keep a consistent set of replica Pods operating at all times. As a result, it’s frequently used to ensure the availability of a certain number of identical Pods.

### Common deployment strategies
1. Recreate: all existing pods are terminated and new pods are then generated
2. Rolling: pods are created in a rolling fashion, ramped up slowly until all new pods are running

### Creating a K8s deployment
```
kubectl apply -f deployment.yaml
```
### Checking the minikube cluster for running pods
```
kubectl get pods
```
Now try going into the pod and checking whether the app is running:
```
kubectl exec -it <pod-name> sh
apk update
apk add curl
curl localhost:10000
```
### Checking the self-healing capacity of Kubernetes
```
kubectl delete pod <pod-name>
kubectl get pods
```
We should be able to see two pods still running. Since we deleted a pod, the replicaset controller detected it and spun up another one to keep up the desired state of two pods.

Thanks for reading on till the end of this article. In the next post, we will discuss about Kubernetes Services.