The WebSocket protocol enables persistent, low-latency, and two-way communication between a client and a server. When you access a WebSocket service from outside a Kubernetes cluster, an MSE cloud-native gateway receives and forwards requests to the appropriate backend service based on defined routing rules. This tutorial shows how to deploy a WebSocket application in a Container Service for Kubernetes (ACK) cluster and use an MSE cloud-native gateway for traffic forwarding.
Prerequisites
-
You have created an ACK managed cluster.
-
You have created an MSE cloud-native gateway.
Step 1: Deploy the WebSocket application
For detailed instructions, see Create a stateless workload by using a Deployment.
This tutorial uses a Kubernetes-native service discovery method. A declarative Service API resource registers the backend service with CoreDNS. The sample backend service provides multiple WebSocket endpoints. Apply the following YAML configuration in your ACK cluster:
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: Route WebSocket traffic
Add the ACK cluster as a service source for the gateway, and then 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 ID of the gateway.
-
In the left-side navigation pane, click Routes. Then, click the Source tab.
-
Click Add Source, configure the following parameters, and then click OK.
Parameter
Description
Source Type
Select Container Service.
ACK/ACK Serverless Cluster
Select the cluster where your backend service is deployed.
Listen to Kubernetes Ingress
If you enable this option, the MSE cloud-native gateway automatically listens for changes to Ingress resources and applies the domain name and route configurations defined in those resources.
If you disable this option, the gateway stops listening to Ingress resources, and any previously applied configurations from those resources become invalid.
NoteDomain names and routes that you manually configure in the MSE console take precedence over configurations from 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 ID of the gateway.
-
In the left-side navigation pane, click Routes. Then, click the Services tab.
-
Click Create Service, configure the following parameters, and then click OK.
Parameter
Description
Service Sources
Select Container Service.
Namespaces
Select the namespace of the target cluster.
Services
Select your service from the list.
Add a Sockbin route
-
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 ID of the gateway.
-
In the left-side navigation pane, click Routes. Then, click the Routes tab.
-
Click Add Route, configure the following parameters, and then click Save and Advertise.
Parameter
Description
Route Name
Enter
sockbin-route.Domain Name
Select the default domain name *.
Path
For the match condition, select Prefix. For the path value, enter
/.Scenario
Select Single Service.
Backend Service
Select the target Service and Service Port.
Verify the results
You can use one of the following methods to verify that the WebSocket service is available.
Method 1: Test with the Sockbin UI
-
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 ID of the gateway.
-
On the Overview page of the gateway, go to the Gateway Ingress tab. Find the public endpoint of the associated Server Load Balancer (SLB) instance and open it in a browser.
The gateway routes the request to the Sockbin service UI based on the domain name and path in the WebSocket handshake.
Output: CONNECTING to / ... CONNECTED to / SENT: Hello Sockbin! RESPONSE: { "timestamp": "Tue May 31 2022 07:49:41 GMT+0000 (UTC)", "url": "http://sockb.in/", "reqData": "Hello Sockbin!" }
Method 2: Test with a WebSocket client
For example, you can use a Python WebSocket client to receive a server response with a 1-second delay.
#!/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())