Skip to main content

Command Palette

Search for a command to run...

Kubernetes Production Deployment

Updated
6 min read
Kubernetes Production Deployment

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.

Kubeadm Cluster

After creating Kubeadm cluster come to this article.

Before going to deep down, let's practice some Kubernetes Commands.

To get Namespaces we user below command.

kubectl get namespaces
or you can use "kubectl get ns"

You will get the below result.

Now I want to see what is in default NameSpace

kubectl get pod -ns=default

Currently, nothing is running in the default pod.

Now, I want to create my own NameSpace as django-todo-ns

kubectl create namespace django-todo-ns

Here is the 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 and move into it.

Create a pod.yml directory.

vim pod.yml
// code inside pod.yml

apiVersion: v1  //Api version
kind: Pod     //defining if it is pod or volume
metadata:     //details of project
  name: django-todo   //project name
  namespace: django-todo-ns   //namespace name where it willRUN
spec:
  containers:
  - name: django-todo //container name
    image: trainwithshubham/django-todo:latest //docker image
    ports:
    - containerPort: 8000 /allowd ports

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 12 minutes.

Now will kill it.

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:

  1. 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.

  2. 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.

apiVersion: apps/v1   //API version, can refer official doc
kind: Deployment    //type define as deployent
metadata:
  name: django-todo-deployment  //deployment name
  namespace: django-todo-ns  //namespace name
  labels:
    app: django-todo  //label name
spec:
  replicas: 3   //it will create 3 replica of this project
  selector:        //selector shold be same as label
    matchLabels:
      app: django-todo   //selector name
  template:
    metadata:
      labels:
        app: django-todo  //template name
  spec:
      containers:
      - name: django-todo
        image: trainwithshubham/django-todo:latest
        ports:
        - containerPort: 8000

Now, Apply this deployment by the Kubectl.

kubectl apply -f deployment.yml

I will check if the pods are created or not.

kubectl get pods -n=django-todo-pods

Here three pods are created.

Now, I want only 1 pod instead of 3, so I will just go to deployment.yml file and change replica from 3 to one.

$ vim deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-todo-deployment
  namespace: django-todo-ns
  labels:
    app: django-todo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: django-todo
  template:
    metadata:
      labels:
        app: django-todo
    spec:
      containers:
      - name: django-todo
        image: trainwithshubham/django-todo:latest
        ports:
        - containerPort: 8000

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

$ vi service.yml

apiVersion: v1
kind: Service
metadata:
  name: django-todo-service  #service name
  namespace: django-todo-ns  #namspace name
spec:
  type: NodePort
  selector:
    app: django-todo  #selector name
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80   #port 80 will be mapped to 8000 
      targetPort: 8000    #on this port application running
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30007  //port of whole cluster

Now, let's Apply this service by the Kubectl.

kubectl apply -f service.yml

Servicee Created.

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!!

Now, Let's hit the Worker Node's PublicIP with port 3007

Here is your application is running smoothly.

You can all the primary task on your Master node.

No need to go anywhere.

Thank you.

Saif Ali

Senior Software Engineer

Kubernetes Production Deployment