Ingress Nginx基本认证
1.使用htpasswd工具创建生成nginx认证用户
1.安装htpasswd工具
# yum install httpd -y
2.使用htpasswd工具生成测试用户名和密码
# htpasswd -c auth admin
New password:
Re-type new password:
Adding password for user admin
# ls auth
auth
# cat auth
admin:$apr1$GMNGjVIB$TJYrLDghftoZa.fAu9c3Q/
2.将创建的密码文件用secrets资源存储
# kubectl create secret generic nginx-basic-auth --from-file=auth
secret/nginx-basic-auth created
# kubectl get secrets nginx-basic-auth
NAME TYPE DATA AGE
nginx-basic-auth Opaque 1 81s
3.部署测试应用
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
namespace: default
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
containers:
- name: nginx
image: nginx:1.15.2
ports:
- name: nginxprot
containerPort: 80
protocol: TCP
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: default
labels:
app: nginx
spec:
ports:
- name: nginxprot
protocol: TCP
port: 80
targetPort: 80
selector:
app: nginx
type: ClusterIP
4.创建Ingress用于认证信息
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-basic-auth
namespace: default
generation: 1
annotations:
# 登录的提示信息
nginx.ingress.kubernetes.io/auth-realm: Please Input Your Username and Passowrd
# 对应认证信息,也就是我们创建的secrets资源名称,里面保存了我们创建的有效用户
nginx.ingress.kubernetes.io/auth-secret: nginx-basic-auth
# 指定认证类型
nginx.ingress.kubernetes.io/auth-type: basicbernetes.io/auth-type: basic
spec:
ingressClassName: nginx
rules:
- host: ingress.basic.auth
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx
port:
number: 80
5.访问测试
本文参考:Ingress-Nginx使用指南上篇 - 尹正杰 - 博客园
标签:Ingress,name,nginx,ingress,auth,认证,Nginx,basic From: https://www.cnblogs.com/sl08/p/18556453