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 func (reference)
- Linux
curl -o /usr/local/bin/func https://github.com/knative/func/releases/download/knative-v1.8.0/func_linux_amd64 chmod a+x /usr/local/bin/func
- MacOS
brew install func
(if you do not have brew, head over to brew)
- Linux
-
Install kn (reference)
- Linux
curl -o /usr/local/bin/kn https://storage.googleapis.com/knative-nightly/client/latest/kn-linux-amd64 chmod 755 /usr/local/bin/kn
- MacOS
brew install knative/client/kn
(if you do not have brew, head over to brew)
- Linux
Install cert-manager and Knative Serving into your Cluster
- Install cert-manager
kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.10.0/cert-manager.yaml kubectl apply -f cm-clusterissuer.yaml
- Knative Serving
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.0/serving-crds.yaml kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.0/serving-core.yaml kubectl apply -f https://github.com/knative/net-kourier/releases/download/knative-v1.8.0/kourier.yaml kubectl patch configmap/config-network \ --namespace knative-serving \ --type merge \ --patch '{"data":{"ingress-class":"kourier.ingress.networking.knative.dev"}}' kubectl apply -f https://github.com/knative/net-certmanager/releases/download/knative-v1.8.0/release.yaml
Decide on which domain to use for your functions
- (option 1) If you want to use sslip.io as your domain for the fuctions, run this
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.0/serving-default-domain.yaml
- (option 2) Replace knative.example.com with your domain suffix
kubectl patch configmap/config-domain \ --namespace knative-serving \ --type merge \ --patch '{"data":{"knative.example.com":""}}'
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
- Create a function on your machine documentation
Format:func create -l <language> <function-name>
Example:
func create -l go hello
- Build and run the function
func run --build
- Test the function
func invoke
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\"}]}"