简体   繁体   English

无法使用 Google Kubernetes Engien Nginx 入口控制器获得 HTTP 基本身份验证

[英]Can't get HTTP basic auth with Google Kubernetes Engien Nginx ingress controller

According to the Kubernetes docs, the Nginx Ingress Controller supports adding basic authentication.根据 Kubernetes 文档,Nginx 入口控制器支持添加基本身份验证。 The required Ingress annotations that I'm setting are:我正在设置的必需的 Ingress 注释是:

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

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

I cannot find any logs from the ingress controller that would indicate errors, but basic auth is not present.我找不到来自入口控制器的任何指示错误的日志,但不存在基本身份验证。 In case it matters, I am using cert-manager to provision Let's Encrypt TLS certificates, which is working nicely.以防万一,我正在使用 cert-manager 来配置 Let's Encrypt TLS 证书,它运行良好。

You are using wrong controller/annotations.您使用了错误的控制器/注释。 These annotations are for https://github.com/kubernetes/ingress-nginx which has this official image这些注释适用于具有此官方图像的https://github.com/kubernetes/ingress-nginx

You have examples of how to deploy the controller here您有如何在此处部署控制器的示例

If you want to use gcr.io/google_containers/nginx-ingress-controller:0.9.0-beta.11, the annotations are:如果你想使用 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"

You can find full articles here or here .您可以在此处此处找到完整的文章。

To configure basic authentication on Nginx Ingress there are two things that should be in place:要在 Nginx Ingress 上配置基本身份验证,应该做两件事:
(I assume you already have ingress controller running on your cluster) (我假设您已经在集群上运行了入口控制器)

  1. A Secret with a name and content of username/passwords in base64 encoded line should exist: (In this example, the name “basic-auth” is used as a name of the Secret but you can choose any valid name you want.) Username: foo , password: bar一个名称和内容为 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. An Ingress object should exist in the same namespace with the Secret: Ingress 对象应该与 Secret 存在于同一个命名空间中:
    (here we use a default namespace for Ingress and 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 -

To verify your configuration check replies using GET request with or without authentication:要使用带有或不带有身份验证的 GET 请求来验证您的配置检查回复:
( 30xxx - get port number from output of the command kubectl get svc --all-namespaces | grep ingress-nginx | grep NodePort ) 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
...

On the next step, you can add SSL or TLS configuration of your choice to Ingress object specification.在下一步中,您可以将您选择的 SSL 或 TLS 配置添加到 Ingress 对象规范。

Update : The above example still works fine on the current nginx ingress controller v0.44.0更新:上面的例子在 当前的 nginx 入口控制器v0.44.0 上仍然可以正常工作

I recon cert-manager configuration shouldn't affect the way ingress controller manages plain HTTP traffic if SSL/HTTPS isn't enforced.我重新确认,如果未强制实施 SSL/HTTPS,则cert-manager配置不应影响入口控制器管理普通 HTTP 流量的方式。

htpasswd could be installed as a part of Ubuntu/Debian package apache2-utils or httpd-tools for CentOS: htpasswd可以作为 CentOS 的 Ubuntu/Debian 软件包apache2-utilshttpd-tools的一部分安装:

apt install apache2-utils
yum install httpd-tools

For ngninxinc version of the ingress-controller consider reading the issue #200对于入口控制器的ngninxinc版本,请考虑阅读问题 #200

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

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