Prometheus -1-1-1

1.1Prometheus 是一个开源的系统监控和报警系统,现在已经加入到 CNCF 基金会,成为继 k8s 之后

第二个在 CNCF 托管的项目,在 kubernetes 容器管理系统中,通常会搭配 prometheus 进行监控,同 时也支持多种 exporter 采集数据,还支持 pushgateway 进行数据上报,Prometheus 性能足够支撑 上万台规模的集群。

下次有空多学一下relabel_configs,对k8s监控的服务发现很有帮助

prometheus 特点

1.多维度数据模型

每一个时间序列数据都由 metric 度量指标名称和它的标签 labels 键值对集合唯一确定:

这个 metric 度量指标名称指定监控目标系统的测量特征(如:http_requests_total- 接收 http 请 求的总计数)。labbels 开启了 Prometheus 的多维数据模型:对于相同的度量名称,通过不同标签列表 的结合, 会形成特定的度量维度实例。(例如:所有包含度量名称为/api/tracks 的 http 请求,打上method=POST 的标签,则形成了具体的 http 请求)。这个查询语言在这些度量和标签列表的基础上进行过 滤和聚合。改变任何度量上的任何标签值,则会形成新的时间序列图。

2.灵活的查询语言(PromQL)

可以对采集的 metrics 指标进行加法,乘法,连接等操作; 3.可以直接在本地部署,不依赖其他分布式存储;

4.通过基于 HTTP 的 pull 方式采集时序数据;

5.可以通过中间网关 pushgateway 的方式把时间序列数据推送到 prometheus server 端; 6.可通过服务发现或者静态配置来发现目标服务对象(targets)。 7.有多种可视化图像界面,如 Grafana 等。

8.高效的存储,每个采样数据占 3.5 bytes 左右,300 万的时间序列,30s 间隔,保留 60 天,消耗 磁盘大概 200G。

9.做高可用,可以对数据做异地备份,联邦集群,部署多套 prometheus,pushgateway 上报数据

1.3 prometheus 组件

从上图可发现,Prometheus 整个生态圈组成主要包括 prometheus server,Exporter, pushgateway,alertmanager,grafana,Web ui 界面,Prometheus server 由三个部分组成, Retrieval,Storage,PromQL

1.Retrieval 负责在活跃的 target 主机上抓取监控指标数据 

2.Storage 存储主要是把采集到的数据存储到磁盘中 

3.PromQL 是 Prometheus 提供的查询语言模块。

1.Prometheus Server:

用于收集和存储时间序列数据。

2.Client Library:

客户端库,检测应用程序代码,当 Prometheus 抓取实例的 HTTP 端点时,客户端库会将所有跟踪

的 metrics 指标的当前状态发送到 prometheus server 端。 3.Exporters:

prometheus 支持多种 exporter,通过 exporter 可以采集 metrics 数据,然后发送到 prometheus server 端,所有向 promtheus server 提供监控数据的程序都可以被称为 exporter

4.Alertmanager:

从 Prometheus server 端接收到 alerts 后,会进行去重,分组,并路由到相应的接收方,发出 报警,常见的接收方式有:电子邮件,微信,钉钉, slack 等。

5.Grafana:

监控仪表盘,可视化监控数据

6.pushgateway:

各个目标主机可上报数据到 pushgatewy,然后 prometheus server 统一从 pushgateway 拉取

数据。

1.4 prometheus 几种部署模式

基本 HA 模式

基本的 HA 模式只能确保 Promthues 服务的可用性问题,但是不解决 Prometheus Server 之间 的数据一致性问题以及持久化问题(数据丢失后无法恢复),也无法进行动态的扩展。因此这种部署方式适 合监控规模不大,Promthues Server 也不会频繁发生迁移的情况,并且只需要保存短周期监控数据的 场景。

基本 HA + 远程存储方案

在解决了 Promthues 服务可用性的基础上,同时确保了数据的持久化,当 Promthues Server 发 生宕机或者数据丢失的情况下,可以快速的恢复。 同时 Promthues Server 可能很好的进行迁移。因 此,该方案适用于用户监控规模不大,但是希望能够将监控数据持久化,同时能够确保 Promthues Server 的可迁移性的场景。

