# Kubernetes Basics

### 1\. What is Kubernetes and why do we use it?

It is a Container orchestration tool developed by Google used for multi-environment, multi-container deployment.

As we know Docker containers do not have features like Autoscaling and Autohealing. So to prevent this issue and achieve both features (Autoscaling and Autohealing) Kubernetes provides us such a platform where we can deploy production-ready micro-services applications or small apps with fewer failures and downtimes and can back up and restore easily.

### 2\. What are Autoscaling and Autohealing?

* **Autoscaling**: It is a feature based on traffic, Kubernetes itself scales (number of replicas of a pod) up or down its resources (pod) called Autoscaling.
    
    Let's take an example of "The Great Indian Festival Sale" where the Amazon server is getting heavy traffic and when this traffic reaches its threshold, the resource (pod) scales itself.
    
* **Auto healing**: It is a feature of Kubernetes where resources (Node and Pod) are detected in case of failure or crash and recovered to ensure the system remains in a healthy state within a cluster.
    

### 3\. Monolith vs Microservices architecture?

| Monolith | Microservices |
| --- | --- |
| All the services of an application run in a single system or a container. | Every service of an application runs in a different system or container. |
| Services are tightly coupled in this architecture. | All the services are loosely coupled. |
| During the maintenance of one service, all other services need to be down. | We need downtime for a service that is under maintenance others will be up and running and we can use it. |

### 4\. Kubernetes architecture.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692788238845/5a9acbb2-f429-4e17-84cf-d63cde75cfe1.png align="center")

A Kubernetes cluster contains two things mainly the *control plane* (which is also called as master node) and the *Node* (worker node).

* **Control plane**: It is a part of the Kubernetes cluster that contains the required components to manage all the worker nodes of the cluster and this part is also called as master node.
    
    ***There are four components of a master node as follows:***
    
    **Scheduler:** Basically if a pod is created it has no node assigned. So, It is responsible for scheduling the pod on a node based on its CPU, RAM and resources on the node. It just decides which pod should be placed on which node.
    
    **Controller Manager**: This is the component that continuously monitors the various components of clusters and works towards managing and restarting to the desired state.
    
    **etcd**: It is a tool that contains all the Node information, pods, configurations, secrets, accounts, rules bindings, etc.
    
    It's a key-value data store and to communicate with etcd server we use ETCD CTL which is a command line tool.
    
    **API-server:** It is used to authenticate users, validate requirements, retrieve data, update etcd. Communicate with worker nodes.
    
* **Node**: we call the node a worker node where the application runs.
    
    ***There are the following components of Node:***
    
    **Kubelet**: It is an agent that runs on each worker node and makes sure containers described in the pod specs are running in the pods and healthy status. Kubelet reports the status of the pod to the API server.
    
    **Service Proxy**: Pods communicate with each other using pod IP which is allowed by the pod network. And service proxy is responsible for the accessibility of service from outside using IP tables. Eg. a user can access a service from outside using a service proxy. Service proxy runs on each working node in a Kubernetes cluster.
    
* **CRI (Container Runtime Interface):** Container runtime is a fundamental component of a Kubernetes cluster that manages the container execution lifecycle of a container in a cluster.
    
    Kubernetes uses **Container-d** (docker engine) and **CRI-O** to run a container. CRI-O is an alternative to Container-d.
    
* **pods**: This is the smallest object in a Kubernetes cluster. Kubernetes does not deploy a container directly, a container is encapsulated in a pod and a pod runs on a worker node.
    
    Pod is a scalable unit based on load or traffic. A new node can also be added in case further scaling is needed.
    
* **Replica sets**: Replica sets prevent users from accessing the app directly. A replication controller gives high availability and it helps in load balancing as well so that based on load user requests will hit a particular pod or service.
    
* **Deployments**: A pod is a single unit or an instance of an application. Deployments allow us to update the pod's infrastructure with their replicas and rolling updates, etc.
    
* **Services**: It's a component of the cluster that helps to connect our applications with other applications/ databases. We are using it for communication between microservices or to make the app accessible to outside users.
    
* **Kubectl**: It is a command line tool to communicate with a Kubernetes cluster.
    

### 5\. Kubernetes cluster creation using kubeadm.

