Table of contents
Permalink📍Introduction
Welcome to my Kubernetes blog series. In this introductory blog, we'll explore Kubernetes, its features, significance, and architecture. Our journey will also lead us to delve into the realms of monolithic and microservice architectures. Join me as we dive into the fascinating world of Kubernetes and embark on our container orchestration journey.🚀
Permalink📍Kubernetes
Kubernetes is an open-source container orchestration/management tool that automates container deployment, scaling, and load balancing of containerized applications. It was developed by Google in 2008 to manage their applications. It is written in GoLang and is popularly used in the industry to manage containerized applications.
It provides great features:
Container Orchestration ( clustering of any number of containers running on a different network. )
Auto Scaling ( Vertical & Horizontal )
Auto Healing
Configuration Management
Load balancing
Fault Tolerance ( Node/POD/Failure )
Rollback ( Going back to the previous version )
All the top Cloud Providers support Kubernetes. With all these features Kubernetes is ideal for managing a containerized microservice architecture. Let's see what is microservice application.
Kubernetes is also known as K8s since there are '8' letters between 'K' and 's' in Kubernetes.
Permalink📍Monolithic & Microservice Architecture
There are two types of architecture for building applications:
Monolithic architecture (aka Stand alone machine)
Microservice architecture
Permalink✔Monolithic architecture
Monolithic means single therefore, monolithic architecture is a tightly coupled architecture where every service or functionality of an application is closely dependent on each other and is integrated into a single unit. Let's take an example of an e-commerce website with a monolithic architecture:
In the above example, we can see an e-commerce app where all its services and functionalities like Sign-in, Checkout, Add to Cart, Customer Care, Payment, etc. are present in a single instance i.e. in a single server.
Advantages:
It is simple to deploy as the entire application is in a single instance.
Testing the application is simpler.
Communication between components is faster.
Disadvantages:
Single Point of Failure.
If I have to work on a single component (e.g.: Sign-in) the whole app needs to be taken down.
If the application grows then it can become complex to maintain with various services.
Permalink✔Microservice architecture
Microservice is a distributed loosely coupled architecture where each component or service is running on a different server. The microservice architecture of the above e-commerce app can be visualized like this:
Here, each of the services is running a separate instance and is communicating with each other via the internet.
Advantages:
Services can be scaled independently based on specific requirements, leading to better resource utilization.
Failures in one service do not necessarily affect others, leading to more resilient applications.
When working on a particular service or a feature we do not need to down the whole application.
Different services can use different technology stacks, allowing teams to choose the best tools for each task.
Disadvantages:
Developing and maintaining a distributed system is more complex and requires expertise
Maintaining data consistency across services can be complex, especially in scenarios requiring transactions.
Communication between services requires careful design and can lead to increased overhead.
Inter-service communication introduces network latency, which can impact overall application performance.
Microservice architecture is used more frequently nowadays, and containerizing a microservice application means running all the services of the application in a separate container. Popularly Docker containers are used to deploy services and applications.
Permalink✔Why use Kubernetes❓
Disadvantages of Docker containers and how Kubernetes overcomes them:
Docker doesn't automatically recover containers from node failures. Kubernetes can detect node failures and automatically reschedule affected containers to healthy nodes.
Docker lacks built-in tools for the automatic scaling of containers, and K8s provide autoscaling of the containers.
Docker alone doesn't provide native tools for health monitoring and automated container recovery. Kubernetes constantly monitors the health of containers and can automatically restart or replace unhealthy containers.
Docker requires manual configuration for networking, storage, and resource allocation. Kubernetes uses declarative YAML files to define application configurations, making it easier to manage and version these configurations
Docker doesn't inherently provide mechanisms for ensuring high availability and load distribution across nodes. Kubernetes manages container placement, rescheduling, and load distribution, enhancing reliability and availability.
And many more...
All these features, make Kubernetes an ideal tool to use for containerized microservice applications.
Permalink📍Kubernetes Architecture
Kubernetes Architecture also called Kubernetes Cluster contains 8 major components.
A cluster is nothing but a group of servers therefore, to create a cluster we need at least two servers. In the above example, we have 2 servers/nodes forming a K8s cluster:
Master Node ( a.k.a Control Plane ): Controls the overall cluster. Contains 4 components:
Scheduler
etcd
Control Manager
API Server
Node ( Worker node ): Where actual work is executed like running containers as directed by the master node. Contains 2 components:
Kubelet
Kube-proxy ( Service-proxy )
Let's dive into u=this Kubernetes cluster and understand each component in detail.
Permalink📍K8s Architecture Components
Permalink✔Master Node
Also known as the Control Plane is responsible for managing and controlling the cluster's overall state and operations. The main components of the control plane are:
API Server ( kube-api-server ):
Front-end for the control plane.
Provides an interface for users, applications, and other components to interact with the Kubernetes cluster.
It serves as the entry point for executing commands, managing resources, creating and updating objects (such as pods, services, and deployments), and querying the state of the cluster.
etcd:
Similar to a database that is designed to store data in a simple key-value format.
etcd
is specifically optimized for distributed environments and is used to maintain configuration data, metadata, and the current state of a cluster.It ensures consistency, high availability, and reliability in a distributed environment
Data stored in
etcd
is persistent, meaning it is stored on disk and survives restarts or crashes.Distributed across multiple nodes to ensure that data is replicated and available even if some nodes fail.
Scheduler ( kube-scheduler ):
When users request the creation and management of Pods, Kube-scheduler is going to take action on these requests.
Handles POD creation and management.
Responsible for assigning workloads (such as pods) to worker nodes within the cluster.
Optimizes resource utilization, maintains load balance, and ensures that pods are placed on appropriate nodes based on their resource requirements and constraints.
Plays a role in enabling and supporting certain aspects of auto-scaling.
Controller-Manager:
Ensures that the desired state of objects in the cluster matches the actual state.
If K8s is on the cloud, then it will be a cloud-controller-manager, for non-cloud it will be a kube-controller-manager.
Different controllers manage various resources like nodes, pods, services, and more:
Node Controller: Ensures the correct number of nodes are running.
Replication Controller: Maintains the desired number of replicas for a set of pods.
Pods Lifecycle Controller: Monitors and maintains the lifecycle of individual pods.
Endpoints Controller: Populates the Endpoints object (record of IP addresses and ports) for services.
Service Account & Token Controllers: Create default accounts and API access tokens for pods.
Namespace Controller: Ensures that namespaces are correctly configured and maintained.
Volume Controller: Manages the lifecycle of volumes used by pods.
Service Controller: Responsible for maintaining the services in a consistent state.
Permalink✔Node ( Worker node )
Nodes are machines in the cluster responsible for running containers and executing workloads. Each worker node has the following 3 components:
Kubelet:
An agent that runs on each node and interacts with the control plane ( API-server ).
Ensures containers are running inside pods and that the nodes are functioning properly.
Container Engine ( Runtime ):
Software that runs containers, such as Docker.
Works with Kubelet.
It's responsible for creating and managing container instances.
Kube-Proxy ( service-proxy ):
Maintains network rules for communication between pods and provides load balancing for service traffic.
Assign IP to each pod. ( dynamic )
It runs on each pod, ensuring each pod will get its unique IP Address.
Pod:
A pod is the smallest deployable unit in Kubernetes.
It is a part of the worker node you can see in the diagram represented with orange boxes.
It represents a single instance of a running process in a cluster.
A pod can contain one ( typically ) or more containers that share the same network namespace, IP address, and storage.
Permalink✔Kubectl
Kubectl is the command-line tool used to interact with Kubernetes clusters.
It acts as a client to the Kubernetes API server and allows users to manage various aspects of the cluster.
Some common
kubectl
commands include creating and deleting resources, checking cluster status, scaling applications, and more.
Let's do a hands-on and implement the Kubernetes architecture. Kubernetes Cluster can be set up using:
Local:
- MiniKube
Production:
Kubeadm ( Bare Metal )
EKS ( Elastic Kubernetes Service [AWS] )
AKS ( Azure Kubernetes Service )
GKE ( Google Kubernetes Engine )
Visit this GitHub URL: https://github.com/LondheShubham153/kubestarter
Start your Kubernetes journey by forking this GitHub URL.
Permalink📍MiniKube Setup
It is a Local Setup you can use it for practicing Kubernetes. Minikube runs on a single server and creates a virtual Kubernetes cluster using containers. It runs a Kubernetes Cluster inside a container that contains the components kube-scheduler, etcd, kubelet, etc. each service running inside a container. You can visualize it as many containers running inside a big container.
Permalink✔EC2 instance Setup
Step 1
: Launch an EC2 instance
Step 2
: Name it as 'minikube-server'
Step 3
: Select 'Ubuntu' AMI
Step 4
: Select instance type 't2.medium'
Step 5
: Click on 'Create a new key pair'
Step 6
: Name your key-pair 'minikube-key' or any name you like and click on 'Create key pair'
Step 7
: Click on 'Launch instance'
Step 8
: If you see this, then your EC2 instance has been successfully created, you can click on 'Instances' and see your instance.
Step 9
: Wait for some time to start the instance till shows 'Running'.
Step 10
: Select your instance and click on the 'Connect' option at the top right.
Step 11
: Select the 'SSH client' option and follow the steps mentioned.
Step 12
: Open your Terminal for Linux and Mac, and open 'GitBash' for 'Windows'. Go to your 'Downloads' using cd Downloads
folder where the private key file is usually located.
Verify it using this command in the 'Downloads' folder: ls | grep -i minikube
We have to change the permission to only read for the owner.
Step 13
: Copy the chmod
command and paste it into your terminal
Step 14
: Copy the ssh
command and paste it into your terminal
Type 'yes' for the prompt:
And you will be connected to your EC2 instance:
Now go to this GitHub URL Kuberstarter and follow the instructions to install and set up MiniKube.
After setting up the Minikube type the command docker ps
you will see one container is running on your instance:
Using the kubectl get nodes
command you can see a 'control-plane' server is running:
This is the container that is running a virtual Kubernetes cluster with its components running inside a container. To see that type the command minikube ssh
to enter this running container and then execute the docker ps
command.
You can see all the components of the Kubernetes cluster are running inside the containers.
📝Note: Make sure you terminate this instance within 1 hour since 't2.medium' is not under the free tier and cost charges on an hourly basis.🖊
Permalink📍Kubeadm Setup
This is used in production, and for this, we need to set up AWS Instances:
kubernetes-master
kubernetes-node
Step 1
: Click on 'Launch Instance' and give the name as 'kubernetes-master' and on the right-hand side in the 'Number of Instances' type '2'.
Step 2
: Select 'Ubuntu' AMI and instance type 't2.medium'.
Step 3
: Select the 'minikube-key' key pair we created while creating minikube instance.
Step 4
: Click on 'Launch instance'
Step 5
: Click on each 'instance id' and that particular instance will be opened in two separate types.
Step 6
: Rename one of the instances as 'kubernetes-node'. This is our worker node.
So now we have a master instance which will contain four components and a node instance that will contain two components:
Step 7
: Now connect these two instances using the SSH client as we saw in the Minikube setup section in two separate GitBash terminals. Keep it side by side.
Since they are both similar we will modify it to identify which is the master node and the worker node.
Go to your Master instance terminal and do the following steps:
Step 1
: Click on the 'GitBash' icon on the control bar of the GitBash terminal. You will see a window like this:
Step 2
: Click on 'Options' and you will see this window:
Step 3
: Select 'Foreground' in the Colours section, select 'Red' color and click 'OK'
Step 4
: Click on 'Apply'. You will the text is now in 'Red'.
Do similar steps to your worker node terminal with the 'Blue' color. Now, you can identify your master instance and node instance with 'Red' and 'Blue' colors respectively. Like this:
Now, follow the Kubeadm installation steps in this GitHub URL Kuberstarter
📝Note: Make sure you terminate this instance within 1 hour since 't2.medium' is not under the free tier and cost charges on an hourly basis.🖊
Permalink📍Conclusion
Thank you for reading this blog! 📖 Hope you have gained some value. In the future blog, we will be launching a Kubernetes cluster with nginx running on it. Until then get more familiarized with the Kubernetes cluster. If you want to practice Kubernetes make sure you do it on Minikube or at killercoda.com where you get K8s playground for free.
If you enjoyed this blog and found it helpful, please give it a like 👍, share it with your friends, share your thoughts, and give me some valuable feedback.😇 Don't forget to follow me for more such blogs! 🌟