0

我正在尝试为我当前的 Kubernetes 架构配置额外的粘性会话层。我不想通过主 LoadBalancer 服务路由每个请求,而是希望通过 nginx 粘性会话的上层路由请求。我正在遵循https://kubernetes.github.io/ingress-nginx/examples/affinity/cookie/上的指南

我正在使用 Azure Cloud 进行集群部署。以前,使用具有 LoadBalancer 类型的服务会自动生成一个外部 IP 地址供用户连接到我的集群。现在我需要为我的用户配置静态 IP 地址以连接到,并使用 nginx 入口。我该怎么做?我按照这里的指南 - https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/static-ip但入口的外部地址仍然是空的!!

我做错了什么?

kubectl 获取

# nginx-sticky-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-ingress-lb
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  externalTrafficPolicy: Local
  type: LoadBalancer
  ports:
  - port: 80
    name: http
    targetPort: 80
  - port: 443
    name: https
    targetPort: 443
  selector:
    # Selects nginx-ingress-controller pods
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

# nginx-sticky-controller.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-ingress-controller
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: ingress-nginx
      app.kubernetes.io/part-of: ingress-nginx
  template:
    metadata:
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.31.0
        name: nginx-ingress-controller
        ports:
        - containerPort: 80
          hostPort: 80
        - containerPort: 443
          hostPort: 443
        resources:
          limits:
            cpu: 0.5
            memory: "0.5Gi"
          requests:
            cpu: 0.5
            memory: "0.5Gi"
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
        args:
        - /nginx-ingress-controller
        - --publish-service=$(POD_NAMESPACE)/nginx-ingress-lb
# nginx-sticky-server.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/session-cookie-name: "nginx-sticky-server"
    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:
  - http:
      paths:
      - backend:
          # This assumes http-svc exists and routes to healthy endpoints.
          serviceName: my-own-service-master
          servicePort: http
4

1 回答 1

0

好的,我让它工作了。我认为不同之处在于您使用的云提供商,对于 Azure Cloud,您应该遵循他们的文档以及他们在 Kubernetes 集群中实现入口控制器的方式。

链接在这里用于部署入口控制器。他们在 Kubernetes 集群中创建公共 IP 地址并将其与入口控制器链接起来的方式是可行的。到目前为止,我可以确认撰写本文的时间。

完成上述链接中的步骤部署后,我可以像往常一样应用入口 .yaml 文件,即kubectl apply -f nginx-sticky-server.yaml设置 nginx 粘性会话。如果您的 ingress .yaml 文件中声明的服务名称和服务端口正确,则 nginx 入口控制器应将您的用户请求重定向到正确的服务。

于 2020-04-27T12:55:42.193 回答