基本 HA + 远程存储 + 联邦集群方案

Promthues 的性能瓶颈主要在于大量的采集任务,因此用户需要利用 Prometheus 联邦集群的特 性,将不同类型的采集任务划分到不同的 Promthues 子服务中,从而实现功能分区。例如一个 Promthues Server 负责采集基础设施相关的监控指标,另外一个 Prometheus Server 负责采集应用 监控指标。再有上层 Prometheus Server 实现对数据的汇聚。

1.5 prometheus 工作流程

1. Prometheus server 可定期从活跃的(up)目标主机上(target)拉取监控指标数据,目标主

机的监控数据可通过配置静态 job 或者服务发现的方式被 prometheus server 采集到,这种方式默认的 pull 方式拉取指标;也可通过 pushgateway 把采集的数据上报到 prometheus server 中;还可通过 一些组件自带的 exporter 采集相应组件的数据;

2.Prometheus server 把采集到的监控指标数据保存到本地磁盘或者数据库;

3.Prometheus 采集的监控指标数据按时间序列存储,通过配置报警规则,把触发的报警发送到 alertmanager

4.Alertmanager 通过配置报警接收方,发送报警到邮件,微信或者钉钉等 

5.Prometheus 自带的 web ui 界面提供 PromQL 查询语言,可查询监控数据

6.Grafana 可接入 prometheus 数据源,把监控数据以图形化形式展示出

1.6 prometheus 如何更好的监控 k8s?

对于 Kubernetes 而言,我们可以把当中所有的资源分为几类:

1、基础设施层(Node):集群节点,为整个集群和应用提供运行时资源 2、容器基础设施(Container):为应用提供运行时环境

3、用户应用(Pod):Pod 中会包含一组容器,它们一起工作,并且对外提供一个(或者 一组)功能

4、内部服务负载均衡(Service):在集群内,通过 Service 在集群暴露应用功能,集群内 应用和应用之间访问时提供内部的负载均衡

5、外部访问入口(Ingress):通过 Ingress 提供集群外的访问入口,从而可以使外部客户 端能够访问到部署在 Kubernetes 集群内的服务

因此,在不考虑 Kubernetes 自身组件的情况下,如果要构建一个完整的监控体系,我们应该 考虑,以下 5 个方面:

1)、集群节点状态监控:从集群中各节点的 kubelet 服务获取节点的基本运行状态; 

2)、集群节点资源用量监控:通过 Daemonset 的形式在集群中各个节点部署 Node Exporter 采集节点的资源使用情况;

3)、节点中运行的容器监控:通过各个节点中 kubelet 内置的 cAdvisor 中获取个节点中所有容器的运行状态和资源使用情况;

4)、从黑盒监控的角度在集群中部署 Blackbox Exporter 探针服务,检测 Service 和 Ingress 的可用性;

5)、如果在集群中部署的应用程序本身内置了对 Prometheus 的监控支持,那么我们还应该找 到相应的 Pod 实例,并从该 Pod 实例中获取其内部运行状态的监控指标。

2安装采集节点资源指标组件 node-exporter

node-exporter 是什么?

采集机器(物理机、虚拟机、云主机等)的监控指标数据,能够采集到的指标包括 CPU, 内存,磁 盘,网络,文件数等信息。

安装 node-exporter 组件,在 k8s 集群的控制节点操作

# kubectl create ns monitor-sa

把课件里的 node-exporter.tar.gz 镜像压缩包上传到 k8s 的各个节点,

手动解压:  docker load -i node-exporter.tar.gz

 docker load -i node-exporter.tar.gz

node-export.yaml 文件在课件,可自行上传到自己 k8s 的控制节点,内容如下: 

# cat node-export.yaml

apiVersion: apps/v1

kind: DaemonSet

metadata:

  name: node-exporter

  namespace: monitor-sa

  labels:

    name: node-exporter

