入口 Nginx NLB 获取客户端真实 IP(读取 PROXY 协议时损坏的标头:“”)

问题描述 投票:0回答:2

我已经通过私有 NLB(网络负载均衡器)公开了入口 nginx 控制器。我想在入口 Nginx 上启用主机白名单。
我的用例是允许从 VPC1 到 VPC2 的请求,并且只应允许来自 VPC1 的请求通过此私有 nginx。为此,我使用了下面的注释

nginx.ingress.kubernetes.io/whitelist-source-range

我从中得到的问题是 ingress-nginx 没有接收客户端真实 IP。经过一些研究后,我发现我必须在 NLB 上启用代理协议。为此,我添加了以下注释和配置。

         annotations:
              service.beta.kubernetes.io/aws-load-balancer-backend-protocol: tcp
              service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout: '60'
              service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: 'true'
              service.beta.kubernetes.io/aws-load-balancer-type: nlb
              service.beta.kubernetes.io/aws-load-balancer-internal: "true"
              service.beta.kubernetes.io/aws-load-balancer-target-group-attributes: proxy_protocol_v2.enabled=true
          metrics:
            enabled: true
          config:
            use-proxy-protocol: "true"
            real-ip-header: "proxy_protocol"

准确来说我只添加了这部分

       config:
            use-proxy-protocol: "true"
            real-ip-header: "proxy_protocol"

       service.beta.kubernetes.io/aws-load-balancer-target-group-attributes: proxy_protocol_v2.enabled=true
    
I've also tried this annotation with same config
       service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: "*"

我收到的错误是

broken header: "" while reading PROXY protocol, client: xx.xx.xx.xx

我无法弄清楚我做错了什么。非常感谢任何帮助。

更新1:
我检查了此注释未启用 aws 控制台代理协议。当我手动启用它时,一切正常。但我不明白为什么这不起作用,这与我使用的 ingress nginx 版本有关吗?

kubernetes-helm kubernetes-ingress nlb ingress-nginx proxy-protocol
2个回答
0
投票

为了启用代理协议并使该注释起作用

service.beta.kubernetes.io/aws-load-balancer-target-group-attributes: proxy_protocol_v2.enabled=true

我们必须使用 aws 负载均衡器控制器。使用此注释
service.beta.kubernetes.io/aws-load-balancer-type: nlb
kubernetes 使用自己的负载均衡器控制器,但不支持此注释。
要使用 aws 负载均衡器控制器,我们需要添加这些注释。

service.beta.kubernetes.io/aws-load-balancer-type: external
service.beta.kubernetes.io/aws-load-balancer-nlb-target-type: instance/ip (on the basis of your use case).

有关更多详细信息,请参阅this文档。
如果您想更深入地了解并检查 kubernetes 代码库,请点击此链接


0
投票

已接受的答案需要更新。现在需要 aws-loadbalancer-controller。

© www.soinside.com 2019 - 2024. All rights reserved.