Kubernetes is an open-source container orchestration platform that provides a way to manage containerized applications across a cluster of nodes. Kubernetes Deployments are an essential part of Kubernetes, as they enable you to manage the rollout of containerized applications across a cluster of nodes.

A Deployment in Kubernetes is a higher-level abstraction that manages the lifecycle of one or more replicas of a pod. A pod is a Kubernetes object that represents a single instance of a running application. A deployment ensures that a specified number of replicas of the pod are running at any given time. If any replicas fail, the deployment replaces them with new replicas.

In this blog post, we will discuss how to create a deployment in Kubernetes.

Related Articles

Prerequisites

Before we dive into creating a deployment, we need to ensure that we have a few things in place:

  • A Kubernetes cluster with kubectl configured to connect to the cluster
  • A Docker image of the application we want to deploy.

For the purposes of this blog post, we will assume that we already have a Docker image of our application stored in a container registry.

Creating a Deployment

To create a deployment, we need to define a deployment manifest, which is a YAML file that describes the desired state of our deployment.

Here is an example deployment manifest for a simple Node.js application:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-registry/my-app:v1
        ports:
        - containerPort: 3000

Let's break down this deployment manifest:

  • apiVersion: This specifies the API version of the Kubernetes object we are creating. In this case, we are using the apps/v1 API version.
  • kind: This specifies the kind of Kubernetes object we are creating. In this case, we are creating a Deployment.
  • metadata: This section contains metadata about the deployment, such as the name of the deployment.
  • spec: This section contains the desired state of the deployment.
    • replicas: This specifies the number of replicas of our application that should be running at any given time. In this example, we want three replicas.
    • selector: This specifies how Kubernetes should select which pods to manage with this deployment. In this example, we are using the app: my-app label to select the pods.
    • template: This section describes the pod template that Kubernetes will use to create new replicas of our application.
      • metadata: This section contains labels that Kubernetes will use to identify the pod.
    • spec: This section describes the containers that should be running in the pod.
      • name: This specifies the name of the container.
      • image: This specifies the Docker image that should be used for the container.
      • ports: This specifies which ports should be exposed by the container.

Once we have created our deployment manifest, we can create the deployment using the kubectl create command:

$ kubectl create -f deployment.yaml

This will create a deployment with the desired state specified in the deployment manifest.

Managing Deployments

Now that we have created our deployment, we can manage it using various Kubernetes commands.

To view information about our deployment, we can use the kubectl get command:

$ kubectl get deployments

This will show us a list of all deployments in our cluster, including the name, desired number of replicas, current number of replicas, and the age of each deployment.

To view more detailed information about a specific deployment, we can use the kubectl describe command:

$ kubectl describe deployment my-app-deployment

This will show us detailed information about our my-app-deployment, including the number of replicas, the selector, the pod template, and the current status of the deployment.

If we need to update our deployment, we can modify the deployment manifest and apply the changes using the kubectl apply command:

$ kubectl apply -f deployment.yaml

This will update our deployment with the new desired state specified in the updated deployment manifest.

If we need to scale our deployment up or down, we can use the kubectl scale command:

kubectl scale deployment my-app-deployment --replicas=5

This will scale our deployment to have five replicas.

If we need to roll out a new version of our application, we can create a new deployment with the updated Docker image and then update the service to point to the new deployment. This is known as a rolling update.

Here is an example rolling update manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment-v2
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
        version: v2
    spec:
      containers:
      - name: my-app
        image: my-registry/my-app:v2
        ports:
        - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
    version: v2
  ports:
  - name: http
    port: 80
    targetPort: 3000

In this example, we are creating a new deployment with the updated Docker image and a new version label (version: v2). We are also updating the service to use the new deployment by specifying the new version label in the selector.

We can apply this manifest using the kubectl apply command:

$ kubectl apply -f rolling-update.yaml

This will create a new deployment with the updated Docker image and update the service to use the new deployment. Kubernetes will automatically perform a rolling update, replacing the old replicas with the new replicas one at a time to ensure zero downtime.

Conclusion

In conclusion, creating a deployment in Kubernetes is a simple process that involves defining a deployment manifest and using kubectl create to create the deployment. Once the deployment is created, we can manage it using various Kubernetes commands, such as kubectl get, kubectl describe, kubectl apply, and kubectl scale.

We can also perform rolling updates to deploy new versions of our application with zero downtime. Deployments are an essential part of Kubernetes and enable us to manage the lifecycle of our containerized applications across a cluster of nodes.