安装 Prometheus

官网

https://prometheus.io/

GitHub

https://github.com/coreos/kube-prometheus

组件说明

安装

下载
$ git clone https://github.com/coreos/kube-prometheus.git
$ cd kube-prometheus/manifests

修改 grafana-service.yaml 文件,使用 nodepode 方式访问 grafana:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: grafana
  name: grafana
  namespace: monitoring
spec:
  type: NodePort
  ports:
  - name: http
    port: 3000
    targetPort: http
    nodePort: 31100
  selector:
    app: grafana

修改 prometheus-service.yaml,改为 nodepode:

apiVersion: v1
kind: Service
metadata:
  labels:
    prometheus: k8s
  name: prometheus-k8s
  namespace: monitoring
spec:
  type: NodePort
  ports:
  - name: web
    port: 9090
    targetPort: web
    nodePort: 31200
  selector:
    app: prometheus
    prometheus: k8s
  sessionAffinity: ClientIP

修改 alertmanager-service.yaml,改为 nodepode

apiVersion: v1
kind: Service
metadata:
  labels:
    alertmanager: main
  name: alertmanager-main
  namespace: monitoring
spec:
  type: NodePort
  ports:
  - name: web
    port: 9093
    targetPort: web
    nodePort: 31300
  selector:
    alertmanager: main
    app: alertmanager
  sessionAffinity: ClientIP

创建名称空间和CRD

$ kubectl create -f manifests/setup

等待资源可用后,安装

$ kubectl create -f manifests/

访问

访问 Prometheus

http://MasterIP:31200/graph

访问 Grafana

http://MasterIP:31100

访问报警平台 AlertManager

http://MasterIP:31300/

Prometheus PromQL

Prometheus提供一个函数式的表达式语言PromQL (Prometheus Query Language),可以使用户实时地查找和聚合时间序列数据。

获取集群级别的CPU使用率:

sum (rate (container_cpu_usage_seconds_total{id="/"}[1m])) / sum (machine_cpu_cores) * 100

每个Pod的CPU使用率:

sum (rate (container_cpu_usage_seconds_total{image!=""}[1m])) by (pod_name)