【问题标题】:haproxy source ip address shows Kubernetes node ip addresshaproxy源ip地址显示Kubernetes节点ip地址
【发布时间】:2020-07-21 15:26:07
【问题描述】:

我在 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 似乎破坏了我为网站提供服务的能力。当我尝试访问某个网站时,它会说“无法访问此网站”或“安全连接失败”

代理服务

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"

【问题讨论】:

    标签: kubernetes haproxy filebeat


    【解决方案1】:

    您可以通过将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 标头的内容来建立其内部概念,即请求的源地址,而不是启动入站连接的实际IP 地址。

    【讨论】:

    • 在看到这个答案之前,我找到了一个替代解决方案,即在我的 ha 代理配置中设置 `http-request set-src hdr(x-forwarded-for)`
    • 我已经编辑了我的答案,以便为未来的读者添加这个替代方案
    猜你喜欢
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    相关资源
    最近更新 更多