Create your first deployment on a Kubernetes Cluster using Nginx - Step by step guide ?


Deployments represent a set of multiple, identical Pods with no unique identities. Basically, a Deployment runs multiple replicas of your application and automatically replaces any instances that fail or become unresponsive. By so doing, they help ensure that one or more instances of your application are available to serve user requests.

Here at Outsource Path, as part of our Server Management Services, we regularly help our Customers to perform related AWS Kubernetes queries.

In this context, we shall look into how to create your first deployment on a Kubernetes Cluster using Nginx. 


More about Kubernetes Cluster deployment

A Deployment provides declarative updates for Pods and ReplicaSets. The state in the Deployment and the Deployment Controller changes the actual state to the desired state at a controlled rate.

We can create and manage a Deployment by using the "kubectl" Kubernetes command line interface. Kubectl uses the Kubernetes API to interact with the cluster.


The stages Kubernetes Cluster includes:

  • Progressing: Kubernetes marks a Deployment as progressing when the Deployment creates a new ReplicaSet, the Deployment is scaling up its newest ReplicaSet or scaling down its older ReplicaSet or new Pods become ready or available. 
  • Complete: Kubernetes marks a Deployment as complete when all of the replicas associated with the Deployment have been updated, are available, no old replicas for the Deployment are running.
  • Failed: This can occur due to insufficient quota, readiness probe failures, image pull errors, insufficient permissions.


How to Create a Kubenetes Deployment for NginX ?

To create our first deployment let's just create a new directory to create our Object/Deployment File. Use the following command to create a new directory in your system:

$ mkdir my-first-deployment
$ cd my-first-deployment/

Before we proceed, verify the status of the cluster.

To check the Nodes available in the cluster and to check the  version of the "kubectl" use the following commands:

$ sudo kubectl version
$ sudo kubectl get nodes

Once you have Nodes available in your cluster you are ready to create your deployment.

Create a file "my-first-deployment.yml" with the following block of code

$ vim my-first-deployment.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

Here,

  • apiVersion: APIVersion defines the versioned schema of this representation of an object.
  • kind: Kind of object you want to create like Deployment, Service, Configmap and more.
  • name: Name must be unique within a namespace. 
  • labels: Map of string keys and values that can be used to organize and categorize objects
  • spec: Specification of the desired behavior of the Deployment.
  • replicas: Number of desired pods.
  • selector: Label selector for pods. Existing ReplicaSets whose pods are selected by this will be the ones affected by this deployment. It must match the pod template's labels.


Now you are ready to create your deployment using the following commands:

$ sudo kubectl apply -f my-first-deployment.yml
$ sudo kubectl  get deployments

Here, you will see that the deployment has been created with two pods that are available to use.

You can get the details of the pods using the following command:

$ sudo kubectl get pods

If you want to know more about the deployment you can use this "kubectl describe" command to get the complete details of the deployment:

$ sudo kubectl get deployments
$ sudo kubectl describe  deployments nginx-deployment

If you no more require the deployment you can delete it using "kubectl delete command":

$ sudo kubectl  get deployments
$ sudo kubectl delete deployments nginx-deployment
$ sudo kubectl  get deployments

Here, you will see that the deployment is no more available after it is deleted.

Once you delete the deployment the pods too get deleted.

You can check for the availability of the pods using the following command:

$ sudo  kubectl  get pods

Now, you can see that the pods have been deleted after deleting the deployment and are not available.


[ Need help in configuring Kubernetes Cluster ? We can help you. ]


Conclusion

This article covers the complete steps to create your first NginX Deployment on Kubernetes. In fact, Deployments are managed by the Kubernetes Deployment controller. Deployments use a Pod template, which contains a specification for its Pods. The Pod specification determines how each Pod should look like: what applications should run inside its containers, which volumes the Pods should mount, its labels, and more. When a Deployment's Pod template is changed, new Pods are automatically created one at a time.


Recent Post