Kubernetes Basics

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?

MonolithMicroservices
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.

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:

      # 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.

      sudo su
      kubeadm init  # command to initialize the kubernetes master node
    

    Example:

    Set up local kubeconfig (both for the root user and normal user):

      mkdir -p $HOME/.kube
      sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
      sudo chown $(id -u):$(id -g) $HOME/.kube/config
    

    Example:

    Apply Weave network:

      kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
    

    Generate a token on the master node so, worker nodes can join with the master:

      kubeadm token create --print-join-command
    

    Example: (Please note - copy kubeadm join command from below output we got by executing kubeadm token command:

    Expose port 6443 in the Security group of the master node instance to connect with worker node.

  • 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.

      sudo su
      kubeadm reset pre-flight checks
    

    Example:

    Paste the join command you got from the master node and append --v=5 at the end.

      # 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
    

    After succesful join:

  • Verify Cluster connection (On Master Node):

      kubectl get nodes
    

  • Optional: Test a demo pod:

      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
    

Thank you for reading my Blog !

Happy Learning !