grafana-kubernetes plugin

簡介

grafanakubernetes 提供了一套模板,用來展示集羣的運行負載。

github: https://github.com/grafana/kubernetes-app

關於監控的指標和界面展示官方文檔已經很詳細了,這裏不再贅述。

值得一提的是,要使用該模板插件,需要部署kube-state-merticsnode-exporter以及prometheus,grafana

使用組件鏡像如下如下:

image_name tag
prom/prometheus v2.2.1
grafana/grafana 5.1.2
gcr.io/google_containers/kube-state-metrics v1.3.1
quay.io/prometheus/node-exporter v0.15.0

以下部署環境基於k8s 1.8.1,截止2018/08/03 爲止kuberetes 插件最近版本爲 1.0.1

部署

prometheus

  1. 先創建一個名爲prometheusnamespace
$ kubectl create ns prometheus
  1. prometheus添加rbac權限認證
$ wget https://github.com/prometheus/prometheus/blob/master/documentation/examples/rbac-setup.yml
$ kubectl apply -f rbac-setup.yaml
  1. 爲prometheus 添加 configmap

prometheus配置可以使用 https://github.com/prometheus/prometheus/blob/master/documentation/examples/prometheus-kubernetes.yml

$ wget https://github.com/prometheus/prometheus/blob/master/documentation/examples/prometheus-kubernetes.yml
$ kubectl create cm prometheus-server-conf --from-file=./prometheus-kubernetes.yml --namespace=prometheus
  1. 爲prometheus 添加service
apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
  namespace: prometheus
  annotations:
      prometheus.io/scrape: 'true'
      #prometheus.io/path:   /
      prometheus.io/port:   '9090'
spec:
  selector: 
    app: prometheus-server
  type: NodePort  
  ports:
    - port: 9090
      targetPort: 9090 
      nodePort: 30000
  1. 爲prometheus創建deployment:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: prometheus-deployment
  namespace: prometheus
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: prometheus-server
    spec:
      serviceAccountName: prometheus
      containers:
        - name: prometheus
          image: prom/prometheus:v2.2.1
          args:
            - "--config.file=/etc/prometheus/prometheus-kubernetes.yml"
            - "--storage.tsdb.path=/prometheus/"
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: prometheus-config-volume
              mountPath: /etc/prometheus/
            - name: prometheus-storage-volume
              mountPath: /prometheus/
      volumes:
        - name: prometheus-config-volume
          configMap:
            defaultMode: 420
            name: prometheus-server-conf
        - name: prometheus-storage-volume
          emptyDir: {}

grafana

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: grafana
  namespace: prometheus
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana:5.1.2
        ports:
        - containerPort: 3000
          protocol: TCP
        volumeMounts:
        - mountPath: /etc/ssl/certs
          name: ca-certificates
          readOnly: true
        - mountPath: /var/lib/grafana
          name: grafana-storage
      volumes:
      - name: ca-certificates
        hostPath:
          path: /etc/ssl/certs
      - name: grafana-storage
        hostPath:
          path: /data/grafana
      nodeName: walker-2
---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: prometheus
spec:
  # In a production setup, we recommend accessing Grafana through an external Loadbalancer
  # or through a public IP.
  # type: LoadBalancer
  # You could also use NodePort to expose the service at a randomly-generated port
  type: NodePort
  ports:
  - port: 80
    targetPort: 3000
    nodePort: 30080
  selector:
    app: grafana

Note: 將grafana 固定到node-2節點上,使用本地的/data/grafana 作爲存儲目錄

node-exporter

apiVersion: v1
kind: Service
metadata:
  annotations:
    prometheus.io/scrape: 'true'
  labels:
    app: node-exporter
    name: node-exporter
  name: node-exporter
  namespace: kube-system
spec:
  clusterIP: None
  ports:
  - name: scrape
    port: 9100
    protocol: TCP
  selector:
    app: node-exporter
  type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: node-exporter
  namespace: kube-system
spec:
  template:
    metadata:
      labels:
        app: node-exporter
      name: node-exporter
    spec:
      containers:
      - image: prom/node-exporter:master
        imagePullPolicy: IfNotPresent
        name: node-exporter
        ports:
        - containerPort: 9100
          hostPort: 9100
          name: scrape
      hostNetwork: true
      hostPID: true

Note: node-exporter 使用了hostPort,直接將容器端口映射到主機9100端口上。

安裝 grafana-kubernetes-app

https://grafana.com/plugins/grafana-kubernetes-app/installation

官方文檔中給出了安裝方法通過grafana-cli命令來安裝插件。
考慮到版本一致問題和內部網絡不通Internet,因此使用安裝包的形式來安裝。

安裝包下載地址: https://grafana.com/api/plugins/grafana-kubernetes-app/versions/1.0.1/download

當前 grafana 容器運行在walker-2計算節點上

$ cd /data/grafana/plugins
$ unzip grafana-kubernetes-app-31da28.zip

然後重啓一下 grafana pod.

image

點擊enable 來使用該插件

配置grafana-kubernetes-app

要使用kubernetes插件,需要兩項配置:prometheus 數據源,k8s 集羣訪問方式

  1. 添加prometheus數據源

image

  1. 添加k8s集羣

image

Note: apiserver 使用6443端口,以https形式提供服務。客戶端訪問apiserver需要認證客戶端證書。該集羣使用kubeadm 安裝,會有一個/etc/kubernetes/admin.conf 文件,裏面包含了客戶端的證書和密碼base64編碼。複製 client-certificate-dataclient-key-data的base64編碼,分別執行 echo "<base64 code>" | base64 -d 就能還原成證書源文件。

隨後就能使用插件了。

image

Troubleshooting

插件提供了3個dashboard. 其中k8s Node 圖表無法正常顯示。

image

官方issue : #13

收集節點信息,依賴node-exporter 暴露的http接口。以獲取cpu使用率接口爲例,其格式如下:

node_cpu{cpu="cpu0",daemon="node-exporter",instance="172.16.6.249:9100",job="kubernetes-service-endpoints",kubernetes_name="node-exporter",kubernetes_namespace="kube-system",mode="idle",name="node-exporter"}

dashboard 中獲取的cpu數據的公式如下:

image

可以看到k8s Node模板裏面是使用 nodename 字段來獲取不同節點的數據,但是node-exporter 暴露出來的接口中,並沒有nodename 字段,因此導致了模板查詢不到數據,從而無法繪製圖形。

解決方法如下:

點擊 k8s Node dashboard 頁面中的設置按鈕,選擇Variable 菜單

image

可以看到當前有4個變量,點擊右上角New 創建一個新變量

image

點擊保存後,會彈出一個提示框。選擇overwrite 即可。

image

image

再回到 k8s Node dashboard 頁面,可以看到多了instance 一欄

image

隨後編輯各個panel, 將引用了nodename的地方改爲instance。這裏以cpu panel 爲例:

image

可以看到,修改後圖形能正常展示了。

附上一張修改後的圖:

image

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章