【问题标题】:Can't get HTTP basic auth with Google Kubernetes Engien Nginx ingress controller无法使用 Google Kubernetes Engien Nginx 入口控制器获得 HTTP 基本身份验证
【发布时间】:2019-01-04 16:38:08
【问题描述】:

根据 Kubernetes 文档,Nginx Ingress Controller 支持添加基本身份验证。我正在设置的所需 Ingress 注释是:

nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: namespace/secret
nginx.ingress.kubernetes.io/auth-realm: "Authentication Required"

我的入口控制器图像是:gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.11

我在入口控制器中找不到任何指示错误的日志,但基本身份验证不存在。万一这很重要,我正在使用 cert-manager 来配置 Let's Encrypt TLS 证书,效果很好。

【问题讨论】:

    标签: docker authentication nginx kubernetes


    【解决方案1】:

    您使用了错误的控制器/注释。这些注释适用于https://github.com/kubernetes/ingress-nginx,其中有这个官方image

    您有如何部署控制器here的示例

    如果要使用 gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.11,注解为:

    ingress.kubernetes.io/auth-type: basic
    ingress.kubernetes.io/auth-secret: basic-auth
    ingress.kubernetes.io/auth-realm: "Authentication Required"
    

    【讨论】:

    • 注意:OP 对当前版本的 nginx ingress 有正确的注释,但对于他们使用的旧 0.9 版本,这个答案是正确的。
    【解决方案2】:

    你可以找到完整的文章herehere

    要在 Nginx Ingress 上配置基本身份验证,需要做好两件事:
    (我假设您已经在集群上运行了入口控制器)

    1. 应存在名称和内容为 base64 编码行中的用户名/密码的 Secret: (在此示例中,名称“basic-auth”用作 Secret 的名称,但您可以选择任何您想要的有效名称。)用户名:foo,密码:bar

       $ htpasswd -c auth foo
       New password: bar
       Re-type new password: bar
       Adding password for user foo
      
       $ kubectl create secret generic basic-auth --from-file=auth
       secret "basic-auth" created
      
       $ kubectl get secret basic-auth -o yaml
       apiVersion: v1
       data:
         auth: Zm9vOiRhcHIxJE9GRzNYeWJwJGNrTDBGSERBa29YWUlsSDkuY3lzVDAK
       kind: Secret
       metadata:
         name: basic-auth
         namespace: default
       type: Opaque
      
    2. Ingress 对象应与 Secret 存在于同一命名空间中:
      (这里我们为 Ingress 和 Secret 使用默认命名空间)

       echo "
       apiVersion: extensions/v1beta1
       kind: Ingress
       metadata:
         name: ingress-with-auth
         annotations:
           # type of authentication
           nginx.ingress.kubernetes.io/auth-type: basic
           # name of the secret that contains the user/password definitions
           nginx.ingress.kubernetes.io/auth-secret: basic-auth
           # message to display with an appropriate context why the authentication is required
           nginx.ingress.kubernetes.io/auth-realm: \"Authentication Required - foo\"
       spec:
         rules:
         - host: foo.bar.com
           http:
             paths:
             - path: /
               backend:
                 serviceName: <your_backend_service_name>
                 servicePort: <your_backend_service_port>
       " | kubectl create -f -
      

    使用带或不带身份验证的 GET 请求验证您的配置检查回复:
    30xxx - 从命令kubectl get svc --all-namespaces | grep ingress-nginx | grep NodePort 的输出中获取端口号)

    $ curl -v http://cluster.node.ip.address:30xxx/ -H 'Host: foo.bar.com'
    ...
    < HTTP/1.1 401 Unauthorized
    ...
    
    $ curl -v http://cluster.node.ip.address:30xxx/ -H 'Host: foo.bar.com' -u 'foo:bar'
    ...
    < HTTP/1.1 200 OK
    ...
    

    在下一步中,您可以将您选择的 SSL 或 TLS 配置添加到 Ingress 对象规范中。

    更新:上面的示例在current nginx ingress controller v0.44.0 上仍然可以正常工作

    我认为 cert-manager 配置不应该影响入口控制器管理纯 HTTP 流量的方式,如果 SSL/HTTPS 未强制执行。

    htpasswd 可以作为 Ubuntu/Debian 软件包的一部分安装 apache2-utils 或 CentOS 的 httpd-tools

    apt install apache2-utils
    yum install httpd-tools
    

    对于 ngninxinc 版本的入口控制器,请考虑阅读 issue #200

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 2018-01-11
      • 2021-03-19
      • 2020-05-06
      • 1970-01-01
      • 2021-03-25
      • 2017-09-23
      • 2017-12-06
      • 2020-05-08
      相关资源
      最近更新 更多