How to Deploy Applications with Kubernetes Deployment

KubernetesKubernetesBeginner
Practice Now

Introduction

Kubernetes has become a leading platform for container orchestration, enabling developers and DevOps teams to efficiently deploy and manage their applications. In this tutorial, we will explore the process of deploying applications with Kubernetes Deployment, covering the fundamental concepts, the deployment process, and advanced customization techniques.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedDeploymentGroup(["`Advanced Deployment`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/CoreConceptsGroup(["`Core Concepts`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/AdvancedDeploymentGroup -.-> kubernetes/rollout("`Rollout`") kubernetes/AdvancedDeploymentGroup -.-> kubernetes/scale("`Scale`") kubernetes/CoreConceptsGroup -.-> kubernetes/architecture("`Architecture`") subgraph Lab Skills kubernetes/create -.-> lab-398436{{"`How to Deploy Applications with Kubernetes Deployment`"}} kubernetes/apply -.-> lab-398436{{"`How to Deploy Applications with Kubernetes Deployment`"}} kubernetes/rollout -.-> lab-398436{{"`How to Deploy Applications with Kubernetes Deployment`"}} kubernetes/scale -.-> lab-398436{{"`How to Deploy Applications with Kubernetes Deployment`"}} kubernetes/architecture -.-> lab-398436{{"`How to Deploy Applications with Kubernetes Deployment`"}} end

Understanding Kubernetes Deployment

What is Kubernetes Deployment?

Kubernetes Deployment is a Kubernetes API object that provides declarative updates for Pods and ReplicaSets (the next-generation Replication Controller). It manages the deployment and scaling of a set of Pods, and provides a way to update Pods incrementally with new configurations or images.

Key Features of Kubernetes Deployment

  • Scaling: Kubernetes Deployment can scale up or down the number of Pods based on usage demands.
  • Rollout and Rollback: Kubernetes Deployment supports rolling updates and rollbacks, allowing you to easily update an application and roll back to a previous version if needed.
  • Self-Healing: Kubernetes Deployment automatically replaces Pods that fail, gets stuck, or are deleted.
  • Declarative Configuration: Kubernetes Deployment uses a declarative configuration, making it easier to manage and version control your application deployments.

Deployment Components

A Kubernetes Deployment consists of the following key components:

  1. Deployment: The Deployment resource that manages the lifecycle of Pods.
  2. ReplicaSet: The ReplicaSet resource that ensures the desired number of Pods are running.
  3. Pods: The Pods that contain the actual application containers.
graph TD Deployment --> ReplicaSet ReplicaSet --> Pods

Deployment Use Cases

Kubernetes Deployment is commonly used for the following scenarios:

  • Stateless Applications: Deploying and managing stateless web applications, microservices, and APIs.
  • Scaling Applications: Automatically scaling applications based on resource usage or user demand.
  • Canary Deployments: Gradually rolling out changes to an application by deploying a new version alongside the old version.
  • Blue-Green Deployments: Deploying a new version of an application alongside the old version, and then switching traffic to the new version.

Deploying Applications with Kubernetes

Creating a Kubernetes Deployment

To deploy an application with Kubernetes Deployment, you need to create a Deployment YAML file. Here's an example:

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

This Deployment will create three replicas of the labex/my-app:v1 container, and expose port 80.

Deploying the Application

To deploy the application, save the YAML file and run the following command:

kubectl apply -f deployment.yaml

This will create the Deployment and the associated ReplicaSet and Pods.

Verifying the Deployment

You can verify the Deployment status using the following commands:

kubectl get deployments
kubectl get replicasets
kubectl get pods

This will show the current state of the Deployment, ReplicaSet, and Pods.

Scaling the Deployment

To scale the Deployment, you can update the replicas field in the YAML file and apply the changes:

spec:
  replicas: 5

Then, run kubectl apply -f deployment.yaml to update the Deployment.

Updating the Application

To update the application, you can change the container image in the Deployment YAML file and apply the changes:

spec:
  template:
    spec:
      containers:
        - name: my-app
          image: labex/my-app:v2

Then, run kubectl apply -f deployment.yaml to update the Deployment. Kubernetes will perform a rolling update, replacing the old Pods with the new Pods.

Customizing Kubernetes Deployment

Deployment Strategies

Kubernetes Deployment supports different update strategies to control how the new Pods are rolled out:

  1. Recreate: Terminates the existing Pods before creating new ones.
  2. RollingUpdate: Gradually replaces the old Pods with new Pods.

You can specify the update strategy in the Deployment YAML file:

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1

Deployment Pausing and Resuming

You can pause a Deployment to prevent automatic updates, make changes, and then resume the Deployment:

kubectl rollout pause deployment/my-app
## Make changes to the Deployment
kubectl rollout resume deployment/my-app

Deployment History and Rollback

Kubernetes Deployment keeps a history of all revisions, allowing you to easily roll back to a previous version:

kubectl rollout history deployment/my-app
kubectl rollout undo deployment/my-app

Deployment Probes

Kubernetes Deployment supports health checks using probes, which can be configured in the Pod template:

spec:
  containers:
    - name: my-app
      livenessProbe:
        httpGet:
          path: /healthz
          port: 8080
      readinessProbe:
        httpGet:
          path: /ready
          port: 8080

The liveness probe checks if the container is still running, and the readiness probe checks if the container is ready to receive traffic.

Deployment Annotations and Labels

You can add custom annotations and labels to the Deployment, ReplicaSet, and Pods to add metadata and enable integration with other Kubernetes features:

metadata:
  annotations:
    my-annotation: value
  labels:
    my-label: value

These annotations and labels can be used for things like monitoring, service discovery, and more.

Summary

By the end of this tutorial, you will have a solid understanding of Kubernetes Deployment and how to leverage it to streamline the deployment of your applications. You will learn to create and configure Kubernetes Deployments, customize deployment settings, and ensure the reliable and scalable deployment of your applications in a Kubernetes environment.

Other Kubernetes Tutorials you may like