grafana-kubernetes plugin安装和使用

x33g5p2x  于2021-12-25 转载在 其他  
字(4.9k)|赞(0)|评价(0)|浏览(168)

简介

grafanakubernetes 提供了一套模板,用来展示集群的运行负载。

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

关于监控的指标和界面展示官方文档已经很详细了,这里不再赘述。

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

使用组件镜像如下如下:

image_nametag
prom/prometheusv2.2.1
grafana/grafana5.1.2
gcr.io/google_containers/kube-state-metricsv1.3.1
quay.io/prometheus/node-exporterv0.15.0

以下部署环境基于k8s 1.8.1,截止2018/08/03 为止kuberetes 插件最近版本为 1.0.1

部署prometheus

1、先创建一个名为prometheusnamespace

$ kubectl create ns prometheus

2.为prometheus添加rbac权限认证

$ wget https://github.com/prometheus/prometheus/blob/master/documentation/examples/rbac-setup.yml
$ kubectl apply -f rbac-setup.yaml

3.为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

4.为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

5.为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 集群访问方式

  1. 添加prometheus数据源

  1. 添加k8s集群

Note: apiserver 使用6443端口,以https形式提供服务。客户端访问apiserver需要认证客户端证书。该集群使用kubeadm 安装,会有一个/etc/kubernetes/admin.conf 文件,里面包含了客户端的证书和密码base64编码。复制 client-certificate-dataclient-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 为例:

可以看到,修改后图形能正常展示了。

附上一张修改后的图:

相关文章