如何在Kubernetes中为多个服务设置粘性会话?

时间:2020-05-02 19:19:11

标签: nginx kubernetes websocket sticky-session session-affinity

我有2个服务:

  1. 带有Nginx(两个副本)的estest / websocket API服务

  2. 守护程序服务(1个副本)

守护程序服务将在某个时候向前端发出一个websocket事件。但是,该事件似乎无法成功地从守护程序服务发送到前端。

我还尝试将事件从API服务器发送到前端,并且该事件已成功发送到前端。 (也许是因为前端已连接到API WebSocket服务器)。

对于粘性会话我做了什么:

---
apiVersion: "v1"
kind: "Service"
metadata:
  name: "daemon"
  namespace: app
spec:
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 80
  selector:
    app: "daemon"
  type: "NodePort"
  sessionAffinity: ClientIP  
---
---
apiVersion: "v1"
kind: "Service"
metadata:
  name: "api"
  namespace: app
spec:
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 80
  selector:
    app: "api"
  type: "NodePort"
  sessionAffinity: ClientIP
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
  annotations:
    getambassador.io/resource-downloaded: '2020-03-30T16:10:34.466Z'
  name: api
  namespace: app
spec:
  prefix: /api
  service: api:80
load_balancer:
  policy: ring_hash
  cookie:
    name: sticky-cookie
    ttl: 60s
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
  annotations:
    getambassador.io/resource-downloaded: '2020-03-30T16:10:34.466Z'
  name: api-ws
  namespace: app
spec:
  prefix: /private
  service: api:80
  use_websocket: true
load_balancer:
  policy: ring_hash
  cookie:
    name: sticky-cookie
    ttl: 60s
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
  annotations:
    getambassador.io/resource-downloaded: '2020-03-30T16:10:34.466Z'
  name: api-daemon
  namespace: app
spec:
  prefix: /daemon
  service: daemon:80
  use_websocket: true
load_balancer:
  policy: ring_hash
  cookie:
    name: sticky-cookie
    ttl: 60s

1 个答案:

答案 0 :(得分:0)

来自kubernetes.io DaemonSet docs

服务:使用相同的Pod选择器创建服务,然后使用该服务访问随机节点上的守护程序。 (无法到达特定节点。)

所以我认为sessionAffinity无法与DaemonSet一起使用。