1 min* to run a service in Kubernetes — kapp tool

May 06, 2018 0 Comments

1 min* to run a service in Kubernetes — kapp tool

 

 




*maybe more, maybe less, but it’s popular to have some number (preferably
smaller) in a title. I remember tech/programming books with titles such as “Learn X in 10 days”. After a while, those days in the title changed to


hours and now we are talking in minutes… Second, micro-,nano-seconds next?

Recently, I wrote a post titled From Makefile to Go semantic versioning service on Kubernetes where I talked about creating a simple service in Go that runs on Kubernetes and simply returns you the next semantic version. For example: you send something like this minor/0.1.0 to the service, and the service would respond with 0.1.1.

In the conclusion of that article I mentioned that most of my time was spent figuring out the Kubernetes deployment files (Helm), Makefile and I wasn’t actually spending a lot of time on the code.

Since I have done some React development I remembered a tool called create-react-app that helps you create a basic React app that you can build and run in the browser in seconds. I thought it would be useful if there was something similar for apps/services one would like to quickly get up and running in Kubernetes.

kapp tool was born — a tool that helps you create a Go service and have it running in Kubernetes in no time!

You can download the first release of the app from here. And full source code is available on the GitHub repo.

Quick overview of kapp

With kapp installed, you can create your first Go service like this:

cd $GOPATH/src/github.com/peterj 
kapp create helloworld --package github.com/peterj/helloworld

This will create a folder called helloworld under the current folder with the following files:

helloworld 
├── .gitignore ├── Dockerfile ├── Makefile ├── VERSION.txt ├── docker.mk ├── helm │ └── helloworld │ ├── Chart.yaml │ ├── templates │ │ ├── helpers.tpl │ │ ├── deployment.yaml │ │ └── service.yaml │ └── values.yaml ├── main.go └── version └── version.go

You get a Dockerfile for your service, Helm chart to install it and bunch of useful Makefile targets in the Makefile and docker.mk files.

Check out the gif below that shows you how to create a new app, initialize it, build it and run it locally.

kapp demo

Deploying to Kubernetes

Now that you have your app created, it’s time to deploy and run it in Kubernetes. There are a couple of steps involved to get from an app running locally (as a binary), to an app running in a container on Kubernetes:


  1. Create a Dockerfile

  2. Build and tag the Docker image

  3. Push the image to the registry

  4. Create Kubernetes deployment file

  5. Create Kubernetes service file

  6. Deploy the Kubernetes deployment and service

When you ran kapp you already got a Dockerfile and Helm chart (Kubernetes deployment, service file) for your app. And with the Makefile — you also get the tasks you can run to perform various steps.

Let’s start with setting the Docker registry first:

$ docker login # you can skip this if you're already logged in 
$ export DOCKER
REGISTRY=[registry_username]

With Docker registry set, we can build the image:

Then we can push it to the registry:

Finally, we can deploy our app to Kubernetes:

That’s it! Your app is not deployed and running in Kubernetes. At this point you can either create a proxy to the cluster (e.g. kubectl proxy) or get a terminal running inside the cluster and access your service.

Iterate quickly

With the initial deployment of your service completed, you are all set for quickly iterating on the app.

You can make changes to your code, optionally bump the app version and run:

This will upgrade the existing application that’s already running in Kubernetes.

Conclusion

This was a fun side-project to work on and I can see using it to bootstrap any new (Go) service development. Source code for the project is available on GitHub. All PRs and issues are more than welcome!


Tag cloud