gpt4 book ai didi

kubernetes - 如何使用kubernetes服务从Google Network Load Balancer获取客户端IP

转载 作者:行者123 更新时间:2023-12-02 11:29:03 24 4
gpt4 key购买 nike

我在GKE中使用类型:LoadBalancer创建了一个kubernetes服务。

apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
type: LoadBalancer
ports:
- name: http
port: 80
targetPort: http
selector:
app: nginx

这是nginx服务,请尝试获取原始客户端IP。喜欢
        location / {
echo $remote_addr;
echo $http_x_forwarded_for;
}

但是结果将是:
10.140.0.97

$remote_addr就像kubernetes IP内部一样。
$http_x_forwarded_for为空。

我不知道为什么这不是文件所说的。

我读了什么

https://cloud.google.com/load-balancing/docs/network

Network Load Balancing is a pass-through load balancer, which means that your firewall rules must allow traffic from the client source IP addresses.



https://cloud.google.com/kubernetes-engine/docs/concepts/network-overview#ext-lb

If your Service needs to be reachable from outside the cluster and outside your VPC network, you can configure your Service as a LoadBalancer, by setting the Service's type field to LoadBalancer when defining the Service. GKE then provisions a Network Load Balancer in front of the Service. The Network Load Balancer is aware of all nodes in your cluster and configures your VPC network's firewall rules to allow connections to the Service from outside the VPC network, using the Service's external IP address. You can assign a static external IP address to the Service. Visit Configuring Domain Names with Static IP Addresses for more information.

最佳答案

只需添加externalTrafficPolicy: Local

spec:
externalTrafficPolicy: Local
type: LoadBalancer

Packets sent to Services with Type=LoadBalancer are source NAT’d by default, because all schedulable Kubernetes nodes in the Ready state are eligible for load-balanced traffic. So if packets arrive at a node without an endpoint, the system proxies it to a node with an endpoint, replacing the source IP on the packet with the IP of the node (as described in the previous section).



引用
  • https://kubernetes.io/docs/tutorials/services/source-ip/#source-ip-for-services-with-typeloadbalancer
  • https://github.com/kubernetes/kubernetes/issues/10921
  • 关于kubernetes - 如何使用kubernetes服务从Google Network Load Balancer获取客户端IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62144912/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com