Build and Deploying Go Binaries with this Little Kubernetes Gem, ko
There are a lot of Kubernetes tools to help you package and deploy applications, over 70 at least. The all have pros and cons and folks have their favorites. This post is not about comparing them, I just want to let you on in a little secret:
If all you do is deal with the Go language then you ought to know this little Kubernetes gem: ko . ko is widely used in knative development but somehow very little advocacy has been done around this tool.
ko is lost in the GitHub maze within a Google repository it is a true pre-cut diamond. There is another _ruby_ in there called crane that we should write about as well, but today let’s talk about ko and put you KO with a sort post and some simple instructions.
It allows you to:
- Build Go binaries
- Containerize them and publish to a registry
- Automatically update Kubernetes manifests to references the correct container image
All of this in one single command. It is really a neat little tool that once you look at it carefully is actually very powerful.
With a working Go environment a simple go get will give you ko:
And you will be off to the races.
Configure a Registry
Since a container will be created, you need a place to publish it, i.e a registry. To specify the registry that you want to use, set the KO_DOCKER_REPO variable, for instance:
You can off course use Docker hub , but these days I have gotten used to Google container registry.
To deploy your code to Kubernetes, write a standard Kubernetes manifest with an import path that is similar to a Go import path like (that’s the key):
Then to start the build, containerization and deployment a single ko command is necessary.
You will see a Pod running and you will be able to call your Go function:
That’s it !!!! I told you this was going to be short and sweet.
Build/publish but do not deploy
If all you want to do is build the Go binary and publish an image to the registry then, with the Demo project cloned in your $GOPATH.
If you want to get started with ko, you can try the kodemo repository is a simple demo project. It has the following structure:
You can certainly build your code locally:
Have fun with this little Kubernetes gem.