Build and Deploying Go Binaries with this Little Kubernetes Gem, ko

Build and Deploying Go Binaries with this Little Kubernetes Gem, ko
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.

Install

With a working Go environment a simple go get will give you ko:

go get github.com/google/go-containerregistry/cmd/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:

export KO_DOCKER_REPO=gcr.io/triggermesh

You can off course use Docker hub , but these days I have gotten used to Google container registry.

Usage

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):

<pre>
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: hello-world
spec:
  ...
  template:
  ...
    spec:
      containers:
      - name: hello-world
        image: github.com/sebgoa/kodemo/cmd/hello
</pre>

Then to start the build, containerization and deployment a single ko command is necessary.

ko apply -f config/

You will see a Pod running and you will be able to call your Go function:

$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-world-54557f6647-wsng6 1/1 Running 0 50s

$ kubectl port-forward pod/hello-world-54557f6647-wsng6 8080:8080 &
[1] 99038

$ curl localhost:8080
Handling connection for 8080
Hello world !

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.

ko publish github.com/sebgoa/kodemo/cmd/hello

Demo project

If you want to get started with ko, you can try the kodemo repository is a simple demo project. It has the following structure:

<pre>
$ tree
.
├── LICENSE
├── README.md
├── cmd
│   └── hello.go
└── config
    └── deploy.yaml
</pre>

You can certainly build your code locally:

go build cmd/hello/hello.go

Have fun with this little Kubernetes gem.

Related Posts

Join our newsletter

Subscribe to TriggerMesh newsletter. You'll get only the most important news about TriggerMesh