Skip to content

Instantly share code, notes, and snippets.

@masrurimz
Created August 25, 2024 06:03
Show Gist options
  • Select an option

  • Save masrurimz/d7ef2ed39045b6a71f6caa2258e0a8f5 to your computer and use it in GitHub Desktop.

Select an option

Save masrurimz/d7ef2ed39045b6a71f6caa2258e0a8f5 to your computer and use it in GitHub Desktop.
Set Up a Kubernetes Cluster with KNative as a Serverless Backend Locally

Set Up a Kubernetes Cluster with KNative as a Serverless Backend Locally

Unlocking the Power of Serverless with Kubernetes and KNative

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.

Key Steps

  1. Create the Cluster: Ensure the correct ports are exposed.
  2. Install KNative: Set up the core components.
  3. Configure the Kourier Gateway: Align it with your cluster's port configuration.
  4. Set Up Local DNS: Use sslip.io for simple domain management.
  5. Deploy a Sample Go Application: Verify your setup with a simple "Hello, World" app.

1. Create a Cluster with Custom Port Configuration

We’ll start by creating a Kubernetes cluster with custom port configuration using k3d. This ensures your cluster is ready to handle traffic correctly.

  1. Create the cluster configuration file:

    # clusterconfig.yaml
    
    apiVersion: k3d.io/v1alpha4
    kind: Simple
    servers: 1
    ports:
      - port: 80:31080
      - port: 443:31443
  2. Apply the configuration and create the cluster:

    k3d cluster create ${clusterName} --config clusterconfig.yaml
  3. 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.


2. Install KNative

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.yaml

For more details, refer to the official KNative documentation.

With KNative Serving installed, you’re ready to set up the ingress gateway.


3. Configure Kourier as 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.

  1. Download the Kourier YAML file:

    curl -Lo kourier.yaml https://github.com/knative/net-kourier/releases/download/knative-v1.15.1/kourier.yaml
  2. Modify the configuration:

    Open kourier.yaml and find the Service with metadata.name: kourier. Modify it to expose the correct ports and change the service type to NodePort.

    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
  3. Install Kourier with the customized YAML:

    kubectl apply --filename kourier.yaml
  4. 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"}}'
  5. 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.


4. Set Up Local DNS with sslip.io

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-serving

You can now access services using a URL like http://<your-service-name>.default.127.0.0.1.sslip.io.


5. Deploy a Sample Go Application

Finally, let’s deploy a simple Go application to verify that everything is working correctly.

  1. 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!"
  2. Deploy the application:

    kubectl apply --filename service.yaml
  3. 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   RevisionMissing
    

    Once 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
    
  4. 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!
    

Wrapping Up

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!

Credits and Acknowledgments

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment