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

6/28/2016

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

-- PaulMB
containers
docker
gcloud
kubernetes
networking

3 Answers

8/2/2017

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 
-- Nestor Urquiza
Source: StackOverflow

9/18/2017
kubectl describe svc servicename | grep 'LoadBalancer Ingress'
-- Shivam Tiwari
Source: StackOverflow

6/28/2016

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
-- DoiT International
Source: StackOverflow