Author | Winter Island Ali cloud container platform engineer
Introduction: Although default on Knative WebSocket support and gRPC, but in use will find that sometimes you want to own WebSocket or gRPC deployed to Knative in, there is still not a smooth variety. While the final investigation found that most of their procedural problems or configuration errors. Paper gives an example of WebSocket gRPC and when to deploy in a production-related services or test environment can be used to test examples given herein Knative and services.
WebSocket
If you own a manual configuration Istio Gateway supports WebSocket will need to turn on websocketUpgrade
the function. But in fact use Knative Serving deployment comes with this capability. In this example the complete code https://github.com/knative-sample/websocket-chat , which is an example of a chat group based on the realization WebSocket.
Using a browser to connect to the service deployment you can see the window of a window and sending information to receive information. When you send a message after all connected users can receive your incoming messages. So you can use two browser windows are connected to the service, a window to send a message a window receives a message, in order to verify WebSocket service is normal.
This example is in the gorilla / websocket number of optimization basis:
- Add a vendor-dependent code, you can download it directly
- Added Dockerfile Makefile and can be directly compiled binary and mirrored
- Added yaml file Knative Sevice's (
service.yaml
), you can submit directly to the use of cluster Knative - It can also be used directly compiled mirror
registry.cn-hangzhou.aliyuncs.com/knative-sample/websocket-chat:2019-10-15
Knative 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 execution of code clone down kubectl apply -f service.yaml
the service deployed to Knative, and then directly access the service address can be used.
View ksvc list, and get access to the 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 use the browser to open http://websocket-chat.default.serverless.kuberun.com to see the group chat window.
Opening two windows, a window in which to send a message, another window also received this message by WebSocket.
gRPC
gRPC can not be accessed directly through the browser, need to interact through client-side and server-side. The complete code for this example on https://github.com/knative-sample/grpc-ping-go , mirroring, test gRPC This example will give a service can be directly used.
Knative 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 down execution kubectl apply -f service.yaml
will deploy the service in Knative.
Ksvc and get access to a list of domain names:
└─# 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 gRPC server address is grpc-ping.default.serverless.kuberun.com, the port is 80, then 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
By two examples show respectively gRPC WebSocket and deployment methods:
- WebSocket chat example shows a way by transmitting and receiving messages
- gRPC shows a process gRPC remote call by starting a client's way
About the author:
Winter Island, Ali cloud container platform engineer, responsible for container Ali cloud platform Knative related work.
For more details ACK: https://www.aliyun.com/product/kubernetes
Welcome to Knative exchange group
"Alibaba Cloud native micro-channel public number (ID: Alicloudnative) focus on micro service, Serverless, container, Service Mesh and other technical fields, focusing popular technology trends in cloud native, cloud native large-scale landing practice, do most understand cloud native developers technology public number. "
This article from the blog article multiple platforms OpenWrite release!