ZBLOG

k8s:prometheus页面添加鉴权

要在k8s:prometheus页面中添加鉴权,您可以执行以下步骤:

1.创建一个Kubernetes Secret来存储用户名和密码。

kubectl create secret generic prometheus-auth --from-literal=username=<your_username> --from-literal=password=<your_password>
  1. 创建一个 ConfigMap 来存储 Prometheus 配置文件。
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
data:
  prometheus.yml: |
    global:
      scrape_interval:     15s # Set the scrape interval to every 15 seconds.
      evaluation_interval: 15s # Evaluate rules every 15 seconds.

    rule_files:

    # A scrape configuration containing exactly one endpoint to scrape:
    # Here it's Prometheus itself.
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['localhost:9090']

      - job_name: 'example'
        basic_auth:
          username: {{ with $secret := secrets "prometheus-auth" }}{{ $secret.data.username }}{{ end }}
          password: {{ with $secret := secrets "prometheus-auth" }}{{ $secret.data.password }}{{ end }}
        static_configs:
          - targets: ['example.com']
            labels:
              group: 'example'
  1. 在您的 Kubernetes 群集上安装 Prometheus,将 ConfigMap 和 Secret 应用于部署。
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: prometheus-deployment
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: prometheus
  replicas: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/name: prometheus
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus:v2.0.0
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: config-volume
              mountPath: /etc/prometheus/
      volumes:
        - name: config-volume
          configMap:
            name: prometheus-config
        - name: secret-volume
          secret:
            secretName: prometheus-auth

  1. 暴露 Prometheus 服务。
apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
spec:
  type: NodePort # Expose the service as a NodePort to make it easier to access from outside the cluster.
  selector:
    app.kubernetes.io/name: prometheus # Match the labels of the deployment.
  ports:
    - port: 80 # Expose Prometheus on port 80.
      targetPort: 9090 # Forward traffic to port 9090 in the container.
      nodePort: <node_port> # Choose a node port to use for accessing Prometheus.
  1. 在浏览器中访问 Prometheus 界面,您将被提示输入用户名和密码。

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?