# 1.在monitoring名称空间部署consul服务
cat > consul.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: consul
namespace: monitoring
labels:
app: consul
spec:
selector:
matchLabels:
app: consul
replicas: 1
template:
metadata:
name: consul
labels:
app: consul
spec:
containers:
- name: consul
image: consul:latest
ports:
- containerPort: 8500
protocol: TCP
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: consul
namespace: monitoring
labels:
app: consul
spec:
selector:
app: consul
ports:
- name: consul
protocol: TCP
port: 8500
targetPort: 8500
EOF
kubectl apply -f consul.yaml
# 2.编写prometheus-additional并在monitoring名称空间创建secret资源
cat > prometheus-additional.yaml << EOF
- job_name: consul
consul_sd_configs:
- server: consul.monitoring.svc:8500
relabel_configs:
- source_labels: [__meta_consul_tags]
regex: .*,prome,.*
action: keep
- source_labels: [__meta_consul_service]
target_label: job
EOF
kubectl create secret generic additional-configs --from-file=prometheus-additional.yaml -n monitoring
# 3.手动修改kube-prometheus/manifests中的prometheus-prometheus.yaml文件增加additionalScrapeConfigs配置段
vim kube-prometheus/manifests/prometheus-prometheus.yaml
additionalScrapeConfigs:
name: additional-configs
key: prometheus-additional.yaml
# 并重新应用prometheus-prometheus.yaml文件
kubectl apply -f kube-prometheus/manifests/prometheus-prometheus.yaml
# 4.修改名为prometheus-k8s的ClusterRole权限,并更新资源
cat > prometheus-clusterRole.yaml << EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus-k8s
rules:
- apiGroups:
- ""
resources:
- nodes
- services
- endpoints
- pods
- nodes/proxy
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- configmaps
- nodes/metrics
verbs:
- get
- nonResourceURLs:
- /metrics
verbs:
- get
EOF
kubectl apply -f prometheus-clusterRole.yaml