简体   繁体   中英

How to set sticky session for multiple services in kubernetes?

I have 2 services:

  1. Restful/websocket API service with Nginx (2 replicas)

  2. Daemon service (1 replica)

The daemon service will emit a websocket event to the frontend at some point. However, the event doesn't seem to be emitted successfully to the frontend from the daemon service.

I also tried to emit events from the API server to the frontend, and the event was successfully emitted to the front end. (maybe because the frontend is connected to the API WebSocket server).

What I have done for sticky-session:

---
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

From kubernetes.io DaemonSet docs :

Service: Create a service with the same Pod selector, and use the service to reach a daemon on a random node. (No way to reach specific node.)

So I think sessionAffinity cannot work with DaemonSet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM