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 an Container Service for Kubernetes cluster and forward requests by using a cloud-native gateway.
Prerequisites
An ACK cluster is created. For more information, see Create an ACK managed cluster.
A Microservices Engine (MSE) cloud-native gateway is created. For more information, see Create a cloud-native gateway.
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
Log on to the MSE console, and select a region in the top navigation bar.
In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.
In the left-side navigation pane, choose Service Management > Sources.
On the Sources page, click Add Source in the upper-left corner. 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.
NoteThe 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
Log on to the MSE console, and select a region in the top navigation bar.
In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.
In the left-side navigation pane, choose Service Management > Services.
On the Services page, click Add Service in the upper-left corner. 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
Log on to the MSE console, and select a region in the top navigation bar.
In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the name of the gateway.
In the left-side navigation pane, choose Routes > Route Settings.
In the upper-left corner of the Route Settings page, click Add Route.
In the Add Route panel, configure the parameters.
In the Request Information step, configure the parameters and click Next.
Parameter
Description
Route Name
Enter
sockbin-route
.Domain Name
Select the default associated domain name * from the drop-down list.
Match Rule
Select Prefix Match from the Path matching condition drop-down list. Enter a path that starts with a forward slash (
/
).In the Destination Service step, select Single Service for Destination Service, select the destination service and service port number, and then click Save and Release.
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.
The gateway routes requests based on the domain name and path that are carried in the requests during a WebSocket handshake. You can use the following configuration to access the gateway and go to the Sockbin service page.
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())