0

我正在尝试在 EKS 环境中应用 NLB 粘性会话。

有 2 个工作节点(EC2)连接到 NLB 目标组,每个节点有 2 个 nginx pod。

我想连接到本地系统上的同一个 pod 进行测试。

但看起来每次尝试使用“curl”命令都连接了不同的 pod。

这是我的测试 yaml 文件和测试命令。

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: udptest
  labels:
     app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: container
        image: nginx 
        ports:
        - containerPort: 80
      nodeSelector:
        zone: a
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: udptest2
  labels:
     app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: container
        image: nginx 
        ports:
        - containerPort: 80
      nodeSelector:
        zone: c
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-nlb
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer
#!/bin/bash
number=0

while :
do
    if [ $number -gt 2 ]; then
        break
    fi
curl -L -k -s -o /dev/null -w "%{http_code}\n" <nlb dns name>
done

每次尝试都如何通过 NLB 的 sticy 会话连接到特定的 pod?

4

2 回答 2

0

您需要启用它:

annotations:
  service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
  service.beta.kubernetes.io/aws-load-balancer-target-group-attributes: stickiness.enabled=true,stickiness.type=source_ip
于 2021-09-09T01:31:00.340 回答
0

据我了解,当服务类型为 LoadBalancer 时,不支持sessionAffinity的 ClientIP 值。

您可以使用 Nginx 入口控制器并在那里实现关联。

https://kubernetes.github.io/ingress-nginx/examples/affinity/cookie/

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/session-cookie-name: "test-cookie"
    nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/affinity-mode: persistent
    nginx.ingress.kubernetes.io/session-cookie-hash: sha1
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        backend:
          serviceName: service
          servicePort: port

好文章:https ://zhimin-wen.medium.com/sticky-sessions-in-kubernetes-56eb0e8f257d

于 2021-09-08T09:34:20.213 回答