How to get client ip from Google Network Load Balancer with kubernetes service

RammusXu

I created a kubernetes service in GKE with type:LoadBalancer.

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

It's a nginx service and try to get origin client IP. like

        location / {
            echo $remote_addr;
            echo $http_x_forwarded_for;   
        }

But the result will get:

10.140.0.97

$remote_addr is like inside kubernetes IP.

$http_x_forwarded_for is empty.

I don't know why this is not like document said.

What I read

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.

RammusXu

Just add 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).

Reference

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Access external client IP from behind Google Compute Engine network load balancer

AWS Network Load Balancer unable to get client IP

Client IP when behind AWS network load balancer from Python

Kubernetes load balancer service packet source IP

How can I get the client IP address in gRPC from a load balancer that adds x-real-ip and x-forward-for to the header?

How to get the URL or IP of a load balancer from DNS of a website?

How to get client IP address from inside a Azure Kubernetes with a LoadBalancer service

Google Cloud Kubernetes Load Balancer Service: ERR_CONNECTION_REFUSED

Migrating an external Kubernetes service from load balancer to ingress controller

How to specify static IP address for Kubernetes load balancer?

How to configure google load balancer to route to cloud run service using url mask but strip service from url

EKS - Network Load Balancer exposes internal IP - how to expose it to the Internet?

How do I find out the external IP of a Load Balancer service?

Getting Original Ip from Google Load Balancer with backend bucket and Cloudflare

Are these IP from Google's Cloud Load Balancer and if it is normal?

Kubernetes load balancer not getting a public ip

Kubernetes External Load Balancer Service on DigitalOcean

Not able to add ipv6 type Load Balancer IP in kubernetes service

Restrict service to internal network BUT load balancer?

How do i get client ip with kubernetes?

troubleshoot Google kubernetes load balancer unhealthy nodes

HTTPS Load Balancer to expose a Workload on Google Kubernetes

GCP Couldn't reach Kubernetes External Load Balancer IP from outside

Google Cloud TCP Load Balancer forward ip

ASP.Net: Get IP Address of VMware client in load balancer environment

If I have a public load balancer, how does direct service-to-service communication get load balanced?

How does a Network Load Balancer work with an Ingress Controller in Kubernetes (AWS/EKS)?

Access client's IP address (not the load balancer's) from Meteor, with Modulus

Laravel Request returning load balancer ip instead of client ip