kube-lego icon indicating copy to clipboard operation
kube-lego copied to clipboard

X-Forwarded-For & X-Real-IP = Addr of GKE Ingress Controller

Open DocBradfordSoftware opened this issue 10 years ago • 26 comments

When using the example for NGINX on GCE, everything seems to work, but I am not getting the headers from the original client. It appears that the values for X-Real-IP & X-Forwarded-For are the private ip addr of the gke ingress controller.

DocBradfordSoftware avatar Nov 15 '16 19:11 DocBradfordSoftware

@DocBradfordSoftware if you are using NGINX example then you are actually using Nginx Ingress Controller, not GCE Ingress Controller. But traffic is forwarded to your Nginx Ingress Controller via GCE load balancer, I suppose. If so, take a look at Proxy protocol usage for Nginx Ingress Controller.

tyranron avatar Nov 15 '16 20:11 tyranron

At aws I would add this to my config map

use-proxy-protocol: "true"

If I try that with google container engine I get this error.

while reading PROXY protocol, client: 10.0.0.1, server: 0.0.0.0:443
2016-11-15T21:24:41.460205161Z 2016/11/15 21:24:41 [error] 198#198: *69 broken header

This is probably a gke issue, so I will head over there.

DocBradfordSoftware avatar Nov 15 '16 21:11 DocBradfordSoftware

have the same error once the use-proxy-protocol is enabled. Does it means that we have to setup haproxy to use it properly if the nginx service is set as NodePort?

kenng avatar Feb 06 '17 05:02 kenng

GCE Load Balancers don't support proxy protocol. To get the real IP addresses with an nginx ingress, you have to rely on this alpha feature introduced in kubernetes 1.4:

https://kubernetes.io/docs/user-guide/load-balancer/#loss-of-client-source-ip-for-external-traffic

Closing this as it's not a kube-lego bug/limitation. Let me know how it goes...

simonswine avatar Feb 06 '17 18:02 simonswine

Work like a charm! need to set the type as LoadBalancer. @simonswine so there is no way to avoid using LoadBalancer if we need to forward the client IP to the ingress? As the Network Load Balancing rule is kinda expensive for small project.

I have already disabled the httpLoadBalancing, still saw one loadbalancing rule got created once the type type is set as LoadBalancer. gcloud container clusters describe my-project

addonsConfig:
  httpLoadBalancing:
    disabled: true

kenng avatar Feb 11 '17 07:02 kenng

@kenng have you found a solution? My nginx ingress still shows 127.0.0.1 only as client ip

alex88 avatar Jun 10 '17 23:06 alex88

@alex88, per the reference above, you need to add an annotation to the nginx controller service

apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: nginx-ingress
  annotations:
    service.beta.kubernetes.io/external-traffic: OnlyLocal
spec:
  type: LoadBalancer
  ports:
  - port: 80
    name: http
  - port: 443
    name: https
  selector:
    app: nginx

DocBradfordSoftware avatar Jun 11 '17 19:06 DocBradfordSoftware

@DocBradfordSoftware unfortunately that didn't help. :( I've a domain pointing to the GCE load balancer IP with an A record, if I use curl with the domain name, nginx logs shows: 127.0.0.1 - [127.0.0.1], if I use curl with the load balancer IP address it correctly shows my client IP :/

alex88 avatar Jun 11 '17 19:06 alex88

I used the echo service in the example. and got these values X-Forwarded-For=70.197.xxx.xxx X-Real-IP=70.197.xxx.xxx

double checking with whatsmyip http://www.whatsmyip.org/ i got the same ip: 70.197.xxx.xxx

DocBradfordSoftware avatar Jun 11 '17 20:06 DocBradfordSoftware

Lucky you :) I still can't get my real ip if I use a domain to connect to the load balancer

alex88 avatar Jun 11 '17 21:06 alex88

I am not sure if what i am doing is correct, but the way I find my ip to hook up to the dns record, is that I go to gcloud console networking -> loadbalancing. there should be one instance, click on that and it will show the external ip. Also if you look at the External IP Addresses tab, there should be one IP that points to a Forwarding Rule. Clicking on the forwarding rule, should bring you to the loadbalancing page above.

DocBradfordSoftware avatar Jun 11 '17 21:06 DocBradfordSoftware

@DocBradfordSoftware I've found the discriminant, if I use port 443, I get 127.0.0.1 in the nginx ingress logs, if I use port 80 I get the real client IP

alex88 avatar Jun 12 '17 13:06 alex88

@alex88 Did you find a solution to your issue? I'm seeing the same problem. What version of the Nginx Ingress are you on?

artushin avatar Jun 19 '17 20:06 artushin

@artushin nope, still the same issue, maybe it's because of the ports that the tcp load balancer can use the proxy protocol with, I don't really know.

alex88 avatar Jun 19 '17 20:06 alex88

Just upgraded to 0.9.0-beta.8 (https://github.com/kubernetes/ingress/releases). Looks good now.

Looks like it was probably this: https://github.com/kubernetes/ingress/issues/233

artushin avatar Jun 19 '17 20:06 artushin

@artushin Sorry forgot to mention I was using beta-5, I'll try with the beta 8. Anway, do you have any special config in the service? Like the service.beta.kubernetes.io/external-traffic: OnlyLocal annotation?

alex88 avatar Jun 19 '17 20:06 alex88

I do, but it's a one node dev cluster, and I'm running the nginx pods in a daemonset, so it would probably work even without it. But yeah, based on the k8s docs, if you have nodes that are not running an nginx pod, you should use that annotation if you need source IPs.

artushin avatar Jun 19 '17 20:06 artushin

Oh yeah it works! That's awesome! :D

alex88 avatar Jun 19 '17 20:06 alex88

Now it's just a matter of having nginx trust the forwarded ip and that's it :)

alex88 avatar Jun 19 '17 21:06 alex88

Did somebody also get it working when using a GCLB through an ingress in front of Nginx? The service.beta.kubernetes.io/external-traffic: OnlyLocal doesn't seem to work, because the service is just a NodePort.

Sorry for using this thread, but best way to get a solution :-)

bviolier avatar Jul 25 '17 18:07 bviolier