Overview
Knative is a Kubernetes-based platform for building, deploying, and managing modern serverless workloads. It provides two main components: Serving for deploying and auto-scaling stateless containers, and Eventing for building event-driven architectures with loosely coupled event producers and consumers.
Knative extends Kubernetes with custom resources that abstract away infrastructure complexity. It supports scale-to-zero, automatic TLS, revision-based traffic splitting, and integration with multiple event sources. Knative works with any container runtime and supports gradual rollouts, blue-green deployments, and canary releases out of the box.
Installation
Install Knative Serving
# Install Knative Serving CRDs
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.14.0/serving-crds.yaml
# Install Knative Serving core
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.14.0/serving-core.yaml
# Install networking layer (Kourier)
kubectl apply -f https://github.com/knative/net-kourier/releases/download/knative-v1.14.0/kourier.yaml
# Configure Kourier as default
kubectl patch configmap/config-network \
--namespace knative-serving \
--type merge \
--patch '{"data":{"ingress-class":"kourier.ingress.networking.knative.dev"}}'
# Configure DNS (Magic DNS with sslip.io)
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.14.0/serving-default-domain.yaml
Install Knative Eventing
# Install Eventing CRDs
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.14.0/eventing-crds.yaml
# Install Eventing core
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.14.0/eventing-core.yaml
# Install in-memory channel (dev)
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.14.0/in-memory-channel.yaml
# Install MT Channel-based broker
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.14.0/mt-channel-broker.yaml
Knative CLI
# macOS
brew install knative/client/kn
# Linux
wget https://github.com/knative/client/releases/download/knative-v1.14.0/kn-linux-amd64
chmod +x kn-linux-amd64 && sudo mv kn-linux-amd64 /usr/local/bin/kn
# Verify
kn version
Knative Serving
Deploy a Service
# service.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
namespace: default
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/minScale: "0"
autoscaling.knative.dev/maxScale: "10"
spec:
containers:
- image: gcr.io/knative-samples/helloworld-go
ports:
- containerPort: 8080
env:
- name: TARGET
value: "World"
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "500m"
kubectl apply -f service.yaml
# Or using kn CLI
kn service create hello \
--image gcr.io/knative-samples/helloworld-go \
--port 8080 \
--env TARGET=World \
--scale-min 0 \
--scale-max 10
Service Management (kn CLI)
| Command | Description |
|---|
kn service list | List all services |
kn service describe hello | Describe a service |
kn service update hello --env KEY=val | Update service |
kn service delete hello | Delete service |
kn revision list | List all revisions |
kn revision describe hello-00001 | Describe revision |
kn route list | List routes |
Traffic Splitting
# Split traffic between revisions
kn service update hello \
--traffic hello-00001=80 \
--traffic hello-00002=20
# Tag a revision
kn service update hello \
--tag hello-00002=canary
# Route 100% to latest
kn service update hello \
--traffic @latest=100
# traffic splitting via YAML
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
spec:
template:
spec:
containers:
- image: myregistry/hello:v2
traffic:
- revisionName: hello-00001
percent: 80
- revisionName: hello-00002
percent: 20
tag: canary
Autoscaling Configuration
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello
spec:
template:
metadata:
annotations:
# Autoscaler class (kpa = Knative Pod Autoscaler, hpa = K8s HPA)
autoscaling.knative.dev/class: "kpa.autoscaling.knative.dev"
# Metric type (concurrency or rps)
autoscaling.knative.dev/metric: "concurrency"
# Target concurrency per pod
autoscaling.knative.dev/target: "100"
# Scale bounds
autoscaling.knative.dev/minScale: "1"
autoscaling.knative.dev/maxScale: "50"
# Scale-to-zero grace period
autoscaling.knative.dev/scale-to-zero-pod-retention-period: "60s"
# Scale down delay
autoscaling.knative.dev/scale-down-delay: "30s"
spec:
containerConcurrency: 0 # 0=unlimited
containers:
- image: myregistry/hello:v1
| Annotation | Description |
|---|
minScale | Minimum number of replicas (0 for scale-to-zero) |
maxScale | Maximum number of replicas |
target | Target metric value per pod |
metric | Scaling metric (concurrency or rps) |
scale-to-zero-pod-retention-period | Grace period before scaling to zero |
initial-scale | Initial number of replicas on creation |
Knative Eventing
Broker and Trigger
# Create a broker
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
name: default
namespace: default
---
# Create a trigger (filter events to a service)
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: order-trigger
spec:
broker: default
filter:
attributes:
type: com.example.order.created
source: /api/orders
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: order-processor
Event Sources
# Ping Source (cron)
apiVersion: sources.knative.dev/v1
kind: PingSource
metadata:
name: heartbeat
spec:
schedule: "*/5 * * * *"
contentType: "application/json"
data: '{"message": "heartbeat"}'
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display
---
# API Server Source (K8s events)
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: pod-events
spec:
serviceAccountName: events-sa
mode: Reference
resources:
- apiVersion: v1
kind: Pod
sink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display
Send Events via curl
# Send CloudEvent to broker
curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/default/default" \
-H "Ce-Id: 12345" \
-H "Ce-Specversion: 1.0" \
-H "Ce-Type: com.example.order.created" \
-H "Ce-Source: /api/orders" \
-H "Content-Type: application/json" \
-d '{"orderId": "123", "amount": 99.99}'
Configuration
Global ConfigMap
# Autoscaler config
kubectl edit configmap config-autoscaler -n knative-serving
# Domain config
kubectl edit configmap config-domain -n knative-serving
# Network config
kubectl edit configmap config-network -n knative-serving
Custom Domain
apiVersion: v1
kind: ConfigMap
metadata:
name: config-domain
namespace: knative-serving
data:
example.com: ""
Advanced Usage
Private (Cluster-Local) Services
kn service create internal-api \
--image myregistry/api:v1 \
--cluster-local
Container Probes
spec:
template:
spec:
containers:
- image: myregistry/app:v1
readinessProbe:
httpGet:
path: /health
initialDelaySeconds: 3
periodSeconds: 5
Troubleshooting
| Issue | Solution |
|---|
| Service not reachable | Check ingress controller (Kourier/Istio); verify DNS configuration |
| Pods not scaling up | Check autoscaler logs; verify container starts correctly |
| Scale-to-zero not working | Check minScale annotation; verify autoscaler config |
| Slow cold starts | Set minScale >= 1; optimize container startup time |
| Events not delivered | Check broker and trigger status; verify event type/source filters |
| Revision stuck | Check container image pull; review pod events with kubectl describe |
| Traffic split not applying | Verify revision names exist; check route status |
| TLS certificate errors | Verify cert-manager is installed; check domain configuration |