Developing apps locally on Kubernetes

Harsh Thakur
Harsh Thakur
Published in
4 min readJan 17, 2020

Kubernetes is great for deploying and maintaing microservices but if you tried developing an application designed to run on Kubernes without utilizing some of the developer productive tools the community has made, then you’re going to have a hard time.

What’s the issue?

Let’s say you have created the dockerfile for your application, built the image, maybe pushed it to a registry and loaded it in your local kubernetes environment. Considering that’s a good start, what happens if you have to make a change in your application code to fix a bug or add a feature?

You have to perform most of the steps you previously performed which is time intensive. While developing apps from scratch, this would entierly kill developer productivity as there would lot of times when you intend to fix and add code. In this post, I’ll cover which tools I use to have a consitent workflow when I’m developing apps meant to deployed on kubernetes.

Things to consider

  • What’s your local kubernetes environment?

There are plenty of options to have kubernetes locally, here’s a post which can help you decide. I’ll be using Minikube for this post.

  • Which language are you developing your app in?

Depending on whether the language is compiled or interpreted, the tool to be used changes.

  • Which IDE/editor are you using?

I would highly recommend VS code as it has a lot of useful extensions to offer.

Where to begin?

Efficient image builds

Creating efficient dockerfile would be a good start. An example of a terrible dockerfile would look something like this:

Why is it bad?

Each time you change the code, the dependencies will be fetched from the web. On the other end of the spectrum, a good dockerfile would look something like this:

Utilising multi-stage builds to download the dependency as one stage and a seperate stage for the code would save lots of time during image builds.

Image pushing

During local development, you might not need to push the image to local image registry each time you make a change, so it’s probably best if you set the imagePullPolicy to Never in the resource file when filling out the container’s spec.

Also, automate the process of building and pushing the image to your kubernetes cluster by disk. You can create a bash script to do something like this:

The first argument would be the name of the docker image and the second argument would be the name of the deployment.

  • VS code Extensions

Docker by Microsoft is quite handy in auto-completing and syntax highlighting of the dockerfiles.

For Kubernetes YAML files , I used to use Kubernetes Support by ipedrazas but I’ve moved to using Cloud Code by Google.

If you’re using an interpreted language like Python and looking for a fast and easy solution, you should definitely take a look at this. During development, set the necessary environment variables in the kubernetes configuration of the image for development/debug mode. It is simple to use ksync. Once installed, you can just start with:

ksync init

Then, you can run start the local client :

ksync watch&

Create a configuration based on the label if you have a kubernetes deployment in place like this:

ksync create -l app=users $(pwd)/users /users

The first absolute path is the local address where your code is and the second path is the one in the container. I would suggest to create a temporary local directory and commit the changes to your code base once you’re satisfied, the reason to do this because the local client can crash sometimes for various reasons and the code would be revereted back undoing all the useful changes you’ve done.

Ksync would help hot-reload the code and debug issues faster without having to build image each time you change your code.

Another alternative is:

Cloud Code extension automates most of what skaffold does and does some more. I would recommend having a look at this and this. Skaffold gives more control and configuration options and is great for compiled languages like Java and Go.

Reason for that is skaffold can be integrated with jib for Java and for Go, we have ko to reduce the image build times.

Summary

Developing apps in Kubernetes can get tiresome but by reducing/eliminating image build times using different tools based on various needs, we can make the experience seamless. There are a lot of other options out there, if you’re interested to explore further, have a look at this.

--

--

Harsh Thakur
Harsh Thakur

I explore tech. Currently occupied in cloud native landscape.