ko is a build/publish/deploy
tool for go
applications on Kubernetes
It allows you to:
- build Go binaries
- containerize them and publish to a registry
- automatically update Kubernetes manifests to references the correct container image
run then outside this project root and
$GOPATH
# go lang build/publish/deploy tool
go get -u github.com/google/ko/cmd/ko
You can certainly build/run your code locally:
go run main.go
curl http://localhost:8080
To specify the registry that you want to use, set the KO_DOCKER_REPO variable, for instance:
to publish locally set: export KO_DOCKER_REPO=ko.local
export PROJECT_ID=ngx-starter-kit
export KO_DOCKER_REPO=gcr.io/${PROJECT_ID}
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/xmlking/ko-demo
Then to start the build, containerize and deploy a single ko
command is necessary.
ko apply -f deploy/
# -P or --preserve-import-paths
ko apply -P -f deploy/
# Deploy to minikube w/o registry.
ko apply -L -f deploy/
# This is the same as above.
KO_DOCKER_REPO=ko.local ko apply -f deploy/
To deploy in a different namespace:
ko -n nondefault apply -f deploy/
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-59868cf5f9-5gqhj 1/1 Running 0 5s
kubectl port-forward pod/hello-world-59868cf5f9-5gqhj 8080:8080 &
[1] 99038
curl localhost:8080
Handling connection for 8080
Hello world !
ko delete -f 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/xmlking/ko-demo
# publish to local docker repo
ko resolve -L -f deploy/
ko is also useful to help manageing releases
# publish to docker repo ar KO_DOCKER_REPO
ko resolve -P -f deploy/ > release.yaml
# publish to local docker repo
ko resolve -P -L -f deploy/ > release.yaml
This will publish all of the binary components as container images to gcr.io/xmlking/... and create a release.yaml
file containing all of the configuration for your application with inlined image references.
This resulting configuration may then be installed onto Kubernetes clusters via:
kubectl apply -f release.yaml
docker run -it -p 8080:8080 ko.local/github.com/xmlking/ko-demo
# test
curl http://localhost:8080