paint-brush
1 min* to run a service in Kubernetes — kapp toolby@peterj
1,982 reads
1,982 reads

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

by PeterMay 2nd, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In my previous post titled <a href="https://hackernoon.com/from-makefile-to-go-semantic-versioning-service-on-kubernetes-3fae678db87c" target="_blank"><strong>From Makefile to Go semantic versioning service on Kubernetes</strong></a><strong> </strong>I talked about creating a simple Go service that runs on Kubernetes and returns you the next semantic version. For example: you send something like this <code class="markup--code markup--p-code">minor/0.1.0</code> to the service, and the service responds with <code class="markup--code markup--p-code">0.1.1</code>&nbsp;.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - 1 min* to run a service in Kubernetes — kapp tool
Peter HackerNoon profile picture

In my previous post titled From Makefile to Go semantic versioning service on Kubernetes I talked about creating a simple Go service that runs on Kubernetes and returns you the next semantic version. For example: you send something like this minor/0.1.0 to the service, and the service responds with 0.1.1 .

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

Since I have done some React development I remembered a tool called create-react-app — this tool helps you create a basic React app that you can build and run in the browser within 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.

That’s how the kapp tool was born — a tool that helps you create Go services and have them running in Kubernetes in no time!

You can download the first release of the app from here. 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/peterjkapp create helloworld --package github.com/peterj/helloworld

This creates 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 to Kubernetes 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.

Create a new app and building it locally

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:

  • 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. Together with the Makefile, you also get tasks that perform the steps above for you.

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 now 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 to 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 (make bump-version) and run:

make upgrade

To 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!

Thanks for Reading!

Any feedback on this article is more than welcome! You can also follow me on Twitter and GitHub. If you liked this and want to get notified when I write more stuff, you should subscribe to my newsletter!

*****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?