Knative — Let’s go serverless!

Sangram Mohite
3 min readNov 21, 2019

--

An introduction to Knative; a serverless platform built on top of Kubernetes

Knative (pronounced: kay-nay-tiv) extends Kubernetes to provide a set of middleware components that are essential to build modern, source-centric, and container-based applications that can run anywhere; on-premise, in the cloud, or even in a third-party data center.

Each of the components under the Knative project attempt to identify common patterns and codify the best practices that are shared by successful, real-world, Kubernetes-based frameworks and applications. Knative components focus on solving mundane but difficult tasks such as:

  • Deploying a container
  • Routing and managing traffic with blue/green deployment
  • Scaling automatically and sizing workloads based on demand
  • Binding running services to eventing ecosystems

Demo

Assumes Google Cloud as the platform. You can modify with corresponding scripts for other cloud platforms on their respective Kubernetes services.

Create a Kubernetes cluster

Enable relevant Cloud APIs

gcloud services enable \
cloudapis.googleapis.com \
container.googleapis.com \
containerregistry.googleapis.com

Create cluster

gcloud container clusters create knative \
--zone=us-central1-b \
--cluster-version=latest \
--num-nodes=3 \
--machine-type=n1-standard-4 \
--enable-autoscaling --min-nodes=1 --max-nodes=5 \
--enable-autorepair \
--scopes=service-control,service-management,compute-rw,storage-ro,cloud-platform,logging-write,monitoring-write,pubsub,datastore

Verify running nodes

kubectl get nodes

Create cluster-admin binding to the current user

kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole=cluster-admin \
--user=$(gcloud config get-value core/account)

Install Istio

Install Istio from the Knative third party images

kubectl apply --filename https://raw.githubusercontent.com/knative/serving/v0.2.1/third_party/istio-1.0.2/istio.yaml

Enabled auto-injection of the Istio sidecar for the default namespace

kubectl label namespace default istio-injection=enabled

Verify Istio pods are running

kubectl get pods --namespace=istio-system

Install Knative

Install Knative using the released images

kubectl apply --filename https://github.com/knative/serving/releases/download/v0.2.1/release.yaml

Verify the Knative pods are running

For Serving:

kubectl get pods --namespace=knative-serving

For Build:

kubectl get pods --namespace=knative-build

Create a sample service

Create a sample helloworld.yaml

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: "helloworld"
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: "gcr.io/knative-samples/helloworld-go"
env:
- name: "TARGET"
value: "world"

Replace TARGET env. variable for a value of your choice!

Deploy the service

kubectl apply -f helloworld.yaml

Verify service is created

kubectl get ksvc

Configure networking

Check the ingress gateway for Istio

kubectl get service --namespace=istio-system knative-ingressgateway

Check the ingress external IP address IP_ADDRESS

Check the domain for the service we created

kubectl get ksvc helloworld --output jsonpath='{.status.domain}'

Check if you get response from the service using the external IP

curl -H "Host: helloworld.default.example.com" http://IP_ADDRESS

Verify running pods

kubectl get pods

View the configuration, revision and routing information

kubectl get configuration,revision,route

Install knctl

Download the binary

wget https://github.com/cppforlife/knctl/releases/download/v0.3.0/knctl-linux-amd64

Install using following script

mv knctl-linux-amd64 /usr/local/bin/knctl
sudo chmod +x /usr/local/bin/knctl

Verify knctl is installed and is able to make a request to our service

knctl curl --service helloworld

Check the Istio ingress gateway service

kubectl get svc knative-ingressgateway --namespace istio-system

Check the compute address lists

gcloud compute addresses list

Patch the service with the new static IP address (Create a env. variable CLUSTER_REGION)

kubectl patch svc knative-ingressgateway --namespace istio-system --patch \
$(gcloud compute addresses describe \
knative-ingress --region $CLUSTER_REGION --format json | \
jq -cr "{spec: {loadBalancerIP: .address}}")

Check the IP address has changed

kubectl describe svc knative-ingressgateway --namespace istio-system

Check the address list again

gcloud compute addresses list

Check ingress list from knctl

knctl ingress list

Verify that the service is responding

knctl curl --service hello

Simulate load

Install hey

go get -u github.com/rakyll/hey

Run hey with the applicable request for load simulation

hey -z 30s -c 50 "http://helloworld.default.<<IP ADDRESS>>.sslip.io" && kubectl get pods

You can check the pods again and see that they wake up on account of new requests.

Continue your journey with Knative! — https://knative.dev/docs/

Thank you.

--

--

Sangram Mohite

Cloud Solution Architecture | AI/ML | UX | Product Management | Polymath | 🏳️‍🌈 Singer 🎤 Cat dad 🐈♥️ Movies and TV 🎥📺 Wildlife and Travel 🦁✈️