简体   繁体   中英

How to get the clients external ip, im using Gcloud/kubernetes

i recently started working with kubernetes on gcloud, its been pretty smooth so far, but i cant seem to get the clients/user external ip address on my app using wildfly(jsf) any ideas would be appreciated! I expose my pod using the following command:

kubectl expose rc modcluster-replication-controller --name=modcluster --type="LoadBalancer"

  • 1 pod running wildfly standalone mode
  • 1 pod running mod-cluster
  • 1 pod running postgres
  • 1 rc running mod-cluster-replication controler
  • 1 expose rc mod-cluster-replication controler port 80
  • 1 gcloud loadbalancer

Im using kubernetes, gcloud, modcluster, wildfly based off Ticket-monster Kubernetes

My suggestion (if your application is HTTP/HTTPs on ports 80/443) is to take advantage of the Ingress controller which basically expose the services as an HTTP/HTTPs load balancer that injects the X-Forwarded-For in the packets.

This will reveal the source/client's IP address. Please follow the tutorial Details on the X-Forwarded-For field are available here

Sample call that I just tested with that tutorial: LB IP: 130.211.10.191

Tcpdump inside the container:

$ tcpdump -n -l -w - | strings

Output:
Host: 130.211.10.191
Cache-Control: max-age=0
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; CrOS x86_64 7978.74.0) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/50.0.2661.103 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
If-None-Match: "574da256-264"
If-Modified-Since: Tue, 31 May 2016 14:40:22 GMT
X-Cloud-Trace-Context:
6b36a7d93d60dc6921417796255466d5/14093000126457324029
Via: 1.1 google
X-Forwarded-For: 81.47.XXX.XXX, 130.211.10.191    # the IP starting with
81. is my local IP
X-Forwarded-Proto: http
Connection: Keep-Alive
JxHTTP/1.1 304 Not Modified

k8s version 1.7 (just tested in 1.7.2) makes this a breeze. Just use spec:externalTrafficPolicy:Local in your LoadBalancer Service. It will serve port 80 and 443 without any issue. For instance:

apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
  - port: 443
    protocol: TCP
    targetPort: 443
    name: https
  selector:
    app: myapp
    role: myrole
  type: LoadBalancer
  loadBalancerIP: 104.196.208.195
  externalTrafficPolicy: Local 
kubectl describe svc servicename | grep 'LoadBalancer Ingress'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM