【问题标题】:haproxy use pod name as server namehaproxy 使用 pod 名称作为服务器名称
【发布时间】:2022-01-08 23:09:27
【问题描述】:

我有一个 haproxy 作为在 k8s 中运行的负载均衡器,并有一条通往具有两个正在运行的 pod 的服务的路由。我希望 haproxy 中的服务器命名与我的服务背后的 pod 名称相对应。如果我没记错的话,下面的 configmap / annotation 值应该这样做:https://haproxy-ingress.github.io/docs/configuration/keys/#backend-server-naming。但对我来说它没有,对于我的生活,我无法找出原因。我的配置的相关部分如下所示:

控制器部署:

kind: Deployment
metadata:
  labels:
    run: haproxy-ingress
  name: haproxy-ingress
  namespace: haproxy-controller
spec:
  replicas: 2
  selector:
    matchLabels:
      run: haproxy-ingress
  template:
    metadata:
      labels:
        run: haproxy-ingress
    spec:
      serviceAccountName: haproxy-ingress-service-account
      containers:
        - name: haproxy-ingress
          image: haproxytech/kubernetes-ingress
          args:
            - --configmap=haproxy-controller/haproxy-ingress
            - --configmap-errorfiles=haproxy-controller/errorfile-conf
            - --default-ssl-certificate=haproxy-controller/haproxy-tls
            - --ingress.class=haproxy

控制器服务:

kind: Service
metadata:
  labels:
    run: haproxy-ingress
  name: haproxy-ingress
  namespace: haproxy-controller
spec:
  selector:
    run: haproxy-ingress
  type: ClusterIP
  ports:
    - name: https
      port: 443
      protocol: TCP
      targetPort: 443

控制器配置图:

kind: ConfigMap
metadata:
  name: haproxy-ingress
  namespace: haproxy-controller
data:
  server-ssl: "true"
  scale-server-slots: "2"
  cookie-persistence: "LFR_SRV"
  backend-server-naming: "pod"
  backend-config-snippet: |
    cookie LFR_SRV indirect nocache insert maxidle 10m httponly secure

后端服务器入口:

kind: Ingress
metadata:
  name: liferay-dxp
  namespace: backend
  annotations:
    kubernetes.io/ingress.class: "haproxy"
spec:
  tls:
    - secretName: backend-tls
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: backend
                port:
                  number: 443

haproxy.conf 生成的后端部分如下所示:

  mode http
  balance roundrobin
  option forwardfor
  cookie LFR_SRV indirect nocache insert
  ###_config-snippet_### BEGIN                                                                                                                                          
  cookie LFR_SRV indirect nocache insert maxidle 10m httponly secure
  ###_config-snippet_### END
  server SRV_1 10.xx.xx.xx:443 check ssl alpn h2,http/1.1 weight 128 cookie SRV_1 verify none
  server SRV_2 10.xx.xx.xx:443 check ssl alpn h2,http/1.1 weight 128 cookie SRV_2 verify none

除了backend-server-naming: "pod",一切正常。我也无法从here 获得任何 session-cookie-* 属性来工作。因此,我使用backend-config-snippet 用我的自定义设置覆盖生成的haproxy.conf 中的cookie 行(我添加了maxidle 10m httponly secure)。我做错了什么?

【问题讨论】:

  • 使用哪个kubernetes版本?是云端还是本地?
  • Kubernetes 版本:v1.19.13-eks-8df270(AWS 云)

标签: kubernetes cookies haproxy haproxy-ingress


【解决方案1】:

这里有一些提示可以帮助您解决问题。

确保您知道 haproxy-ingress 控制器的确切版本:

查看您共享的清单文件,很难判断您在集群中运行的是哪个确切版本的 haproxy-ingress-controller 容器(顺便说一句,将其保留为不带标签的生产环境中的最佳做法是违反最佳实践的,请阅读更多内容它here)。

要使backend-server-naming 配置密钥起作用,至少需要v0.8.1 (it was backported)

在继续进行故障排除之前,请首先仔细检查您的入口部署的兼容性。

我对“backend-server-naming=pod”行为的观察

配置动态更新:

如果我对configuration key 的官方文档理解正确,将后端的服务器命名设置为 pod 名称 (backend-server-naming=pod) 而不是 sequences,确实支持 haproxy 配置的动态重新加载,但是 不 目前支持对后端部分服务器名称的 haproxy 运行时配置进行动态更新(haproxy-ingress 作者 herehere 对此进行了解释)

这意味着您需要首先重新启动您的 haproxy-ingress 控制器实例,以便能够看到后端服务器名称的变化反映在 haproxy 配置中,例如由于 Pod 崩溃而出现新的 Pod 副本或 POD_IP 更改的情况(预计基于序列命名的服务器条目的添加/更新)。

入口类:

我已经成功地测试了v0.13.4 上的backend-server-naming=pod 设置(参见下面的测试),classified Ingress 类型基于ingressClassName 字段,而不是像您的情况那样已弃用的注释kubernetes.io/ingress.class

我并不是说您的配置不起作用(它也应该起作用),但重要的是要知道,配置的动态更新(包括对后端配置的更改)不会发生在未分类的 Ingress 资源或错误分类的资源上一,除非你真的在运行v0.12 或更新版本。

测试:

# Ingress class 
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  name: my-class
  annotations:
    ingressclass.kubernetes.io/is-default-class: "true"
spec:
  controller: haproxy-ingress.github.io/controller

# Demo Ingress resource
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    haproxy-ingress.github.io/backend-server-naming: "pod"
  name: echoserver
spec:
  ingressClassName: my-class
  rules:
  - http:
      paths:
      - backend:
          service:
            name: echoserver
            port:
              number: 8080
        path: /
        pathType: Prefix

带注释的 HA 代理配置:

backend default_echoserver_8080
    mode http
    balance roundrobin
    acl https-request ssl_fc
    http-request set-header X-Original-Forwarded-For %[hdr(x-forwarded-for)] if { hdr(x-forwarded-for) -m found }
    http-request del-header x-forwarded-for
    option forwardfor
    http-response set-header Strict-Transport-Security "max-age=15768000" if https-request
    # pod name start
    server echoserver-75d6f584bb-jlwb8 172.17.0.2:8080 weight 1 check inter 2s
    # pod name end
    server srv002 127.0.0.1:1023 disabled weight 1 check inter 2s
    server srv003 127.0.0.1:1023 disabled weight 1 check inter 2s
    server srv004 127.0.0.1:1023 disabled weight 1 check inter 2s
    ...

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多