"Hello, World!" Docker to Kubernetes: A Step-by-Step Guide

Deploying Docker Images to Kubernetes with Helm: A Step-by-Step Guide

Achin Gupta's photo
·

5 min read

"Hello, World!" Docker to Kubernetes:  A Step-by-Step Guide

Introduction

Docker and Kubernetes:

Docker and Kubernetes are often discussed together in the DevOps world. While they are necessary tools for any software engineer today, they serve two completely different purposes.
Docker is a containerization platform that allows developers to package applications and their dependencies into containers.
Kubernetes is a container orchestration platform that automates the deployment, scaling, and management of containerized applications.

Understanding Helm:

  • Helm is a package manager for Kubernetes, simplifying the deployment and management of applications.

  • It utilizes charts, repositories, and releases to streamline the deployment process.

Key Benefits:

  • Streamlined Deployment: Helm abstracts away Kubernetes complexities, making deployment easier.

  • Version Control: Enables versioning and rollback of application deployments for consistency.

  • Templating and Customization: Allows dynamic configuration based on user-defined values.

Note: You can use these steps to deploy any Docker image in Kubernetes.
We will be using the docker image created in part one of this blog. https://guptaachin.hashnode.dev/print-hello-world-to-docker

Pre-requisites

  1. Basic knowledge of command line.

  2. Install Docker-Desktop on your local machine

  3. Enable Kubernetes on Docker Desktop

  4. Install Helm on your local machine

  5. A docker Image to deploy (e.g., "hello-world-image" from a previous "Hello, World!" Python to Docker blog post). For each image, one helm chart is created

That is it.

Now let's delve in Each of these steps deeper.

Hands-on Exercise

To follow along with the code discussed here, you can find it in this GitHub repository: hello-world-to-kubernetes

Outline for Hands on

  1. Preliminary directory setup

  2. Generate a helm chart for our "hello-world-image" Docker image.

  3. Configure Helm Chart

  4. Deploy "hello-world-helm" helm chart in Kubernetes.

  5. Verify Deployment

Step 1: Preliminary directory setup

Before diving into the helm chart creation, let's set up our directory structure.

In the root directory of your source code (e.g., hello-world-to-kubernetes), create a directory named helm. You can do this using the command line: mkdir helm.

Here's a quick overview of the directory structure we're setting up:

hello-world-to-kubernetes
├── helm
└── src

We'll be focusing on the helm directory for this exercise.

Step 2: Generate a helm chart for our "Hello World" application.

Before proceeding, ensure you have a valid Docker image. We'll be using the "hello-world-image" Docker image created in a previous part of this blog.

To verify if you have the Docker image, run:

  • Linux: docker image ls | grep hello-world-image

  • Windows: docker image ls | findstr hello-world-image

% docker image ls | grep hello-world-image
hello-world-image   0.0.1   432eb901ade2   21 hours ago    191MB

Now, let's create the Helm chart, which essentially is just a way to instruct Kubernetes on deploying our docker image.
This is much easier that it sounds.

  • Navigate to the root directory of your source code (hello-world-to-kubernetes).

  • Change directory into the helm directory: cd helm.

  • Generate the Helm chart using the command: helm create hello-world-helm.

This command will create a new directory called hello-world-helm under the helm directory.

Step 3: Configure Helm Chart

  1. Update the values.yaml file with your Docker image details (name and tag).

  2. Adjust the port to expose if necessary.

a) Supply the image name to helm chart.

  1. Open the values.yaml file located in hello-world-helm.

  2. Locate the image: YAML key and change the value to your Docker image name (e.g., hello-world-image).

  3. Also, update the tag associated with your Docker image (e.g., 0.0.1).

    Now our hello-world-to-kubernetes\helm\hello-world-helm\values.yaml looks like below

b) Fix port to 5000.

  1. Open the values.yaml file again.

  2. Find the service: YAML key and change the port to the desired port you want to expose, such as 5000.

    Now our hello-world-to-kubernetes\helm\hello-world-helm\values.yaml looks like below

c) [Optional]Fix Notes

  1. Open file hello-world-helm/templates/NOTES.txt

  2. Replace all occurrences of 8080 with 5000

  3. Now, our values.yaml file should look like this:

     image:
       repository: hello-world-image
       tag: "0.0.1"
     service:
       port: 5000
    

Step 4: Deploy hello-world helm chart in Kubernetes

After making minor adjustments to the helm chart, we are now ready to deploy into Kubernetes.

  1. Now let's go in our helm-chart directory cd hello-world-helm

  2. Run below command to efficiently handle both installing and updating Helm charts on a Kubernetes cluster

     helm upgrade --install hello-world-chart -n hello-world-namespace --create-namespace .
    

    But now you know the drill, Replace {hello-world-chart} with the name of the Helm chart you want to install, and {hello-world-namespace} with the desired namespace where the release will be installed or upgraded.

    You will receive an output as below

  3. Well ! you are done. Now there are 2 options to check your Pods and services

Step 5: Verify Deployment

See your application in Kubernetes

  • This command retrieves the list of pods in the hello-world-namespace.

      # Retrieve the list of pods in the hello-world-namespace.
      kubectl get pods --namespace hello-world-namespace 
      # NAME                                 READY   STATUS    RESTARTS   AGE
      # hello-world-chart-7f6f49648d-tkf6g   1/1     Running   0          26m
    

  • -o=jsonpath="{.items[0].metadata.name}": This flag specifies the output format using jsonpath. It extracts the name of the first pod.

      # Get the name of the pod
      kubectl get pods -n hello-world-namespace -o=jsonpath="{.items[0].metadata.name}"
      # hello-world-chart-7f6f49648d-tkf6g
    

Access Your Application

  1. After executing the below command, the name of the pod will be stored in the $POD_NAME variable. You can then use this variable in subsequent commands.

    • In PowerShell
    # Store the name of the pod
    $POD_NAME = kubectl get pods -n hello-world-namespace -o=jsonpath="{.items[0].metadata.name}"
  • In Bash Shell
    # Store the name of the pod
    POD_NAME=$(kubectl get pods -n hello-world-namespace -o=jsonpath="{.items[0].metadata.name}")
  1. You can use the POD_NAME variable in your port forwarding command:

     kubectl port-forward --namespace hello-world-namespace $POD_NAME 5000:5000
    

  2. Once port-forwarding is established, click on the following link to access your application http://127.0.0.1:5000/

    You should see Hello, World! on your browser. The only difference is you are now running in Kubernetes application.

    Cleanup (Optional)

    You can use this helm chart to deploy in any Kubernetes cluster.
    To clean Kubernetes deployment run

     helm delete hello-world-chart -n hello-world-namespace
    

Conclusion

By following this guide, you've learned how to deploy Docker images to Kubernetes using Helm. Explore further possibilities with Docker, Kubernetes, and Helm to enhance your DevOps practices.

Where to go from here ?

Now that we know how to create a Docker image, helm chart and create a Kubernetes deployment, I would strongly encourage you to use this recipe to use Docker and Kubernetes for all your daily projects.
If you get stuck, please leave a comment here.
Thank you for reading.