Table of contents
- Introduction
- Pre-requisites
- Hands-on Exercise
- Access Your Application
- Conclusion
- Where to go from here ?
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
Basic knowledge of command line.
Install Docker-Desktop on your local machine
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
Preliminary directory setup
Generate a helm chart for our "hello-world-image" Docker image.
Configure Helm Chart
Deploy "hello-world-helm" helm chart in Kubernetes.
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
Update the
values.yaml
file with your Docker image details (name and tag).Adjust the port to expose if necessary.
a) Supply the image name to helm chart.
Open the
values.yaml
file located inhello-world-helm
.Locate the
image:
YAML key and change thevalue
to your Docker image name (e.g.,hello-world-image
).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.
Open the
values.yaml
file again.Find the
service:
YAML key and change theport
to the desired port you want to expose, such as5000
.Now our
hello-world-to-kubernetes\helm\hello-world-helm\values.yaml
looks like below
c) [Optional]Fix Notes
Open file
hello-world-helm/templates/NOTES.txt
Replace all occurrences of
8080
with5000
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.
Now let's go in our helm-chart directory
cd hello-world-helm
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
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
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}")
You can use the
POD_NAME
variable in your port forwarding command:kubectl port-forward --namespace hello-world-namespace $POD_NAME 5000:5000
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 runhelm 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.