DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

3 1 1 1 1

How to Deploy Docker Images in Kubernetes: Step-by-Step Guide

Kubernetes Deployments with Docker Images

Kubernetes deployments allow you to declaratively manage applications, enabling you to scale, update, and maintain applications running in Pods. When working with Docker containers in Kubernetes, the Docker images serve as the foundation for creating Pods, which are the smallest deployable units in Kubernetes.

In this article, we’ll explore how to use Docker images in Kubernetes deployments and provide step-by-step instructions for deploying applications with Docker images in a Kubernetes cluster.


Key Concepts for Kubernetes Deployments with Docker Images

  1. Pod: A Pod is the smallest unit in Kubernetes. It is a group of one or more containers (in our case, Docker containers) that share the same network and storage. A Pod can host a single Docker container or multiple containers working together.

  2. Deployment: A Deployment is a Kubernetes object used to manage a set of identical Pods. Deployments ensure that a specified number of Pods are always running, even when they fail or are updated. A Deployment can be used to deploy Docker containers on a large scale.

  3. Docker Image: A Docker image is a lightweight, stand-alone, and executable package that contains everything needed to run a piece of software β€” the code, runtime, libraries, and dependencies. In Kubernetes, Docker images are used to define the container that will run inside a Pod.

  4. ReplicaSet: A ReplicaSet ensures that a specified number of identical Pods are running at any given time. It is automatically created by the Deployment controller and manages the replication of Pods.

  5. Container: A container is an instance of a Docker image running inside a Pod. In Kubernetes, containers are encapsulated inside Pods.


Steps to Deploy a Docker Image in Kubernetes

Let's go through the process of deploying a Docker image in a Kubernetes cluster.


Step 1: Prepare Your Docker Image

  1. Build the Docker Image: Ensure that you have your Docker image ready. You can create it using a Dockerfile and then build it using the docker build command.

Example Dockerfile for a Node.js application:

   # Use official Node.js image
   FROM node:14

   # Set the working directory
   WORKDIR /app

   # Copy package.json and install dependencies
   COPY package.json .
   RUN npm install

   # Copy the rest of the app
   COPY . .

   # Expose the application port
   EXPOSE 8080

   # Run the app
   CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

Build the image:

   docker build -t my-node-app .
Enter fullscreen mode Exit fullscreen mode
  1. Push the Docker Image to a Registry: Once you’ve built your Docker image, you need to push it to a container registry. You can use Docker Hub, Google Container Registry (GCR), Amazon Elastic Container Registry (ECR), or any other container registry.

For Docker Hub:

   docker tag my-node-app yourdockerhubusername/my-node-app:latest
   docker push yourdockerhubusername/my-node-app:latest
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Kubernetes Deployment Manifest

A Deployment manifest is a YAML file that defines the desired state of your Kubernetes deployment. It includes the Docker image to be used, the number of replicas (Pods), and other configurations.

Here’s an example deployment.yaml file to deploy the my-node-app Docker image:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-node-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-node-app
  template:
    metadata:
      labels:
        app: my-node-app
    spec:
      containers:
        - name: my-node-app
          image: yourdockerhubusername/my-node-app:latest
          ports:
            - containerPort: 8080
Enter fullscreen mode Exit fullscreen mode
  • apiVersion: Specifies the version of the API to use.
  • kind: Defines the type of resource, in this case, a Deployment.
  • metadata: Contains the name of the Deployment.
  • spec: Contains the specifications of the deployment.
    • replicas: The number of Pods you want to run.
    • selector: Defines the label selector to match Pods.
    • template: The template for the Pods that will be created by the Deployment. This includes the Docker image and ports for the container.

Step 3: Apply the Deployment to Kubernetes

Once you have created the deployment.yaml file, you can apply it to your Kubernetes cluster using the kubectl command.

kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

This command tells Kubernetes to create a Deployment and Pods based on the configuration in the YAML file. Kubernetes will pull the Docker image from the registry (Docker Hub in this case) and create Pods to run the application.


Step 4: Expose the Application with a Service

To make your application accessible from outside the Kubernetes cluster, you need to expose it using a Service.

Here's an example service.yaml file to expose the my-node-app Deployment:

apiVersion: v1
kind: Service
metadata:
  name: my-node-app-service
spec:
  selector:
    app: my-node-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer
Enter fullscreen mode Exit fullscreen mode
  • apiVersion: Specifies the API version for the Service resource.
  • kind: The kind of Kubernetes resource, in this case, a Service.
  • selector: This selector is used to match Pods with the label app: my-node-app.
  • ports: The port on which the service will be exposed (port 80) and the port the container listens to (8080).
  • type: The type of the Service. In this case, LoadBalancer will provision an external load balancer (in cloud environments) to expose the service.

Apply the Service configuration to Kubernetes:

kubectl apply -f service.yaml
Enter fullscreen mode Exit fullscreen mode

Now, your application is exposed on port 80 (or using an external load balancer if in a cloud environment).


Step 5: Verify the Deployment

Once the Deployment and Service are applied, you can verify their status:

  • To check the status of the Deployment:
  kubectl get deployments
Enter fullscreen mode Exit fullscreen mode
  • To check the status of Pods:
  kubectl get pods
Enter fullscreen mode Exit fullscreen mode
  • To check the status of the Service and get the external IP (if using LoadBalancer):
  kubectl get services
Enter fullscreen mode Exit fullscreen mode

Scaling the Deployment

One of the key features of Kubernetes is scaling. You can easily scale the number of replicas (Pods) in your deployment by updating the replicas field in your Deployment YAML file or by using the kubectl scale command.

Example:

kubectl scale deployment my-node-app-deployment --replicas=5
Enter fullscreen mode Exit fullscreen mode

This will scale the Deployment to 5 Pods.


Rolling Updates and Rollbacks

Kubernetes supports rolling updates for deployments, ensuring that your application is updated without downtime. When you update the Docker image version or change configurations, Kubernetes will automatically manage the update by gradually replacing Pods while keeping the service available.

To update the Docker image:

  1. Modify the deployment.yaml file with the new image tag:
   image: yourdockerhubusername/my-node-app:new-tag
Enter fullscreen mode Exit fullscreen mode
  1. Apply the changes:
   kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Kubernetes will perform a rolling update, ensuring no downtime for your application.

To roll back to a previous version of your deployment:

kubectl rollout undo deployment/my-node-app-deployment
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using Docker images in Kubernetes deployments is a powerful way to manage containerized applications at scale. With Kubernetes, you get the benefits of automated scaling, rolling updates, and high availability for your Docker-based applications. By defining your deployments in YAML files, you can manage, scale, and update your applications in a consistent and reliable way.


Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (2)

Collapse
 
abhay_yt_52a8e72b213be229 profile image
Abhay Singh Kathayat β€’

Thank you for your feedback and kind words about the simplicity of the article! I completely understand your point about using a pod or leveraging Kubernetes services for better clarity. My intention was to keep the explanation beginner-friendly while focusing on the foundational concepts. However, I appreciate your suggestion and will definitely consider diving deeper into these topics in a future article or video. Your input helps me improve and serve the community better. Also, feel free to follow me on Twitter (@Abhaysingh281) for more updates and discussions. Thanks again for sharing your thoughts!

Collapse
 
angelotheman profile image
Angel Oduro-Temeng Twumasi β€’

I love how simple this article is. However, you could have easily used a pod or the build ups of kubernetes servicing.

I hope you understand.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay