Skip to content

Instantly share code, notes, and snippets.

@xmlking
Created July 6, 2019 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmlking/4cafc809544eb6f2d380dc4adb27f304 to your computer and use it in GitHub Desktop.
Save xmlking/4cafc809544eb6f2d380dc4adb27f304 to your computer and use it in GitHub Desktop.
ko : Deploy GoLang microservices to k8s

Demo of ko

ko is a little unknown gem, lost in the GitHub maze.

ko is widely used in knative development but somehow very little advocacy has been done around this tool.

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.

Install

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

go get github.com/google/go-containerregistry/cmd/ko

Configure 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

Usage

Write a Kubernetes manifest with an import path that is similar to a Go import path like:

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

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 !

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:

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

You can certainly build your code locally:

go build cmd/hello/hello.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment