# K8s Tutorial

*Originally published on March 24, 2024 on* [*my previous blog*](https://somesh-banerjee.github.io/blogs/blog/16)*.*

---

The focus of this blog is to deploy a simple application using Kubernetes. We will explore the basic functionalities of Kubernetes such as scaling, load balancing, and rolling updates, etc.

I have followed various tutorials and documentation to deploy a simple application using Kubernetes but mainly I have referred to the Microsoft Learn **Introduction to Kubernetes** module.
It's also my first time deploying an application using Kubernetes, so I will be sharing my experience and the steps I followed to deploy the application.
Also, I will be using Windows as my host machine.

The application we will be deploying is a simple node.js application that has few REST APIs.

Let's get started!

### Prerequisites

- Basic knowledge of Docker
- Basic knowledge of Kubernetes such as what is a pod, deployment, service, etc.

## MicroK8s

MicroK8s is a lightweight Kubernetes distribution that runs entirely on your workstation or edge device. It's a great way to get started with Kubernetes without the need for a full-blown Kubernetes cluster.

### Installing Multipass

1. To install MicroK8s, we will use multipass to create a VM and then install MicroK8s on that VM. You can install multipass from [here](https://github.com/canonical/multipass).

**If you are using installing in windows and your VM Box is installed at custom location, you may have to use the following command to create a link between actual and default location of VM Box, since VM Box is used with Multipass and its location is hardcoded in the code. [Check this for more](https://github.com/canonical/multipass/issues/972#issuecomment-594146388)**

```bash
mklink /d "C:\Program Files\Oracle" "D:\Program Files\Oracle"
```

2. Once you have installed multipass, you can create a VM using the following command:

```bash
multipass launch --name microk8s-vm --memory 4G --disk 40G
```

3. You can access the VM using the following command:

```bash
multipass shell microk8s-vm
```

4. Now, you are inside the VM. You can install MicroK8s using the following command:

```bash
sudo snap install microk8s --classic
```

Now that we have installed MicroK8s, let's prepare the cluster for deployment.

### Preparing the Cluster

First, we need to enable some add-ons in MicroK8s. We will check the status of the add-ons using the following command:

```bash
sudo microk8s.status --wait-ready
```

Among the list, we will only enable the DNS(for dns service), dashboard(for a general purpose UI dashboard), and registry(for storing private container images) add-ons.

```bash
sudo microk8s.enable dns dashboard registry
```

### Exploring the Cluster

If you have gone through the basic, you know that we use `kubectl` to interact with the Kubernetes cluster. But in the case of MicroK8s, we have `microk8s.kubectl` instead of `kubectl`. To make things easier, we can create an alias for `microk8s.kubectl` as `kubectl`.

```bash
sudo snap alias microk8s.kubectl kubectl
```

Now you can use `kubectl` instead of `microk8s.kubectl`. The various commands you can use with `kubectl` are:

1. To check the nodes in the cluster `kubectl get nodes`.
2. To check the pods in the cluster `kubectl get pods`.
3. To check the services in the cluster `kubectl get services`.
4. To check the deployments in the cluster `kubectl get deployments`.
5. To create a deployment `kubectl create -f <deployment-file.yaml>`.
6. To scale a deployment `kubectl scale --replicas=3 deployment/<deployment-name>`.

Also, you can add `-o wide` to above commands to get more details about the resources.

You can use nginx as a sample application to deploy in the cluster. You can use the following command to deploy nginx:

```bash
kubectl create deployment nginx --image=nginx
```

### Deploying the Application

Now that we have explored the cluster, let's deploy the application. The application we will be deploying is a simple node.js application that has few REST APIs. The application is available [here](https://github.com/somesh-banerjee/k8s-tutorial).

We will clone the repository in the VM and create the docker image of the application. We will then push the image to the registry and deploy the application in the cluster.

```bash
docker build -t <image-name> .
docker push <image-name> <registry-name>/<image-name>
kubectl create -f <deployment-file.yaml>
```

The deployment file will have the details of the deployment, service, and ingress. It will also have the image from the registry.
The deployment file will look something like this:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: express-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: express-app
  template:
    metadata:
      labels:
        app: express-app
    spec:
      containers:
        - name: express-app
          image: localhost:32000/express-app:v2 # Image from the registry, localhost:32000 is the local registry
          ports:
            - containerPort: 3000
```

The Registry part is different in MicroK8s. You can use the local registry to store the images. The local registry is available at `localhost:32000`. For more details, you can refer to the [documentation](https://microk8s.io/docs/registry-built-in). In Real-world scenarios, you can use Docker Hub or any other registry to store the images.

All the configurations and scripts are available in the repository. You can refer to the repository for more details.

To test the application, you can use curl or Postman to hit the REST APIs. Getting the IP of the service will be discussed in the Load Balancing section.

### Scaling the Application

To scale the application, you can use the following command:

```bash
kubectl scale --replicas=5 deployment/express-app
```

You can check the pods using `kubectl get pods` and you will see that the number of pods has increased to 5.

### Load Balancing

To implement load balancing, we can create a service of type `LoadBalancer`. The service will have the details of the deployment and the port on which the application is running.

```yaml
apiVersion: v1
kind: Service
metadata:
  name: express-service
spec:
  selector:
    app: express-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: LoadBalancer
```

After creating the service, you can get the IP of the service using the following command:

```bash
kubectl get services
```

You will see the IP of the service in the `EXTERNAL-IP` column. You can use this IP to hit the REST APIs. You can also use the internal IP of the service to hit the REST APIs using curl from the VM.

### Rolling Updates

Rolling updates are easy to implement in Kubernetes. You can update the image of the deployment and Kubernetes will take care of the rest.
Just change the image in the deployment file and apply the changes using the following command:

```bash
kubectl apply -f <deployment-file.yaml>
```

Kubernetes will create new pods with the new image and then delete the old pods.

### Cleaning Up

After we are done with the tutorial, we can clean up the resources using the following commands:

```bash
sudo microk8s.disable dashboard dns registry
sudo snap remove microk8s
exit
# In windows
multipass stop microk8s-vm
multipass delete microk8s-vm
multipass purge
```

The commands are self-explanatory, in brief, we are removing everything we added for the tutorial.

