要为kube-prometheus添加basic_auth_users,可以按照以下步骤进行操作:
创建一个包含用户名和密码的文件。例如,创建一个名为“users.htpasswd”的文件,并将用户名和密码添加到该文件中。
在kube-prometheus的manifests目录下创建一个名为“auth-proxy-secret.yaml”的文件,并使用以下内容填充它:
apiVersion: v1
kind: Secret
metadata:
name: auth-proxy-secret
namespace: monitoring
type: Opaque
data:
users.htpasswd: <base64-encoded contents of the users.htpasswd file>
其中,“
- 在kube-prometheus的manifests目录下创建一个名为“auth-proxy-configmap.yaml”的文件,并使用以下内容填充它:
apiVersion: v1
kind: ConfigMap
metadata:
name: auth-proxy-configmap
namespace: monitoring
data:
auth_proxy.cfg: |
http_listen = ":8080"
upstreams = ["http://localhost:9090"]
htpasswd_file = "/etc/nginx/users.htpasswd"
server_name = "prometheus.example.com"
server_tokens = off
location / {
proxy_pass http://localhost:9090;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Authentication settings.
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/users.htpasswd;
}
其中,“upstreams”应替换为kube-prometheus的上游地址,“htpasswd_file”应指向先前创建的用户文件。
- 在kube-prometheus的manifests目录下创建一个名为“auth-proxy-deployment.yaml”的文件,并使用以下内容填充它:
apiVersion: apps/v1
kind: Deployment
metadata:
name: auth-proxy
namespace: monitoring
spec:
replicas: 1
selector:
matchLabels:
app: auth-proxy
template:
metadata:
labels:
app: auth-proxy
spec:
containers:
- name: auth-proxy
image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.27.1
args:
- /bin/bash
- -c
- |
set -x
# Create htpasswd file.
echo "${HTPASSWD}" > /etc/nginx/users.htpasswd
# Start the ingress controller.
/entrypoint.sh nginx-ingress-controller --configmap=$(POD_NAMESPACE)/auth-proxy-configmap --default-backend-service=$(POD_NAMESPACE)/monitoring-grafana --annotations-prefix=nginx.ingress.kubernetes.io --enable-ssl-passthrough=true
env:
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: HTPASSWD
valueFrom:
secretKeyRef:
key: users.htpasswd
name: auth-proxy-secret
其中,“image”应替换为所需版本的NGINX Ingress Controller。此处使用的是0.27.1版本。
- 在kube-prometheus的manifests目录下创建一个名为“auth-proxy-service.yaml”的文件,并使用以下内容填充它:
apiVersion: v1
kind: Service
metadata:
name: auth-proxy
namespace: monitoring
spec:
selector:
app: auth-proxy
ports:
- name: http
port: 80
targetPort: 8080
- 将所有上述文件应用到kube-prometheus的命名空间中:
kubectl apply -f auth-proxy-secret.yaml -n monitoring
kubectl apply -f auth-proxy-configmap.yaml -n monitoring
kubectl apply -f auth-proxy-deployment.yaml -n monitoring
kubectl apply -f auth-proxy-service.yaml -n monitoring
现在,kube-prometheus已经配置为使用basic_auth_users进行身份验证。您可以通过访问“http://