Grafana安装和配置

下载安装Grafana需要的镜像

上传heapster-grafana-amd64_v5_0_4.tar.gz镜像到k8s的各个master节点和k8s的各个node节点,然后在各个节点手动解压:
docker load -i heapster-grafana-amd64_v5_0_4.tar.gz

镜像所在的百度网盘地址如下:

链接:https://pan.baidu.com/s/1TmVGKxde_cEYrbjiETboEA 提取码:052u

在k8s的master1节点创建grafana.yaml

cat  >grafana.yaml <<  EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: monitoring-grafana
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      task: monitoring
      k8s-app: grafana
  template:
    metadata:
      labels:
        task: monitoring
        k8s-app: grafana
    spec:
      containers:
      - name: grafana
        image: k8s.gcr.io/heapster-grafana-amd64:v5.0.4
        ports:
        - containerPort: 3000
          protocol: TCP
        volumeMounts:
        - mountPath: /etc/ssl/certs
          name: ca-certificates
          readOnly: true
        - mountPath: /var
          name: grafana-storage
        env:
        - name: INFLUXDB_HOST
          value: monitoring-influxdb
        - name: GF_SERVER_HTTP_PORT
          value: "3000"
          # The following env variables are required to make Grafana accessible via
          # the kubernetes api-server proxy. On production clusters, we recommend
          # removing these env variables, setup auth for grafana, and expose the grafana
          # service using a LoadBalancer or a public IP.
        - name: GF_AUTH_BASIC_ENABLED
          value: "false"
        - name: GF_AUTH_ANONYMOUS_ENABLED
          value: "true"
        - name: GF_AUTH_ANONYMOUS_ORG_ROLE
          value: Admin
        - name: GF_SERVER_ROOT_URL
          # If you're only using the API Server proxy, set this value instead:
          # value: /api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
          value: /
      volumes:
      - name: ca-certificates
        hostPath:
          path: /etc/ssl/certs
      - name: grafana-storage
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  labels:
    # For use as a Cluster add-on (https://github.com/kubernetes/kubernetes/tree/master/cluster/addons)
    # If you are NOT using this as an addon, you should comment out this line.
    kubernetes.io/cluster-service: 'true'
    kubernetes.io/name: monitoring-grafana
  name: monitoring-grafana
  namespace: kube-system
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
  selector:
    k8s-app: grafana
  type: NodePort
EOF

通过kubectl apply 更新grafana

kubectl  apply -f grafana.yaml

kubectl get pods -n kube-system

查看grafana的servicekubectl get svc -n kube-system:

monitoring-grafana   NodePort    10.111.173.47    <none>        80:31044/TCP             3h54m

显示如下:

上面可以看到grafana暴露的宿主机端口是31044

我们访问k8s集群的master节点ip:31044即可访问到grafana的web界面

Grafan界面接入prometheus数据源

1)登陆grafana,在浏览器访问

192.168.0.6:31044

账号密码都是admin

可看到如下界面:

 2)配置grafana界面:
开始配置grafana的web界面:
选择Create your first data source

Name: Prometheus 

Type: Prometheus

HTTP 处的URL写 如下:

http://prometheus.monitor-sa.svc:9090

下载炫酷的免费监控模板
https://grafana.com/dashboards?dataSource=prometheus&search=kubernetes

https://grafana.com/grafana/dashboards

也可直接导入node_exporter.json监控模板,这个可以把node节点指标显示出来

node_exporter.json在百度网盘地址如下:

链接:https://pan.baidu.com/s/1vF1kAMRbxQkUGPlZt91MWg 提取码:kyd6

还可直接导入docker_rev1.json,可以把容器相关的数据展示出来
docker_rev1.json在百度网盘地址如下:

链接:https://pan.baidu.com/s/17o_nja5N2R-g9g5PkJ3aFA 提取码:vinv

怎么导入监控模板,按如下步骤

上面Save & Test测试没问题之后,就可以返回Grafana主页面

 点击左侧+号下面的Import,出现如下界面

 选择Upload json file,出现如下

选择一个本地的json文件,我们选择的是上面让大家下载的node_exporter.json这个文件,选择之后出现如下

 注:箭头标注的地方Name后面的名字是node_exporter.json定义的

Prometheus后面需要变成Prometheus,然后再点击Import,就可以出现如下界面:

 导入docker_rev1.json监控模板,步骤和上面导入node_exporter.json步骤一样,导入之后显示如下:

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