How to create a Kubernetes deployment with custom image

KubernetesKubernetesBeginner
Practice Now

Introduction

This tutorial will guide you through the fundamentals of Kubernetes deployments, including how to deploy and configure them, as well as how to manage and update your deployments for your containerized applications. By the end of this tutorial, you will have a solid understanding of Kubernetes deployments and how to leverage them to manage the lifecycle of your applications.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedDeploymentGroup(["`Advanced Deployment`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/run("`Run`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/AdvancedDeploymentGroup -.-> kubernetes/scale("`Scale`") subgraph Lab Skills kubernetes/describe -.-> lab-417503{{"`How to create a Kubernetes deployment with custom image`"}} kubernetes/create -.-> lab-417503{{"`How to create a Kubernetes deployment with custom image`"}} kubernetes/run -.-> lab-417503{{"`How to create a Kubernetes deployment with custom image`"}} kubernetes/apply -.-> lab-417503{{"`How to create a Kubernetes deployment with custom image`"}} kubernetes/scale -.-> lab-417503{{"`How to create a Kubernetes deployment with custom image`"}} end

Understanding Kubernetes Deployments

Kubernetes deployments are a fundamental concept in the Kubernetes ecosystem, providing a declarative way to manage the lifecycle of containerized applications. Deployments abstract the underlying Pods and ReplicaSets, simplifying the process of scaling, updating, and rolling back your applications.

Deployment Basics

A Kubernetes Deployment is a higher-level abstraction that manages the creation and management of ReplicaSets, which in turn manage the lifecycle of Pods. Deployments provide a declarative way to define the desired state of your application, and Kubernetes will work to ensure that the actual state matches the desired state.

graph TD A[Deployment] --> B[ReplicaSet] B[ReplicaSet] --> C[Pod] C[Pod] --> D[Container]

The key components of a Kubernetes Deployment are:

Component Description
Deployment Defines the desired state of the application
ReplicaSet Ensures the desired number of replicas are running
Pod The basic unit of deployment, containing one or more containers

Deployment Architecture

Kubernetes Deployments leverage the underlying Kubernetes architecture to provide a robust and scalable application management solution. When you create a Deployment, Kubernetes will automatically create a ReplicaSet to manage the desired number of Pods. As you update your application, Kubernetes will create a new ReplicaSet with the updated configuration and gradually migrate traffic to the new Pods, ensuring a smooth rollout.

graph TD A[Deployment] --> B[ReplicaSet 1] B[ReplicaSet 1] --> C[Pod 1] B[ReplicaSet 1] --> C[Pod 2] A[Deployment] --> D[ReplicaSet 2] D[ReplicaSet 2] --> E[Pod 3] D[ReplicaSet 2] --> E[Pod 4]

Deployment Lifecycle Management

Kubernetes Deployments provide a rich set of features for managing the lifecycle of your applications, including scaling, updating, and rolling back. You can easily scale your application by modifying the replicas field in your Deployment configuration. When you update your application, Kubernetes will create a new ReplicaSet with the updated configuration and gradually migrate traffic to the new Pods, ensuring a smooth rollout. If necessary, you can also roll back to a previous version of your application.

## Scale the deployment
kubectl scale deployment my-app --replicas=5

## Update the deployment
kubectl set image deployment/my-app my-app=my-app:v2

## Rollback the deployment
kubectl rollout undo deployment/my-app

By understanding the basics of Kubernetes Deployments, including their architecture and lifecycle management capabilities, you can effectively manage the deployment and evolution of your containerized applications.

Deploying and Configuring Kubernetes Deployments

Deploying and configuring Kubernetes Deployments is a straightforward process that allows you to manage the lifecycle of your containerized applications. In this section, we'll explore the steps to create a Deployment, configure it with custom settings, and scale it to meet your application's needs.

Creating a Deployment

To create a Kubernetes Deployment, you can use the kubectl create deployment command or define a Deployment manifest in YAML format. Here's an example of a Deployment manifest:

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: my-app:v1
        ports:
        - containerPort: 8080

This Deployment will create three replicas of the my-app container, each exposing port 8080.

Configuring a Deployment

Kubernetes Deployments offer a wide range of configuration options to customize your application's behavior. You can specify various settings, such as environment variables, resource requests and limits, liveness and readiness probes, and more. Here's an example of a more complex Deployment configuration:

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: my-app:v2
        ports:
        - containerPort: 8080
        env:
        - name: DATABASE_URL
          value: postgres://user:password@db/myapp
        resources:
          requests:
            cpu: 100m
            memory: 128Mi
          limits:
            cpu: 500m
            memory: 512Mi
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
          periodSeconds: 10
          failureThreshold: 3

This configuration sets environment variables, resource requests and limits, and a liveness probe for the my-app container.

Scaling a Deployment

One of the key benefits of Kubernetes Deployments is the ability to easily scale your application up or down. You can scale a Deployment by modifying the replicas field in the Deployment manifest or by using the kubectl scale command:

## Scale the deployment to 5 replicas
kubectl scale deployment my-app --replicas=5

Kubernetes will automatically create or remove Pods to match the desired number of replicas, ensuring that your application can handle the increased or decreased load.

By understanding how to create, configure, and scale Kubernetes Deployments, you can effectively manage the deployment and scaling of your containerized applications.

Managing and Updating Kubernetes Deployments

Kubernetes Deployments provide a robust set of features for managing the lifecycle of your containerized applications, including updating and rolling back deployments. In this section, we'll explore how to update a Deployment, manage the rollout process, and perform a rollback if necessary.

Updating a Deployment

Updating a Kubernetes Deployment is a straightforward process. You can modify the Deployment manifest to change the container image, environment variables, or any other configuration settings. When you apply the updated manifest, Kubernetes will create a new ReplicaSet with the new configuration and gradually migrate traffic to the new Pods, ensuring a smooth rollout.

## Update the container image
kubectl set image deployment/my-app my-app=my-app:v2

## Apply the updated deployment manifest
kubectl apply -f deployment.yaml

Managing Rollouts

Kubernetes Deployments provide a built-in rollout management system that allows you to monitor the progress of an update and, if necessary, pause or roll back the deployment. You can use the kubectl rollout command to interact with the rollout process.

## Check the rollout status
kubectl rollout status deployment/my-app

## Pause the rollout
kubectl rollout pause deployment/my-app

## Resume the rollout
kubectl rollout resume deployment/my-app

Rolling Back a Deployment

If an update to your Deployment introduces issues, you can easily roll back to a previous version of your application. Kubernetes maintains a history of all Deployment revisions, allowing you to roll back to a specific revision.

## List the deployment revisions
kubectl rollout history deployment/my-app

## Rollback to a previous revision
kubectl rollout undo deployment/my-app --to-revision=2

Deployment Best Practices

When managing Kubernetes Deployments, it's important to follow best practices to ensure the reliability and maintainability of your applications. Some key best practices include:

  • Use semantic versioning for your container images
  • Implement liveness and readiness probes to ensure the health of your Pods
  • Configure resource requests and limits to prevent resource starvation
  • Leverage deployment strategies like Blue/Green or Canary deployments for safer updates
  • Implement automated testing and continuous deployment pipelines

By understanding how to manage and update Kubernetes Deployments, you can effectively manage the lifecycle of your containerized applications and ensure they are always running in a healthy and up-to-date state.

Summary

Kubernetes deployments are a powerful tool for managing the lifecycle of containerized applications. In this tutorial, you learned about the key components of a Kubernetes deployment, including the Deployment, ReplicaSet, and Pod. You also explored the deployment architecture and how Kubernetes manages the lifecycle of your applications, including scaling, updating, and rolling back. With this knowledge, you can now confidently create and manage Kubernetes deployments to ensure the reliability and scalability of your containerized applications.

Other Kubernetes Tutorials you may like