Introduction
Serverless computing has revolutionized the way we build and deploy applications, allowing developers to focus on writing code rather than managing infrastructure. Kubernetes, the powerful container orchestration platform, provides several serverless frameworks, and Knative is one of the most popular solutions.
In this guide, we will explore Knative, its architecture, and how to build serverless functions that react to various events in a Kubernetes cluster.
The Problem: Managing Event-Based Processing
Traditional applications require developers to set up and manage servers, configure scaling policies, and handle infrastructure complexities. This becomes challenging when dealing with event-driven architectures, such as:
- Processing messages from Kafka or NATS
- Responding to HTTP requests
- Triggering functions on cronjobs
- Automating workflows inside Kubernetes
Manually setting up and scaling these workloads is inefficient. Knative solves this by providing a robust, event-driven serverless solution on Kubernetes.
What is Knative?
Knative is a Kubernetes-native serverless framework that enables developers to deploy and scale serverless applications efficiently. It eliminates the need for external FaaS (Function-as-a-Service) platforms and integrates seamlessly with Kubernetes events, message queues, and HTTP triggers.
Why Use Knative?
Built on Kubernetes with strong community support
Supports multiple runtimes: Python, Node.js, Go, Java, and more
Works with event sources like Kafka, NATS, HTTP, and Cron
Scales down to zero when no requests are received
Backed by major cloud providers like Google, Red Hat, and VMware
Installing Knative on Kubernetes
To start using Knative, first install its core components on your Kubernetes cluster.
Step 1: Install Knative Serving
Knative Serving is responsible for running serverless workloads. Install it using:
kubectl apply -f https://github.com/knative/serving/releases/latest/download/serving-crds.yaml
kubectl apply -f https://github.com/knative/serving/releases/latest/download/serving-core.yaml
Step 2: Install a Networking Layer
Knative requires a networking layer like Istio, Kourier, or Contour. To install Kourier:
kubectl apply -f https://github.com/knative/net-kourier/releases/latest/download/kourier.yaml
kubectl patch configmap/config-network --namespace knative-serving --type merge --patch '{"data":{"ingress-class":"kourier.ingress.networking.knative.dev"}}'
Verify that Knative is installed:
kubectl get pods -n knative-serving
Deploying a Serverless Function
Step 1: Writing a Function
Let’s create a simple Python function that responds to HTTP requests.
Create a file hello.py with the following content:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, Serverless World!"
Step 2: Creating a Container Image
Build and push the image to a container registry:
docker build -t <your-dockerhub-username>/hello-knative .
docker push <your-dockerhub-username>/hello-knative
Step 3: Deploying the Function with Knative
Create a Knative service:
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello-knative
spec:
template:
spec:
containers:
- image: <your-dockerhub-username>/hello-knative
Apply the YAML file:
kubectl apply -f hello-knative.yaml
Step 4: Testing the Function
Retrieve the function URL:
kubectl get ksvc hello-knative
Invoke the function:
curl http://<SERVICE-URL>
You should see the output:
Hello, Serverless World!
Using Event Triggers
Kafka Trigger
Knative Eventing enables you to trigger functions using Kafka topics. Install Knative Eventing:
kubectl apply -f https://github.com/knative/eventing/releases/latest/download/eventing-crds.yaml
kubectl apply -f https://github.com/knative/eventing/releases/latest/download/eventing-core.yaml
Create a Kafka trigger:
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: kafka-trigger
spec:
broker: default
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: hello-knative
Apply the trigger:
kubectl apply -f kafka-trigger.yaml
Publish a message to Kafka:
echo '{"message": "Hello Kafka!"}' | kubectl -n knative-eventing exec -i kafka-producer -- kafka-console-producer.sh --broker-list my-cluster-kafka-bootstrap:9092 --topic my-topic
CronJob Trigger
Run a function every 5 minutes using a cron trigger:
apiVersion: sources.knative.dev/v1beta1
kind: PingSource
metadata:
name: cron-trigger
spec:
schedule: "*/5 * * * *"
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: hello-knative
Apply the trigger:
kubectl apply -f cron-trigger.yaml
Conclusion
Knative provides a powerful and scalable serverless framework for Kubernetes. By integrating with HTTP, Kafka, and CronJob triggers, it enables truly event-driven serverless architectures without managing infrastructure.
What’s your experience with Knative? Let’s discuss in the comments! 
What Jobs and CronJobs are