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

pas*_*oop 6 proxy oauth nginx kubernetes

我们使用本页描述的方法保护了 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"
Run Code Online (Sandbox Code Playgroud)

设置要保护的服务,并且我们遵循本教程,每个集群仅部署一个 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>
Run Code Online (Sandbox Code Playgroud)

然而,一个简单的网站加载通常需要大约 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
Run Code Online (Sandbox Code Playgroud)

但这也没有改善延迟。我们仍然看到所有 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)使GUI按预期加载(快速)
  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"
Run Code Online (Sandbox Code Playgroud)

auth-url是入口用用户的 cookie 查询的内容。因此,oauth2 服务的本地 DNS 与外部 dns 名称相同,但没有 SSL 通信,并且由于它是 DNS,所以它是永久的(而集群 IP 不是)

pas*_*oop 6

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

我发现的修复是将注释设置为以下内容

  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"
Run Code Online (Sandbox Code Playgroud)

auth-url是入口用用户的 cookie 查询的内容。因此,oauth2 服务的本地 DNS 与外部 dns 名称相同,但没有 SSL 通信,并且由于它是 DNS,所以它是永久的(而集群 IP 不是)

  • 先生,我庆祝你为这个答案而诞生的日子。你为我节省了很多你永远不会知道的时间。 (2认同)

Nep*_*cen 5

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

实际上,这意味着,您将流量发送到集群外部(所谓的发夹模式),然后通过 Ingress 的外部 IP 返回,路由到内部 ClusterIP 服务(这会增加额外的跃点),而不是直接使用 ClusterIP/Service DNS 名称(您位于 Kubernetes 集群中):

nginx.ingress.kubernetes.io/auth-url: "http://oauth2.infra-system.svc.cluster.local/oauth2/auth"
Run Code Online (Sandbox Code Playgroud)