k8s-ingress配置websocket支持


具体来说,使用的ingress-controller不一样,有关的设置也不太一样

Kubernetes Ingress Controller (k8s官方)

参考 http://github.com/kubernetes/ingress-nginx

官方文档地址:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/?spm=5176.2020520152.0.0.74e016ddRLPYlr

https://kubernetes.github.io/ingress-nginx/user-guide/miscellaneous/#websockets

https://github.com/kubernetes/ingress-nginx/blob/1614027cd42aa3fac53d62afc235b86adec7543d/docs/examples/multi-tls/README.md

在 nginx-ingress-controller 中默认已经支持websocket访问,官方文档中说明websocket不需要特别的配置,但需要在注解中配置以下三项

nginx.ingress.kubernetes.io/proxy-http-version: "1.1"
nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"

网上看到的另一种写法配置

没实践过,若是配置的话,最好单独创建一个ingress规则yaml文件,别人其他的写在一起

地址:

解决办法:
annotations 加入

proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: sx-data-server
  namespace: spot-standard 
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/configuration-snippet: |
       proxy_set_header Upgrade "websocket";
       proxy_set_header Connection "Upgrade";
spec:
  rules:
  - host: sx-data-server.internal.junengcloud.com
    http:
      paths:
      - path: /
        backend:
          serviceName: sx-data-server
          servicePort: 61051

NGINX Ingress Controller (nginx官方)

http://github.com/nginxinc/kubernetes-ingress

文档地址:https://docs.nginx.com/nginx-ingress-controller/configuration/ingress-resources/advanced-configuration-with-annotations/

https://github.com/nginxinc/kubernetes-ingress/tree/v2.1.0/examples/websocket

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: cafe-ingress
  annotations:
    nginx.org/websocket-services: "ws-svc" # 具体的服务名
spec:
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea
        pathType: Prefix
        backend:
          service:
            name: tea-svc
            port:
              number: 80
      - path: /coffee
        pathType: Prefix
        backend:
          service:
            name: coffee-svc
            port:
              number: 80
      - path: /ws
        pathType: Prefix
        backend:
          service:
            name: ws-svc
            port:
              number: 8008