How to create a Kubernetes deployment with custom image?

KubernetesKubernetesBeginner
Practice Now

Introduction

This tutorial will guide you through the process of creating a Kubernetes deployment with a custom Docker image. You will learn the fundamentals of Kubernetes deployments and how to deploy your own containerized application on a Kubernetes cluster.


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

Kubernetes Deployment Basics

What is a Kubernetes Deployment?

A Kubernetes Deployment is a declarative way to manage the lifecycle of a set of replicated Pods. It ensures that a specified number of Pod replicas are running at any given time, and provides mechanisms for updating Pods, scaling the application, and rolling back changes.

Key Features of Kubernetes Deployments

  • Desired State: Deployments allow you to define the desired state of your application, including the number of replicas, the container image to use, and any other configuration settings.
  • Self-Healing: Deployments automatically create new Pods to replace any that fail or are deleted, ensuring your application is always running.
  • Rolling Updates: Deployments support rolling updates, allowing you to update your application's container image or configuration without downtime.
  • Scaling: Deployments make it easy to scale your application up or down by adjusting the number of replicas.

Deployment Architecture

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

The above diagram illustrates the basic architecture of a Kubernetes Deployment. The Deployment manages a ReplicaSet, which in turn manages the lifecycle of the Pods that make up the application.

Deployment Lifecycle Management

Kubernetes Deployments provide the following lifecycle management capabilities:

Operation Description
create Create a new Deployment
update Update an existing Deployment (e.g., change the container image)
scale Scale the number of replicas up or down
rollback Rollback to a previous version of the Deployment

These operations can be performed using the Kubernetes API or the kubectl command-line tool.

Creating a Kubernetes Deployment

Deployment YAML Configuration

To create a Kubernetes Deployment, you need to define a YAML configuration file that describes the desired state of your application. Here's an example:

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

This Deployment configuration creates three replicas of a container image named labex/my-app:v1, which listens on port 80.

Creating the Deployment

To create the Deployment, you can use the kubectl create command:

kubectl create -f deployment.yaml

This will create the Deployment in your Kubernetes cluster.

Verifying the Deployment

You can use the kubectl get command to verify that the Deployment was created successfully:

kubectl get deployments

This will list all the Deployments in your cluster, including the one you just created.

You can also use the kubectl describe command to view more detailed information about the Deployment:

kubectl describe deployment my-deployment

This will show you the current status of the Deployment, including the number of available and unavailable replicas.

Updating the Deployment

To update the Deployment, you can edit the YAML configuration file and apply the changes using the kubectl apply command:

kubectl apply -f deployment.yaml

This will update the Deployment with the new configuration.

Deploying a Custom Docker Image

Building a Custom Docker Image

Before you can deploy a custom Docker image to your Kubernetes cluster, you need to build the image. Here's an example of how to build a simple "Hello, World!" application using Docker:

## Create a new directory for the project
mkdir my-app
cd my-app

## Create a new Dockerfile
cat << EOF > Dockerfile
FROM nginx:latest
COPY index.html /usr/share/nginx/html/
EOF

## Create the index.html file
cat << EOF > index.html
<h1>Hello, LabEx!</h1>
EOF

## Build the Docker image
docker build -t labex/my-app:v1 .

This will create a new Docker image named labex/my-app:v1 that includes a simple "Hello, World!" web page.

Deploying the Custom Image

To deploy the custom Docker image to your Kubernetes cluster, you can update the image field in the Deployment YAML configuration:

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

Then, apply the updated configuration using kubectl apply:

kubectl apply -f deployment.yaml

This will create a new Deployment that uses the custom labex/my-app:v1 Docker image.

Verifying the Deployment

You can use the kubectl get and kubectl describe commands to verify that the Deployment was created successfully and that the Pods are running the custom Docker image:

kubectl get deployments
kubectl describe deployment my-deployment

You can also access the application by exposing the Deployment as a Kubernetes Service:

kubectl expose deployment my-deployment --type=LoadBalancer --port=80

This will create a new Service that exposes the Deployment on port 80. You can then access the application using the Service's external IP address.

Summary

In this Kubernetes tutorial, you have learned how to create a deployment with a custom Docker image. You now understand the basics of Kubernetes deployments, how to create a deployment, and how to deploy your own containerized application on a Kubernetes cluster. This knowledge will help you manage and scale your applications more efficiently using the Kubernetes platform.

Other Kubernetes Tutorials you may like