kubernetes - 如何在 pod 上获取客户端的公共 IP?
问题描述
我有一个基于 Python-Flask 的应用程序。我想在客户访问我的入口端点时获取他们的公共 IP。
我已经尝试将 externalTrafficPolicy 更改为 Local 和 Cluster。
我的 Pod YAML 文件
apiVersion: v1
kind: Pod
metadata:
labels:
run: webplatform
name: webplatform-deployment-6d68c99fc7-xlb8j
namespace: prod
spec:
containers:
- command:
- python
- /app/app.py
envFrom:
- secretRef:
name: webplatform-secret
optional: false
image: docker.fuchicorp.com/webplatform-prod:0.5
imagePullPolicy: Always
name: webplatform-container
imagePullSecrets:
- name: nexus-creds
serviceAccount: webplatform-service-account
serviceAccountName: webplatform-service-account
我的服务 YAML 文件
apiVersion: v1
kind: Service
metadata:
name: webplatform-service
namespace: prod
spec:
externalTrafficPolicy: Cluster
ports:
- nodePort: 32744
port: 7101
protocol: TCP
targetPort: 5000
selector:
run: webplatform
sessionAffinity: None
type: NodePort
我的 Ingress 资源 YAML 文件
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
certmanager.k8s.io/cluster-issuer: letsencrypt-fuchicorp-prod
kubernetes.io/ingress.class: nginx
generation: 2
name: ingress-webplaform
namespace: prod
spec:
rules:
- host: academy.fuchicorp.com
http:
paths:
- backend:
serviceName: webplatform-service
servicePort: 7101
tls:
- hosts:
- academy.fuchicorp.com
secretName: letsencrypt-sec-webplatform-prod
当我看到日志时,我在日志上看到了 Ingress-Controllers IP
INFO: 10.16.0.16 - - [28/Sep/2019 20:06:12] "GET / HTTP/1.1" 200 -
解决方案
TL;DR
client IP should be available via the X-Forwarded-For
HTTP header
It should be provided by the load balancer (the ingress controller). Assuming your cluster is running on the cloud (aws, gcp, etc.), you get the client IP via the X-Forwarded-For
HTTP header.
If its an on-prem k8s cluster (you run it on your own private cloud/ local machine), configure your load-balancer to do that- http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream
推荐阅读
- python - 在 Django 中从 Python 调用 Ajax
- mysql - 有没有办法在按 FIELD 排序时对 MySQL 中的两个行值赋予相等的权重?
- c# - 奇怪的轮子统一旋转
- stata - 有没有办法解决Stata的r(601),同时循环导入的excel文件的追加?
- javascript - 降低在反应中跟踪每个文件上传进度的性能常量
- pine-script - 价格框的 Pine 脚本
- karate - 我可以在空手道中使用 Scenario Outline 读取 2 个 .csv 文件吗
- database - 使用基于计时器的 gamma 脚本将数据从 Cogent Datahub 记录到数据库
- asp.net-core - 在不引导整个站点的情况下运行 NSwag
- python - 会议录音记录和总结