K8s Nginx Ingress 配置 Basic Auth 访问

✨ 为 K8s Nginx Ingress Controller 配置 HTTP Basic Auth,增强安全。首先使用 htpasswd 或 openssl 命令创建认证文件,然后创建 Secret,并将认证文件作为其值。最后,通过在 Ingress 中设置相应的注解来启用基本认证。成功应用后,访问 Ingress 对应的 URL 将需要输入用户名和密码。扩展:在 Nginx 配置文件中增加 auth_basic 和 auth_basic_user_file 指令以配置 Basic 认证。

创建认证文件

可选的命令行工具有 `htpasswd` 和 `openssl`
  • htpasswd
# htpasswd是apache httpd工具包中的工具
# 安装htpasswd
## centos
yum install httpd-tools -y
## ubuntu
sudo apt-get install apache2-utils -y
# 创建认证文件
$ htpasswd -c auth user
New password: 
Re-type new password: 
Adding password for user user
# 查看密码文件内容
$ cat auth
user:$apr1$GzVG5cSb$F3lI0iF2HLP.xElrwNTAx/
  • openssl
# 使用apr1算法得到密码hash值
$ openssl passwd <your_password>
# 自行配置密码文件
$ vim auth
# 文件格式如下
user1:password1
user2:password2:comment
# 保存文件,文件内容最终如下
$ cat auth
user:$apr1$DipqQuBb$dzBFomxhKgqxgswLRLnAA1

创建 Secret

# 此处采用默认kube-config
$ kubectl create secret generic basic-auth --from-file=auth
secret/basic-auth created
# 查看secret
$ kubectl get secret basic-auth -o yaml 
apiVersion: v1
data:
  auth: JGFwcjEkRGlwcVF1QmIkZHpCRm9teGhLZ3F4Z3N3TFJMbkFBMQ==
kind: Secret
...

创建 Ingress

Nginx Ingress配置通过注解(annotations)来实现,基本认证主要有以下三个配置项

  • nginx.ingress.kubernetes.io/auth-type 认证类型
  • nginx.ingress.kubernetes.io/auth-secret 即上一步创建的Secret名称
  • nginx.ingress.kubernetes.io/auth-realm 当认证的时候显示一个合适的上下文信息

ingress-auth.yaml内容如下

apiVersion: networking.k8s.io/v1beta1                                                                                                                                                          
kind: Ingress                                                                                                                                                                                  
metadata:                                                                                                                                                                                      
  annotations:                                                                                                                                                                                                                                                                                                                            
    nginx.ingress.kubernetes.io/auth-realm: '"请登录"'                                                                                                                                   
    nginx.ingress.kubernetes.io/auth-secret: basic-auth                                                                                                                                       
    nginx.ingress.kubernetes.io/auth-type: basic                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
  name: ingress-auth                                                                                                                                                                             
  namespace: default                                                                                                                                                                                                                                                                                   
spec:                                                                                                                                                                                          
  rules:                                                                                                                                                                                       
  - host: example.com                                                                                                                                                                 
    http:                                                                                                                                                                                      
      paths:                                                                                                                                                                                   
      - backend:                                                                                                                                                                               
          serviceName: example-service                                                                                                                                                              
          servicePort: 8080                                                                                                                                                                    
        path: /auth/(/|$)(.*)                                                                                                                                                             
        pathType: ImplementationSpecific
# 应用上面的Ingress资源描述文件
$ kubectl apply -f ingress-auth.yaml

成功应用后,此时我们再访问对应的url入口时,就会提示需要输入用户名和密码了

扩展:Nginx 中配置Basic认证
# nginx.conf 中增加
auth_basic 'Login';
auth_basic_user_file basic-auth;