Kubernetes is really starting to be a de-facto standard in how we deploy things to the cloud. It provides a ton of functionality around docker images that it deploys while at the same time making optimal use of the hardware that it is running on. Having worked with traditional servers and then VMs, I have found Kubernetes to be quite refreshing. A lot of the tedious things that you had to deal with are just handled by default.
Running kubernetes locally is really quite simple. You only need to have docker desktop installed. That should be almost an automatic these days if you are developing software for the cloud. Now if you are someone that has docker desktop installed but hasn’t messed with kubernetes, don’t worry. A lot of folks do write apps and build docker images for them without needing to play with it. They are either leveraging a hosted service, like AWS ECS, to run and manage their containers or they have someone on their team that is hogging all the fun. Here’s a quick tutorial on how to start a container on kubernetes locally.
Step 1: Create a docker image
You might already have an image kicking around for an app you are developing. Let’s assume that you are starting from scratch though. Create a file called
Dockerfile with the following contents.
alpine is a minimal linux distro and we are going to install HTTPie on it. The working directory is set to be
/opt/. The image opens a shell using the command
RUN apk add httpie
Build the image using the following command:
docker build --no-cache -t hello-world:docker-desktop .
You can verify that the image is built and working as expected by run it in interactive tty mode. That will allow you to interact with the shell as you would through a console.
docker run -it hello-world:docker-desktop
In the shell, you can run httpie from the container. That basically proves that your simple docker image was built correctly.
No need to clean up. As soon as you exit the shell the container will stop running.
Step 2: Create a kubernetes pod
Now that you have a simple docker image, let’s create a kubernetes pod for it. A pod is a group of one or more containers. In this tutorial, it is just going to be one container that has our small image with HTTPie installed.
docker-desktop context from the docker desktop kubernetes drop down menu. That is the locally running kubernetes instance that you get with docker desktop. You can confirm that you have that context selected using this command:
Let’s create a namespace in your context called
hello-namespace. Namespaces let you group together pods. In fact, you can create multiple pods with the same name as long as they exist in different namespaces.
kubectl create namespace hello-namespace
That gives us the kubernetes infrastructure needed for deploying our pod. A pod is defined using manifest files like the one below. They can either be JSON or YAML but virtually everyone uses YAML. This manifest says to create a pod named
hello-pod with a single container called
hello-container. This container will use the
hello-world image and runs the command
sleep 15m which means it will run for 15 minutes before completing. Copy this manifest into a file named
- name: hello-container
We can deploy the pod using this command:
kubectl apply -f hello-manifest.yaml
We can now see that our pod has successful been deployed using
kubectl get -n hello-namespace pods
Step 3: Shell into your pod
Let’s jump into that pod you just deployed. The command below should look very familiar as it is almost the same as the one we used to shell into your docker container from step 1.
kubectl -n hello-namespace exec -it hello-world /bin/sh
Just like in step 1, let’s run HTTPie.
The pod will complete after 15 minutes but we should still clean up and remove it. Use the command below to delete the pod:
kubectl delete -f hello-word.yaml
This tutorial has walked you through how to deploy a kubernetes pod with a docker image you built. The image that we used is really quite basic but you should be able to see that with a little effort, you can use it to deploy any image you are working on. There is more that you need to tinker with if you are going to setup a Web app or service but what we covered in this post is a good first step in that direction.