Kubernetes 101: Part 1 - Understanding the Fundamental Concepts

Kubernetes 101: Part 1 - Understanding the Fundamental Concepts

ยท

5 min read

Hello Everyone!! Hope y'all doing great in your life and upskilling everyday. Today, I will be writing up about Kubernetes. In this blog, I will be covering basic concepts of kubernetes. Before you start diving into I would like to mention that this is just another blog written on kubernetes but I believe that every single resource that's out there might be helpful on any minor/major topic that you might be struggling to understand. As a learner and k8s struggler I have written this blog to simplify basic concepts and tried to keep it short.

Kubernetes

Kubernetes aka k8s is a container orchestration technology used to manage the deployment and management of hundreds and thousands of containers in a cluster environment.

A lot of stuff in single definition right? Let me break down things for you.

Container Orchestration? - Process of deploying and managing containers is known as Container Orchestration.

Why managing containers? Isn't docker there for that?
Well, yeah! but docker couldn't address certain challenges better than kubernetes. Below are some challenges kubernetes don't hesitate to address efficiently and error-proof.

โ—
Scaling, High Availability, Resource Management, Service Discovery and Load Balancing, Automated Deployments and rollbacks.

Also I mentioned Cluster in definition?
Cluster here refers as machines (entire running system). Cluster has 3 important components i.e. - Nodes, Pods, Containers.
Nodes are 2 types - Worker node and Master Node.

Kubernetes has several main components. These components play an important role.
Pods, Services, Ingress, Deployment, Volumes, ConfigMaps, Secrets.


K8S Architecture

Kubernetes is known for its Master-Slave Architecture. Cluster has 2 nodes i.e. Master Node and Worker Node.

Worker Node

  • Each node has multiple pods to it.

  • Worker node does the actual work.

  • 3 process must be installed on every node.

    Container Runtime - Provides the underlying runtime environment for containers, such as Docker, Containerd, and CRI-O for communication with the Kubelet.
    Kubelet - Responsible for managing pods on the local node, including starting, stopping, and monitoring containers based on pod specifications.
    KubeProxy - Manages network routing and load balancing for services running on the node, ensuring that communication between pods and services is properly routed.

Master Node

4 components that runs on every Master Node.

  • API Server: Exposes the Kubernetes API, which serves as the primary interface for interacting with the cluster. Also, client communicates with Master node through this component. API Server is load balanced.

  • Scheduler: Assigns pods to nodes based on resource availability and scheduling policies. For example, lets assume if you have worker_node_1 30% occupied and worker_node_2 60% occupied. Since worker_node_1 is free and available, scheduler assign pods.

  • Controller Manager: Monitors the state of the cluster and ensures that the desired state matches the current state. Controller Manager is also responsibile to reschedule dead pods and bring back alive.

  • etcd: A distributed key-value store that stores cluster state and configuration data. Basically, it stores cluster state information. It does not store information regarding application data.
    - etcd is distributed storage across all Master Nodes.

So this is a Master-slave architecture of Kubernetes. Let us simplify and understand components as how they are created and utilized.

Fifty shades of grey


K8S Components

Pods

  • Pods are the fundamental building blocks in Kubernetes.

  • Each pod can contain one or more containers.

  • pod is an abstraction over container.

  • Pods share a container network, enabling communication between any pods, regardless of their nodes.

  • Each pod is assigned a single IP address by Kubernetes.

  • Deletion and recreation of the pod are required for port changes.

  • New IP address is allocated on re-creation of pods.

  • Pods are created using Manifest files written in YAML.

Example - basic_pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx

apiVersion: Specifies the version of the Kubernetes API you're interacting with.

  • kind: Defines the type of Kubernetes resource, in this case, a Pod.

  • metadata: Contains metadata about the Pod, such as its name.

  • spec: Describes the specification of the Pod, including its containers.

  • containers: An array of containers running within the Pod.

    • name: Name of the container.

    • image: Docker image to be used for the container. In this case, it's just the Nginx official image without specifying the tag, so it defaults to the latest version.

You can create above basic_pod.yaml file usingkubectl create -f basic_pod.yaml


Minikube and Kubectl

Minikube: Minikube is a one node cluster where master processes and worker processes run on one-node(one machine).

  • Minikube have docker runtime pre-installed.

  • Minikube creates virtual box on your device.

  • Node runs in that virtual box.

  • Therefore, Minikube is 1 node k8s cluster.

  • Can be used for testing k8s on local setup.

Kubectl:*kubectl is a cli tool for k8s cluster.*
As we know Master Processes have a component called "API Server" which enables interaction with cluster by help of 3 clients (UI, API, CLI). Therefore, CLI tool called kubectl is more efficient among 3 clients. Once kubectl sends command to API Server it can create, update, delete pods. Worker Processes enables to pod run on node.

Kubectl Commands

#Get Nodes information
kubectl get nodes

#Get pods information
kubectl get pod

#Create a pod for first time in Node.
kubectl create -f basic_pod.yaml

#After updating/editing basic_pod.yaml.
kubectl apply -f basic_pod.yaml

#Description of pod
kubectl describe pod basic_pod.yaml

#Delete pod
kubectl delete pod basic_pod.yaml

#Execute a command in a specific container within a Pod
kubectl exec -it basic_pod.yaml -c ngnix -- <command>

#Run a Pod named my-pod with an Nginx image
kubectl run my-pod --image=nginx
๐Ÿ’ก
kubectl create is used for creating pod for first time in node. If you edited/updated pod yaml file and use create command it will throw error saying pod already exists. Therefore, edited/updated files should be created again using kubectl apply command.

Pod Accessibility Challenges

  • Pods dynamic IP addresses make direct access inconvenient.

  • Rescheduled Pods may receive different IP addresses.

  • How do we overcome these accessibility challenges???

And that's how Services come into the picture.

So, wrapping up this blog here. In next blog, we will be exploring more crucial components like Services, Deployments, Replica sets, etc., of k8s and how they go hand-in-hand with each other.

If you like my content connect with me on below social handles:
Linkedin
Github
Twitter

Resources

Kubernetes Docs
Kodekloud Mumshad - youtube
Kodekloud Free Lab
CloudAcademy

Did you find this article valuable?

Support Farhan's Scripted Explorations by becoming a sponsor. Any amount is appreciated!