繁体   English   中英

oauth2-proxy 身份验证在 kubernetes 集群上调用缓慢,带有 nginx 入口的身份验证注释

[英]oauth2-proxy authentication calls slow on kubernetes cluster with auth annotations for nginx ingress

我们使用本页描述的方法保护了 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 秒。

我们向auth.domain.com服务添加了proxy_cache ,该服务通过添加来托管我们的代理

        "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我发现:

  1. 从我的本地机器(通过 curl)对我的 oauth 服务器运行 10.000 个查询非常快
  2. 在具有相同 curl 的 nginx 入口上运行 100 个请求很慢
  3. 将curl中的主机名替换为auth服务的集群IP,性能大幅提升
  4. 将注释设置为nginx.ingress.kubernetes.io/auth-url: http://172.20.95.17/oauth2/auth集群 IP 设置
  5. 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 查询的内容。 Hence, a local DNS of the oauth2 service is the same as the external dns name, but without the SSL communication and since it's DNS, it's permanent (while the cluster IP is not)

鉴于不太可能有人想出为什么会发生这种情况,我会回答我的解决方法。

我发现的一个修复是将注释设置为以下

  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 查询的内容。 Hence, a local DNS of the oauth2 service is the same as the external dns name, but without the SSL communication and since it's DNS, it's permanent (while the cluster IP is not)

在我看来,在以下情况下,您会观察到响应时间的延迟增加:
nginx.ingress.kubernetes.io/auth-url: "https://oauth2.${var.hosted_zone}/oauth2/auth"
设置,因为auth server URL 解析为外部服务(在这种情况下,负载均衡器的 VIP 位于入口控制器前面)。

实际上,这意味着,您将 go 与集群外部的流量(所谓的 发夹模式)分开,然后通过 Ingress 的外部 IP 返回,该入口路由到内部 ClusterIP 服务(这增加了额外的跃点),而不是直接使用 ClusterIP/Service DNS 名称(您留在 Kubernetes 集群内):

nginx.ingress.kubernetes.io/auth-url: "http://oauth2.infra-system.svc.cluster.local/oauth2/auth"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM