traefik透传用户端的真实ip(基于k8s平台)

实践是知识的母亲,知识是生活的明灯。这篇文章主要讲述traefik透传用户端的真实ip(基于k8s平台)相关的知识,希望能为你提供帮助。
traefik透传用户端的真实ip(基于k8s平台)参考链接:
https://www.jianshu.com/p/15f3498a7fad
https://www.bboy.app/2020/04/16/traefik%E5%90%8E%E7%AB%AF%E5%BA%94%E7%94%A8%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E7%9C%9F%E5%AE%9Eip/
https://doc.traefik.io/traefik/v2.0/routing/entrypoints/#forwarded-header

traefik透传用户端的真实ip(基于k8s平台)

文章图片
![image-20210826221205651]
当一次http请求进来,经过elb,elb日志里面收到的使用户的真实ip也就是$remote_addr不妨叫ip0,elb把请求转发到后端的api网关traefik,此时traefik日志传入的是elb的ip,不妨叫ip1,traefik加载集群的ingress规则转发到k8s集群内部前端pod,前端pod调用后端服务pod接口,也是采用内部域名ingress的方式转发,所以后端pod日志里传入的ip不妨叫ip2,实际上是traefik实例的ip,我们基于可信和问题定位,需要在后端业务pod日志里传入客户端真实的ip,使得多级中间件代理变的透明,所以需要开启内网透传
elb的内网透传配置
server { listen 80; server_name ip.test.com; location / { proxy_pass http://10.0.0.6; #http1.1版本支持tcp keep alive长连接 proxy_http_version 1.1; #把客户端请求头传进来 Proxy_set_header Host $http_host; #设置透传代理参数X-Forwarded-For proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

traefik的内网透传配置
EntryPoints 是 Traefik 的网络入口点。它们定义了接收请求的端口(无论是 HTTP 还是 TCP)
traefik透传用户端的真实ip(基于k8s平台)

文章图片

traefik透传用户端的真实ip(基于k8s平台)

文章图片

这是entrypoint配置的可用选项列表的完整参考
traefik透传用户端的真实ip(基于k8s平台)

文章图片

安全模式:信任来自elb ip的转发标头
apiVersion: v1 data: traefik.toml: | insecureSkipVerify = true defaultEntryPoints = ["http","https"] #entrypoints配置采用的是数组的格式定义,我们定义了两个entropoint 一个是http一个是https [entryPoints] [entryPoints.http] address = ":80" #开启http方式的的X-Forwarded-For参数,192.168.1.7ip应该是你elb的真实ip,可以是多个elb实例ip [entryPoints.http.forwardedHeaders] trustedIPs = ["127.0.0.1/32", "192.168.1.7"] #配置http强制跳转https(重定向) [entryPoints.https.redirect] entryPoint = "http" [entryPoints.https] address = ":443" #开启https方式的的X-Forwarded-For参数,192.168.1.7ip应该是你elb的真实ip,可以是多个elb实例ip [entryPoints.https.forwardedHeaders] trustedIPs = ["127.0.0.1/32", "192.168.1.7"] [entryPoints.https.tls] [[entryPoints.https.tls.certificates]] certFile = "/ssl/tls.crt" keyFile = "/ssl/tls.key"

【traefik透传用户端的真实ip(基于k8s平台)】不安全模式:始终信任转发的标头(如果在内网可以配置,外网就有sql注入的风险)
apiVersion: v1 data: traefik.toml: | insecureSkipVerify = true defaultEntryPoints = ["http","https"] #entrypoints配置采用的是数组的格式定义,我们定义了两个entropoint 一个是http一个是https [entryPoints] [entryPoints.http] address = ":80" #开启http方式的的X-Forwarded-For参数,192.168.1.7ip应该是你elb的真实ip,可以是多个elb实例ip [entryPoints.http.forwardedHeaders] insecure = true #配置http强制跳转https(重定向) [entryPoints.https.redirect] entryPoint = "http" [entryPoints.https] address = ":443" #开启https方式的的X-Forwarded-For参数,192.168.1.7ip应该是你elb的真实ip,可以是多个elb实例ip [entryPoints.https.forwardedHeaders] insecure = true [entryPoints.https.tls] [[entryPoints.https.tls.certificates]] certFile = "/ssl/tls.crt" keyFile = "/ssl/tls.key"


    推荐阅读