Kubernetes使用prometheus+grafana作一個簡單的監控方案

前言

本文介紹在k8s集羣中使用node-exporter、prometheus、grafana對集羣進行監控。
其實現原理有點相似ELK、EFK組合。node-exporter組件負責收集節點上的metrics監控數據,並將數據推送給prometheus, prometheus負責存儲這些數據,grafana將這些數據經過網頁以圖形的形式展示給用戶。java

在開始以前有必要了解下Prometheus是什麼?
Prometheus (中文名:普羅米修斯)是由 SoundCloud 開發的開源監控報警系統和時序列數據庫(TSDB).自2012年起,許多公司及組織已經採用 Prometheus,而且該項目有着很是活躍的開發者和用戶社區.如今已經成爲一個獨立的開源項目。Prometheus 在2016加入 CNCF ( Cloud Native Computing Foundation ), 做爲在 kubernetes 以後的第二個由基金會主持的項目。 Prometheus 的實現參考了Google內部的監控實現,與源自Google的Kubernetes結合起來很是合適。另外相比influxdb的方案,性能更加突出,並且還內置了報警功能。它針對大規模的集羣環境設計了拉取式的數據採集方式,只須要在應用裏面實現一個metrics接口,而後把這個接口告訴Prometheus就能夠完成數據採集了,下圖爲prometheus的架構圖。node

 

Prometheus的特色:
一、多維數據模型(時序列數據由metric名和一組key/value組成)
二、在多維度上靈活的查詢語言(PromQl)
三、不依賴分佈式存儲,單主節點工做.
四、經過基於HTTP的pull方式採集時序數據
五、能夠經過中間網關進行時序列數據推送(pushing)
六、目標服務器能夠經過發現服務或者靜態配置實現
七、多種可視化和儀表盤支持python

prometheus 相關組件,Prometheus生態系統由多個組件組成,其中許可能是可選的:
一、Prometheus 主服務,用來抓取和存儲時序數據
二、client library 用來構造應用或 exporter 代碼 (go,java,python,ruby)
三、push 網關可用來支持短鏈接任務
四、可視化的dashboard (兩種選擇,promdash 和 grafana.目前主流選擇是 grafana.)
四、一些特殊需求的數據出口(用於HAProxy, StatsD, Graphite等服務)
五、實驗性的報警管理端(alartmanager,單獨進行報警彙總,分發,屏蔽等 )linux

promethues 的各個組件基本都是用 golang 編寫,對編譯和部署十分友好.而且沒有特殊依賴.基本都是獨立工做。

golang

部署

如今咱們正式開始部署工做。這裏假設你已經爲你的K8S集羣部署過kube-dns或者coredns了。
1、環境介紹
操做系統環境:centos linux 7.5 64bit
K8S軟件版本: 1.12.3
Master節點IP: 10.40.0.151/24web

Node01節點IP: 10.40.0.152/24數據庫

Node02節點IP: 10.40.0.153/24json

 

2、採用daemonset方式部署node-exporter組件centos

cat 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: 31672
    protocol: TCP
  type: NodePort
  selector:
    k8s-app: node-exporter

3、部署prometheus組件api

一、rbac文件

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組件的配置文件

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 deployment 文件

cat prometheus.yaml 

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:v2.0.0
        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: data
        - mountPath: "/etc/prometheus"
          name: config-volume
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 500m
            memory: 2500Mi
      serviceAccountName: prometheus    
      volumes:
      - name: data
        emptyDir: {}
      - name: config-volume
        configMap:
          name: prometheus-config      
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: prometheus
  name: prometheus
  namespace: kube-system
spec:
  type: NodePort
  ports:
  - port: 9090
    targetPort: 9090
    nodePort: 30003
  selector:
    app: prometheus

 

四、經過上述yaml文件建立相應的對象

kubectl create -f node-exporter.yaml
kubectl create -f rbac-setup.yaml
kubectl create -f configmap.yaml
kubectl create -f promethues.yaml

 

五、查看相關pod和service

# kubectl get pods -n kube-system
NAME                                   READY   STATUS    RESTARTS   AGE
coredns-779dfc4d59-rtpmk               1/1     Running   0          48s
kubernetes-dashboard-b54f75c69-tnn4h   1/1     Running   0          90m
node-exporter-sflqg                    1/1     Running   0          9m44s
node-exporter-xfsf8                    1/1     Running   0          9m44s
prometheus-58dc44f44c-z86rv            1/1     Running   0          8m44s
# kubectl get svc -n kube-system
NAME                   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kube-dns               ClusterIP   10.250.0.2     <none>        53/UDP,53/TCP    117s
kubernetes-dashboard   NodePort    10.250.1.89    <none>        443:38443/TCP    102m
node-exporter          NodePort    10.250.0.165   <none>        9100:31672/TCP   10m
prometheus             NodePort    10.250.0.53    <none>        9090:30003/TCP   9m53s

 

六、Node-exporter對應的nodeport端口爲31672,經過訪問http://10.40.0.152:31672/metrics 能夠看到對應的metrics

 

七、prometheus對應的nodeport端口爲30003,經過訪問http://10.40.0.152:30003/targets 能夠看到prometheus已經成功鏈接上了k8s的apiserver

 

八、在prometheus的WEB界面上提供了基本的查詢K8S集羣中每一個POD的CPU使用狀況,可使用以下查詢條件查詢:

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

 

上述的查詢有出現數據,說明node-exporter往prometheus中寫入數據正常,接下來咱們就能夠部署grafana組件,實現更友好的webui展現數據了。

 

5、部署grafana組件
一、grafana deployment配置文件

cat grafana.yaml

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.0.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-persistent-storage
          mountPath: /var
      volumes:
      - name: grafana-persistent-storage
        emptyDir: {}

---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: kube-system
  labels:
    app: grafana
    component: core
spec:
  type: NodePort
  ports:
    - port: 3000
      nodePort: 31000
  selector:
    app: grafana

部署grafana

kubectl create -f grafana.yaml

 

查看grafana pod和service

# kubectl get pod -n kube-system
NAME                                   READY   STATUS    RESTARTS   AGE
coredns-779dfc4d59-rtpmk               1/1     Running   0          101m
grafana-core-6759c8945-5f4sv           1/1     Running   0          91m
kubernetes-dashboard-b54f75c69-tnn4h   1/1     Running   0          3h11m
node-exporter-sflqg                    1/1     Running   0          110m
node-exporter-xfsf8                    1/1     Running   0          110m
prometheus-58dc44f44c-z86rv            1/1     Running   0          109m
# kubectl get svc -n kube-system
NAME                   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
grafana                NodePort    10.250.1.230   <none>        3000:31000/TCP   93m
kube-dns               ClusterIP   10.250.0.2     <none>        53/UDP,53/TCP    103m
kubernetes-dashboard   NodePort    10.250.1.89    <none>        443:38443/TCP    3h23m
node-exporter          NodePort    10.250.0.165   <none>        9100:31672/TCP   112m
prometheus             NodePort    10.250.0.53    <none>        9090:30003/TCP   111m

 

能夠看到grafana nodeport端口爲31000,可以使用nodeip+nodeport的方式訪問grafana  http://10.40.0.152:31000

 

默認用戶名和密碼都是admin

配置數據庫源爲prometheus,導入面板

 

 

能夠直接輸入模板編號315在線導入,或者下載好對應的json模板文件本地導入,面板模板下載地址https://grafana.com/dashboards/315

 

在線加載模板OK,選擇prometheus數據庫實例

 

 大功告成,能夠看到炫酷的監控頁面了。

相關文章
相關標籤/搜索