MENU

Kubernetes 监控:Prometheus + grafana + node-exporter

2019 年 05 月 21 日 • 应用服务器

本文写的并不完整,完整版请看这里

很长时间之前就说要搞监控,结果拖到了现在,本篇文章主要是参照 (zhào bān) 这篇文章进行的,感谢先行者,我这里只是解决了目录持久化的问题,顺便更新了镜像版本,先来大概了解需要部署的组件。

组件说明
node-exporter负责收集node节点metrics监控数据,推送给prometheus
prometheus中文名普罗米修斯,负责存储node-exporter发送过来的数据
grafana将数据通过页面以图形的形式展现给用户

先说明一下我的环境吧,系统centos7.6-64,我是用二进制安装的kubernetes集群的,版本为1.13.6,两个master三个node,看一下kube-system空间正在跑的东西

[root@master-1 ~]# kubectl -n kube-system get pod 

今早部署的WEB UI,昨天部署的metrics-server,注意这个metrics-servernode-exportermetrics不是一个东西,下面开始部署node-exporter组件

部署node-exporter组件

这个组件是要在每个node节点上运行的,所以直接用的DaemonSet方式去部署的,yaml文件如下。

[root@master-1 ~]# cat monitored/node-exporter/node-exporter.yaml 
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: kube-system
  labels:
    k8s-app: node-exporter
spec:
  template:
    metadata:
      labels:
        k8s-app: node-exporter
    spec:
      containers:
      - image: prom/node-exporter
        name: node-exporter
        ports:
        - containerPort: 9100
          protocol: TCP
          name: http
---
apiVersion: v1
kind: Service
metadata:
  labels:
    k8s-app: node-exporter
  name: node-exporter
  namespace: kube-system
spec:
  ports:
  - name: http
    port: 9100
    nodePort: 31000
    protocol: TCP
  type: NodePort
  selector:
    k8s-app: node-exporter

就这样,可以部署了。

[root@master-1 ~]# kubectl create -f monitored/node-exporter/node-exporter.yaml
[root@master-1 ~]# kubectl get pod -n kube-system -w

正常启动了,访问一下试试,刚刚指定了nodeport的端口为31000,所以访问一下这个就行了。

[root@master-1 ~]# for i in node-{1..3}
>  do  
>   echo $i &&  curl -s $i:31000/metrics | head
> done

没啥子问题,这个很简单,不需要什么数据持久化之类的东西,下面开始部署prometheus组件

部署prometheus组件

首先要进行rbac授权,文件内容如下。

[root@master-1 ~/monitored/prometheus]# cat rbac-setup.yaml 
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups: [""]
  resources:
  - nodes
  - nodes/proxy
  - services
  - endpoints
  - pods
  verbs: ["get", "list", "watch"]
- apiGroups:
  - extensions
  resources:
  - ingresses
  verbs: ["get", "list", "watch"]
- nonResourceURLs: ["/metrics"]
  verbs: ["get"]
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: kube-system
---
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: kube-system

其二需要创建configmap来存储prometheus的配置文件,内容如下。

[root@master-1 ~/monitored/prometheus]# cat configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: kube-system
data:
  prometheus.yml: |
    global:
      scrape_interval:     15s
      evaluation_interval: 15s
    scrape_configs:

    - job_name: 'kubernetes-apiservers'
      kubernetes_sd_configs:
      - role: endpoints
      scheme: https
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      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-nodes'
      kubernetes_sd_configs:
      - role: node
      scheme: https
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      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

    - job_name: 'kubernetes-cadvisor'
      kubernetes_sd_configs:
      - role: node
      scheme: https
      tls_config:
        ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      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

    - 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

    - job_name: 'kubernetes-services'
      kubernetes_sd_configs:
      - role: service
      metrics_path: /probe
      params:
        module: [http_2xx]
      relabel_configs:
      - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_probe]
        action: keep
        regex: true
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: blackbox-exporter.example.com:9115
      - source_labels: [__param_target]
        target_label: instance
      - action: labelmap
        regex: __meta_kubernetes_service_label_(.+)
      - source_labels: [__meta_kubernetes_namespace]
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_service_name]
        target_label: kubernetes_name

    - job_name: 'kubernetes-ingresses'
      kubernetes_sd_configs:
      - role: ingress
      relabel_configs:
      - source_labels: [__meta_kubernetes_ingress_annotation_prometheus_io_probe]
        action: keep
        regex: true
      - source_labels: [__meta_kubernetes_ingress_scheme,__address__,__meta_kubernetes_ingress_path]
        regex: (.+);(.+);(.+)
        replacement: ${1}://${2}${3}
        target_label: __param_target
      - target_label: __address__
        replacement: blackbox-exporter.example.com:9115
      - source_labels: [__param_target]
        target_label: instance
      - action: labelmap
        regex: __meta_kubernetes_ingress_label_(.+)
      - source_labels: [__meta_kubernetes_namespace]
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_ingress_name]
        target_label: kubernetes_name

    - job_name: 'kubernetes-pods'
      kubernetes_sd_configs:
      - role: pod
      relabel_configs:
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: true
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
      - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
        action: replace
        regex: ([^:]+)(?::\d+)?;(\d+)
        replacement: $1:$2
        target_label: __address__
      - action: labelmap
        regex: __meta_kubernetes_pod_label_(.+)
      - source_labels: [__meta_kubernetes_namespace]
        action: replace
        target_label: kubernetes_namespace
      - source_labels: [__meta_kubernetes_pod_name]
        action: replace
        target_label: kubernetes_pod_name

