Skip to content

Get the IP address of the client #7607

Closed
@facundomedica

Description

@facundomedica

This is a question.

How can I get the client's IP address from inside my container?

I'm printing the requests that arrive to my container:

POST /shared/login HTTP/1.1
Host: primeratest.REDACTED.co
Accept-Encoding: gzip
Content-Length: 364
Content-Type: application/x-www-form-urlencoded
User-Agent: okhttp/3.8.1
X-B3-Sampled: 1
X-B3-Spanid: 5dc565d1235a089c
X-B3-Traceid: 5dc565d1235a089c
X-Envoy-Internal: true
X-Forwarded-For: 10.xxx.0.4 // this is an internal IP
X-Forwarded-Proto: https
X-Request-Id: e78ed2dc-9eb8-934b-b311-75e1bce9869

I have a gateway and a VirtualService:

---
kind: Service
apiVersion: v1
metadata:
  name: api
  labels:
    app: api
spec:
  selector:
    app: api
  ports:
    - port: 5000
      protocol: TCP
      targetPort: 5000
      name: http
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: api-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    tls:
      mode: SIMPLE
      serverCertificate: /etc/istio/ingressgateway-certs/tls.crt
      privateKey: /etc/istio/ingressgateway-certs/tls.key
    hosts:
    - "primeratest.redacted.co"
  - port:
      number: 80
      name: http
      protocol: HTTP
    tls:
      httpsRedirect: true
    hosts:
    - "primeratest.redacted.co"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: api
spec:
  hosts:
  - "primeratest.redacted.co"
  gateways:
  - api-gateway
  http:
  - match:
    - uri:
        prefix: /
    route:
    - destination:
        host: api
        port:
          number: 5000

Activity

dermyrddin

dermyrddin commented on Aug 3, 2018

@dermyrddin

Just meet the same issue.
Found some suggestions for adding "use_remote_address": true to envoy config but can't figure out how to do that...

facundomedica

facundomedica commented on Aug 3, 2018

@facundomedica
Author

I found exactly the same, but I think that that's something internal, I don't now how to access it either

ijsnellf

ijsnellf commented on Aug 3, 2018

@ijsnellf
Contributor
facundomedica

facundomedica commented on Aug 3, 2018

@facundomedica
Author

One solution could be use Cloudflare as a Proxy, which adds some useful headers and also adds the client's IP to the x-forwarded-for header.

POST /shared/signup HTTP/1.1
Host: primeratest.redacted.co
Accept: */*
Accept-Encoding: gzip
Accept-Language: en-us
Cf-Connecting-Ip: XXX.XXX.XXX.XXX // my client's IP address
Cf-Ipcountry: AR
Cf-Ray: 444b6cef4d4d2eab-MIA
Cf-Visitor: {"scheme":"https"}
Content-Length: 245
Content-Type: multipart/form-data; boundary=53B4D475-D9FE-44F3-B8DE-B471B27DD107
External-Ip: testing
Testing: facundo2
User-Agent: CocoaRestClient/23 CFNetwork/901.1 Darwin/17.6.0 (x86_64)
X-B3-Sampled: 1
X-B3-Spanid: 0e298cff6925ccd3
X-B3-Traceid: 0e298cff6925ccd3
X-Envoy-External-Address: 10.128.0.2
X-Forwarded-For: XXX.XXX.XXX.XXX, 10.128.0.2 // being the first one my client's public IP
X-Forwarded-Proto: https
X-Request-Id: b8b4910b-6273-9b9e-868f-a73e1ee29c1d
dermyrddin

dermyrddin commented on Aug 4, 2018

@dermyrddin

Unfortunately, I can't use Cloudflare

swistaczek

swistaczek commented on Oct 4, 2018

@swistaczek

Is there any solution for this problem?

dermyrddin

dermyrddin commented on Oct 4, 2018

@dermyrddin

@swistaczek you can use NLB instead of ELB #8337

But there is an issue in K8S if you run several clusters in same AWS region kubernetes/kubernetes#69264

changed the title [-]Get the IP address from the client[/-] [+]Get the IP address of the client[/+] on Oct 4, 2018
facundomedica

facundomedica commented on Oct 4, 2018

@facundomedica
Author

I've changed the issue's title to make it more clear

Dagon-

Dagon- commented on Oct 12, 2018

@Dagon-

I'm just running into this as well. According to this:
#7679
Setting externalTrafficPolicy: Local in the ingress-gateway service will cause the real client IP to be passed through.

That's causing connectivity issues for me though.

jonasdkhansen

jonasdkhansen commented on Oct 23, 2018

@jonasdkhansen

I also have this problem. I have a GKE cluster, and I'm using the istio ingress as loadbalancer from outside, and sending the traffic to a simple nginx pod. But the nginx is getting the internal istio ingress ip, and not the real users ip.

jonasdkhansen

jonasdkhansen commented on Oct 23, 2018

@jonasdkhansen

I got it working with adding externalTrafficPolicy: Local to the istio-ingressgateway service, and then in my nginx conf i added a header with $http_x_forwarded_for :)

jaepyoung

jaepyoung commented on Oct 24, 2018

@jaepyoung

I have one container of node.js and it is not getting client ip. I am using EKS. Is there any workaround to fix this issue? externalTrafficPolicy: Local doesn't work for me. Do I need to set up another thing?

songrgg

songrgg commented on Nov 29, 2018

@songrgg
Contributor

Thanks, externalTrafficPolicy: Local works for me, I hosted Kubernetes on Tencent Cloud.

27 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @swistaczek@ayj@psaia@dermyrddin@cameronbraid

        Issue actions

          Get the IP address of the client · Issue #7607 · istio/istio