• 周五. 12月 2nd, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

k8s的tomcat多pod session会话保持配置

admin

11月 28, 2021

问题:k8s启动的tomcat服务,每次访问都带了session会话,每次调整不同的pod导致session实效,系统登录失败。

处理:在server中添加session保持

sessionAffinity: ClientIP
实验:
启动服务:

[[email protected] test]# more Deploy.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: spring-k8s
spec:
  replicas: 4
  selector:
    matchLabels:
      app: spring-k8s
  template:
    metadata:
      labels:
        app: spring-k8s
    spec:
      containers:
        - name: spring-k8s
          image: jiangbo920827/spring-k8s:liveness
          ports:
            - containerPort: 8080
[[email protected] test]# more Service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: spring-k8s
spec:
  selector:
    app: spring-k8s
  type: NodePort
  ports:
    - nodePort: 30008
      port: 80
      protocol: TCP
      targetPort: 8080
容器启动:

[[email protected] ~]# kubectl get pods |grep sprin
spring-k8s-7db85b44fc-4d9xg   1/1       Running   0          1h
spring-k8s-7db85b44fc-4rjp4   1/1       Running   0          1h
spring-k8s-7db85b44fc-64v76   1/1       Running   0          1h
spring-k8s-7db85b44fc-s5hhw   1/1       Running   0          1h
没加session亲和信参数:分布到各个pod

[[email protected] test]# for i in {1..10};do curl http://192.168.10.98:30008/hostname;echo ;done
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4rjp4
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-64v76
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-s5hhw
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-64v76
spring-k8s-7db85b44fc-s5hhw
添加session亲和性参数:sessionAffinity: ClientIP

[[email protected] test]# more Service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: spring-k8s
spec:
  selector:
    app: spring-k8s
  type: NodePort
  ports:
    - nodePort: 30008
      port: 80
      protocol: TCP
      targetPort: 8080
  sessionAffinity: ClientIP
[[email protected] test]# for i in {1..10};do curl http://192.168.10.98:30008/hostname;echo ;done
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg
spring-k8s-7db85b44fc-4d9xg

  

  

  

做一个决定,并不难,难的是付诸行动,并且坚持到底。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注