Introduction to Kubernetes
Tags: kubernetes, docker, devops, programming
Published: 2024-04-17 08:25:59

If you work in tech then you would've definitely come across the name Kubernetes a number of times, but if you're not exactly clear on what it is and what it does then this post is definitely for you.

Let us begin with the textbook definition and break it down as we dive deeper.

## What is Kubernetes?
Kubernetes, also known as K8s, is an open-source system for managing, deploying, and scaling containerized applications. 

<img width="100%" style="width:100%" src="https://media.giphy.com/media/oc8j2H8wlq2sM/giphy.gif?cid=790b7611jbmh4g8zxvqakhg87cm5udkwtt6mmv6qa7lgql5e&ep=v1_gifs_search&rid=giphy.gif&ct=g">

Great but wait, what are containers? well for that you can take a quick look at one of my other post where I talk about [Docker](https://dev.to/shasheeshpurohit/a-beginner-friendly-approach-to-docker-36p)

so assuming you know what containers are now, you will be amazed by how Kubernetes can handle managing your containers efficiently.

Getting back on track, so in simple words 

> Kubernetes automates tasks like container management, including: Application deployment, Application rollouts, Application scaling, and Application monitoring.

As an organization scales, they might have a large number of containerized micro services each having different numbers of replicas which might make it difficult for the teams to maintain all the containers or keep track of them efficiently.

Hence Kubernetes orchestrates containers by managing their deployment, scaling, and operation within clusters of hosts. It abstracts away the underlying infrastructure complexities, allowing users to focus on application development. Kubernetes utilizes declarative configurations to specify desired states, automatically handling container placement, health monitoring, and restarts as needed. 

It provides services like load balancing, storage orchestration, and automated rollouts for seamless updates. Through its scheduler, Kubernetes optimizes resource utilization and maintains high availability by distributing containers across nodes based on resource requirements and constraints. 

Overall, Kubernetes streamlines container management, enhancing scalability, reliability, and efficiency in modern cloud-native environments.

In simpler terms: 
Imagine you have a big box of toys, like LEGO blocks, and you want to organize them neatly and play with them. Kubernetes is like a magical toy organizer. It helps put your toys in the right place and makes sure they're working well together. So, if you have lots of toy cars, it puts them in one spot, and if you have toy dolls, it puts them in another. If a toy stops working or gets tired, Kubernetes helps fix it or gives it a rest. Basically, it's like having a helpful friend who keeps your toys organized and happy so you can play with them smoothly.

<img width="100%" style="width:100%" src="https://media.giphy.com/media/3ZALZoBtI1KJa/giphy.gif?cid=790b7611xu8lndmeg3do0peh3uf1ydun685yiepi8tph45id&ep=v1_gifs_search&rid=giphy.gif&ct=g">

## Kubernetes architecture

Kubernetes cluster can be divided into below components

1) Control Plane: This is like the brain of the playground. It's where all the important decisions are made. Just like your brain tells your body what to do, the control plane in Kubernetes decides where to put your applications, how many copies to make, and how to keep them safe and happy.

2) Data Plane: Now, imagine the actual playing area of the playground where you interact with your toys. This is the data plane. It's where your applications run and do their work. Like when you play with your toys on the ground, the data plane is where your applications run on the computers or servers in the Kubernetes cluster.

3) Worker Plane: This is like the helpers in the playground who help you play and keep everything running smoothly. In Kubernetes, the worker plane is made up of all the computers or servers (called nodes) that do the actual work of running your applications. They take instructions from the control plane and make sure your applications are running and doing what they're supposed to.

So, in simple terms, the control plane decides what needs to be done, the worker plane does the work, and the data plane is where the actual action happens. Together, they make sure your applications run smoothly in Kubernetes, just like having a fun time in a well-organized playground!

## Communication in K8s cluster
Every command in K8s cluster has to go through an API server, even if the command is from within the cluster itself. The API server acts as a gateway to execute the commands. So without going through the API server:

<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExNWl1ZmVyeTFpM2FuYWR3bjB0MnVodHJmMjJrb2liZGM1Z2RjejJraSZlcD12MV9naWZzX3NlYXJjaCZjdD1n/8abAbOrQ9rvLG/giphy.gif">


## Declarative and Procedural Model

Let us first understand the difference between declarative and procedural model:
In a declarative model, desired outcomes are stated without specifying how to achieve them, like giving a recipe without cooking steps. Procedural models detail step-by-step instructions to achieve outcomes, akin to a cooking recipe with explicit cooking instructions. Declarative focuses on the "what," while procedural focuses on the "how."

Kubernetes true power lies in it's declarative model where the user specifies the config which defines the final state and it automatically works on attaining the desired state mentioned in the config file. It also keeps track for any issues which might change the state from the desired state eg: if any pod crashes then it will automatically spin up a new one so that the number of pods are equal to what is mentioned in the config.

## Kubectl

Kubectl is a command-line tool used to interact with Kubernetes clusters. It allows users to deploy, manage, and troubleshoot applications and resources within Kubernetes clusters. With kubectl, users can execute commands to create, inspect, update, and delete Kubernetes resources such as pods, services, deployments, and more.

## How to declare state in Kubernetes?

In Kubernetes, declaring state is done through manifests, which are YAML or JSON files describing desired configurations of resources. 

These manifests specify what the state of the cluster should be, such as the desired number of pods, container images, ports, and other settings. Users define resources like deployments, services, pods, and replicasets, along with their attributes and relationships. 

Once created or updated, Kubernetes compares the current state of the cluster with the desired state described in the manifests and takes actions to reconcile any differences, ensuring that the cluster matches the declared state.

## Kubernetes objects

Kubernetes consists of the following objects:

1. **Pods**: Smallest deployable units in Kubernetes, containing one or more containers and shared resources like IP address and storage volumes.
  
2. **Deployments**: Manages the lifecycle of pods, allowing easy scaling, rolling updates, and rollbacks of application containers.

3. **Services**: Defines a logical set of pods and a policy to access them, providing stable endpoints for inter-pod communication.

4. **ConfigMaps**: Stores configuration data separately from application code, enabling easy management and updating of configuration settings.

5. **Secrets**: Securely stores sensitive information such as passwords, API tokens, and SSH keys, ensuring they are only accessible to authorized containers.

6. **Volumes**: Provides persistent storage to containers, allowing data to persist beyond the lifecycle of individual pods.

7. **Namespaces**: Provides a way to partition cluster resources, enabling multiple users or teams to share a cluster while maintaining isolation.

8. **Nodes**: Individual machines (physical or virtual) that make up a Kubernetes cluster, where containers are deployed and run.

9. **Ingress**: Manages external access to services in a cluster, typically routing traffic to the appropriate services based on rules defined by the user.

10. **DaemonSets**: Ensures that a specific pod runs on all or a subset of nodes in a cluster, typically used for cluster-wide tasks like logging or monitoring.