kubernetes部署prometheus
⼀、在k8s中部署prometheus
⾸先先下载prometheus镜像,不然部署时再下载会很慢
我这⾥⽤的是v2.2.1版本
[root@master ~]# docker pull prom/prometheus:v2.2.1
⼆、创建⼀个存储prometheus数据的⽬录和sa账号
在你指定的prometheus节点中创建此⽬录,如果yanl⽂件中不配置指定节点,那每个节点都需要创建
[root@master ~]# mkdir /data
[root@master ~]# chmod 777 /data
创建⼀个sa账号
[root@master ~]# kubectl create serviceaccount monitor -n monitor-sa
把sa账号monitor通过clusterrolebing绑定到clusterrole上
[root@master ~]# kubectl create clusterrolebinding monitor-clusterrolebinding -n monitor-sa --clusterrole=cluster-admin  --serviceaccount=monitor-sa:moni tor
三、编写存储卷、pod、svc这三个yaml⽂件
1、编写存储卷configMap⽂件
[root@master ~]# vim prometheus-cfg.yaml
kind: ConfigMap
apiVersion: v1
metadata:
labels:
app: prometheus
name: prometheus-config
namespace: monitor-sa
data:
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 1m
scrape_configs:
- job_name: 'kubernetes-node'
kubernetes_sd_configs:
- role: node
relabel_configs:
- source_labels: [__address__]
regex: '(.*):10250'
replacement: '${1}:9100'
target_label: __address__
action: replace
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- job_name: 'kubernetes-node-cadvisor'
kubernetes_sd_configs:
- role:  node
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
relabel_configs:
- action: labelmap
regex: __meta_kubernetes_node_label_(.+)
- target_label: __address__
replacement: kubernetes.default.svc:443
- source_labels: [__meta_kubernetes_node_name]
regex: (.+)
target_label: __metrics_path__
replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
- job_name: 'kubernetes-apiserver'
kubernetes_sd_configs:
- role: endpoints
scheme: https
tls_config:
ca_file: /var/run/secrets/kubernetes.io/
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
relabel_configs:
-
source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
action: keep
regex: default;kubernetes;https
- job_name: 'kubernetes-service-endpoints'
kubernetes_sd_configs:
- role: endpoints
relabel_configs:
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
action: replace
target_label: __scheme__
regex: (https?)
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
action: replace
target_label: __address__
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
- action: labelmap
regex: __meta_kubernetes_service_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_service_name]
action: replace
target_label: kubernetes_name
配置⽂件解释
scrape_interval: 15s:默认值为 15s,收集数据的时间间隔,每隔15s收集⼀次数据,scrape_interval可以基于全局或基于单个metric 定义
scrape_timeout: 10s:采集数据的超时时间,默认为10s
evaluation_interval: 1m:默认为1m,对告警规则做定期计算,然后更新告警状态,evaluation_interval只有全局值
告警状态如下三种:
inactive:没有触发阈值
pending:已触发阈值但未满⾜告警持续时间
firing:已触发阈值且满⾜告警持续时间
2、编写prometheus pod⽂件
[root@master ~]# vim prometheus-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-server    //控制器名称
namespace: monitor-sa  //命名空间
labels:
app: prometheus  //标签
spec:
replicas: 1  //副本数
selector:
matchLabels:
app: prometheus  //pod标签
component: server
#matchExpressions:
#- {key: app, operator: In, values: [prometheus]}
#- {key: component, operator: In, values: [server]}
template:  //模板
metadata:
labels:
app: prometheus
component: server
annotations:
prometheus.io/scrape: 'false'  //是否收集数据
spec:
nodeName: k8s-node  //指定节点
serviceAccountName: monitor  //指定sa
containers:
- name: prometheus
nodeselectorimage: prom/prometheus:v2.2.1
imagePullPolicy: IfNotPresent    //拉取镜像规则,本地有载在本地获取,如果本地没有从仓库拉取        command:
- prometheus
- --config.file=/etc/l
- --storage.tsdb.path=/prometheus
- --ion=720h
ports:
- containerPort: 9090
protocol: TCP
volumeMounts:
- mountPath: /etc/l
name: prometheus-config
subPath: l
- mountPath: /prometheus/
name: prometheus-storage-volume
volumes:  //将configmap存储卷挂在到上⾯的容器对应的⽬录中
- name: prometheus-config
configMap:
name: prometheus-config
items:
- key: l
path: l
mode: 0644
- name: prometheus-storage-volume
hostPath:
path: /data
type: Directory
3、编写service配置⽂件
vim prometheus-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: prometheus
namespace: monitor-sa
labels:
app: prometheus
spec:
type: NodePort
ports:
- port: 9090
targetPort: 9090
nodePort: 30000
protocol: TCP
selector:
app: prometheus
component: server
四、创建、查看、验证
1、创建
[root@master prometheus_yaml]# kubectl apply -f prometheus-cfg.yaml [root@master prometheus_yaml]# kubectl apply -f prometheus-deploy.yaml [root@master prometheus_yaml]# kubectl apply -f prometheus-svc.yaml
2、查看pod
[root@master prometheus_yaml]# kubectl get po -n monitor-sa
NAME                                READY  STATUS    RESTARTS  AGE
node-exporter-78gwm                  1/1    Running  1          29h
node-exporter-859n8                  1/1    Running  1          29h
node-exporter-wljqh                  1/1    Running  1          29h
prometheus-server-7dbc5cd454-kpv8c  1/1    Running  0          68m
查看service
[root@master prometheus_yaml]# kubectl get svc -n monitor-sa
NAME        TYPE      CLUSTER-IP      EXTERNAL-IP  PORT(S)          AGE prometheus  NodePort  10.98.102.185  <none>        9090:30000/TCP  69m 查看configMap
[root@master prometheus_yaml]# kubectl get configmaps -n monitor-sa NAME                DATA  AGE
prometheus-config  1      70m
3、验证
访问192.168.241.100:30000

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。