Kubeadm is a tool with the help of it we can create a minimum viable cluster up and running. It gives us the below two commands with the help of those we can create a Kubernetes cluster.

`kubeadm init`: It initiates or installs the minimum required components in the Kubernetes cluster.

`kubeadm join`: It is used to join the master node and a worker node. So that we can access everything present in the worker node from a master node.

***Steps to create a Kubernetes cluster using kubeadm:***

* **Pre-requisites**
    
    Ubuntu OS (Xenial or Later)
    
    t2.medium instance or higher as one node needs at least 2 CPU
    
* Create two t2.medium instance one is for the master node and another for the worker node, connect both of them on your local system using terminal.
    
* **Steps to be performed on both Master and Worker nodes:**
    
    ```bash
    # Swith the user as root and update the package index
    sudo su
    apt update -y
    
    # Install docker as we will use docker to run container in a pod.
    apt install docker.io -y
    
    # Start and enable the docker post docker installation.
    systemctl start docker
    systemctl enable docker
    
    # Download public signing keys for the kubernetes package repositories.
    curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg
    echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' > /etc/apt/sources.list.d/kubernetes.list
    
    # Update the package index and install kubeadm, kubectl and kubelet
    apt update -y
    apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y
    ```
    
* **Steps to be performed on the *master node*:**
    
    Initialize the Kubernetes master node.
    
    ```bash
    sudo su
    kubeadm init  # command to initialize the kubernetes master node
    ```
    
    Example:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985433714/0502bd8b-3331-42d1-8259-7ab535c19fe0.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985463724/5fb92d36-220e-47f1-a0b1-cdbecd68890a.png align="center")
    
    Set up local kubeconfig (both for the root user and normal user):
    
    ```bash
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    ```
    
    Example:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985537353/032014aa-b75f-4f4e-9861-eba0abbcc3bd.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985558101/ff94def0-a193-46ec-b0cd-4cd8e10e873f.png align="center")
    
    Apply Weave network:
    
    ```bash
    kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985579818/ab0abd0a-9adb-40d1-81e3-57e64feecdc9.png align="center")
    
    Generate a token on the master node so, worker nodes can join with the master:
    
    ```bash
    kubeadm token create --print-join-command
    ```
    
    Example: (<mark>Please note </mark> \- copy <mark>kubeadm join </mark> command from below output we got by executing <mark>kubeadm token</mark> command:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985638594/aefcd215-e597-423a-8fb0-73af98231b9f.png align="center")
    
    Expose port 6443 in the Security group of the master node instance to connect with worker node.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985658327/86033e80-b9ea-4e1f-a81c-da37237f3d00.png align="center")
    
* **Steps to be performed on the Worker node:**
    
    Run the following commands on the worker node to reset if anything got initialized related to the master node by mistake over worker node.
    
    ```bash
    sudo su
    kubeadm reset pre-flight checks
    ```
    
    Example:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985692802/001bc2a7-3af9-4934-b106-cbd52dfd531e.png align="center")
    
    Paste the join command you got from the master node and append `--v=5` at the end.
    
    ```bash
    # check this command you would have get on master node while executing "kubeadm tocken create" command
    # add --v=5 at the end of command 
    kubeadm join 172.31.49.198:6443 --token roh4tl.a47ljm1xghnhxn32     --discovery-token-ca-cert-hash sha256:4fa55ca99fdde6b8d7308cba5ef0ddfb0da53d79b79662b55a6b45913a6ff021 --v=5
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985739206/aa4f48dd-684c-4198-ab1a-18e75f393b7d.png align="center")
    
    After succesful join:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985759448/0e19152c-514e-456b-bb45-2afe42702d6c.png align="center")
    
* **Verify Cluster connection (On Master Node):**
    
    ```bash
    kubectl get nodes
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985890589/e8a97654-9967-4362-8cab-388a592b7670.png align="center")
    
* **Optional: Test a demo pod:**
    
    ```bash
    kubectl run hello-world-pod --image=busybox --restart=Never --command -- sh -c "echo 'Hello, World' && sleep 3600"
    kubectl run nginx --image=nginx
    kubectl get pods
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1692985969499/bb68a458-ab8c-493a-b9f9-e52830dad457.png align="center")
    

*Thank you for reading my Blog !*

Happy Learning !
