in

A Quick Guide to Knative Serverless Framework for Beginners

default image

Hey there! Serverless computing has become all the rage over the past few years. It allows developers like you and me to run applications without worrying about servers or infrastructure. Sounds great right?

One of the most popular open-source serverless frameworks out there is called Knative. In this comprehensive guide, I‘ll walk you through everything you need to know about Knative as a fellow developer.

What Exactly is Knative?

Knative is an open source Kubernetes-based platform created to make deploying, running and managing modern serverless workloads super easy. It was started in 2018 by heavyweights like Google, IBM, SAP and RedHat among others.

In a nutshell, Knative provides a set of middleware components that work together on top of Kubernetes to power serverless applications. This allows you to focus on writing code instead of ops.

The key components of Knative are:

  • Knative Serving: Scales your containers up and down automatically based on demand. It leverages Istio service mesh and Kubernetes for this.

  • Knative Eventing: Helps connect event producers to event consumers using pub/sub semantics. Think of it as a serverless event-bus.

  • Knative Build: Does continuous integration and builds container images from source code.

By combining these, Knative aims to provide a common platform for deploying modern container and function based apps that can leverage the auto-scaling capabilities of Kubernetes.

Why Use Knative Over Other Options?

Good question! Here are some of the standout benefits in my opinion:

  • No Vendor Lock-in: Knative introduces a uniform API that lets your apps run anywhere – VMs, bare metal, public cloud etc.

  • Effortless Auto Scaling: Knative Serving can automatically scale your app instances up and down based on traffic. No more capacity planning!

  • Open Source Freedom: Knative is 100% open source under Apache 2.0 license. You can view and contribute to the code.

  • Cloud Native: It is built purely for Kubernetes and cloud-native architectures, optimizing your apps for scale.

  • Event Driven: Makes it simple to react to events and build event-driven pipelines with pub/sub workflows.

  • Faster Deployment: Deploy apps faster on Kubernetes without configuring complex components like ingress or autoscaling.

  • Improved Productivity: Devs can focus on business logic rather than infra – things like scaling, routing, rollouts are handled automatically.

  • Portable: Provides a common layer for hybrid and multi-cloud to prevent fragmention.

Based on these benefits, I think Knative provides a very compelling serverless platform – especially for Kubernetes users looking to embrace a cloud native architecture.

Understanding How Knative Works Its Magic

Knative architecture is composed of a few key components that come together to provide the serverless capabilities:

Knative Architecture

Image source: knative.dev

  • Knative API: Exposes developer-friendly API and controls Kubernetes objects

  • Autoscaler: Scales your workload based on configuration and demand

  • Activator: Helps minimize cold starts by spinning up pods on-demand

  • Istio: Handles important networking tasks like telemetry, traffic routing, security

  • Kubernetes: Manages underlying resources – pods, services, deployments etc.

  • Monitoring: Out-of-the-box observability with Prometheus, Grafana and Zipkin

  • Build: Runs builds from source and publishes container images

  • Eventing: Enables event-driven apps with pub/sub messaging and event propagation

  • Serving: Deploys and manages serverless containers maintained by Revisions

  • Client: Knative CLI for creating and managing Knative resources

In a nutshell, Knative provides the middleware to deploy and run serverless applications on top of Kubernetes in a standardized way. The components like Build, Eventing and Serving help provide the core differentiation like auto scaling, eventing, rollouts etc. on top of Kubernetes.

Istio service mesh handles all of the networking complexity like service discovery, load balancing, telemetry etc. The Knative API and controllers expose a developer-friendly abstraction to deploy apps without having to directly work with low level Kubernetes primitives. Pretty neat!

Now that you have a good idea of how Knative works under the covers, let‘s look at some of the resources that make up its API.

Key Resources in Knative API

Knative defines some core custom resources that act as high-level building blocks:

Knative Service

This is the central resource for deploying your serverless apps. You define the container images, autoscaling limits, resources and configuration for your app. Knative handles deploying and serving them with auto scaling.

Knative Configuration

Holds the desired code and config state for your app. Allows you to re-use them across multiple services and environments.

Knative Route

Maps a network endpoint to one or more Knative services. You can think of it like ingress rules to allow traffic into your app from outside.

Knative Revision

Provides an immutable snapshot of your app‘s code, config and traffic settings. Revisions allow you to safely roll back changes.

