haproxy source ip address 显示Kubernetes节点ip地址

haproxy source ip address shows Kubernetes node ip address

我在 Kubernetes 容器中安装了 HAproxy 运行。这是示例日志的样子

<134>Jul 20 13:11:37 haproxy[6]: <SOURCE_ADDRESS> [20/Jul/2020:13:11:37.713] front gameApi/game-api-test 0/0/0/9/9 200 384 - - ---- 37/37/0/0/0 0/0 {<FORWARD_FOR_ADDRESS>} "GET /api/games/lists?dtype=brandlist HTTP/1.1"

这里是 haproxy kubernetes 节点 ip 地址,我需要它是 clinet/forwardfor ip 地址,以便 filebeat 能够正确解析地理位置。

编辑:

我找到了一个使用 haproxy 的解决方案,只需设置 http-request set-src hdr(x-forwarded-for)

但是尝试使用解决方案 externalTrafficPolicy: Local 似乎破坏了我的 haproxies 服务网站的能力。当我尝试访问某个网站时,它会说“无法访问此站点”或“安全连接失败”

haproxy 服务

apiVersion: v1

kind: Service
metadata: 
  name: haproxy-test
  labels:
    app: haproxy-test
  namespace: test
  annotations:
    # Note that the backend talks over HTTP.
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: http
    service.beta.kubernetes.io/aws-sload-balancer-ssl-cert: arn:aws:acm:us-east-2:redacted:certificate/redacted
    service.beta.kubernetes.io/aws-load-balancer-ssl-ports: "https"
spec: 
  type: LoadBalancer
  externalTrafficPolicy: Local
  ports: 
  - port: 80
    name: http
    targetPort: 80
    protocol: "TCP"
  - port: 443
    name: https
    targetPort: 80
    protocol: "TCP"
  selector: 
    app: haproxy-test
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: haproxy-test
  namespace: test
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: haproxy-test
    spec:
      containers:
      - name: haproxy-test
        image: <redacted>.dkr.ecr.us-east-2.amazonaws.com/haproxy:$TAG
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: "50m"
          requests:
            cpu: "25m"

您可以通过将 externalTrafficPolicy 设置为 local 来保留源 IP。检查此问题以获取更多详细信息 How do Kubernetes NodePort services with Service.spec.externalTrafficPolicy=Local route traffic?

或者使用 http-request set-src hdr(x-forwarded-for) 配置 HAProxy 使用 X-Forward-For header 的内容来建立请求源地址的内部概念,而不是实际的 IP 地址正在启动入站连接。