Introduction to Knative

Knative is a newly announced framework from the folks at Google and Pivotal (and other companies like Red Hat and IBM played minor roles) focused on “serverless” style event driven functions. Let’s have a look at what Knative is.

The Knative framework is built on top of Kubernetes and Istio which provide a an Application runtime (container based) and advanced network routing respectively. This follows the Unix philosophy of “do one thing, and do it well” and allows Knative to focus on its core components of Build, Serving, and Eventing while using Custom Resource Definitions (CRD) to extend the Kubernetes functionality.

Build

Build provides a pluggable model for building your applications [into containers] from source code. It’s based on the Google container building service but can be extended to build containers a number of ways using Build Templates.

Since Knative provides CRDs for its components you can request a build quite simply by creating a Kubernetes manifest that specifies the build template to use along with the source location:

apiVersion: build.knative.dev/v1alpha1
kind: Build
metadata:
name: kaniko-build
spec:
serviceAccountName: build-bot
source:
git:
url: https://github.com/my-user/my-repo
revision: master
template:
name: kaniko
arguments:
- name: IMAGE
value: us.gcr.io/my-project/my-app

The example above demonstrates using the Kaniko build template which can build Docker images without needing Docker installed or special privileges to build an application from github and then push the resulting image to a Docker registry.

Serving

Serving extends Kubernetes to provide for deploying and running serverless workloads. It provides a scale-to-zero compute runtime driven by requests and uses Istio to provide routing to the workload.

Serving will scale the workload based on load. If requests come in when your application is scaled to Zero it will hold those requests while it scales your application back up and then pass the request on. This can cause some significant response time which makes it somewhat unsuitable for general web application usage, but is perfect for batch or event drive jobs (which is exactly it’s purpose!).

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: autoscale-go
namespace: default
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: samples/autoscale-go

The above example is a simple Kubernetes manifest for creating a basic scale-to-zero instance of a container along with the appropriate istio routing.

Eventing

Eventing provides a way to consume and produce [CloudEvent specification based] events. It can receive events from pluggable event sources and provides delivery through various pub/sub style broker services.

Much of the Eventing components have been taken from Pivotal’s Riff project and Riff now provides an easy to use frontend to Knative.

apiVersion: flows.knative.dev/v1alpha1
kind: Flow
metadata:
name: k8s-event-flow
namespace: default
spec:
serviceAccountName: feed-sa
trigger:
eventType: dev.knative.k8s.event
resource: k8sevents/dev.knative.k8s.event
service: k8sevents
parameters:
namespace: default
action:
target:
kind: Route
apiVersion: serving.knative.dev/v1alpha1
name: read-k8s-events

The above example creates a feed from Kubernetes events and passes them to the application being run by Serving called read-k8s-events for processing.

Try it out

If you don’t have access to a Kubernetes cluster (or don’t want to take it for a spin yourself) you can watch the following video in which I deploy an application using Knative.

Deploying an Application using Knative. Skip to the 3:00 mark to get straight to the demo.

If you do have access to a Kubernetes cluster you can find instructions and tutorials at the Knative Documentation repo.