spec:

  selector:

    matchLabels:

    name: node-exporter

  template:

    metadata:

      labels:

        name: node-exporter

    spec:

      hostPID: true

      hostIPC: true

      hostNetwork: true

      containers:

      - name: node-exporter

        image: prom/node-exporter:v0.16.0

        ports:

        - containerPort: 9100

        resources:

          requests:

            cpu: 0.15

        securityContext:

          privileged: true

        args:

        - --path.procfs

        - /host/proc

        - --path.sysfs

        - /host/sys

        - --collector.filesystem.ignored-mount-points

        - '"^/(sys|proc|dev|host|etc)($|/)"'

        volumeMounts:

        - name: dev

          mountPath: /host/dev

        - name: proc

          mountPath: /host/proc

        - name: sys

          mountPath: /host/sys

        - name: rootfs

          mountPath: /rootfs

      tolerations:

      - key: "node-role.kubernetes.io/master"

        operator: "Exists"

        effect: "NoSchedule"

      volumes:

        - name: proc

          hostPath:

            path: /proc

        - name: dev

          hostPath:

            path: /dev

        - name: sys

          hostPath:

            path: /sys

        - name: rootfs

          hostPath:

            path: /

kubectl apply -f node-export.yaml

#查看 node-exporter 是否部署成功

kubectl get pods -n monitor-sa

NAME                  READY  STATUS    RESTARTS  AGE

node-exporter-5dh6b  1/1    Running  0          11m

node-exporter-qd9f7  1/1    Running  0          11m

通过 node-exporter 采集数据

curl http://主机 ip:9100/metrics

#node-export 默认的监听端口是 9100,可以看到当前主机获取到的所有监控数据

curl http://192.168.172.163:9100/metrics | grep node_cpu_seconds

显示 192.168.172.163 主机 cpu 的使用情况

curl http://192.168.172.163:9100/metrics | grep node_cpu_seconds

  % Total    % Received % Xferd  Average Speed  Time    Time    Time  Current

                                Dload  Upload  Total  Spent    Left  Speed

100 78237  100 78237    0    0  10.5M      0 --:--:-- --:--:-- --:--:-- 12.4# HELP node_cpu_seconds_total Seconds the cpus spent in each mode.

M

# TYPE node_cpu_seconds_total counter

node_cpu_seconds_total{cpu="0",mode="idle"} 64776.09

node_cpu_seconds_total{cpu="0",mode="iowait"} 16.41

node_cpu_seconds_total{cpu="0",mode="irq"} 0

node_cpu_seconds_total{cpu="0",mode="nice"} 0.63

node_cpu_seconds_total{cpu="0",mode="softirq"} 121.24#HELP:解释当前指标的含义,上面表示在每种模式下 node 节点的 cpu 花费的时间,以 s 为单位 #TYPE:说明当前指标的数据类型,上面是 counter 类型 node_cpu_seconds_total{cpu="0",mode="idle"} :

cpu0 上 idle 进程占用 CPU 的总时间,CPU 占用时间是一个只增不减的度量指标,从类型中也可以 看出 node_cpu 的数据类型是 counter(计数器)

counter 计数器:只是采集递增的指标

curl http://192.168.172.163:9100/metrics | grep node_load

# HELP node_load1 1m load average.

# TYPE node_load1 gauge

node_load1 0.1

node_load1 

该指标反映了当前主机在最近一分钟以内的负载情况,系统的负载情况会随系统资源的

使用而变化,因此 node_load1 反映的是当前状态,数据可能增加也可能减少,从注释中可以看出当前 指标类型为 gauge(标准尺寸)

gauge 标准尺寸:统计的指标可增加可减少

3.在 k8s 集群中安装 Prometheus server 服务

3.1创建 sa 账号

#在 k8s 集群的控制节点操作,创建一个 sa 账号

kubectl create serviceaccount monitor -n monitor-sa

#把 sa 账号 monitor 通过 clusterrolebing 绑定到 clusterrole 上

kubectl create clusterrolebinding monitor-clusterrolebinding -n monitor-sa --clusterrole=cluster-admin --serviceaccount=monitor-sa:monitor

#执行上面授权也会报错,那就需要下面的授权命令:

