Creating your first deployment on a Kubernetes Cluster

In this article, we will see how to create your first deployment on a Kubernetes Cluster. As an example, we will create a deployment for Nginx.

Once we have a running Kubernetes cluster, we can deploy our containerized applications on top of it. We can create a Kubernetes Deployment configuration to achieve this.

A Deployment provides declarative updates for Pods and ReplicaSets. We describe a desired 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.

There are three stages in a deployment lifecycle:

  1. 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 
  2. Complete: Kubernetes marks a Deployment as complete when all of the replicas associated with the Deployment have been updated, are available, and no old replicas for the Deployment are running.
  3. Failed: This can occur due to insufficient quota, readiness probe failures, image pull errors or insufficient permissions.

Pre-requisites

  1. AWS Account (Create one if you don’t have one)
  2. A Kubernetes Cluster

Note: You can use VMs too to create a cluster if you do not want to try on AWS EC2 Instances.

What we will do

  1. Create a Kubernetes Deployment for Nginx.

Create a Kubernetes 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

kubectl version

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,

  • apiVersionAPIVersion defines the versioned schema of this representation of an object.
  • kind: The kind of object you want to create like Deployment, Service, Configmap, and more.
  • name: The name must be unique within a namespace
  • labelsMap 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

kubectl apply

In the above screenshot, you can 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

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

kubectl get deployments

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

kubectl  get deployments

In the above screenshot, you can 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

kubectl  get pods

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

Conclusion

In this article, we saw the steps to create your first Nginx Deployment on Kubernetes. We also saw how the details regarding the deployment can be extracted. Along with this, we explored the commands to delete the deployment.

Share this page:

0 Comment(s)