ZBLOG

k8s:prometheus页面添加鉴权

要在k8s:prometheus页面添加鉴权,需要进行以下步骤:

  1. 创建一个Kubernetes Secret对象,用于存储用户名和密码。
  1. $ kubectl create secret generic prometheus-basic-auth --from-literal=username=myuser --from-literal=password=mypass
  1. 创建一个ConfigMap对象,用于存储nginx配置文件。
  1. $ kubectl create configmap prometheus-nginx-conf --from-file=nginx.conf
  1. 创建一个Deployment对象,用于运行nginx容器,并挂载上面创建的Secret和ConfigMap。
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: prometheus-nginx
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: prometheus-nginx
  10. template:
  11. metadata:
  12. labels:
  13. app: prometheus-nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx
  18. ports:
  19. - containerPort: 80
  20. volumeMounts:
  21. - name: nginx-conf-volume
  22. mountPath: /etc/nginx/conf.d/
  23. - name: basic-auth-volume
  24. mountPath: /etc/nginx/.htpasswd/
  25. subPath: .htpasswd
  26. volumes:
  27. - name: nginx-conf-volume
  28. configMap:
  29. name: prometheus-nginx-conf
  30. items:
  31. - key : nginx.conf
  32. path : default.conf
  33. - name : basic-auth-volume
  34. secret :
  35. secretName : prometheus-basic-auth
  1. 创建一个Service对象,将Deployment暴露为NodePort类型的Service。
  1. apiVersion : v1
  2. kind : Service
  3. metadata :
  4. name : prometheus-service
  5. spec :
  6. type : NodePort
  7. selector :
  8. app : prometheus-nginx
  9. ports :
  10. - name : http
  11. port : 80
  12. targetPort : 80
  1. 配置Ingress对象,将请求转发到Service。
  1. apiVersion: networking.k8s.io/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: prometheus-ingress
  5. spec:
  6. rules:
  7. - host: prometheus.example.com # Replace with your domain name
  8. http:
  9. paths:
  10. - backend:
  11. serviceName: prometheus-service
  12. servicePort: http
  13. path: /prometheus/
  1. 在ConfigMap中创建nginx.conf文件,配置鉴权相关的内容。
  1. server {
  2. listen 80;
  3. server_name localhost;
  4. location /prometheus/ {
  5. auth_basic "Restricted";
  6. auth_basic_user_file /etc/nginx/.htpasswd/default;
  7. proxy_pass http://prometheus-service/;
  8. }
  9. }

这样就完成了在k8s:prometheus页面添加鉴权的步骤。用户访问/prometheus路径时会弹出认证框,输入正确的用户名和密码后才能访问Prometheus页面。

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?