Cloud Native 9 min read

Deploy WebSocket and gRPC Services on Knative: A Step‑by‑Step Guide

This article walks you through deploying a WebSocket chat service and a gRPC ping service on Knative, covering repository cloning, Docker image preparation, Knative Service YAML configuration, kubectl deployment commands, and browser‑based verification to ensure the services run correctly in a serverless environment.

Alibaba Cloud Native
Alibaba Cloud Native
Alibaba Cloud Native
Deploy WebSocket and gRPC Services on Knative: A Step‑by‑Step Guide

Overview

Knative Serving can run both WebSocket and gRPC workloads without additional Istio configuration. The article provides two minimal Go examples—one for a WebSocket chat service and one for a gRPC ping service—together with Docker images and Knative Service definitions.

WebSocket Example

The source code (including vendor dependencies, Dockerfile, Makefile and service.yaml) is available at https://github.com/knative-sample/websocket-chat. A pre‑built image can be pulled from

registry.cn-hangzhou.aliyuncs.com/knative-sample/websocket-chat:2019-10-15

. Deploy the service with: kubectl apply -f service.yaml Typical service.yaml:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: websocket-chat
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/websocket-chat:2019-10-15
        ports:
        - name: http1
          containerPort: 8080

After deployment, retrieve the service URL with kubectl get ksvc and open it in a browser. Using two browser windows—one to send messages and the other to receive—verifies that the WebSocket upgrade works correctly.

gRPC Example

The complete code and a ready‑to‑use image are hosted at https://github.com/knative-sample/grpc-ping-go. The image can be pulled from

registry.cn-hangzhou.aliyuncs.com/knative-sample/grpc-ping-go:2019-10-15

. Deploy the service using its service.yaml: kubectl apply -f service.yaml Typical service.yaml for the gRPC server:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: grpc-ping
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/grpc-ping-go:2019-10-15
        ports:
        - name: h2c
          containerPort: 8080

Obtain the service URL with kubectl get ksvc (e.g., grpc-ping.default.serverless.kuberun.com). Test the service by running the client container:

docker run --rm registry.cn-hangzhou.aliyuncs.com/knative-sample/grpc-ping-go:2019-10-15 \
  /client -server_addr="grpc-ping.default.serverless.kuberun.com:80" -insecure

The client prints repeated lines such as “Ping got hello – pong”, confirming successful gRPC (h2c) communication through Knative.

Key Points

Knative Serving automatically handles WebSocket upgrades and HTTP/2 cleartext (h2c) for gRPC without extra Istio settings.

Typical workflow: clone the repository, build or pull the Docker image, apply the Knative Service YAML, and verify the endpoint with kubectl get ksvc and appropriate client tools.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerServerlessKubernetesGogRPCWebSocketKnative
Alibaba Cloud Native
Written by

Alibaba Cloud Native

We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.