Kubernetes Architecture
What is Kubernetes?
Kubernetes, also known as K8s is an open-source container management tool that automates container deployment, scaling & load balancing. It schedules, runs, and manages isolated containers that are running on virtual/physical/cloud machines. All top cloud providers support Kubernetes.
Example:
Suppose a company has multiple microservices that need to be deployed and managed across a cluster of servers. Without Kubernetes, the process of deploying and scaling these services manually can be complex and time-consuming. However, by using Kubernetes, the company can define and manage the desired state of their applications using declarative configurations. Kubernetes takes care of the underlying infrastructure, scheduling containers, distributing resources, and handling failovers automatically. This allows the company to focus more on application development and less on infrastructure management.
What are the benefits of using k8s?
- Scalability: Kubernetes allows you to scale your applications easily. You can horizontally scale your application by adding more replicas of your pods to handle increased traffic or workload. It enables seamless scaling based on demand, ensuring applications can handle increased traffic or workload without manual intervention.
Example: A popular e-commerce website experiences a sudden surge in traffic during a holiday sale. With Kubernetes, the application can automatically scale up the number of containers to handle the increased load, ensuring smooth user experience and preventing downtime.
- High Availability: Kubernetes provides built-in mechanisms for ensuring high availability of applications. It supports automatic container restarts, pod rescheduling, and node failover. If a pod or node fails, Kubernetes automatically reschedules the workload to healthy nodes, minimizing downtime and maintaining service availability.
Example: In a production environment, a critical service failure due to a node crash can lead to significant downtime and impact business operations. With Kubernetes, you can configure replicas of your application across multiple nodes, and if one node fails, Kubernetes automatically reallocates the workload to healthy nodes, ensuring high availability and minimizing service disruption.
Load Balancing and Service Discovery: Kubernetes offers built-in load balancing mechanisms to distribute network traffic across multiple containers or pods. It also provides service discovery, allowing containers to easily find and communicate with other containers or services within the cluster.
Example: Imagine a microservices architecture with multiple services communicating with each other. With Kubernetes service discovery and load balancing, services can dynamically discover and communicate with each other by using their DNS names, enabling seamless communication and eliminating the need for hard-coded IP addresses.
Automated Rollouts and Rollbacks: Kubernetes facilitates easy and automated rollouts and rollbacks of application updates. With features like rolling updates and versioned deployments, you can gradually update your application while minimizing downtime and impact on users. If an issue arises, you can quickly rollback to a previous known good version.
Example: Suppose you are deploying a new version of your application that introduces new features or bug fixes. With Kubernetes, you can perform a rolling update, gradually updating the pods while ensuring a smooth transition. If any issues are detected, you can rollback to the previous version with minimal user impact.
Let's look at some key features of Kubernetes with examples.
- Container orchestration: Kubernetes provides powerful tools for managing containerized applications. It allows you to create and manage containers, deploy and scale applications, and monitor their health.
Example: Suppose you have a web application that runs in a container. With Kubernetes, you can easily deploy and manage the containers using a simple YAML file.
- Load balancing: Kubernetes provides built-in load balancing capabilities that distribute traffic across multiple replicas of an application.
Example: Let's say you have a web application with multiple replicas running on different nodes. Kubernetes automatically balances the traffic between the replicas, ensuring that the application is highly available.
- Automatic scaling: Kubernetes can automatically scale your applications based on the demand, using horizontal pod autoscaling (HPA).
Example: Suppose you have an e-commerce website that experiences a sudden spike in traffic during the holiday season. Kubernetes can automatically scale up the application by adding more containers to handle the traffic.
- Rolling updates: Kubernetes allows you to update your applications without downtime using rolling updates.
Example: Let's say you have a web application running in a container, and you want to update it to the latest version. Kubernetes can deploy the new version in a rolling manner, updating one container at a time, while keeping the application available to users.
- Self-healing: Kubernetes ensures that your applications are highly available by automatically restarting containers that fail, and replacing nodes that go down.
Example: Suppose you have a web application running in a container, and the container fails. Kubernetes can automatically restart the container, ensuring that the application remains available.
Explain the architecture of Kubernetes
Explain the architecture of Kubernetes.
The Kubernetes architecture is based on a master-slave model, where the master node manages the entire cluster while the worker nodes host the containers.
Kubernetes control plane:- manages Kubernetes clusters and the workloads running on them. Include components like the API Server, Scheduler, and Controller Manager.
Kubernetes data plane:- machines that can run containerized workloads. Each node is managed by the kubelet, an agent that receives commands from the control plane.
Pods:- pods are the smallest unit provided by Kubernetes to manage containerized workloads. A pod typically includes several containers, which together form a functional unit or microservice.
Persistent storage:- local storage on Kubernetes nodes is ephemeral and is deleted when a pod shuts down. This can make it difficult to run stateful applications. Kubernetes provides the Persistent Volumes (PV) mechanism, allowing containerized applications to store data beyond the lifetime of a pod or node. This is part of an extensive series of guides about CI/CD.
Control plane: The control plane is responsible for managing the Kubernetes cluster's overall state and consists of the API server, etcd, scheduler, and controller manager.
etcd basically database, it store key value
→ etcd is a distributed key-value store that stores the configuration data of the Kubernetes cluster. It stores the state of the nodes, services, deployments, and other objects in the cluster. It provides a highly available, fault-tolerant data store that can be easily scaled up or down.
API server: The API server is the main management component of Kubernetes, responsible for handling API requests from users and other components.
The Kubernetes architecture consists of two main components:
Master Node / Control Plane: The control plane is responsible for managing the cluster's overall state and controlling the behavior of the worker nodes. It includes the following components:
API server: The API server is the main management component of Kubernetes, responsible for handling API requests from users and other components.
etcd: etcd is a distributed key-value store that stores the configuration data of the Kubernetes cluster, such as the state of nodes, services, and deployments.
Scheduler: The scheduler is responsible for scheduling tasks and allocating resources to the worker nodes.
Controller manager: The controller manager is responsible for managing various controllers that handle different tasks, such as scaling, replication, and self-healing.
Worker Nodes: The worker nodes are responsible for running the containers and providing the resources needed for the applications to run. It includes the following components:
kubelet: kubelet is an agent that runs on each node and is responsible for managing the containers and ensuring that they are running correctly.
kube-proxy: kube-proxy is responsible for routing network traffic to the appropriate container.
Container runtime: Kubernetes supports multiple container runtimes, such as Docker, containerd, and CRI-O, to manage the containers.
Kubernetes Architecture Components in Detail:
- API Server:
The API server is the primary control plane component that serves as the cluster's front end. It exposes the Kubernetes API, which allows users and other components to interact with the cluster. It is responsible for validating and processing API requests and updating the etcd datastore. The API server also provides authentication and authorization mechanisms to control access to the cluster.
- etcd:
etcd is a distributed key-value store that stores the configuration data of the Kubernetes cluster. It stores the state of the nodes, services, deployments, and other objects in the cluster. It provides a highly available, fault-tolerant datastore that can be easily scaled up or down.
- Scheduler:
The scheduler is responsible for scheduling tasks and allocating resources to the worker nodes. It watches for new pods that need to be scheduled and assigns them to nodes based on resource availability and other constraints.
- Controller Manager:
The controller manager is responsible for managing various controllers that handle different tasks, such as scaling, replication, and self-healing. It watches for changes in the cluster's state and takes corrective actions to ensure that the desired state is maintained.
- kubelet:
kubelet is an agent that runs on each node and is responsible for managing the containers and ensuring that they are running correctly. It receives the pod specifications from the API server and communicates with the container runtime to create, start, stop, and delete containers.
- kube-proxy:
kube-proxy is responsible for routing network traffic to the appropriate container. It maintains the network rules and ensures that the communication between the containers is secure and efficient.
- Container runtime:
The container runtime is responsible for managing the containers and providing a secure and isolated environment for running the applications. Kubernetes supports multiple container runtimes, such as Docker, containerd, and CRI-O, to manage the containers.
Write the difference between kubectl and kubelets?
kubectl is the command-line interface (CLI) tool for working with a Kubernetes cluster. It communicates with the API server to perform various operations on the cluster, such as deploying applications, scaling resources, and inspecting logs.
Kubelet is the technology that applies, creates, updates, and destroys containers on a Kubernetes node. Kubelets are the agents that run on each node in the cluster and communicate with the API server to ensure the desired state of the cluster is maintained. They manage the containers running on their node and communicate the status of those containers to the API server.
Explain the role of the API server.
The Kubernetes API server is the front end of the Kubernetes control plane and is how users interact with their Kubernetes cluster.
It is the interface used to manage, create, and configure Kubernetes clusters. It's how the users, external components, and parts of your cluster all communicate with each other.
At the center of the Kubernetes control plane are the API server and the HTTP API that it exposes, allowing you to query and manipulate the state of Kubernetes objects.
Kubernetes Components:
Kubernetes has several components that work together to enable the management of containerized workloads. These components include:
*POD
The smallest unit in Kubernetes.
POD is a group of one or more containers that are deployed together on the same host.
A Cluster is a group of nodes.
A Cluster has at least one worker node and a master node.
In Kubernetes, the control unit is the pod, not the containers.
Consist of one or more tightly coupled containers.
POD runs on a node, which is controlled by the master.
Kubernetes only knows about PODS (does not know about individuals container).
Cannot start containers without a POD.
One Pod usually contains one container.
*Replica sets
To prevent users from losing access to the app, the replication controller gives high availabilities.
Help in load balancing and scaling.
*Deployment
Pods deploy single instances of an application.
Deployment allows updating the pod's infrastructure with Replicas, Rolling updates, etc.
*Services
Helps us connect our applications with other applications/databases etc.
Services enable pods to communicate with each other and with the outside world
*ConfigMaps:
A ConfigMap is a Kubernetes resource that is used to store configuration data for an application. It enables the separation of application configuration from the application code, making it easier to manage and update the configuration.
*Secrets:
A Secret is a Kubernetes resource that is used to store sensitive data like passwords, API keys, and certificates. Secrets are encrypted and stored in etcd, ensuring that the sensitive data is secure.
*Kubectl
A command line tool used to communicate with a Kubernetes cluster's control plane.
Creates the live object for the configuration.
Thank you for reading this blog. If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.
— Happy Learning !!!