Setting up a local environment for experimentation or development can be frustrating, especially when working with serverless platforms like KNative. In this guide, I'll walk you through the process of configuring and deploying KNative on a Kubernetes cluster using k3d. By the end, you'll have everything you need to start building and testing serverless applications locally.
- Create the Cluster: Ensure the correct ports are exposed.
- Install KNative: Set up the core components.
- Configure the Kourier Gateway: Align it with your cluster's port configuration.
- Set Up Local DNS: Use
sslip.iofor simple domain management. - Deploy a Sample Go Application: Verify your setup with a simple "Hello, World" app.
We’ll start by creating a Kubernetes cluster with custom port configuration using k3d. This ensures your cluster is ready to handle traffic correctly.
-
Create the cluster configuration file:
# clusterconfig.yaml apiVersion: k3d.io/v1alpha4 kind: Simple servers: 1 ports: - port: 80:31080 - port: 443:31443
-
Apply the configuration and create the cluster:
k3d cluster create ${clusterName} --config clusterconfig.yaml -
Verify that the cluster is successfully created and running:
k3d cluster list
You should see output similar to this:
NAME SERVERS AGENTS LOADBALANCER knative 1/1 0/0 true
This setup ensures your cluster is ready for the next steps.
KNative Serving allows you to deploy and manage serverless applications on Kubernetes. Let’s get it installed.
Install KNative Serving using YAML:
# Install the required custom resources:
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.15.2/serving-crds.yaml
# Install the core components of Knative Serving:
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.15.2/serving-core.yamlFor more details, refer to the official KNative documentation.
With KNative Serving installed, you’re ready to set up the ingress gateway.
To route traffic to your KNative services, we’ll configure Kourier as the ingress gateway. This step is crucial for managing how traffic flows into your cluster.
-
Download the Kourier YAML file:
curl -Lo kourier.yaml https://github.com/knative/net-kourier/releases/download/knative-v1.15.1/kourier.yaml
-
Modify the configuration:
Open
kourier.yamland find theServicewithmetadata.name: kourier. Modify it to expose the correct ports and change the service type toNodePort.apiVersion: v1 kind: Service metadata: name: kourier namespace: kourier-system labels: networking.knative.dev/ingress-provider: kourier spec: ports: - name: http2 port: 80 protocol: TCP targetPort: 8080 nodePort: 31080 # Add this line - name: https port: 443 protocol: TCP targetPort: 8443 nodePort: 31443 # Add this line selector: app: 3scale-kourier-gateway type: NodePort # Change from LoadBalancer
-
Install Kourier with the customized YAML:
kubectl apply --filename kourier.yaml
-
Set Kourier as the default ingress for KNative Serving:
kubectl patch configmap/config-network \ --namespace knative-serving \ --type merge \ --patch '{"data":{"ingress-class":"kourier.ingress.networking.knative.dev"}}' -
Verify the Kourier setup:
kubectl get svc kourier --namespace kourier-system
The output should confirm the correct ports are exposed:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kourier NodePort 10.96.12.245 <none> 80:31080/TCP,443:31443/TCP 1m
Your Kourier ingress gateway is now set up, making your KNative services accessible.
To make your deployment accessible, we’ll use sslip.io, a free wildcard DNS service. This step simplifies domain management for local development.
Check the KNative Serving domain configuration:
kubectl describe configmap/config-domain --namespace knative-servingYou can now access services using a URL like http://<your-service-name>.default.127.0.0.1.sslip.io.
Finally, let’s deploy a simple Go application to verify that everything is working correctly.
-
Create a deployment script:
# service.yaml apiVersion: serving.knative.dev/v1 kind: Service metadata: name: helloworld-go namespace: default spec: template: spec: containers: - image: ghcr.io/knative/helloworld-go:latest env: - name: TARGET value: "Hello, Knative Serving is up and running with Kourier!"
-
Deploy the application:
kubectl apply --filename service.yaml
-
Check the deployment status:
kubectl get ksvc
Initially, you might see:
NAME URL LATESTCREATED LATESTREADY READY REASON helloworld-go http://helloworld-go.default.127.0.0.1.sslip.io helloworld-go-fqqs6 Unknown RevisionMissingOnce ready:
NAME URL LATESTCREATED LATESTREADY READY REASON helloworld-go http://helloworld-go.default.127.0.0.1.sslip.io helloworld-go-fqqs6 helloworld-go-fqqs6 True -
Test the deployment:
Access the application using the provided URL:
curl -v http://helloworld-go.default.127.0.0.1.sslip.io
Expected output:
Hello, Knative Serving is up and running with Kourier!
Congratulations! You've successfully set up a Kubernetes cluster with KNative as a serverless backend, configured Kourier as the ingress gateway, and deployed a sample application. This setup provides a powerful, scalable environment for testing and development, right on your local machine.
If you found this guide helpful, please share it with your network or leave a comment. Let’s continue the conversation and explore the endless possibilities of serverless computing! Happy coding!
This article is based on a tutorial from Knative.dev published in 2020. We've updated it to reflect the latest versions and use k3d instead of kind.