k8s service 增加会话保持模式

会话保持模式

  • 基于客户端地址的会话保持模式,即来自同一个客户端发起的所有请求都会转发到固定的一个Pod上。此模式可以使在spec中添加sessionAffinity:ClientIP配置
# service 中增加  sessionAffinity: ClientIP ; 默认  sessionAffinity: None

[root@k8sNode0 podqinhexing]# cat test-nodeport.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: nginx-server
        image: nginx:latest

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  type: NodePort
  sessionAffinity: ClientIP # 会话保持模式
  ports:
  - port: 80  # Service端口,自己指定       
    targetPort: 80 # pod端口,注意和上面要选择的pod保持一致
    nodePort: 31122

posted @ 2023-08-30 10:22  lixinliang  阅读(427)  评论(0编辑  收藏  举报