ZBLOG

kube-prometheus添加basic_auth_users

要为kube-prometheus添加basic_auth_users,可以按照以下步骤进行操作:

  1. 创建一个包含用户名和密码的文件。例如,创建一个名为“users.htpasswd”的文件,并将用户名和密码添加到该文件中。

  2. 在kube-prometheus的manifests目录下创建一个名为“auth-proxy-secret.yaml”的文件,并使用以下内容填充它:

  1. apiVersion: v1
  2. kind: Secret
  3. metadata:
  4. name: auth-proxy-secret
  5. namespace: monitoring
  6. type: Opaque
  7. data:
  8. users.htpasswd: <base64-encoded contents of the users.htpasswd file>

其中,“”应替换为将“users.htpasswd”文件的内容进行base64编码后的结果。

  1. 在kube-prometheus的manifests目录下创建一个名为“auth-proxy-configmap.yaml”的文件,并使用以下内容填充它:
  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: auth-proxy-configmap
  5. namespace: monitoring
  6. data:
  7. auth_proxy.cfg: |
  8. http_listen = ":8080"
  9. upstreams = ["http://localhost:9090"]
  10. htpasswd_file = "/etc/nginx/users.htpasswd"
  11. server_name = "prometheus.example.com"
  12. server_tokens = off
  13. location / {
  14. proxy_pass http://localhost:9090;
  15. proxy_set_header Host $host;
  16. proxy_set_header X-Real-IP $remote_addr;
  17. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  18. # Authentication settings.
  19. auth_basic "Restricted Area";
  20. auth_basic_user_file /etc/nginx/users.htpasswd;
  21. }

其中,“upstreams”应替换为kube-prometheus的上游地址,“htpasswd_file”应指向先前创建的用户文件。

  1. 在kube-prometheus的manifests目录下创建一个名为“auth-proxy-deployment.yaml”的文件,并使用以下内容填充它:
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: auth-proxy
  5. namespace: monitoring
  6. spec:
  7. replicas: 1
  8. selector:
  9. matchLabels:
  10. app: auth-proxy
  11. template:
  12. metadata:
  13. labels:
  14. app: auth-proxy
  15. spec:
  16. containers:
  17. - name: auth-proxy
  18. image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.27.1
  19. args:
  20. - /bin/bash
  21. - -c
  22. - |
  23. set -x
  24. # Create htpasswd file.
  25. echo "${HTPASSWD}" > /etc/nginx/users.htpasswd
  26. # Start the ingress controller.
  27. /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
  28. env:
  29. - name: POD_NAMESPACE
  30. valueFrom:
  31. fieldRef:
  32. fieldPath: metadata.namespace
  33. - name: HTPASSWD
  34. valueFrom:
  35. secretKeyRef:
  36. key: users.htpasswd
  37. name: auth-proxy-secret

其中,“image”应替换为所需版本的NGINX Ingress Controller。此处使用的是0.27.1版本。

  1. 在kube-prometheus的manifests目录下创建一个名为“auth-proxy-service.yaml”的文件,并使用以下内容填充它:
  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: auth-proxy
  5. namespace: monitoring
  6. spec:
  7. selector:
  8. app: auth-proxy
  9. ports:
  10. - name: http
  11. port: 80
  12. targetPort: 8080
  1. 将所有上述文件应用到kube-prometheus的命名空间中:
  1. kubectl apply -f auth-proxy-secret.yaml -n monitoring
  2. kubectl apply -f auth-proxy-configmap.yaml -n monitoring
  3. kubectl apply -f auth-proxy-deployment.yaml -n monitoring
  4. kubectl apply -f auth-proxy-service.yaml -n monitoring

现在,kube-prometheus已经配置为使用basic_auth_users进行身份验证。您可以通过访问“http://”来测试它。您将被提示输入用户名和密码,然后才能访问prometheus UI。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=4586

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?