Based on these core resources, you get a very flexible abstraction on top of Kubernetes. You don‘t have to deal with low-level primitives like Deployments, Services or Ingress Resources directly.

Knative handles all that under the hood! Next, let‘s look at some real-world use cases where Knative shines.

Where Knative Really Shines

Knative provides a very effective platform for modern apps that need to handle dynamic workloads and react to events. Here are some good use cases:

Scalable Web Apps

If you have web apps with variable traffic, Knative can easily scale up and down dynamically so you don‘t over-provision resources.

Event-Driven Apps

Knative eventing is perfect for workloads that need to react to events from various sources – great for building resilient event pipelines.

Data Processing

For data pipelines that need to scale up and down like Spark jobs – Knative helps run containers efficiently.

Mobile Backend

Mobile apps need to handle lots of user events and run with low latency. Knative helps run performant mobile backends.

CI/CD Pipelines

Knative builds make it easy to plug building container images as a step in modern CI/CD pipelines in an automated manner.

IoT Backend

For high throughput IoT workloads, Knative helps efficiently run serverless functions triggered by device events.

Hybrid Apps

Knative provides portability across on-prem and cloud allowing you to build hybrid applications with ease.

As you can see, Knative is extremely versatile for running all kinds of modern workloads from legacy monoliths to cutting edge functions.

Installing Knative – Quick and Easy

Knative is designed to run on top of a Kubernetes cluster. So first, you need access to a Kubernetes environment.

Here are some options to run Kubernetes:

  • Local using Minikube or Docker Desktop Kubernetes
  • Managed like GKE, EKS or DigitalOcean Kubernetes
  • On-prem data center or cloud infrastructure

Once you have a Kubernetes cluster, installing Knative is super easy:

  1. Install the pre-requisites like Istio service mesh
  2. Apply the Knative YAML manifests:
kubectl apply -f https://github.com/knative/serving/releases/download/v0.17.4/serving-core.yaml
  1. Configure DNS and ingress

That‘s pretty much it! Knative will now be up and running on your Kubernetes cluster.

Verify by running kubectl get pods --namespace knative-serving. You would see pods getting created for the various Knative components.

For detailed steps, refer to the official installation guide.

Now let‘s look at deploying a sample app quickly.

Deploying Your First App

One of the easiest ways to deploy apps on Knative is using the kn CLI.

Let‘s deploy a simple Node.js app from GitHub source:

# Create Knative service from source 
kn service create node-app \
--image gcr.io/knative-samples/helloworld-node \
--source https://github.com/knative/docs/tree/master/docs/serving/samples/helloworld-node

That‘s it! Knative will build the source into a container image, then deploy it as a scalable Knative service.

You can access the app using the auto-created URL:

curl https://node-app.default.example.com

How easy was that? No need to manually build, create Docker images, write YAML or configure autoscaling. Knative handles all that complexity for you out of the box!

Following Best Practices with Knative

While Knative really simplifies deploying apps, there are some best practices worth keeping in mind:

  • Use revision templates for consistency across environments
  • Right size resource requests and limits
  • Tune autoscaling parameters carefully based on traffic patterns
  • Size your cluster to handle expected peak requests
  • Performance test apps end-to-end before production use
  • Enable tracing using Zipkin for observability
  • Set network timeouts correctly to avoid bad requests
  • Leverage traffic splitting for incremental rollouts
  • Automate deployment using CI/CD pipelines
  • Add logging and distributed tracing for debugging issues

Following cloud native and 12-factor app best practices will result in resilient, production-grade applications.

Wrap Up

Alright my friend! Let‘s do a quick recap of what we learned.

We went through Knative – the popular open source serverless framework for Kubernetes. We understood the key components like Serving, Eventing and Build that make Knative work.

We also looked at the benefits of Knative, including effortless scaling, eventing support, portability and faster deployments. We also covered some sample use cases where Knative shines.

We then installed Knative on a sample Kubernetes cluster and deployed a simple app using the kn CLI.

Finally, we went through some best practices for running production-grade apps on Knative.

Knative provides a powerful serverless platform for Kubernetes that frees developers from infrastructure concerns. With its auto scaling abilities, eventing model and portability, it is a very compelling solution for running modern workloads.

I hope you found this guide helpful in getting started with Knative! Let me know if you have any other questions. Happy serving!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.