Configuration K.O.

Meet ko, a CLI for fast Kubernetes microservice development in Go

Eirini-Eleni Papadopoulou
© Shutterstock / Gutzemberg  

Do you need a CLI that is built specifically for supporting rapid development of Go containers with Kubernetes and minimal configuration? Without further ado, let’s get on with today’s discovery: Meet ko, an interesting tool, particularly for Knative developers. Let’s take a closer look.

Looking for a tool that will help you develop Kubernetes microservices in Go with minimal configuration and as fast as light speed?

Well, you’re in luck! Meet ko, a CLI that supports rapid development of Go containers with Kubernetes.

Let’s take a closer look at what this tool brings.

The features

ko is built around a very simple extension to Go’s model for expressing dependencies using import paths and one of its goals is to make containers invisible infrastructure. How do you do that? Simply replace image references in your Kubernetes yaml with the import path for your Go binary, and ko will handle containerizing and publishing that container image as needed.

Here are some highlights of what you can do with ko:

Determining supported import paths – ko expects to execute in the context of your $GOPATH. This is used to determine what package(s) ko is expected to build.

Commands – ko has four commands, most of which build and publish images as part of their execution. By default, ko publishes images to a Docker Registry specified via KO_DOCKER_REPO. However, these same commands can be directed to operate locally as well via the --local or -L command (or setting KO_DOCKER_REPO=ko.local). The commands are:

  • ko publish – Builds and publishes images for each import path passed as an argument. It prints the images’ published digests after each image is published.
  • ko resolve – Takes Kubernetes yaml files in the style of kubectl apply and determines the set of Go import paths to build, containerize, and publish.
  • ko apply – Is intended to parallel kubectl apply, but acts on the same resolved output as ko resolve emits. It is expected that ko apply will act as the vehicle for rapid iteration during development. As changes are made to a particular application, you can run: ko apply -f unit.yaml to rapidly rebuild, repush, and redeploy their changes. ko apply will invoke kubectl apply under the covers, and therefore apply to whatever kubectl context is active.
  • ko apply --watch (EXPERIMENTAL) – (-W for short) does an initial apply as above, but as it does, it builds up a dependency graph of your program and starts to continuously monitor the filesystem for changes. When a file changes, it re-applies any yamls that are affected.
  • ko delete – Passes through to kubectl delete. It is exposed purely out of convenience for cleaning up resources created through ko apply.

Minikube – You can use ko with minikube via a Docker Registry, but this involves publishing images only to pull them back down to your machine again. To avoid this, ko exposes --local or -L options to instead publish the images to the local machine’s Docker daemon.

There are, however, a couple of common pitfalls that you should be aware of:

  • You need to be on ${GOPATH} or it will not know what package you are in.
  • Typos are the worst. Because ko is insensitive to schemas, it will ignore any string that is not the import path of a “main” package, so if you have a simple typo in your import path then it will be left as is and you will likely see your Pod ErrImagePull.

Head over to the GitHub repo for all the detailed information on this tool or check out the medium post by its creator, Matthew Moore, for some more introductory notes.

SEE ALSO: Why enterprises should run serverless on Kubernetes

Getting started

If you are interested in trying out ko, you can do so by installing via:

go get

Eirini-Eleni Papadopoulou
Eirini-Eleni Papadopoulou is an editor for Just finished her masters in Modern East Asian Studies and plans to continue with her old hobby that is computer science.

Leave a Reply

Be the First to Comment!

Notify of