前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >grafana-kubernetes plugin安装和使用

grafana-kubernetes plugin安装和使用

作者头像
菲宇
发布2019-06-12 16:38:46
2.3K0
发布2019-06-12 16:38:46
举报
文章被收录于专栏:菲宇菲宇

简介

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

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.

image
image

点击enable 来使用该插件

配置grafana-kubernetes-app

要使用kubernetes插件,需要两项配置:prometheus 数据源,k8s 集群访问方式

  1. 添加prometheus数据源
image
image
  1. 添加k8s集群
image
image

Note: apiserver 使用6443端口,以https形式提供服务。客户端访问apiserver需要认证客户端证书。该集群使用kubeadm 安装,会有一个/etc/kubernetes/admin.conf 文件,里面包含了客户端的证书和密码base64编码。复制 client-certificate-dataclient-key-data的base64编码,分别执行 echo "<base64 code>" | base64 -d 就能还原成证书源文件。

随后就能使用插件了。

image
image

Troubleshooting

插件提供了3个dashboard. 其中k8s Node 图表无法正常显示。

image
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
image

可以看到k8s Node模板里面是使用 nodename 字段来获取不同节点的数据,但是node-exporter 暴露出来的接口中,并没有nodename 字段,因此导致了模板查询不到数据,从而无法绘制图形。

解决方法如下:

点击 k8s Node dashboard 页面中的设置按钮,选择Variable 菜单

image
image

可以看到当前有4个变量,点击右上角New 创建一个新变量

image
image

点击保存后,会弹出一个提示框。选择overwrite 即可。

image
image
image
image

再回到 k8s Node dashboard 页面,可以看到多了instance 一栏

image
image

随后编辑各个panel, 将引用了nodename的地方改为instance。这里以cpu panel 为例:

image
image

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

附上一张修改后的图:

image
image
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年09月12日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 部署prometheus
  • 部署grafana
  • 部署node-exporter
    • 安装 grafana-kubernetes-app
      • 配置grafana-kubernetes-app
        • Troubleshooting
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档