问题:k8s启动的tomcat服务,每次访问都带了session会话,每次调整不同的pod导致session实效,系统登录失败。
处理:在server中添加session保持
sessionAffinity: ClientIP
实验:
启动服务:
[root@master 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 [root@master 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
容器启动:
[root@master ~]# 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
[root@master 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
[root@master 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
[root@master 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