k8s使用nginx-ingress支持socket.io

系统新加了消息推送,使用socket.io实现,测试环境已通过没有问题,在线环境k8s部署的时候遇到了障碍,如何使ingress支持socket.io。

惯例先搜索,结果全是ingress-nginx的配置方案。翻nginx-ingress官网吧,(Advanced Configuration with Annotations),找到nginx.org/websocket-services 配置项,后面还给了示例:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress
  annotations:
    nginx.org/websocket-services: "ws-svc"
spec:
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea
        backend:
          serviceName: tea-svc
          servicePort: 80
      - path: /coffee
        backend:
          serviceName: coffee-svc
          servicePort: 80
      - path: /ws
        backend:
          serviceName: ws-svc
          servicePort: 8008

安排上,测试,connect成功。我的配置如下:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: pms-ingress
namespace: pms
annotations:
  nginx.org/client-max-body-size: "200m"
  nginx.org/redirect-to-https: "true"
  nginx.org/proxy-connect-timeout: "300s"
  nginx.org/proxy-send-timeout: "300s"
  nginx.org/proxy-read-timeout: "300s"
  nginx.org/websocket-services: "pms-message"
spec:
tls:
- hosts:
  - xx.xx.com
  secretName: pms-load-balancer
rules:
- host: xx.xx.com
  http:
    paths:
    - path: /api/message
      backend:
        serviceName: pms-message
        servicePort: 80
    - path: /socket.io
      backend:
        serviceName: pms-message
        servicePort: 80

新的问题出现了,我的message服务,同时有提供的restApi,将 nginx.org/websocket-services 配置 pms-message 服务后,socket.io可以访问了,但是普通http的api调用不好使了。

查看ingress的pod中的nginx配置,location /api/message 和 /socket.io 中均多出了下面这两行配置

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

按照测试环境的配置,location /api/message 是不加这两行的。

想了半天,想出了个主意,一个pod <–> 2个service。对message服务的pod,配置两个service,pms-message 和 pms-message-socketio,ingress配置如下:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: pms-ingress
  namespace: pms
  annotations:
    nginx.org/client-max-body-size: "200m"
    nginx.org/redirect-to-https: "true"
    nginx.org/proxy-connect-timeout: "300s"
    nginx.org/proxy-send-timeout: "300s"
    nginx.org/proxy-read-timeout: "300s"
    nginx.org/websocket-services: "pms-message-socketio"
spec:
  tls:
  - hosts:
    - xx.xx.com
    secretName: pms-load-balancer
  rules:
  - host: xx.xx.com
    http:
      paths:
      - path: /api/message
        backend:
          serviceName: pms-message
          servicePort: 80
      - path: /socket.io
        backend:
          serviceName: pms-message-socketio
          servicePort: 80

这样,pms-message 和 pms-message-socketio 两个不同的service实际上访问的是同一个服务,但是pms-message-socketio支持websocket连接协议,pms-message仍然是通过http进行restapi调用。

点击数:100285

发表评论

您的电子邮箱地址不会被公开。

*

code