There is no fog, only Clouds

10 April 2023

Charge your kube with Knative Functions!

by Stefan Krantz

Knative Functions is a serverless platform that enables developers to deploy and run containerized functions, known as serverless functions, on Kubernetes clusters. Knative Functions provides a simple and seamless way to create, deploy, and manage functions, while also offering advanced features such as autoscaling, scaling to zero, and event-driven processing.

Knative Functions is built on top of Kubernetes and utilizes the Kubernetes Custom Resource Definition (CRD) to define and manage functions. Developers can define their functions using YAML or JSON files, and Knative Functions will take care of the rest, such as building the container image, deploying it, and scaling it based on incoming traffic.

Knative Functions supports multiple languages, including Java, Node.js, Go, Python, and Ruby, allowing developers to choose the language that best fits their needs. It also offers built-in support for popular frameworks such as Spring Boot, Quarkus, and Micronaut.

One of the key features of Knative Functions is its ability to scale to zero, meaning that the platform will automatically shut down functions that are not in use to save resources. This can significantly reduce costs for developers, as they only pay for the resources that are actually being used.

In addition to scaling to zero, Knative Functions also supports autoscaling based on incoming traffic, ensuring that functions have enough resources to handle spikes in traffic. It also provides a range of event-driven processing capabilities, allowing developers to build applications that respond to events such as database updates, file uploads, or IoT device data.

Overall, Knative Functions provides a powerful and flexible platform for building serverless applications, allowing developers to focus on writing code rather than worrying about infrastructure management.

This guide will walk you through step-by-step installing Knative Functions on your cluster
 

Prerequisites

This guide assumes you already have a Kubernetes Cluster running and have kubectl cli already installed, if not head over to this guide.

You will also need Docker to run on you local machine to build and test your functions. You can get docker here

To build and deploy the function you will need a Docker Registry. One can be deployed with harbor or you can use for example docker hub

 
 

Additional tools needed for this guide

 
 

Install cert-manager and Knative Serving into your Cluster

Decide on which domain to use for your functions

 
 

Running a container in Knative

kn service create hello \
--image gcr.io/knative-samples/helloworld-go \
--port 8080 \
--env TARGET=World

or

kn service create webserver --image nginx:latest --port 80

 
 

Running functions locally

Run function in Knative

func deploy --registry <registry>

Where <registry> is your registry url

 
 

Private registry

The above example works fine with a public registry such as hub.docker.com. If you need to authenticate to your registry do the following:

kubectl create secret docker-registry <registry-credential-secrets> \
  --docker-server=<private-registry-url> \
  --docker-email=<private-registry-email> \
  --docker-username=<private-registry-user> \
  --docker-password=<private-registry-password>

More information available here: https://knative.dev/docs/serving/deploying-from-private-registry/

Set this credential as default credentials, this way you dont need to modify all service manifests individually:

kubectl patch serviceaccount default -p "{\"imagePullSecrets\": [{\"name\": \"container-registry\"}]}"
tags: