Nginx Ingress设置账号密码

Kubernetes中,应用服务使用ingress暴露后,默认是没有账号密码限制的,只要知道ingress的地址,所有人都可以访问服务(除非应用中有账号密码限制),那么是否可以在k8s中给ingress设置账号密码来限制访问呢?

    如下:

    使用htpasswd命令生产auth文件,如List-1,其中flink处改为你自己的账号,按提示输入密码

#生成auth文件,password处输入密码
$ htpasswd -c auth flink
New password: 
New password:
Re-type new password:
Adding password for user flink

    生成secret,如List-2,basic-auth是secret名称,要指定namespace,因为secret是不能跨namespace访问的

#记得指定namespace
$ kubectl create secret generic basic-auth --from-file=auth -n flink
secret "basic-auth" created

查看生成的secret

#查看basic-auth这个secret
$ kubectl get secret basic-auth -o yaml -n flink
apiVersion: v1
data:
  auth: xxxxx.....
kind: Secret
metadata:
  creationTimestamp: "2020-05-14T01:53:20Z"
  name: basic-auth
  namespace: flink
...

    怎么在ingress中使用这个secret呢,如下List-4,在annotations中使用我们创建好的secret

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-with-auth
  annotations:
    nginx.ingress.kubernetes.io/auth-type: basic
   # 使用刚才创建的secret名称 
    nginx.ingress.kubernetes.io/auth-secret: basic-auth
    # "Authentication Required - flink"这个只是账号密码输错时的一个提示
    nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - flink'
spec:
  rules:
...

    用ingress域名访问应用,现在就会提示账号密码登录。

转载自:https://kubernetes.github.io/ingress-nginx/examples/auth/basic/

原文地址:https://www.cnblogs.com/fat-girl-spring/p/15543397.html