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

1

6

I'm getting the node IP address instead of the client IP. Is it possible to get the client IP with a service of type LoadBalancer? Or will I need to use a ingress controller?

apiVersion: v1
kind: Service
metadata:
  name: app-svc
  labels:
    name: app-svc
    environment: dev
spec:
  type: LoadBalancer
  loadBalancerIP: XXX.XXX.XXX.XXX
  ports:
    - name: http-port
      port: 80
      targetPort: 80
      protocol: TCP
  selector:
      name: app-deploy
Eteocles answered 9/9, 2018 at 20:49 Comment(0)
M
17

You do not need any Ingress controller. However it is required to set the value of the spec.externalTrafficPolicy Service field to "Local" (the default is "Cluster") in Microsoft Azure.

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: LoadBalancer
  externalTrafficPolicy: Local
  ...

See Using source IP.

Mozell answered 10/9, 2018 at 8:18 Comment(1)
for fast experiment I use kubectl edit svs/pentagon-balancer and just change Cluster to Local. Now I can see real user IP in X-Forwarded-For header on backendConsequential

© 2022 - 2024 — McMap. All rights reserved.