All Products
Search
Document Center

Microservices Engine:Route requests of WebSocket applications based on cloud-native gateways

Last Updated:Aug 09, 2024

The WebSocket protocol enables continuous two-way communications between a client and a server. This ensures persistent connections and low latency. When a Container Service for Kubernetes (ACK) cluster accesses the external WebSocket service, a cloud-native gateway receives and forwards requests and distributes the requests to specific backend services based on the predefined routing rules. This topic describes how to deploy a WebSocket application in a Container Service for Kubernetes cluster and forward requests by using a cloud-native gateway.

Prerequisites

Step 1: Deploy a WebSocket application in the ACK cluster

For more information about how to deploy an application, see Create a stateless application by using a Deployment.

In this example, the ACK cluster is used for service discovery. The backend service is registered with CoreDNS by using annotation-based service APIs. The backend service in this example provides multiple WebSocket APIs. The WebSocket application that is deployed in the ACK cluster uses the following resource configuration:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: sockbin
  name: sockbin-app
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: sockbin
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: sockbin
    spec:
      containers:
        - image: therebelrobot/sockbin
          imagePullPolicy: Always
          name: sockbin
          ports:
            - containerPort: 4080
              protocol: TCP
          resources:
            limits:
              cpu: 500m
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: sockbin
  name: sockbin-service
  namespace: default
spec:
  ports:
    - name: http
      port: 4080
      protocol: TCP
      targetPort: 4080
  selector:
    app: sockbin
  sessionAffinity: None
  type: NodePort

Step 2: Use the cloud-native gateway to route requests of the WebSocket application

Add the ACK cluster as a service source of the cloud-native gateway and add the Sockbin service.

Add a service source

  1. Log on to the MSE console, and select a region in the top navigation bar.

  2. In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.

  3. In the left-side navigation pane, click Routes. On the page that appears, click the Sources tab.

  4. On the Source tab, click Add Source. In the Add Source panel, configure the parameters, and click OK.

    Parameter

    Description

    Source Type

    Select Container Service.

    ACK/ACK Serverless Cluster

    Select the cluster in which your backend service is deployed.

    Listen to Kubernetes Ingress

    If you turn on the switch, the cloud-native gateway automatically listens to the changes of Ingress resources and makes the listened configurations of domain names and routes of the Ingress resources take effect.

    If you turn off the switch, the cloud-native gateway no longer listens to the changes of Ingress resources and makes the listened configurations of domain names and routes of the Ingress resources become ineffective.

    Note

    The priorities of the domain names and routes that are manually configured in the MSE console are higher than the priorities of the listened domain names and routes of the Ingress resources.

Add a service

  1. Log on to the MSE console, and select a region in the top navigation bar.

  2. In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.

  3. In the left-side navigation pane, click Routes. On the page that appears, click the Services tab.

  4. On the Services tab, click Add Service. In the Add Service panel, configure the parameters, and click OK.

    Parameter

    Description

    Service Source

    Select Container Service.

    Namespace

    Select the namespace of the destination cluster.

    Services

    Select one or more services.

Add a route from the gateway to the Sockbin service

  1. Log on to the MSE console, and select a region in the top navigation bar.

  2. In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.

  3. In the left-side navigation pane, click Routes, and click the Routes tab.

  4. On the Routes tab, click Add Route. On the page that appears, configure the parameters, and click Save and Advertise.

    Parameter

    Description

    Routing Rule Name

    Enter sockbin-route.

    Domain name

    Select the default associated domain name * from the drop-down list.

    Path

    Select Prefix from the matching condition drop-down list and enter / in the field.

    Scenario

    Select Single Service.

    Backend Service

    Select the destination service and service port.

Verify the result

You can use one of the following methods to verify the availability of the WebSocket service.

Method 1: Conduct testing on the Sockbin service page

  1. Log on to the MSE console, and select a region in the top navigation bar.

  2. In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.

  3. On the Gateway Ingress tab of the Overview page of the gateway, find the public endpoint of the Server Load Balancer (SLB) instance that is associated with the gateway. Then, enter the endpoint in the address bar of your browser and press Enter.

    The gateway performs routing based on the domain name and path that are included in the WebSocket handshake request to open the Sockbin service page.sockbin服务的界面

Method 2: Conduct testing on the WebSocket client in a specific programming language

For example, you can use the WebSocket client in Python to receive a server response with a latency of 1 second.

#!/usr/bin/env python

import asyncio
import websockets

async def hello():
    async with websockets.connect("ws://ip_addr/delay/1000") as websocket:
        await websocket.send("Hello Test")
        text = await websocket.recv()
        print(text)

asyncio.run(hello())