【问题标题】:http basic auth example with nginx ingress controller (nginxinc)带有 nginx 入口控制器 (nginxinc) 的 http 基本身份验证示例
【发布时间】:2021-03-19 12:49:54
【问题描述】:
我无法使用 nginxinc 入口控制器文档获得 http 基本身份验证:https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication
我的用例:
我有一个在 web.mydomain.com 上运行的 Ingress。我需要向这个端点添加基本的 HTTP 身份验证。
有人有运行示例吗?
这是我的基本网络入口:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: cafe-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.org/server-snippets: |
location /auth {
auth_basic "Administrator’s Area";
auth_basic_user_file "/home/madhu/auth";
}
spec:
ingressClassName: nginx # use only with k8s version >= 1.18.0
rules:
- host: web.mydomain.com
http:
paths:
- path: /
backend:
serviceName: web
servicePort: 80
【问题讨论】:
标签:
nginx-config
nginx-ingress
【解决方案1】:
你必须创建一个 kubernetes 密码:
$ htpasswd -c auth foo
New password: <bar>
New password:
Re-type new password:
Adding password for user foo
$ kubectl create secret generic basic-auth --from-file=auth
$ kubectl get secret basic-auth -o yaml
apiVersion: v1
data:
auth: Zm9vOiRhcHIxJE9GRzNYeWJwJGNrTDBGSERBa29YWUlsSDkuY3lzVDAK
kind: Secret
metadata:
name: basic-auth
namespace: default
type: Opaque
如果您使用 helm 将此输出复制到模板文件夹上的文件 secret.yaml 中。
之后,您必须像这样使用 auth-secret 注释:
apiVersion: networking.k8s.io/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: http-svc
servicePort: 80
要了解更多信息,请阅读此文档:https://kubernetes.github.io/ingress-nginx/examples/auth/basic/