然后是部署Prometheus yaml文件内容,包含了services,看原文可以看到这个pod挂载了两个东西,一个是上文创建的ConfigMap,还有一个是emptyDir类型的空目录,这个类型的目录问题很大,如果pod没了或是pod迁移到别的节点这个目录也就没了,所以现在解决一下这个问题。

解决办法其一是使用hostPath且指定服务约束,其二就是使用NFS作为后端存储,这里用NFS了,现在我的NFS并没有部署,所以部署一下吧,顺便把动态供给启了,具体是怎么创建了我这里就不贴了,之前写过,看一下这里就知道了,所以最终部署文件内容如下。

[root@master-1 ~/monitored/prometheus]# cat prometheus.deploy.yaml
kind: Service
apiVersion: v1
metadata:
  labels:
    app: prometheus
  name: prometheus
  namespace: kube-system
spec:
  type: NodePort
  ports:
  - port: 9090
    targetPort: 9090
    nodePort: 31001
  selector:
    app: prometheus
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  labels:
    name: prometheus-deployment
  name: prometheus
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - image: prom/prometheus:latest
        name: prometheus
        command:
        - "/bin/prometheus"
        args:
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--storage.tsdb.path=/prometheus"
        - "--storage.tsdb.retention=24h"
        ports:
        - containerPort: 9090
          protocol: TCP
        volumeMounts:
        - mountPath: "/prometheus"
          name: prometheus-data
        - mountPath: "/etc/prometheus"
          name: config-volume
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 500m
            memory: 2500Mi
      serviceAccountName: prometheus    
      volumes:
      - name: prometheus-data
        persistentVolumeClaim: 
          claimName: prometheus-data
      - name: config-volume
        configMap:
          name: prometheus-config

--- 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus-data
  namespace: kube-system
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: managed-nfs-storage
  resources:
    requests:
      storage: 5Gi

就是这样子,可以创建了撒。

[root@master-1 ~/monitored/prometheus]# kubectl create -f .
[root@master-1 ~/monitored/prometheus]# kubectl -n kube-system get pod -w

再看一眼pv,pvc

[root@master-1 ~]# kubectl -n kube-system get pv,pvc
[root@master-1 ~]# tree /nfs-data/

没问题,以后随你pod怎么漂吧,下面开始部署grafana组件

部署grafana组件

看原文也是有一个emptyDir类型的空目录,所以也得吧这个改一下,弄到NFS存储上,原文还用了ingress来访问界面,我这里这个就不加了,还得去改hosts文件,我直接用nodePort了,使用31002端口,把镜像版本换成了现在的稳定版,所以,完整的部署文件内容如下。

[root@master-1 ~/monitored/grafana]# cat grafana-deploy.yaml 
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: kube-system
  labels:
    app: grafana
    component: core
spec:
  type: NodePort
  ports:
    - port: 3000
      nodePort: 31002
  selector:
    app: grafana
    component: core
---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: grafana-core
  namespace: kube-system
  labels:
    app: grafana
    component: core
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: grafana
        component: core
    spec:
      containers:
      - image: grafana/grafana:5.1.0
        name: grafana-core
        imagePullPolicy: IfNotPresent
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 100m
            memory: 100Mi
        env:
          - name: GF_AUTH_BASIC_ENABLED
            value: "true"
          - name: GF_AUTH_ANONYMOUS_ENABLED
            value: "false"
        readinessProbe:
          httpGet:
            path: /login
            port: 3000
        volumeMounts:
        - name: grafana-data
          mountPath: /var/lib/grafana
      volumes:
      - name: grafana-data
        persistentVolumeClaim: 
          claimName: grafana-data

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-data
  namespace: kube-system
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: managed-nfs-storage
  resources:
    requests:
      storage: 5Gi

阔以创建了,

[root@master-1 ~/monitored/grafana]# kubectl create -f grafana-deploy.yaml 
[root@master-1 ~/monitored/grafana]# kubectl -n kube-system get pod -w 

OK,启动完了,现在可以访问界面了,地址就是node:31002

WEB 界面配置

如果一切正常的话,打开页面就可以看到这个了。

默认用户名密码为admin,登陆之后是这样的,直接点击这里添加数据源。

我们添加的数据为prometheus,所以名字随意,type选择PrometheusURL请填写prometheusService,写内部的DNS名称即可,所以就是这样。

正确配置后点击保存和测试后开始添加仪表盘,点开这个位置,

输入模板ID,最常用的模板ID315,输入稍等后会加载出来,然后指定一下模板的类型,

操作完成之后就可以看到仪表盘了,如下。

随便截了两张图,

到这里监控就算是搭建完成了,大概看了一下报警的配置,需要编辑仪表盘写表达式,然后报警类型也是蛮多的,大概这些。

这个等有时间再细看吧,目前只是安装好了,像是什么自定义监控还没搞,安装好只是冰山一角,我要去 (shān) 数据库了, (shān) 完数据库还要去机房 () 服务器,暂时就这样吧,过。

最后编辑于: 2019 年 07 月 17 日
返回文章列表 文章二维码 打赏
本页链接的二维码
打赏二维码