Configure and Run Serverless Functions on Kubernetes

Run serverless functions on Kubernetes

Serverless functions are standard code parts responsible for dealing with many different events. They offer a cost-efficient approach to implementing microservices.

The technical team can use serverless functions to increase their focus on code and make them respond to certain events with a pay-as-you-go computing model.

On the contrary, Kubernetes offers significant primitives to run the mission-critical application with the help of containers. This process provides autoscaling and automatic responses to application failovers, application deployment strategies, and APIs, enabling resource management and workload provisioning processes.

Running Kubernetes and serverless functions in the same box with extensive infrastructure and its management may sound difficult but can be done with ease using open-source tools, like Knative and OpenFaaS.

In this article, we are going to talk about Knative and how it can be used to run serverless functions on Kubernetes.

What is Knative?

Here, Knative refers to a suite of Kubernetes components, providing serverless capabilities and an event-driven platform helping to run applications and services, which can further be scaled on-demand, powered with extensive monitoring, auto-renewal of SSL/TSL certificates, etc.

Steps to Deploy and Run Serverless Functions on Kubernetes:
Step 1: Installing Basic Tools
  • – refer to this link for downloading and installing Docker for your OS.
  • Installing kubectl:
    • On Linux or macOS, run the command: brew install kubectl and to know its version type, use the command: kubectl version - -client
    • On Windows, open command prompt and run command:


    • Then specify PATH to kubectl binary
    • To check its version, use the command: kubectl version - -client

Note: Ensure to install Kubernetes version 1.20.x, 1.21.x, or its above versions.

  • Installing kn CLI:
    • On Linux or macOS, use the command: brew install know
    • On windows, you can directly download and install it using this link:
      • And then set the PATH for binary
Step 2: Creating a Kubernetes Cluster 

Here, we are going to use Docker Desktop to create the Kubernetes cluster.

  • Open Docker and navigate to Preferences -> Kubernetes, and then click on the Enable Kubernetes.
  • Then click on the Apply & Restart button to save the preferences, and at last, click Install.

Configuring Kubernetes Preferences

  • Check the Docker Desktop dashboard to check the status on the Kubernetes context.

Checking Kubernetes context status

In order to create a Kubernetes cluster, you can also use KIND – a tool that runs local Kubernetes clusters with the help of Docker container nodes.

Use the following command to create Kind Cluster and configure the kubectl context:

curl -sL \\ 
/knative-kind/master/ | sh
Step 3: Installing Knative Serving 

It helps to manage service deployments, amendments, networking, and scaling of elements. The Knative Serving components share services by generating their URLs and providing safe default configurations.

For KIND users: 

  • Install Knative Serving, use command:
curl –sL | sh
  • Install and configure Kourier, use command:
Curl –sL | sh

For Docker Desktop Users:

curl -sL | sh
Step 4: Serverless Functions on Kubernetes

To enable the development and deployment of the platform-specific function as a part of Knative services deployed on Kubernetes, use kn CLI extension func.

It has many function templates and runtimes, which uses Cloud Native Buildpacks to create and post OCI-formatted function images.

Install kn CLI on Kubernetes using Homebrew, use command:

brew tap knative-sandbox/kn-plugins && brew install func

A serverless function can be written in Java, JavaScript, Go, Rust, and Python, but here we’re going to write it in JavaScript.

Step 5: Creating a Function Project
  • Open a command prompt and run the command to create a JavaScript function
kn func create sample-func --runtime node

This will create a new directory, specified as sample-func along with the initialization of a Node.js function project.

Note: You may find out runtimes in Go, Python, Spring Boot, Rust, Quarkus, and TypeScript

The Func.yaml file encompasses its configuration details, which will be used at the time of building and deploying the function.

  • Use buildpack to amend autoscaling options for the Knative service.
  • Open the func.yaml file and amend its envs field with the following value:
- name: TARGET
value: Web

Another file named index.js encompasses function logics specific to the project, which can be used to install other dependencies and is necessary for the project exporting a single default function.

Now, let’s have a look at index.js file contents:

  • It exports the invoke (context) function, in which context is a parameter and an HTTP object storing HTTP request data.
  • Invoke function calls another function named handlePost/handleGet asking for GET request with a void or JS-based type.
  • In the case of the void type, it will not throw any error. Just redirect to the 204 no content response.

Open index.js file and update the handleGet function with its necessary definition:

function handleGet(context) {
return {
target: process.env.TARGET,
query: context.query,
time: new Date().toJSON(),
  • Deploy the function on Kubernetes:
kn func deploy

If you’re running it for the first time, it will ask to specify registry, then enter your registration details:<username>

Once it is mentioned, press Enter key to proceed.

  • Now, open the link in the browser to see its returned value, it will show something similar to this:

docker program

Note: you can also run it locally using the command: kn func run


So, now your serverless function on Kubernetes is created, which would bring the benefits of a serverless entity into the Kubernetes environment.

Have queries for us related to cloud computing solution development or services? If yes, send us your requirements at or book a cloud computing consultation slot with our cloud computing consultants!

This blog post is written by referencing this source - please check it out for additional information on serverless functions, Knative and Kubernetes.
Supportscreen tag