How to access client IP of an HTTP request from Google Container Engine?

家住魔仙堡 提交于 2019-12-01 00:05:21

I assume you set up your service by setting the service's type to LoadBalancer? It's an unfortunate limitation of the way incoming network-load-balanced packets are routed through Kubernetes right now that the client IP gets lost.

Instead of using the service's LoadBalancer type, you could set up an Ingress object to integrate your service with a Google Cloud HTTP(s) Load Balancer, which will add the X-Forwarded-For header to incoming requests.

If anyone gets stuck on this there is a better approach. You can use the following annotations depending on your kubernetes version:

service.spec.externalTrafficPolicy: Local 

on 1.7

or

service.beta.kubernetes.io/external-traffic: OnlyLocal

on 1.5-1.6

before this is not supported

source: https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/

note that there are caveats: https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/#caveats-and-limitations-when-preserving-source-ips

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!