How to deploy WebSocket and gRPC services in Knight?

Keywords: github Windows Makefile Docker

Author: Alibaba cloud container platform engineer, Dongdao

**Guide reading: * * although it is said that Knative supports WebSocket and gRPC by default, it will be found in use that sometimes you want to deploy your own WebSocket or gRPC to Knative, but there are still many problems. Although the final investigation found that most of them are caused by their own program problems or configuration errors. This paper gives an example of WebSocket and gRPC respectively. When you need to deploy related services in the production or test environment, you can use the example in this paper to test the knight service.

WebSocket

If you manually configure Istio Gateway to support WebSocket, you need to enable websocketUpgrade. But this capability comes with a knave serving deployment. The complete code for this example is placed in https://github.com/knative-sample/websocket-chat , which is an example of group chat based on WebSocket.

Using a browser to connect to a deployed service, you can see a window to receive information and a window to send information. When you send a message, all the connected users can receive your message. So you can use two browser windows to connect to the service, one window to send messages and one window to receive messages, to verify whether the WebSocket service is normal.

This example is in gorilla/websocket Some optimizations are made on the basis of:

  • Add vendor dependency in the code, you can download it and use it directly.
  • Dockerfile and Makefile are added to directly compile binary and make image
  • The yaml file (service.yaml) of the known service has been added. You can submit it directly to the known cluster for use.
  • You can also directly use the compiled image registry.cn-hangzhou.aliyuncs.com/known-sample/websocket-chat: 2019-10-15

Knowledge service configuration:

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 the code clone comes down, execute kubectl apply -f service.yaml to deploy the service to knight, and then directly access the service address.

Check the ksvc list and get the access domain name.

└─# kubectl get ksvc
NAME             URL                                                    LATESTCREATED          LATESTREADY            READY   REASON
websocket-chat   http://websocket-chat.default.serverless.kuberun.com   websocket-chat-7ghc9   websocket-chat-7ghc9   True

Now open with browser http://websocket-chat.default.serverless.kuberun.com You can see the group chat window.

Open two windows, send a message in one window, and the other receives the message through WebSocket.

gRPC

gRPC can't be accessed directly through browser, so it needs to interact with client and server. The complete code for this example is placed in https://github.com/knative-sample/grpc-ping-go , this example will give a mirror that can be used directly to test the gRPC service.

Knowledge service configuration:

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

After the code clone comes down, execute kubectl apply -f service.yaml to deploy the service to knight.

Get the ksvc list and access the domain name:

└─# kubectl get ksvc
NAME             URL                                                    LATESTCREATED          LATESTREADY            READY     REASON
grpc-ping        http://grpc-ping.default.serverless.kuberun.com        grpc-ping-p2tft                               Unknown   RevisionMissing
websocket-chat   http://websocket-chat.default.serverless.kuberun.com   websocket-chat-6hgld   websocket-chat-6hgld   True

Now that we know that the address of gRPC server is grpc-ping.default.serverless.kuberun.com, and the port is 80, we can initiate a test request:

└─# 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
2019/10/16 11:35:07 Ping got hello - pong
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854794231 +0800 CST m=+73.061909052
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854827273 +0800 CST m=+73.061942072
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854835802 +0800 CST m=+73.061950606
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854842843 +0800 CST m=+73.061957643
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854849211 +0800 CST m=+73.061964012
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854855249 +0800 CST m=+73.061970049
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854861659 +0800 CST m=+73.061976460
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854875071 +0800 CST m=+73.061989873
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854905416 +0800 CST m=+73.062020221
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.85491183 +0800 CST m=+73.062026630
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.85492533 +0800 CST m=+73.062040133
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854932285 +0800 CST m=+73.062047083
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854946977 +0800 CST m=+73.062061782
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854953311 +0800 CST m=+73.062068112
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854966639 +0800 CST m=+73.062081440
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854973939 +0800 CST m=+73.062088739
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854985463 +0800 CST m=+73.062100268
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854993275 +0800 CST m=+73.062108073
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.854999812 +0800 CST m=+73.062114613
2019/10/16 11:35:07 Got pong 2019-10-16 11:35:07.855012676 +0800 CST m=+73.062127479

Summary

This paper shows the deployment methods of WebSocket and gRPC through two examples:

  • The WebSocket example shows sending and receiving messages through a chat
  • gRPC shows the process of gRPC remote call by starting a client

About the author: Dong Dao, an Alibaba cloud container platform engineer, is responsible for the related work of Alibaba cloud container platform known. Learn more about ACK: https://www.aliyun.com/product/kubernetes

Welcome to the interactive group

"Alibaba cloud native wechat public account (ID: Alicloudnative) focuses on the technology fields such as microservice, Serverless, container, Service Mesh, the trend of jujiao cloud native popular technology, the large-scale implementation practice of cloud native, and makes the technical public account that most understands cloud native developers."

Posted by Thomisback on Thu, 17 Oct 2019 23:53:21 -0700