Kubernetes, making a very basic start

So New Job use K8s. I've managed to avoid it thus far, so I only have a vague understanding of how it works. What's the best way to learn a new tool? Come up with a project you can use it on of course, and the best candidate I have right now is the (never ending) energy monitor project, so here we go.

The plan for today is to deploy a tiny API on my home server using k8s, using locally built images (and no docker registry). The aim is to get the hang of how to deploy stuff, not to build anything proper. It could not get more basic than this!

Step 1 - Create the API #

First thing's first, create a tiny API. We'll need a total of 3 files and 1 folder:


Once these were created, we can build the docker image and test it all works as expected before we do the complicated stuff:

Step 2 - Get it into K8s #

According to the microk8s docs: The image we created is known to Docker. However, Kubernetes is not aware of the newly built image. This is because your local Docker daemon is not part of the MicroK8s Kubernetes cluster. We can export the built image from the local Docker daemon and “inject” it into the MicroK8s image cache

To do that we...

OK I lied earlier, we do have to create one more file - a deployment definition file. I created it in the root folder which may or may not be a good place for it. I'm sure if it's wrong I'll figure it out soon enough.

The important bits here are image: hello-web:local which references my locally built image and imagePullPolicy: Never which will prevent k8s from even trying to get the image from a registry. Ports also tripped me up here, I found containerPort: 80 had to match the port used in the Dockerfile, but that could just be luck and me not understanding something more complicated about port mapping. Again, I'm sure I'll find out as soon as I try doing something more interesting!

Now we are ready to:

Bonus Extras #

Useful commands #

Problems #

Credits #

Most of these instructions were copied from this medium post with some tweaks from the k8s docs