DevOps CICD K8s DockerPrometheus是由SoundCloud開發的一個open source監控警告系統,基於Go語言,是Google BorgMon監控系統的開源版本。2016年,CNCF組織將 Prometheus 納入其第二大開源專案 (第一當然就是 Kubernetes 啦~)。隨著 Kubernetes 在容器排程和管理上確定領先的地位,Prometheus也成為Kubernetes容器監控的標準配備。
Prometheus 主要區別於其他監控系統的特點是:
Prometheus附帶的時序資料庫,可以完成每秒千萬級的資料儲存HTTP為基礎的pull方式收集時序資料,並支援以push方式向中間gateway發送時序資料,更能靈活地應對多種監控場景
Source:https://prometheus.io/docs/introduction/overview/
Prometheus的基本原理是透過HTTP週期性抓取被監控元件的狀態,任意元件只要提供對應的HTTP介面並且符合 Promethues 定義的資料格式,就可以連線 Prometheus 監控。(上圖為 Prometheus 整體架構)
這邊就不細講各元件之間個功能及運作方式了,畢竟筆者也不是很懂:(,而且也不是我們主要的內容,有興趣的讀者再去了解囉~
這邊範例的執行環境是我們在【Day3】建立K8s Cluster環境介紹過的以kubeadm架設的K8s集群。下面介紹如何在K8s中使用Prometheus的安裝與設定 (安裝步驟參考 [Prometheus掌控主機、VM、容器即K8S] 這本書):
這邊安裝會用到之前介紹過的物件喔,還不熟的讀者可以參考我們前面的介紹
為了方便統一管理,我們將 Prometheus 所需用到的 resource-objects 都佈署在名為 kube-ops 的 namespace 下,所以我們先創建此namespace:
kubectl create ns kube-ops
Prometheus 在啟動時會透過一個YAML檔進行設定,在K8s集群使用一個ConfigMap對應即可。這邊先建立一個ConfigMap的YAML:
$ cat prometheus-cm.yaml
apiVersion: v1       
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: kube-ops
data:
  prometheus.yml: |
    global: 
      scrape_interval: 15s 
      scrape_timeout: 15s 
    scrape_configs: 
    - job_name: 'prometheus'
      static_configs: 
      - targets: ['localhost:9090']
要讓 Prometheus 的 Pod 取得更新,就可以將 ConfigMap 以Volume 形式掛載到 Pod 中,這樣Pod中對應的設定檔也會做對應的即時更新。
這邊示範用 Deployment 佈署 Prometheus Pod:
$ cat prometheus-deploy.yaml
apiVersion: apps/v1        
kind: Deployment
metadata:
  labels:
    app: prometheus
  name: prometheus
  namespace: kube-ops
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      serviceAccountName: prometheus
      containers:
      - image: prom/prometheus:v2.21.0
        name: prometheus
        command: 
        - "/bin/prometheus"
        args: 
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--storage.tsdb.path=/prometheus"
        - "--storage.tsdb.retention=24h"
        - "--web.enable-lifecycle"
        ports: 
        - containerPort: 9090
          protocol: TCP 
          name: http
        volumeMounts:
        - mountPath: "/prometheus"
          subPath: prometheus
          name: data
        - mountPath: "/etc/prometheus"
          name: config-volume
        resources:
          requests:
            cpu: 100m
            memory: 512Mi
          limits: 
            cpu: 100m
            memory: 512Mi
      securityContext:
        runAsUser: 0
      volumes:
      - name: data
        emptyDir: {}
      - configMap:
          name: prometheus-config
        name: config-volume    
下面說明一下部分的參數設定:
--config.file:此參數指定設定檔,然後將上面建立的ConfigMap檔案以Volume形式掛載到Pod中--storage.tsdb.path:此參數指定了內建TSDB檔案儲存的路徑,這裡只是為了簡單說明,使用了最簡單的emptyDir來掛載,如果是在現實環境中使用,則這邊的Volume應該使用前面介紹過的PV/PVC方式--storage.tsdb.retention:此參數定義何時要將舊的資料移除。系統預設是15DAYS。可透過此參數改寫--web.enable-lifecycle:此參數開啟熱更新,在使用該參數後,設定檔若發生任何變化,則都可以透過HTTP POST請求localhost:9090/-/reload介面操作使之立即生效為了讓集群內的 Pod 安全存取Kubernetes介面,Kubernetes提供了ServiceAccount 物件,其原理是將存取Kubernetes介面所需的認證憑據 (token和CA憑證) 掛載到容器內部,以便應用程式透過這些憑證存取K8s介面。K8s在後端會對這些憑證進行認證和授權。Prometheus 的服務發現機制正是依賴Kubernetes API實現的,所以需要先為 Prometheus 容器建立一個 ServiceAccount 物件。此外,ServiceAcoount 雖然解決了使用者的認證問題,但還需透過RBAC 設定存取權限。設定YAML檔如下:
$ cat prometheus-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: kube-ops
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata: 
  name: prometheus
rules:
- apiGroups:
  - ""
  resources: 
  - nodes
  - services
  - endpoints
  - pods
  - nodes/proxy
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources: 
  - configmaps
  - nodes/metrics
  verbs:
  - get
- nonResourceURLs:
  - /metrics
  verbs:
  - get
---
apiVersion: rbac.authorization.k8s.io/v1
kind:  ClusterRoleBinding
metadata:
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: kube-ops
roleRef:
  kind: ClusterRole
  name: prometheus
  apiGroup: rbac.authorization.k8s.io    
ServiceAccount物件因為在CKA考試中不是重點,所以筆者就沒有特別介紹,但是這邊有用到該物件,就留給大家自行補充囉~
名為 Prometheus 的 ServiceAccount 物件綁定了一個ClusterRole,此 ClusterRole宣告了 Prometheus Pod 存取集群所需的許可權規則。
在 Pod 建立完畢後,需要將服務expose給外部使用者進行存取,可以透過 Service 來完成該功能,新增一個 Service 的YAML:
$ cat prometheus-svc.yaml
apiVersion: v1       
kind: Service
metadata: 
  name: prometheus
  namespace: kube-ops
  labels:
    app: prometheus
spec: 
  selector: 
    app: prometheus
  type: NodePort 
  ports:
    - name: web 
      port: 9090
      targetPort: 9090
      nodePort: 31111
接著我們將這些物件分別創建:
接著我們看一下我們創立的物件
可以看到,Prometheus Pod是運行在g8node2上,Service expose的port是31111。因此我們可以透過瀏覽器輸入<g8node2-ip>:31111位址來檢視 Prometheus Dashboard 囉~
今天介紹另一種K8s集群監控工具 -- Prometheus,主要讓大家能了解他的用途以及安裝方法,剩下後面的應用就交給讀者們自行摸索囉~ 好啦,今天就到這囉~ 謝謝大家~
Prometheus
Prometheus掌控主機、VM、容器即K8S
You can find me on