# Can not get client real ip in kubernetes on VM

**URL:** <https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369>\
**Category:** Questions\
**Tags:** kubernetes\
**Created:** [September 5, 2019, 2:04pm UTC](https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369 "2019-09-05T14:04:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HYUN\_SUK\_JUNG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hyun_suk_jung/32/1122_2.png) [@HYUN\_SUK\_JUNG](https://discuss.konghq.com/u/HYUN_SUK_JUNG)\
**Post date:** [September 5, 2019, 2:04pm UTC](https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369/1 "2019-09-05T14:04:32Z")

</div>

I want to attach client IP to custom-header via kong-custom-plugin and send it to backe-end.  
I tried several ways but could not get the actual Client IP.

Is there a way to know the actual client IP in the kong plugin or the back-end behind kong?

**1. Env**

- Openstack VM
- Kubernetes
- Kong(1.3.0) with DB(postgresql) and Ingress(0.5.0)
- stable chart

**2. Try**  
ngx.var.remote\_addr=10.220.5.1  
ngx.var.realip\_remote\_addr=10.220.5.1  
kong.client.get\_ip()=10.220.5.1

**real ip**  
cluster ip = 10.221.xx.xx  
pod ip = 10.220.1.XX  
real client ip = 10.25X.XX.XX

**3. X-Forwarded-For**  
I found that request send client IP to back-end by “X-Forwarded-For” header.  
However, X-Forwarded-For is a header value attached by ELB, and nginx and kong are simply forwarded.  
kong.conf  
real\_ip\_header = X-Forwarded-For  
real\_ip\_recursive = on  
trusted\_ips = 0.0.0.0/0,::/0

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [September 5, 2019, 3:43pm UTC](https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369/2 "2019-09-05T15:43:15Z")

</div>

Depending upon which Kubernetes distribution you are using, you might need to set ExternalTrafficPolicy:  
[https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/#preserving-the-client-source-ip](https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/#preserving-the-client-source-ip)

Are you using a TCP load balancer or HTTP load balancer?

---

<div class="post-metadata">

**Author:** ![HYUN\_SUK\_JUNG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hyun_suk_jung/32/1122_2.png) [@HYUN\_SUK\_JUNG](https://discuss.konghq.com/u/HYUN_SUK_JUNG)\
**Post date:** [September 6, 2019, 12:38am UTC](https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369/3 "2019-09-06T00:38:49Z")

</div>

Thank you for your help but ExternalTrafficPolicy is supported only in GCE.

I do not use Load Balancer and GCE  
I use kubernetes 1.15.0 and linux/amd64 openstack

“External Client requests” reaches “Back-End Service”(Pod on VM) through “Kong” (Pod on VM)  
In normal case, which method do I use for getting client real ip?

How can I get the client real ip at kong plugin?

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [September 6, 2019, 3:57pm UTC](https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369/4 "2019-09-06T15:57:35Z")

</div>

> [@HYUN\_SUK\_JUNG](#):
>
> In normal case, which method do I use for getting client real ip?

That depends on what is sitting in-front of Kong.  
Is there anything between Kong and the client?  
If not, then Kong should see the real header already.

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [September 6, 2019, 3:58pm UTC](https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369/5 "2019-09-06T15:58:46Z")

</div>

> [@HYUN\_SUK\_JUNG](#):
>
> How can I get the client real ip at kong plugin?

Use the `kong.client` module to get the IP address of the client: [https://docs.konghq.com/1.3.x/pdk/kong.client/](https://docs.konghq.com/1.3.x/pdk/kong.client/)

---

<div class="post-metadata">

**Author:** ![HYUN\_SUK\_JUNG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hyun_suk_jung/32/1122_2.png) [@HYUN\_SUK\_JUNG](https://discuss.konghq.com/u/HYUN_SUK_JUNG)\
**Post date:** [September 10, 2019, 9:17am UTC](https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369/6 "2019-09-10T09:17:28Z")

</div>

Wow! you are great!

I set ExternalTrafficPolicy on kong proxy.  
When sending a request to kong from a Linux client, I found that it returned an abnormal IP address of the proxy server(? 168.xxx.xxx.xxx). I use proxy environment.  
However, when the client requests from Windows, the client’s actual IP value is returned.  
I have not figured out why Linux and Window return different IPs.  
I gave up using the Linux client for development and decided to use Window.

Thank you again for the power of @hbaqdi.

---

<div class="post-metadata">

**Author:** ![hbagdi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hbagdi/32/562_2.png) [@hbagdi](https://discuss.konghq.com/u/hbagdi)\
**Post date:** [February 12, 2020, 5:12pm UTC](https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369/7 "2020-02-12T17:12:52Z")

</div>

We have a guide on how to setup various properties in Kong and Kubernetes to preserve the real client IP address:

> <https://github.com/Kong/kubernetes-ingress-controller/blob/master/docs/guides/preserve-client-ip.md>

---

<div class="post-metadata">

**Author:** ![HYUN\_SUK\_JUNG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/hyun_suk_jung/32/1122_2.png) [@HYUN\_SUK\_JUNG](https://discuss.konghq.com/u/HYUN_SUK_JUNG)\
**Post date:** [February 13, 2020, 12:13am UTC](https://discuss.konghq.com/t/can-not-get-client-real-ip-in-kubernetes-on-vm/4369/8 "2020-02-13T00:13:12Z")

</div>

Thank you for your guide
