DEV Community

Alex Spinov
Alex Spinov

Posted on

Knative Has a Free API: Serverless Containers on Kubernetes

Knative brings serverless capabilities to Kubernetes. It automatically scales your containers from zero to thousands based on demand, handles traffic routing, and provides event-driven architecture — all on your own infrastructure.

What Is Knative?

Knative is a CNCF incubating project that adds serverless and event-driven capabilities to Kubernetes. It consists of two main components: Serving (request-driven auto-scaling) and Eventing (event-driven architecture).

Key Features:

  • Scale to zero (pay for what you use)
  • Request-based autoscaling
  • Traffic splitting for canary deployments
  • Revision management
  • Event sources and brokers
  • CloudEvents support
  • Custom domains and TLS
  • Knative Functions (CLI)

Installation

# Install Knative Serving
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.15.0/serving-crds.yaml
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.15.0/serving-core.yaml

# Install networking (Kourier)
kubectl apply -f https://github.com/knative/net-kourier/releases/download/knative-v1.15.0/kourier.yaml

# Install Knative Eventing
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.15.0/eventing-crds.yaml
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.15.0/eventing-core.yaml

# Install CLI
brew install knative/client/kn
Enter fullscreen mode Exit fullscreen mode

Knative Serving: Deploy a Service

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: hello-world
spec:
  template:
    metadata:
      annotations:
        autoscaling.knative.dev/minScale: "0"  # Scale to zero!
        autoscaling.knative.dev/maxScale: "100"
        autoscaling.knative.dev/target: "50"  # 50 concurrent requests per pod
    spec:
      containers:
        - image: gcr.io/knative-samples/helloworld-go
          ports:
            - containerPort: 8080
          env:
            - name: TARGET
              value: "World"
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
Enter fullscreen mode Exit fullscreen mode
# Deploy via CLI
kn service create hello --image gcr.io/knative-samples/helloworld-go --env TARGET=World

# List services
kn service list

# Get URL
kn service describe hello -o url
Enter fullscreen mode Exit fullscreen mode

Traffic Splitting (Canary)

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: my-api
spec:
  template:
    metadata:
      name: my-api-v2
    spec:
      containers:
        - image: myapp:v2
  traffic:
    - revisionName: my-api-v1
      percent: 90
    - revisionName: my-api-v2
      percent: 10
      tag: canary  # canary.my-api.example.com
Enter fullscreen mode Exit fullscreen mode

Knative API: Programmatic Access

from kubernetes import client, config

config.load_kube_config()
custom = client.CustomObjectsApi()

# List all Knative services
services = custom.list_namespaced_custom_object(
    group="serving.knative.dev",
    version="v1",
    namespace="default",
    plural="services"
)
for svc in services["items"]:
    url = svc.get("status", {}).get("url", "N/A")
    ready = any(c["type"] == "Ready" and c["status"] == "True"
                for c in svc.get("status", {}).get("conditions", []))
    print(f"Service: {svc['metadata']['name']}, URL: {url}, Ready: {ready}")

# List revisions
revisions = custom.list_namespaced_custom_object(
    group="serving.knative.dev", version="v1",
    namespace="default", plural="revisions"
)
for rev in revisions["items"]:
    print(f"Revision: {rev['metadata']['name']}")
Enter fullscreen mode Exit fullscreen mode

Knative Eventing

# Create a Broker
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
  name: default
---
# Create a Trigger (subscribe to events)
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
  name: order-trigger
spec:
  broker: default
  filter:
    attributes:
      type: com.example.order.created
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: order-processor
Enter fullscreen mode Exit fullscreen mode

Resources


Need to scrape web data for your serverless apps? Check out my web scraping tools on Apify — production-ready actors for Reddit, Google Maps, and more. Questions? Email me at spinov001@gmail.com

Top comments (0)