WebSocket is a computer communications protocol that provides full-duplex communication channels over a single Transmission Control Protocol (TCP) connection. WebSocket is located at the application layer in the Open Systems Interconnection (OSI) model. WebSocket allows a server to push data to clients. Services that comply with WebSocket are WebSocket services. This topic shows you how to use an ingress gateway to access a WebSocket service in an Alibaba Cloud Service Mesh (ASM) instance.

Prerequisites

Step 1: Deploy a sample application

  1. Use kubectl to connect to the ACK cluster. For more information, see Use kubectl to connect to an ACK cluster.
  2. Use the following content to create a YAML file that is named tornado:
    apiVersion: v1
    kind: Service
    metadata:
      name: tornado
      labels:
        app: tornado
        service: tornado
    spec:
      ports:
      - port: 8888
        name: http
      selector:
        app: tornado
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: tornado
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: tornado
          version: v1
      template:
        metadata:
          labels:
            app: tornado
            version: v1
        spec:
          containers:
          - name: tornado
            image: hiroakis/tornado-websocket-example
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 8888
    ---
  3. Run the following command to create the tornado application:
    kubectl apply -f tornado.yaml

Step 2: Configure a routing rule

  1. Log on to the ASM console.
  2. In the left-side navigation pane, choose Service Mesh > Mesh Management.
  3. On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column of the ASM instance.
  4. Create an ingress gateway.
    1. In the Control Plane (Custom Resource Management) section, click the Gateway tab and then Create.
    2. In the Create panel, select default from the Namespace drop-down list and copy the following content to the code editor. Then, click OK.
      apiVersion: networking.istio.io/v1alpha3
      kind: Gateway
      metadata:
        name: tornado-gateway
      spec:
        selector:
          istio: ingressgateway
        servers:
        - port:
            number: 80
            name: http
            protocol: HTTP
          hosts:
          - "*"

      Set the number parameter to 80. This way, the WebSocket service can receive inbound or outbound HTTP and TCP traffic over port 80.

      After you create the tornado-gateway ingress gateway, it is displayed on the Gateway tab.
  5. Create a virtual service.
    1. In the Control Plane (Custom Resource Management) section, click the VirtualService tab and then Create.
    2. In the Create panel, select default from the Namespace drop-down list and copy the following content to the code editor. Then, click OK.
      apiVersion: networking.istio.io/v1alpha3
      kind: VirtualService
      metadata:
        name: tornado
      spec:
        hosts:
        - "*"
        gateways:
        - tornado-gateway
        http:
        - match:
          - uri:
              prefix: /
          route:
          - destination:
              host: tornado
            weight: 100

      Set the hosts parameter to *. This way, all requests can access the WebSocket service.

      After you create the tornado virtual service, it is displayed on the VirtualService tab.

Step 3: Query the IP address of the ingress gateway

  1. Log on to the ACK console.
  2. In the left-side navigation pane of the ACK console, click Clusters.
  3. On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.
  4. In the left-side navigation pane of the details page, choose Services and Ingresses > Services.
  5. At the top of the Services page, select istio-system from the Namespace drop-down list. Find the ingress gateway that is named istio-ingressgateway and view the IP address whose port is 80 in the External Endpoint column.

Use the ingress gateway to access the WebSocket service

  1. Enter http://<IP address of the ingress gateway> in the address bars of four different browsers.
    Service
  2. Run the following commands to access the WebSocket service:
    curl "http://<IP address of the ingress gateway>/api?id=8&value=300"
    
    curl "http://<IP address of the ingress gateway>/api?id=5&value=600"
    
    curl "http://<IP address of the ingress gateway>/api?id=1&value=200"
    
    curl "http://<IP address of the ingress gateway>/api?id=3&value=290"
  3. View the data of the WebSocket service in the four browsers. The data of the WebSocket service in the four browsers is updated at the same time and the same data is displayed, as shown in the following figure.
    WebSocket service