Support Online
Skip to main content

Running a Serverless Node.js Application on Kubernetes with Knative

What Will You Learn in This Guide?

In this guide, you will learn how to set up serverless architecture on Kubernetes.
With Knative Serving, you will run the Node.js application as an auto-scaling service.

🧠 Technical Summary

Subject: Serverless services with Knative on Kubernetes
Problem: Operational burden of pod and scale management
Solution: Automatic scaling and scale-to-zero with Knative Serving
Steps: Knative installation → Network configuration → Service deployment → Testing


Preliminary Preparations

The following should be ready before continuing:

  • A Kubernetes cluster with at least 2 CPUs and 4 GB RAM
  • kubectl command line tool
  • A Node.js image published on Docker Hub
  • Access to the cluster via kubectl

1. Knative Serving Setup

Knative Serving runs serverless workloads on Kubernetes.

kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.0/serving-crds.yaml
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.0/serving-core.yaml
  • These commands install Knative Serving components on the cluster.

Verify installation:


kubectl get pods -n knative-serving
  • All pods must be in Running state.

2. Network Layer (Courier) Configuration

  1. Knative uses an ingress layer to route incoming traffic.
  • Kourier is preferred in this guide.

kubectl apply -f https://github.com/knative/net-kourier/releases/download/knative-v1.8.0/kourier.yaml

Set Knative to use Kourier:


kubectl patch configmap/config-network \
-n knative-serving \
--type merge \
--patch '{"data":{"ingress-class":"kourier.ingress.networking.knative.dev"}}'

Get external IP address:



kubectl get svc kourier -n kourier-system

3. Domain Setting with Magic DNS (sslip.io)

  1. Knative's Magic DNS feature is used for testing without a real domain name.

kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.0/serving-default-domain.yaml
  • This configuration automatically generates service URLs.

4. Deploying a Serverless Node.js Service (YAML)

  1. First create a namespace for isolation:

kubectl create namespace serverless-app

Then create the service.yaml file:


apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: genixnode-node-demo
namespace: serverless-app
spec:
template:
spec:
containers:
- image: knative-samples/helloworld-go
ports:
- containerPort: 8080
env:
- name: TARGET
value: "GenixNode Dunyasi"
  • This YAML file defines the Knative Service.

Deploy the service:


kubectl apply -f service.yaml

5. Application Access and Testing

  1. Get the service URL:

kubectl get ksvc -n serverless-app

Test in browser or with curl:


curl http://genixnode-node-demo.serverless-app.IP_ADRESI.sslip.io

🔁 Automatic Scaling (Scale-to-Zero) Test

  1. Knative reduces the number of pods to 0 when there is no traffic.
  • If you do not send a request for a few minutes, the pods will be closed.

kubectl get pods -n serverless-app
  • Pods are started automatically when a new request arrives.

❓ Frequently Asked Questions (FAQ)

1. Why is Knative preferred over Kubernetes? Provides automatic scaling and version management.

2. What does scale-to-zero bring? It reduces resource consumption and cost.

3. Can I use ingress instead of Kourier? Yes, Istio or Contour is supported.

4. Is it possible to use a real domain? Yes, a DNS record can be added.


Result

In this guide, you set up a serverless Node.js service with Knative on Kubernetes. Now you can focus on application development instead of infrastructure management.

You can try this architecture immediately on the GenixNode Kubernetes infrastructure.