由于 Prometheus 是 Golang 编写的程序,所以要安装的话也非常简单,只需要将二进制文件下载下来直接执行即可,前往地址:https://prometheus.io/download 下载我们对应的版本即可。
    Prometheus 是通过一个 YAML 配置文件来进行启动的,如果我们使用二进制的方式来启动的话,可以使用下面的命令:

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

    其中 prometheus.yml 文件的基本配置如下:

    1. global:
    2. scrape_interval: 15s
    3. evaluation_interval: 15s
    4. rule_files:
    5. # - "first.rules"
    6. # - "second.rules"
    7. scrape_configs:
    8. - job_name: prometheus
    9. static_configs:
    10. - targets: ['localhost:9090']

    上面这个配置文件中包含了3个模块:global、rule_files 和 scrape_configs。
    其中 global 模块控制 Prometheus Server 的全局配置:

    • scrape_interval:表示 prometheus 抓取指标数据的频率,默认是15s,我们可以覆盖这个值
    • evaluation_interval:用来控制评估规则的频率,prometheus 使用规则产生新的时间序列数据或者产生警报

    rule_files 模块制定了规则所在的位置,prometheus 可以根据这个配置加载规则,用于生成新的时间序列数据或者报警信息,当前我们没有配置任何规则。
    scrape_configs 用于控制 prometheus 监控哪些资源。由于 prometheus 通过 HTTP 的方式来暴露的它本身的监控数据,prometheus 也能够监控本身的健康情况。在默认的配置里有一个单独的 job,叫做prometheus,它采集 prometheus 服务本身的时间序列数据。这个 job 包含了一个单独的、静态配置的目标:监听 localhost 上的9090端口。prometheus 默认会通过目标的/metrics路径采集 metrics。所以,默认的 job 通过 URL:http://localhost:9090/metrics采集 metrics。收集到的时间序列包含 prometheus 服务本身的状态和性能。如果我们还有其他的资源需要监控的话,直接配置在该模块下面就可以了。

    由于我们这里是要跑在 Kubernetes 系统中,所以我们直接用 Docker 镜像的方式运行即可。

    为了能够方便的管理配置文件,我们这里将 prometheus.yml 文件用 ConfigMap 的形式进行管理:(prometheus-cm.yaml)

    1. apiVersion: v1
    2. kind: ConfigMap
    3. metadata:
    4. name: prometheus-config
    5. namespace: kube-ops
    6. data:
    7. prometheus.yml: |
    8. global:
    9. scrape_interval: 15s
    10. scrape_timeout: 15s
    11. scrape_configs:
    12. - job_name: 'prometheus'
    13. static_configs:
    14. - targets: ['localhost:9090']

    我们这里暂时只配置了对 prometheus 的监控,然后创建该资源对象:

    1. kubectl create -f prometheus-cm.yaml

    配置文件创建完成了,以后如果我们有新的资源需要被监控,我们只需要将上面的 ConfigMap 对象更新即可。现在我们来创建 prometheus 的 Pod 资源:(prometheus-deploy.yaml)

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: prometheus
    5. namespace: kube-ops
    6. labels:
    7. app: prometheus
    8. spec:
    9. selector:
    10. matchLabels:
    11. app: prometheus
    12. template:
    13. metadata:
    14. labels:
    15. app: prometheus
    16. spec:
    17. serviceAccountName: prometheus
    18. containers:
    19. - image: prom/prometheus:v2.4.3
    20. name: prometheus
    21. command:
    22. - "/bin/prometheus"
    23. args:
    24. - "--config.file=/etc/prometheus/prometheus.yml"
    25. - "--storage.tsdb.path=/prometheus"
    26. - "--storage.tsdb.retention=24h"
    27. - "--web.enable-admin-api" # 控制对admin HTTP API的访问,其中包括删除时间序列等功能
    28. - "--web.enable-lifecycle" # 支持热更新,直接执行localhost:9090/-/reload立即生效
    29. ports:
    30. - containerPort: 9090
    31. protocol: TCP
    32. name: http
    33. volumeMounts:
    34. - mountPath: "/prometheus"
    35. subPath: prometheus
    36. name: data
    37. - mountPath: "/etc/prometheus"
    38. name: config-volume
    39. resources:
    40. requests:
    41. cpu: 100m
    42. memory: 512Mi
    43. limits:
    44. cpu: 100m
    45. memory: 512Mi
    46. securityContext:
    47. runAsUser: 0
    48. volumes:
    49. - name: data
    50. persistentVolumeClaim:
    51. claimName: prometheus
    52. - configMap:
    53. name: prometheus-config
    54. name: config-volume

    我们在启动程序的时候,除了指定了 prometheus.yml 文件之外,还通过参数storage.tsdb.path指定了 TSDB 数据的存储路径、通过storage.tsdb.retention设置了保留多长时间的数据,还有下面的web.enable-admin-api参数可以用来开启对 admin api 的访问权限,参数web.enable-lifecycle非常重要,用来开启支持热更新的,有了这个参数之后,prometheus.yml 配置文件只要更新了,通过执行localhost:9090/-/reload就会立即生效,所以一定要加上这个参数。
    我们这里将 prometheus.yml 文件对应的 ConfigMap 对象通过 volume 的形式挂载进了 Pod,这样 ConfigMap 更新后,对应的 Pod 里面的文件也会热更新的,然后我们再执行上面的 reload 请求,Prometheus 配置就生效了,除此之外,为了将时间序列数据进行持久化,我们将数据目录和一个 pvc 对象进行了绑定,所以我们需要提前创建好这个 pvc 对象:(prometheus-volume.yaml)

    1. apiVersion: v1
    2. kind: PersistentVolume
    3. metadata:
    4. name: prometheus
    5. spec:
    6. capacity:
    7. storage: 10Gi
    8. accessModes:
    9. - ReadWriteOnce
    10. persistentVolumeReclaimPolicy: Recycle
    11. nfs:
    12. server: 10.151.30.57
    13. path: /data/k8s
    14. ---
    15. apiVersion: v1
    16. kind: PersistentVolumeClaim
    17. metadata:
    18. name: prometheus
    19. namespace: kube-ops
    20. spec:
    21. accessModes:
    22. - ReadWriteOnce
    23. resources:
    24. requests:
    25. storage: 10Gi

    我们这里简单的通过 NFS 作为存储后端创建一个 pv、pvc 对象:

    1. kubectl create -f prometheus-volume.yaml

    除了上面的注意事项外,我们这里还需要配置 rbac 认证,因为我们需要在 prometheus 中去访问 Kubernetes 的相关信息,所以我们这里管理了一个名为 prometheus 的 serviceAccount 对象:(prometheus-rbac.yaml)

    1. apiVersion: v1
    2. kind: ServiceAccount
    3. metadata:
    4. name: prometheus
    5. namespace: kube-ops
    6. ---
    7. apiVersion: rbac.authorization.k8s.io/v1
    8. kind: ClusterRole
    9. metadata:
    10. name: prometheus
    11. rules:
    12. - apiGroups:
    13. - ""
    14. resources:
    15. - nodes
    16. - services
    17. - endpoints
    18. - pods
    19. - nodes/proxy
    20. verbs:
    21. - get
    22. - list
    23. - watch
    24. - apiGroups:
    25. - ""
    26. resources:
    27. - configmaps
    28. - nodes/metrics
    29. verbs:
    30. - get
    31. - nonResourceURLs:
    32. - /metrics
    33. verbs:
    34. - get
    35. ---
    36. apiVersion: rbac.authorization.k8s.io/v1beta1
    37. kind: ClusterRoleBinding
    38. metadata:
    39. name: prometheus
    40. roleRef:
    41. apiGroup: rbac.authorization.k8s.io
    42. kind: ClusterRole
    43. name: prometheus
    44. subjects:
    45. - kind: ServiceAccount
    46. name: prometheus
    47. namespace: kube-ops

    由于我们要获取的资源信息,在每一个 namespace 下面都有可能存在,所以我们这里使用的是 ClusterRole 的资源对象,值得一提的是我们这里的权限规则声明中有一个nonResourceURLs的属性,是用来对非资源型 metrics 进行操作的权限声明,这个在以前我们很少遇到过,然后直接创建上面的资源对象即可:

    1. kubectl create -f prometheus-rbac.yaml

    还有一个要注意的地方是我们这里必须要添加一个securityContext的属性,将其中的runAsUser设置为0,这是因为现在的 prometheus 运行过程中使用的用户是 nobody,否则会出现permission denied之类的权限错误
    现在我们就可以添加 promethues 的资源对象了:

    1. kubectl create -f prometheus-deploy.yaml
    2. kubectl get pods -n kube-ops

    Pod 创建成功后,为了能够在外部访问到 prometheus 的 webui 服务,我们还需要创建一个 Service 对象:(prometheus-svc.yaml)

    1. apiVersion: v1
    2. kind: Service
    3. metadata:
    4. name: prometheus
    5. namespace: kube-ops
    6. labels:
    7. app: prometheus
    8. spec:
    9. selector:
    10. app: prometheus
    11. type: NodePort
    12. ports:
    13. - name: web
    14. port: 9090
    15. targetPort: http

    为了方便测试,我们这里创建一个NodePort类型的服务,当然我们可以创建一个Ingress对象,通过域名来进行访问:

    1. kubectl create -f prometheus-svc.yaml
    2. kubectl get svc -n kube-ops

    然后我们就可以通过http://任意节点IP:30987访问 prometheus 的 WebUi 服务了。