簡介
grafana
爲 kubernetes
提供了一套模板,用來展示集羣的運行負載。
github: https://github.com/grafana/kubernetes-app
關於監控的指標和界面展示官方文檔已經很詳細了,這裏不再贅述。
值得一提的是,要使用該模板插件,需要部署kube-state-mertics
和node-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
- 先創建一個名爲
prometheus
的namespace
$ kubectl create ns prometheus
- 爲
prometheus
添加rbac
權限認證
$ wget https://github.com/prometheus/prometheus/blob/master/documentation/examples/rbac-setup.yml
$ kubectl apply -f rbac-setup.yaml
- 爲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
- 爲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
- 爲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.
點擊enable
來使用該插件
配置grafana-kubernetes-app
要使用kubernetes插件,需要兩項配置:prometheus
數據源,k8s
集羣訪問方式
- 添加
prometheus
數據源
- 添加k8s集羣
Note: apiserver 使用6443端口,以https形式提供服務。客戶端訪問apiserver需要認證客戶端證書。該集羣使用
kubeadm
安裝,會有一個/etc/kubernetes/admin.conf
文件,裏面包含了客戶端的證書和密碼base64編碼。複製client-certificate-data
和client-key-data
的base64編碼,分別執行echo "<base64 code>" | base64 -d
就能還原成證書源文件。
隨後就能使用插件了。
Troubleshooting
插件提供了3個dashboard. 其中k8s Node
圖表無法正常顯示。
官方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數據的公式如下:
可以看到k8s Node模板裏面是使用 nodename
字段來獲取不同節點的數據,但是node-exporter
暴露出來的接口中,並沒有nodename
字段,因此導致了模板查詢不到數據,從而無法繪製圖形。
解決方法如下:
點擊 k8s Node
dashboard 頁面中的設置按鈕,選擇Variable
菜單
可以看到當前有4個變量,點擊右上角New
創建一個新變量
點擊保存後,會彈出一個提示框。選擇overwrite
即可。
再回到 k8s Node
dashboard 頁面,可以看到多了instance
一欄
隨後編輯各個panel, 將引用了nodename
的地方改爲instance
。這裏以cpu panel 爲例:
可以看到,修改後圖形能正常展示了。
附上一張修改後的圖: