gpt4 book ai didi

docker - Kubernetes 上的 Traefik 在传入连接上错误的客户端 IP

转载 作者:行者123 更新时间:2023-12-02 11:17:07 34 4
gpt4 key购买 nike

我在 Kubernetes 上运行 Traefik,并使用以下配置创建了一个 Ingress:

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: whitelist-ingress
annotations:
kubernetes.io/ingress.class: traefik
traefik.frontend.rule.type: PathPrefix
traefik.ingress.kubernetes.io/whitelist-source-range: "10.10.10.10/32, 10.10.2.10/23"
ingress.kubernetes.io/whitelist-x-forwarded-for: "true"
traefik.ingress.kubernetes.io/preserve-host: "true"
spec:
rules:
- host:
http:
paths:
- path: /endpoint
backend:
serviceName: endpoint-service
servicePort: endpoint-port
---

当我在上述端点上执行 POST 时,Traefik 记录传入 IP 为 172.16.0.1,因此未触发我的白名单。
做一个 ifconfig 我看到 IP 属于 Docker
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
inet 172.26.0.1 netmask 255.255.0.0 broadcast 172.26.255.255

如何保留原始 IP 而不是 docker IP?

编辑

Traefik 作为 LoadBalancer 公开,端口是 443 通过 SSL

这是它的yml配置
---
kind: Service
apiVersion: v1
metadata:
name: traefik
annotations: {}
# service.beta.kubernetes.io/aws-load-balancer-internal: 0.0.0.0/0
spec:
selector:
k8s-app: traefik-ingress
ports:
- protocol: TCP
port: 80
targetPort: 80
name: http
- protocol: TCP
port: 443
targetPort: 443
name: https
type: LoadBalancer
externalTrafficPolicy: Local
externalIPs:
- <machine-ip>
---

kind: Deployment
apiVersion: apps/v1
metadata:
name: traefik-ingress-controller
namespace: default
labels:
k8s-app: traefik-ingress
spec:
replicas: 2
selector:
matchLabels:
k8s-app: traefik-ingress
template:
metadata:
labels:
k8s-app: traefik-ingress
name: traefik-ingress
spec:
hostNetwork: true
serviceAccountName: traefik-ingress-controller
terminationGracePeriodSeconds: 35
volumes:
- name: proxy-certs
secret:
secretName: proxy-certs
- name: traefik-configmap
configMap:
name: traefik-configmap
containers:
- image: traefik:1.7.6
name: traefik-ingress
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 200m
memory: 900Mi
requests:
cpu: 25m
memory: 512Mi
livenessProbe:
failureThreshold: 2
httpGet:
path: /ping
port: 80
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 5
readinessProbe:
failureThreshold: 2
httpGet:
path: /ping
port: 80
scheme: HTTP
periodSeconds: 5
volumeMounts:
- mountPath: "/ssl"
name: "proxy-certs"
- mountPath: "/config"
name: "traefik-configmap"
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
- name: dashboard
containerPort: 8080
args:
- --logLevel=DEBUG
- --configfile=/config/traefik.toml
---

如您所见,这里是 kubectl get svc 的输出
traefik                       LoadBalancer   10.100.116.42    <machine-ip>     80:30222/TCP,443:31578/TCP   <days-up>

请注意,Traefik 运行在单节点 kubernetes 集群中(主节点/工作节点在同一节点上)。

最佳答案

在 LoadBalancer 服务类型文档中 ssl support on aws您可以阅读以下声明:

HTTP and HTTPS will select layer 7 proxying: the ELB will terminate the connection with the user, parse headers and inject the X-Forwarded-For header with the user’s IP address (pods will only see the IP address of the ELB at the other end of its connection) when forwarding requests.



因此,如果您向 traeffik 服务添加以下注释:
service.beta.kubernetes.io/aws-load-balancer-backend-protocol: https

它应该适用于 ingress.kubernetes.io/whitelist-x-forwarded-for: "true"入口配置中存在的注释和转发的 header 由 aws 负载均衡器添加。

免责声明:我尚未测试该解决方案。

问候。

关于docker - Kubernetes 上的 Traefik 在传入连接上错误的客户端 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54366967/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com