当入口映射到同一服务上的 2 个不同端口时,nginx 入口会话关联行为
nginx-ingress session affinity behavior when ingress maps to 2 different ports on the same service
假设我有一个映射到 pod 的服务,它有 2 个容器,1 个公开端口 8080,另一个公开端口 8081。该服务公开两个端口。入口使用 nginx-ingress,并具有基于 cookie 的会话关联注释。它有2条路径,1条是/
映射到8080端口,另外一条是/static
映射到同一个服务的8081端口。会话亲和力是否会以这样的方式工作,无论路径是 /
还是 /static
,来自同一客户端的所有请求都将发送到同一 pod?
以下是完整配置:
入口
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/affinity-mode: "persistent"
nginx.ingress.kubernetes.io/session-cookie-name: "route"
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
spec:
rules:
- host: test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: test-service
port:
number: 8080
- path: /static
pathType: Prefix
backend:
service:
name: test-service
port:
number: 8081
服务
apiVersion: v1
kind: Service
metadata:
name: test-service
spec:
type: ClusterIP
selector:
app: test-pod
ports:
- name: container1
port: 8080
targetPort: 8080
- name: container2
port: 8081
targetPort: 8081
部署
apiVersion: apps/v1
kind: Deployment
...
spec:
...
template:
metadata:
labels:
app: test-pod
spec:
containers:
- name: container1
image: ...
ports:
- containerPort: 8080
- name: container2
image: ...
ports:
- containerPort: 8081
我成功地测试了你的配置。
实际上,此关联注释仅适用于 /
路径 - 这就是 nginx ingress
的方式 - 要使关联注释适用于您需要创建两个入口定义的两个路径:
路径入口 /
:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress-one
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/affinity-mode: "balanced"
nginx.ingress.kubernetes.io/session-cookie-name: "route-one"
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
spec:
rules:
- host: <your-domain>
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: test-service
port:
number: 8080
路径入口 /static
:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress-two
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/affinity-mode: "balanced"
nginx.ingress.kubernetes.io/session-cookie-name: "route-two"
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
spec:
rules:
- host: <your-domain>
http:
paths:
- path: /static
pathType: Prefix
backend:
service:
name: test-service
port:
number: 8081
回到您的主要问题 - 因为我们正在创建两个不同的入口,使用两个不同的 cookie,它们彼此独立。无论对方选择什么,他们每个人都会选择他的“豆荚”来“坚持”。我做了研究,但找不到任何有关设置它的方式以使其按您想要的方式工作的信息。
简单回答你的问题:
Will the session affinity work in such way where all the requests from the same client will be sent to the same pod no matter if the path is /
or /static
?
没有
假设我有一个映射到 pod 的服务,它有 2 个容器,1 个公开端口 8080,另一个公开端口 8081。该服务公开两个端口。入口使用 nginx-ingress,并具有基于 cookie 的会话关联注释。它有2条路径,1条是/
映射到8080端口,另外一条是/static
映射到同一个服务的8081端口。会话亲和力是否会以这样的方式工作,无论路径是 /
还是 /static
,来自同一客户端的所有请求都将发送到同一 pod?
以下是完整配置:
入口
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/affinity-mode: "persistent"
nginx.ingress.kubernetes.io/session-cookie-name: "route"
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
spec:
rules:
- host: test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: test-service
port:
number: 8080
- path: /static
pathType: Prefix
backend:
service:
name: test-service
port:
number: 8081
服务
apiVersion: v1
kind: Service
metadata:
name: test-service
spec:
type: ClusterIP
selector:
app: test-pod
ports:
- name: container1
port: 8080
targetPort: 8080
- name: container2
port: 8081
targetPort: 8081
部署
apiVersion: apps/v1
kind: Deployment
...
spec:
...
template:
metadata:
labels:
app: test-pod
spec:
containers:
- name: container1
image: ...
ports:
- containerPort: 8080
- name: container2
image: ...
ports:
- containerPort: 8081
我成功地测试了你的配置。
实际上,此关联注释仅适用于 /
路径 - 这就是 nginx ingress
路径入口 /
:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress-one
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/affinity-mode: "balanced"
nginx.ingress.kubernetes.io/session-cookie-name: "route-one"
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
spec:
rules:
- host: <your-domain>
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: test-service
port:
number: 8080
路径入口 /static
:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress-two
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/affinity-mode: "balanced"
nginx.ingress.kubernetes.io/session-cookie-name: "route-two"
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
spec:
rules:
- host: <your-domain>
http:
paths:
- path: /static
pathType: Prefix
backend:
service:
name: test-service
port:
number: 8081
回到您的主要问题 - 因为我们正在创建两个不同的入口,使用两个不同的 cookie,它们彼此独立。无论对方选择什么,他们每个人都会选择他的“豆荚”来“坚持”。我做了研究,但找不到任何有关设置它的方式以使其按您想要的方式工作的信息。 简单回答你的问题:
Will the session affinity work in such way where all the requests from the same client will be sent to the same pod no matter if the path is
/
or/static
?
没有