Example: use the custom builder with ko
Note: Skaffold now includes a
ko
builder.
When you use the ko
builder, you do not need to provide a custom build shell
script or install the ko
binary.
This example shows how the custom builder can be used to
build artifacts with ko.
- building a single Go file app with ko
- tagging using the tagPolicy (
sha256
), to mimic the behavior of ko
- deploying a single container pod using
kubectl
Before you begin
For this tutorial to work, you will need to have Skaffold and a Kubernetes cluster set up.
To learn more about how to set up Skaffold and a Kubernetes cluster, see the getting started docs.
Tutorial
This tutorial will demonstrate how Skaffold can build a simple Hello World Go application with ko and deploy it to a Kubernetes cluster.
First, clone the Skaffold repo and navigate to the custom example for sample code:
git clone https://github.com/GoogleContainerTools/skaffold.git
cd skaffold/examples/custom
Take a look at the build.sh
file, which uses ko
to containerize source code:
#!/usr/bin/env bash
set -Eefuo pipefail
if ! [ -x "$(go env GOPATH)/bin/ko" ]; then
pushd "$(mktemp -d)"
curl -L https://github.com/ko-build/ko/archive/v0.13.0.tar.gz | tar --strip-components 1 -zx
go build -o "$(go env GOPATH)"/bin/ko .
popd
fi
output=$("$(go env GOPATH)"/bin/ko publish --local --preserve-import-paths --tags= . | tee)
ref=$(echo "$output" | tail -n1)
docker tag "$ref" "$IMAGE"
if [[ "${PUSH_IMAGE}" == "true" ]]; then
echo "Pushing $IMAGE"
docker push "$IMAGE"
else
echo "Not pushing $IMAGE"
fi
and the skaffold config, which configures image ko://github.com/GoogleContainerTools/skaffold/examples/custom
to build with build.sh
:
apiVersion: skaffold/v4beta5
kind: Config
build:
artifacts:
- image: ko://github.com/googlecontainertools/skaffold/examples/custom
custom:
buildCommand: ./build.sh
dependencies:
paths:
- "**/*.go"
- go.*
- .ko.yaml
tagPolicy:
sha256: {}
The k8s/pod.yaml
manifest file uses the same image reference:
apiVersion: v1
kind: Pod
metadata:
name: getting-started-custom
spec:
containers:
- name: getting-started-custom
image: ko://github.com/googlecontainertools/skaffold/examples/custom
For more information about how this works, see the Skaffold custom builder documentation.
Now, use Skaffold to deploy this application to your Kubernetes cluster:
skaffold run --tail --default-repo <your repo>
With this command, Skaffold will build the github.com/googlecontainertools/skaffold/examples/custom
artifact with ko and deploy the application to Kubernetes.
You should be able to see Hello, World! printed every second in the Skaffold logs.
Cleanup
To clean up your Kubernetes cluster, run:
skaffold delete