当使用ingress的方式做Web服务的反向代理时,有时会需要增加对反向页面的权限认证(例如:反向代理到kibana页面时),还好Kubernets非常强大,这么简单的特性当然也是支持的,主要依靠secret和注解方式来实现。
虽然很少会把ingress直接对外服务,但这种情况也不是没有。
第一步,创建账号密码文件
这里需要使用到密码文件生成工具htpasswd,在ubuntu下可以使用以下命令进行安装:
sudo apt-get install apache2-utils随后使用htpasswd命令创建密码文件auth,以及两个用户user1和user2:
$ htpasswd -c auth user1
New password: <bar>
New password:
Re-type new password:
Adding password for user user1
$ htpasswd auth user2
2nd user:
htpasswd auth user2
New password: <bar>
New password:
Re-type new password:
Adding password for user user2第二步,创建kubernets secret
kubectl -n <namespace> create secret generic basic-auth --from-file=auth
secret "basic-auth" created第三步,创建ingress配置文件
ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-with-auth
您暂时无权查看此隐藏内容!
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /
        backend:
          serviceName: echoheaders
          servicePort: 80参考链接:
https://www.linkedin.com/pulse/kubernetes-add-basic-auth-protection-you-ingresse-controllers-laza
https://github.com/kubernetes/contrib/tree/master/ingress/controllers/nginx/examples/auth






 Asynq任务框架
Asynq任务框架 MCP智能体开发实战
MCP智能体开发实战 WEB架构
WEB架构 安全监控体系
安全监控体系




kubectl get secret basic-auth -o yaml
apiVersion: v1
data:
auth: Zm9vOiRhcHIxJE9DRzZYeWJcJGNrKDBGSERBa29YWUlsSDkuY3lzVDAK #这里的结构直接决定你能否正常使用,auth: xxxxxx
kind: Secret
metadata:
name: basic-auth
namespace: default
type: Opaque
https://rocdu.gitbook.io/ingress-nginx-docs-cn/docs/user-guide/nginx-configuration/configmap
参考文档