Introducing Golang Functions to Kubeless

Andres Martinez
Apr 23, 2018 · 4 min read

Automatic Build Process

With Kubeless you don’t need to worry about the process of installing dependencies in your local environment. You don’t even need a working Golang environment, the process of compiling and installing the required Go dependencies is handled by Kubeless and takes place in your Kubernetes cluster. Let’s see an example of how this work!

  • Is a non-standard log utility that we will use for demonstrating the installation of external dependencies.
▶ curl -O
▶ curl -O
▶ kubeless function deploy isprime \
--from-file func.go \
--dependencies Gopkg.toml \
--handler func.IsPrime \
--runtime go1.10
INFO[0000] Deploying function...
INFO[0000] Function isprime submitted for deployment
INFO[0000] Check the deployment status executing 'kubeless function ls isprime'
▶ kubeless function call isprime --data '5'
5 is prime
▶ kubeless function call isprime --data '9'
9 is not prime
▶ kubectl logs -l function=isprime
2018/04/20 12:05:45 "GET /healthz HTTP/1.1" 200 kube-probe/.
time="2018-04-20T13:00:04Z" level=info msg="Checking if 5 is prime"

Storing the function as a Docker image

The process of installing and compiling go dependencies and code can be a heavy task that can take several minutes. We want to use our function in production and we want it to scale as fast as possible so we should avoid to compile every time a new pod with our functions is created. To achieve that it is possible to enable the function image builder of Kubeless that generates and stores a Docker image with your function in your favorite Docker registry (I’ll use the DockerHub). To enable it simply create a secret with your registry credentials, activate the feature and restart the controller to reload the changes:

▶ kubectl patch configmap -n kubeless kubeless-config \
-p '{"data":{"enable-build-step":"true"}}'
configmap "kubeless-config" patched
▶ kubectl create secret docker-registry kubeless-registry-credentials \
--docker-server= \
--docker-username=<YOUR_USERNAME> \
--docker-password=<YOUR_PASSWORD> \
secret "registry-credentials" created
▶ kubectl delete pod -n kubeless -l kubeless=controller
▶ kubectl get job -l function=isprime
build-isprime-1e434ab1e8 1 1 16m
▶ kubectl get pod -l function=isprime -ojsonpath='{ .items[0].spec.containers[0].image }'
▶ kubeless function deploy isprime --runtime-image

Size and performance comparison

Since Golang functions are just a single binary on top of a minimal Debian image the size of a Golang images are much smaller than interpreted languages like Python or NodeJS. In particular, an example function like the above with dependencies could be around two times bigger for Python, five times bigger for Ruby or ten times bigger for NodeJS.

# Golang
function_duration_seconds_sum{method="POST"} 0.002489176
function_duration_seconds_count{method="POST"} 10
# Python
function_duration_seconds_count{method="POST"} 10.0
function_duration_seconds_sum{method="POST"} 0.09038214599786443

Function statistics

Like other runtimes, Golang functions expose Prometheus metrics that reflect, among others metrics, the average execution time or the number of executions/errors per function. Apart from that, there are other tools like InfluxDB that can give you similar statistics like the memory/CPU usage. You can have a visual representation of these metrics using Grafana:

Memory Usage for “isprime” pod

Bitnami Perspectives

Loved by devs, Trusted by ops: Bitnami makes easy to use cloud images, containers, and VMs that work on any platform.

Andres Martinez

Written by

Software Engineer at Bitnami

Bitnami Perspectives

Loved by devs, Trusted by ops: Bitnami makes easy to use cloud images, containers, and VMs that work on any platform.