如何使用Gcloud / kubernetes来获取客户端的外部IP地址

我最近开始在gcloud上使用kubernetes,到目前为止它一直非常stream畅,但我似乎无法得到客户端/用户外部ip地址在我的应用程序使用wildfly(jsf)任何想法将不胜感激! 我使用以下命令公开我的窗格:

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

  • 1荚运行野蛮独立模式
  • 1 pod运行mod-cluster
  • 1荚运行postgres
  • 1 rc运行mod-cluster-replication控制器
  • 1暴露rc mod-cluster-replication控制器端口80
  • 1个gcloud负载均衡器

我使用kubernetes,gcloud,modcluster,wildfly基于closuresTicket-monster Kubernetes

我的build议(如果你的应用是端口80/443上的HTTP / HTTP)就是利用Ingress控制器,这个控制器基本上把服务公开为一个HTTP / HTTPs负载均衡器,在数据包中注入X-Forwarded-For。

这将显示源/客户端的IP地址。 请按照教程在这里X-Forwarded-For字段的详细信息

我刚刚使用该教程进行的示例调用:LB IP:130.211.10.191

容器内的Tcpdump:

$ 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 1.7版(刚刚在1.7.2中testing)使得这一切变得轻而易举。 只需在您的LoadBalancer服务中使用spec:externalTrafficPolicy:Local即可。 它将服务于港口80和443没有任何问题。 例如:

 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'