nginx - oauth2-proxy 身份验证在 kubernetes 集群上调用缓慢,带有 nginx 入口的身份验证注释
问题描述
我们使用本页描述的方法保护了 K8S 集群上的一些服务。具体来说,我们有:
nginx.ingress.kubernetes.io/auth-url: "https://oauth2.${var.hosted_zone}/oauth2/auth"
nginx.ingress.kubernetes.io/auth-signin: "https://oauth2.${var.hosted_zone}/oauth2/start?rd=/redirect/$http_host$escaped_request_uri"
设置要保护的服务,我们按照本教程在每个集群中只部署一个 oauth2_proxy。我们设置了 2 个代理,它们都具有关联性,要放置在与 nginx 入口相同的节点上。
$ kubectl get pods -o wide -A | egrep "nginx|oauth"
infra-system wer-exp-nginx-ingress-exp-controller-696f5fbd8c-bm5ld 1/1 Running 0 3h24m 10.76.11.65 ip-10-76-9-52.eu-central-1.compute.internal <none> <none>
infra-system wer-exp-nginx-ingress-exp-controller-696f5fbd8c-ldwb8 1/1 Running 0 3h24m 10.76.14.42 ip-10-76-15-164.eu-central-1.compute.internal <none> <none>
infra-system wer-exp-nginx-ingress-exp-default-backend-7d69cc6868-wttss 1/1 Running 0 3h24m 10.76.15.52 ip-10-76-15-164.eu-central-1.compute.internal <none> <none>
infra-system wer-exp-nginx-ingress-exp-default-backend-7d69cc6868-z998v 1/1 Running 0 3h24m 10.76.11.213 ip-10-76-9-52.eu-central-1.compute.internal <none> <none>
infra-system oauth2-proxy-68bf786866-vcdns 2/2 Running 0 14s 10.76.10.106 ip-10-76-9-52.eu-central-1.compute.internal <none> <none>
infra-system oauth2-proxy-68bf786866-wx62c 2/2 Running 0 14s 10.76.12.107 ip-10-76-15-164.eu-central-1.compute.internal <none> <none>
但是,一个简单的网站加载通常需要大约 10 秒,而安全服务上不存在代理注释则需要 2-3 秒。
我们通过添加添加proxy_cache
到auth.domain.com
托管我们代理的服务中
"nginx.ingress.kubernetes.io/server-snippet": <<EOF
proxy_cache auth_cache;
proxy_cache_lock on;
proxy_ignore_headers Cache-Control;
proxy_cache_valid any 30m;
add_header X-Cache-Status $upstream_cache_status;
EOF
但这也没有改善延迟。我们仍然看到所有 HTTP 请求都触发了代理中的日志行。奇怪的是,只有一些请求需要 5 秒。
我们不确定是否: - 代理将每个请求转发给 oauth 提供者 (github) 或 - 缓存身份验证
我们使用 cookie 身份验证,因此,理论上,oauth2_proxy应该只是解密 cookie,然后向 nginx 入口返回 200。因为它们都在同一个节点上,所以应该很快。但事实并非如此。有任何想法吗?
编辑 1
我进一步分析了情况。在浏览器中访问我的身份验证服务器https://oauth2.domain.com/auth
并复制copy for curl
我发现的请求:
- 从我的本地机器(通过 curl)对我的 oauth 服务器运行 10.000 个查询非常快
- 在具有相同 curl 的 nginx 入口上运行 100 个请求很慢
- 将curl中的主机名替换为auth服务的集群IP,性能大幅提升
- 将注释设置为
nginx.ingress.kubernetes.io/auth-url: http://172.20.95.17/oauth2/auth
(例如设置主机 == 集群 IP)使 GUI 加载按预期(快速) - 无论 curl 是在 nginx-ingress 上还是在任何其他 pod(例如测试 debian)上运行,结果都是一样的
编辑 2
我发现一个更好的解决方法是将注释设置为以下
nginx.ingress.kubernetes.io/auth-url: "http://oauth2.infra-system.svc.cluster.local/oauth2/auth"
nginx.ingress.kubernetes.io/auth-signin: "https://oauth2.domain.com/oauth2/start?rd=/redirect/$http_host$escaped_request_uri"
这auth-url
是入口使用用户的 cookie 查询的内容。因此,oauth2 服务的本地 DNS 与外部 dns 名称相同,但没有 SSL 通信,因为它是 DNS,所以它是永久的(而集群 IP 不是)
解决方案
鉴于不太可能有人想出为什么会发生这种情况,我会回答我的解决方法。
我发现的一个修复是将注释设置为以下
nginx.ingress.kubernetes.io/auth-url: "http://oauth2.infra-system.svc.cluster.local/oauth2/auth"
nginx.ingress.kubernetes.io/auth-signin: "https://oauth2.domain.com/oauth2/start?rd=/redirect/$http_host$escaped_request_uri"
这auth-url
是入口使用用户的 cookie 查询的内容。因此,oauth2 服务的本地 DNS 与外部 dns 名称相同,但没有 SSL 通信,因为它是 DNS,所以它是永久的(而集群 IP 不是)
推荐阅读
- mysql - 数据库备份表单 RDS 到 S3 保存空的备份 sql.gz 文件 - 如何进行故障排除
- c# - 使用离散傅里叶变换计算无限周期谱
- javascript - 使用 Javascript 通过 FullCalendar 将事件添加到 Google 日历
- python - 从子级访问 mixin 成员变量(列)
- mysql - 获取同时满足两种语言过滤器的所有用户
- matlab - 来自 Fortran 的 Matlab - 传输大矩阵的问题
- ios - 无法使用捆绑标识符“com.apple.test.WebDriverAgentRunner-Runner”启动进程
- asp.net-core - AspNetCore WebApi ODataController 无法反序列化 JSON 有效负载
- odbc - Eiffel 18.07 试图从示例中运行 ODBC 连接
- ios - Swift:通过在其超级视图窗口中的平移手势更改(翻译)UIView 位置