In this project, we will explore the fundamental concepts of Kubernetes and guide you through the process of setting up your cluster. You will learn how to create Pod YAML files, Deployment YAML files, and Service YAML files to manage your applications effectively.
But that's not all! We'll also cover advanced topics like auto scaling and auto healing, which will ensure that your cluster can handle fluctuations in traffic and recover from failures seamlessly. Additionally, we'll dive into the powerful features of Kubeadm, a popular tool for Kubernetes cluster bootstrapping.
To make your cluster even more efficient, we'll introduce you to labels and selectors. These mechanisms allow you to organize and group your resources, making it easier to manage and scale your applications.
To deploy the application on Kubernetes you need Kubeadm cluster. Don't worry I already have a perfect article on it you can refer here.
Launching your First Kubernetes Cluster with Nginx running using Kubeadm
After creating Kubeadm cluster come to this article. Before going to deep down, let's practice some Kubernetes Commands.
To get Namespaces we user the below command.
kubectl get namespaces
or you can use "kubectl get ns"
You will get the below result.
Now, I will run djnago application in this namespace. Let's come to the main point and start our project deploying step by step.
Create a directory as deploy_k8s_project and move into it.
Now, I will run djnago application in this namespace. Let's come to the main point and start our project deploying step by step.
Create a directory as deploy_k8s and move into it.
Clone the Django-todo repository from Github.
You can clone the repository from Github using the git clone. https://github.com/nahidkishore/django-todo-app-docker.git command.
Now, if I want to create my own NameSpace as django-todo-ns
kubectl create namespace django-todo-ns
Create a pod.yml directory.
vim pod.yml
apiVersion: v1
kind: Pod
metadata:
name: django-todo
namespace: django-todo-ns
spec:
containers:
- name: django-todo
image: nahid0002/django-todo-app:latest
ports:
- containerPort: 8000
Note: Before going to write these files please go through the Official doc of Kubernetes, here is the link.
https://kubernetes.io/docs/concepts/workloads/pods/
Now, will apply this configuration in the cluster.
kubectl apply -f pod.yml
Now I will go to the Worker node and check if I get the container or not.
On Worker Node:
sudo docker ps
Here we found the container is running. Let's go inside this container
docker exec -it ContanerID bash
Now I Curl here,
curl -L http://127.0.0.1:8000
Here it is running refer below.
Now, Let's understand auto-healing.
If you can see on Master there is one pod running with 0 restarts.
Now, I will go to the Worker node and kill the container. You can see current container is running from last 8 minutes.
Now will kill it.
dokcer kill conatiner ID
Now again I will do docker ps
The Magic is here, Container is again restarted about a minute ago.
On Master, I will check again pod status.
Here we have restart 1 now.
It's called Auto-Healing!!!
Note: If you want to kill the pod in real then you have to delete the pod.yml file.
kubectl delete -f pod.yml
Labels and Selectors:
In Kubernetes, labels and selectors are used for organizing and selecting resources within the cluster. They are key-value pairs attached to Kubernetes objects, such as pods, services, or deployments, to help identify and group them based on specific characteristics or properties.
Labels are arbitrary metadata attached to Kubernetes objects. They can represent various attributes, such as an object's purpose, environment, version, or any other relevant information. Labels are designed to be flexible and can be customized based on your application's requirements. For example, you can label pods with "app=frontend" and "env=production" to indicate that they belong to the production environment and serve as frontend components.
Selectors, on the other hand, are used to identify and group objects based on their labels. They provide a way to query and select a set of resources that match a particular label or a combination of labels. Selectors are typically used when defining services, deployments, replica sets, or other resources that need to target a specific set of objects based on their labels. For example, you can create a service that selects all pods with the label "app=frontend" to ensure that traffic is routed to those pods.
Selectors can be defined using equality-based requirements or set-based requirements:
Equality-based requirements: Selects resources based on the exact match of labels. For example, selecting all pods with the label "app=frontend" and "env=production" would specify two equality-based requirements.
Set-based requirements: Allow more complex selections using operators such as "in", "not in", "exists", and "does not exist". These operators enable you to define selectors that match objects based on a range of label values or whether a label exists or not.
Labels and selectors are powerful concepts in Kubernetes that enable you to organize, manage, and target resources effectively within your cluster. They facilitate grouping resources logically, implementing service discovery, defining deployments, and more.
Let's create a deployment.yml file for more clarification.
vim deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: django-todo-deployment
namespace: django-todo-ns
labels:
app: django-todo
spec:
replicas: 4
selector:
matchLabels:
app: django-todo
template:
metadata:
labels:
app: django-todo
spec:
containers:
- name: djagno-todo
image: nahid0002/django-todo-app:latest
ports:
- containerPort: 8000
Now, Apply this deployment by the Kubectl.
I will check if the pods are created or not.
Here there are Four pods are created.
Now, I want only 1 pod instead of 4, so I will just go to deployment.yml file and change replica from 4 to one.
Again run the same command.
kubectl apply -f deployment.yml
If you can see, here we get ouput as "deployment.apps/django-todo-deployment configured" instead of "created"
Now, let's check again pods list.
It is called Scaling !!!!!!
Now, I want to run this application and open this pod for public traffic.
How, we will do it?
We will give a deployment to this Pod and will give a Label to it and will create a service that interacts with this label and access the application.
There are three types of services.
Node Port : Will access through the Node port
Cluster IP : Create a cluster IP and access it
Load Balancer : Through Load Balancing.
Now, I will create service.yml file
apiVersion: v1
kind: Service
metadata:
name: django-todo-service
namespace: django-todo-ns
spec:
type: NodePort
selector:
app: django-todo
ports:
# By default and for convenience, the `targetPort` is set to the same value as the `port` field.
- port: 80
targetPort: 8000
# Optional field
# By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
nodePort: 30007
Now, let's Apply this service by the Kubectl.
kubectl apply -f service.yml
Let's check service under "django-todo-ns" namespace
kubectl get service -n=django-todo-ns
As you can see, we need to allow port 30007 on the Worker Node.
Let's do it!!
Here is your application is running smoothly. You can all the primary task on your Master node. No need to go anywhere.
Congratulations! You have successfully Launching your Kubernetes Cluster with Production Deployment
Thank you for reading this blog. I hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.
— Happy Learning !!!
Let’s connect !!!