*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
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!
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.
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:
- Create a Dockerfile
- Build and tag the Docker image
- Push the image to the registry
- Create Kubernetes deployment file
- Create Kubernetes service file
- 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:
$ make build.image
Then we can push it to the registry:
$ make push.image
Finally, we can deploy our app to Kubernetes:
$ make install.app
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.
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.
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!