要在k8s:prometheus页面中添加鉴权,您可以执行以下步骤:
1.创建一个Kubernetes Secret来存储用户名和密码。
kubectl create secret generic prometheus-auth --from-literal=username=<your_username> --from-literal=password=<your_password>
- 创建一个 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'
- 在您的 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
- 暴露 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.
- 在浏览器中访问 Prometheus 界面,您将被提示输入用户名和密码。




