一、编写 Namespace,pro_ns.yaml 文件

[root@k8s-master01 ~]# mkdir  prometheus
[root@k8s-master01 ~]# cd prometheus
[root@k8s-master01 prometheus]# vim pro_ns.yaml

apiVersion: v1
kind: Namespace
metadata:
name: prometheus

:wq保存

二、编写 ConfigMap,pro_cm.yaml 文件

[root@k8s-master01 ~]# cd prometheus
[root@k8s-master01 prometheus]# vim pro_cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: prometheus
data:
prometheus.yml: |
global:
scrape_interval: 15s
scrape_timeout: 15s
evaluation_interval: 30s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']




:wq保存

三、编写 PersistentVolume、PersistentVolumeClaim,pro_pvc.yaml 文件

[root@k8s-master01 ~]# cd prometheus
[root@k8s-master01 prometheus]# vim pro_pvc.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
name: prometheus-local
labels:
app: prometheus
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
storageClassName: local-storage
nfs:
server: 192.168.3.20
path: /data/nfs/prometheus
#local:
# path: /data/nfs/prometheus
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: Exists
persistentVolumeReclaimPolicy: Retain
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: prometheus-data
namespace: prometheus
spec:
selector:
matchLabels:
app: prometheus
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: local-storage



:wq保存

四、编写 ServiceAccount、ClusterRole、ClusterRoleBinding,pro_rbac.yaml 文件

[root@k8s-master01 ~]# cd prometheus
[root@k8s-master01 prometheus]# vim pro_rbac.yaml


apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus
namespace: prometheus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus
rules:
- apiGroups:
- ""
resources:
- nodes
- services
- endpoints
- pods
- nodes/proxy
verbs:
- get
- list
- watch
- apiGroups:
- "extensions"
resources:
- ingresses
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- configmaps
- nodes/metrics
verbs:
- get
- nonResourceURLs:
- /metrics
verbs:
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: prometheus
subjects:
- kind: ServiceAccount
name: prometheus
namespace: prometheus



:wq保存

五、编写 Service,pro_svc.yaml 文件

[root@k8s-master01 ~]# cd prometheus
[root@k8s-master01 prometheus]# vim pro_svc.yaml

apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: prometheus
labels:
app: prometheus
spec:
selector:
app: prometheus
type: NodePort
ports:
- name: web
port: 9090
targetPort: http



:wq保存

六、编写 Deployment,pro_deploy.yaml 文件

[root@k8s-master01 ~]# cd prometheus
[root@k8s-master01 prometheus]# vim pro_deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
namespace: prometheus
labels:
app: prometheus
spec:
selector:
matchLabels:
app: prometheus
template:
metadata:
labels:
app: prometheus
spec:
securityContext: #指定运行的用户为root
runAsUser: 0
serviceAccountName: prometheus
containers:
- image: prom/prometheus:v2.30.2
name: prometheus
args:
- "--config.file=/etc/prometheus/prometheus.yml" #通过volume挂载prometheus.yml
- "--storage.tsdb.path=/prometheus" #通过vlolume挂载目录/prometheus
- "--storage.tsdb.retention.time=24h"
- "--web.enable-admin-api" #控制对admin HTTP API的访问,其中包括删除时间序列等功能
- "--web.enable-lifecycle" #支持热更新,直接执行localhost:9090/-/reload立即生效
ports:
- containerPort: 9090
name: http
volumeMounts:
- mountPath: "/etc/prometheus"
name: config-volume
- mountPath: "/prometheus"
name: data
resources:
requests:
cpu: 100m
memory: 512Mi
limits:
cpu: 100m
memory: 512Mi
volumes:
- name: data
persistentVolumeClaim:
claimName: prometheus-data #本地存储
- name: config-volume
configMap:
name: prometheus-config #定义的prometeus.yaml


:wq保存

七、运行.yaml 文件

[root@k8s-master01 ~]# cd prometheus
[root@k8s-master01 prometheus]# kubectl apply -f pro_ns.yaml
[root@k8s-master01 prometheus]# kubectl apply -f pro_cm.yaml
[root@k8s-master01 prometheus]# kubectl apply -f pro_pvc.yaml
[root@k8s-master01 prometheus]# kubectl apply -f pro_rbac.yaml
[root@k8s-master01 prometheus]# kubectl apply -f pro_svc.yaml
[root@k8s-master01 prometheus]# kubectl apply -f pro_deploy.yaml