What is Kubernetes?
K8S is the abbreviation for "Kubernetes", an open-source platform for automating deployment, scaling, and management of containerized applications.
When you look at the logo of Kubernetes, it is actually a ship wheel that is used to control/manage the ship. Here Kubernetes controls/manages the containers.
You can manage all the running containers in one place. Similar to docker swarm.
Multi-container management system. Maintains different services/containers for different functionalities of the application (microservices).
Ex: For Facebook, services should be assigned to authentication, stories, messaging, block lists, etc so that there will be zero downtime.
Kubernetes Architecture
There will be a Master and a Node. Combination of these 2 is called a cluster.
Master Duty: It is to manage the applications running in the node. It ensures that what , how applications should run on a node. It will take care if an application goes wrong.
Node: Applications will be running in the node.
Master Side:
You can setup more nodes using master.
Control Panel: You can control the nodes using this control panel.
API Server: API server of master allows to connect with the nodes.
Scheduler: Used to schedule jobs, containers, tasks to the nodes. Scheduler will contact the API server in the backend and this API server will schedule the jobs to the nodes.
Kubectl: API server provides the interface for managing a Kubernetes cluster, while kubectl is a tool used to access and interact with that interface.
Kubectl gives instructions to API server. API server implements the instructions based as given by kubectl.
etcd:
etcd is a distributed key-value store(database) that is commonly used as a backing store for configuration management. It stores bac
Node Side:
Kubelet: The kubelet communicates with the API server to receive information about what containers should be running on a given node, and then starts and monitors those containers.
Service Proxy:
Also known as a kube-proxy, is a component responsible for routing network traffic to the correct pods or services.
pod
It is the smallest and simplest unit in the Kubernetes. It represents a single instance of a running process in your cluster. Pods contain one or more containers, and they provide the environment for these containers to run in.
pod can be a container or group of containers. pod will be running in a node.
Hands-On ๐
We will be using t2 medium as for this hands-on we require at least 2 CPUs.
Requirements for Kubernetes
You need docker to be installed if you want to run minikube.
sudo apt-get update
sudo apt-get install docker.io
Install minikube (type minikube start ) minikube will install kubernetes for us.
#Install mini kube by following command
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
#start it by
sudo usermod -aG docker $USER && newgrp docker
minikube start --driver=docker
Now minikube is installed. Let's check the containers running. Minikube image is running which contained Kubernetes. This contains the Kubernetes cluster.
docker ps
Let's go inside minikube to see what Kubernetes things persist. It contains a control manager, scheduler,etcd, API server etc
minikube ssh
docker ps
Kubectl
You need to talk to the node from the master. So you need kubectl.
You cant get kubectl by default in this image. You need to install it.
sudo snap install kubectl --classic
To install containerized applications, we use snap.
Now we can talk to API server via Kubectl. Now lets communicate with the API server using kubectl.
Namespace
Asking API server to show how many PODs are there
kubectl get pods
Before going into details, lets know about namespace.
๐คNamespace is like a label given to pod.
Example, If we want to store all the pods related to django app then we can create a namespace called django-app-pod and it will contain all django-apps.
Types of Namespaces
default
Kubernetes includes this namespace so that you can start using your new cluster without first creating a namespace.
kube-node-lease
This namespace holds Lease objects associated with each node. Node leases allow the kubelet to send heartbeats so that the control plane can detect node failure.
kube-public
This namespace is readable by all clients (including those not authenticated). This namespace is mostly reserved for cluster usage, in case that some resources should be visible and readable publicly throughout the whole cluster. The public aspect of this namespace is only a convention, not a requirement.
kube-system
The namespace for objects created by the Kubernetes system.
To check list of namespaces
kubectl get namespaces
This will displays the pods in default namespace.
kubectl get pods
This will displays the pods in kubesystem namespace.
kubectl get pods --namespace kubesystem
To create a namespace
kubectl create namespace django-todo-app(namespace name)
To know no.of nodes
kubectl get nodes
Creating a pod in K8S
Now lets create a pod and allocate to namespace. ๐
Note : Inside pod running containers will takes place. No need to build.
To create a pod in kubernetes, you need to run YAML file. ๐
Lets build a small project.
mkdir kubernetes-projects
cd kubernetes-projects
vim pod.yaml
We need to create pods in this yaml file.
pod will take the images from dockerhub and runs the container.
kubectl create namespace django-todo-app(namespace name)
After adding pods in yaml file, create it using
kubectl apply -f pod.yaml
pod will be created.
Check if it is running or not using
kubectl get pods --namespace=django-todo-app-ns
check if pod is created
minikube ssh
docker ps
Note: If you want to update the running container which created using pod, you cannot do that.
deleting pod
kubectl delete -f pod.yaml
Deployment file: (update of container)
Why docker and pod used in production?
If a container/pod crashes we cannot up the container.
We deleted this pod. We cant get back once deleted /crashed. Instead we can use deployment for Auto healing and Auto scaling.
Deployment file is source of truth for one application. Ex: for django pods there will be one application for entire django application.
Here, for each application like djabngo, python, java there will be a separate deployment file. Same deployment file for django related application. If pod is crashed then we can have replicas of nodes which will be mentioned in deployment file. It is similar to namespace where django related apps will be allocated by one namespace but it is having one limitation. Auto scaling and healing is not possible.
The label is a name where the application is identified with. Selectors labels should match with this label.
Here deployment file of django will match with selector having same label.
Replication is done based on lables and selectors.
Writing a deployment file.
kind : deployment -->have features like auto-scaling and auto-healing.
Hands-On on deployment file ๐
apiVersion
: Specifies the version of the Kubernetes API that this deployment uses. In this case, it is using version apps/v1
.
kind
: Specifies the type of resource that this YAML file is creating. In this case, it is creating a Deployment
.
metadata
: Contains information about the deployment, such as its name and labels.
name
: Specifies a name for the deployment. In this case, it is nameddjango-app-deployment
.labels
: Associates metadata with the deployment in the form of key-value pairs. In this case, it is using the labelapp
with a value ofdjango-app
.namespace
: Specifies the namespace that the deployment belongs to. In this case, it belongs to thedjango-todo-app-ns
namespace.
spec
: Specifies the details of the deployment, such as the number of replicas, selector, and template.
replicas
: Specifies the number of replicas of the deployment that should be running at any given time. In this case, there should be3
replicas.selector
: Specifies how the deployment should select which pods to manage. The deployment will manage pods that have the labels specified inmatchLabels
. In this case, it will select pods with the labelapp
and a value ofdjango-app
.template
: Specifies the properties of the pods that the deployment will create and manage.metadata
: Contains information about the pod, such as its labels. In this case, it is using the labelapp
with a value ofdjango-app
.spec
: Specifies the details of the pod, such as the container and its properties.containers
: Specifies the containers that should run in the pod. In this case, it has only one container.name
: Specifies a name for the container. In this case, it is nameddjango-todo-ctr
.image
: Specifies the Docker image that should be used to create the container. In this case, it is using the imageaasifa/django-todo-app-img
with taglatest
.ports
: Specifies the network ports that should be exposed by the container. In this case, it is exposing port8001
.
When we apply the deployment file pods mentioned in the file will be created with mentioned no.of replicas.
Let's delete (assume crashed) the pods and see if replicas will be created or not.
Here I deleted 3 replicas, but those were recovered by another 3 replicas.
It means it auto-healed. This is the advantage of the deployment file.
--Thats all about k8s basics. ๐Thank you for your time--