kubectl create clusterrolebinding monitor-clusterrolebinding-1 -n monitor-sa -- clusterrole=cluster-admin --user=system:serviceaccount:monitor:monitor-sa

3.2 创建数据目录

#在 god64 作节点创建存储数据的目录: 

]# mkdir /data 

]# chmod 777 /data/

3.3 安装 prometheus 服务

以下步骤均在 

k8s 集群的控制节点操作:

创建一个 configmap 存储卷,用来存放 prometheus 配置信息 prometheus-cfg.yaml ,k8s 的控制节点

安装 prometheus server镜像 prometheus-2-2-1.tar.gz,上传到 k8s 的工作节点

cat prometheus-deploy.yaml 

apiVersion: apps/v1

kind: Deployment

metadata:

  name: prometheus-server

  namespace: monitor-sa

  labels:

    app: prometheus

spec:

  replicas: 1

  selector:

    matchLabels:

      app: prometheus

      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: god64

      serviceAccountName: monitor

      containers:

      - name: prometheus

        image: prom/prometheus:v2.2.1

        imagePullPolicy: IfNotPresent

        command:

          - prometheus

          - --config.file=/etc/prometheus/prometheus.yml

          - --storage.tsdb.path=/prometheus  #旧数据存储目录

          - --storage.tsdb.retention=720h    #何时删除旧数据,默认为 15 天。

          - --web.enable-lifecycle          #开启热加载

        ports:

        - containerPort: 9090

          protocol: TCP

        volumeMounts:

        - mountPath: /etc/prometheus/prometheus.yml

          name: prometheus-config

          subPath: prometheus.yml

        - mountPath: /prometheus

          name: prometheus-storage-volume

      volumes:

        - name: prometheus-config

          configMap:

            name: prometheus-config

            items:

            - key: prometheus.yml

              path: prometheus.yml

              mode: 0644

        - name: prometheus-storage-volume

          hostPath:

            path: /data

            type: Directory

工作 节点我们创建了数据目录/data,你在 k8s 集群的哪个节点创建/data,就让 pod 调度到哪个节点。

kubectl apply -f prometheus-deploy.yaml

#查看 prometheus 是否部署成功

kubectl get pods -n monitor-sa

NAME                                READY  STATUS    RESTARTS  AGE

node-exporter-5dh6b                  1/1    Running  0          4d1h

node-exporter-qd9f7                  1/1    Running  0          4d1h

prometheus-server-5bc47cc46d-6wn4t  1/1    Running  0          2m49s

给 prometheus pod 创建一个 service

cat prometheus-svc.yaml

apiVersion: v1

kind: Service

metadata:

  name: prometheus

  namespace: monitor-sa

  labels:

    app: prometheus

spec:

  type: NodePort

  ports:

    - port: 9090

      targetPort: 9090

      protocol: TCP

  selector:

    app: prometheus

    component: server

kubectl apply -f prometheus-svc.yaml

#查看 service 在物理机映射的端口

kubectl get svc -n monitor-sa

NAME        TYPE      CLUSTER-IP      EXTERNAL-IP  PORT(S)          AGE

prometheus  NodePort  10.106.198.175          9090:31994/TCP  28s

通过上面可以看到 service 在宿主机上映射的端口是 30009,这样我们访问 k8s 集群的控制节点的 ip:30009,就可以访问到 prometheus 的 web ui 界面了

#访问 prometheus web ui 界面 火狐浏览器输入如下地址:

http://192.168.172.164:31994/graph

kubectl get pods -n monitor-sa -o wide

NAME                                READY  STATUS    RESTARTS  AGE    IP                NODE      NOMINATED NODE  READINESS GATES

node-exporter-5dh6b                  1/1    Running  0          4d4h  192.168.172.164     

node-exporter-qd9f7                  1/1    Running  0          4d4h  192.168.172.163    

prometheus-server-5bc47cc46d-6wn4t  1/1    Running  0          143m  10.244.209.134    

curl -X POST http://10.244.209.134:9090/-/reload

你可能感兴趣的:(Prometheus -1-1-1)