ZBLOG

kube-prometheus添加basic_auth_users

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

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

  2. 在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>

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

  1. 在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”应指向先前创建的用户文件。

  1. 在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版本。

  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

  1. 将所有上述文件应用到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://”来测试它。您将被提示输入用户名和密码,然后才能访问prometheus UI。

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?