---
# System prepended metadata

title: 匯入 Dashboard
tags: [Grafana]

---

---

title: 匯入 Dashboard  # 簡報的名稱
tags: Grafana   # 簡報的標籤

---

# 匯入 Dashboard

> [name=李俊毅]
> [time=Wed, Jan 22, 2020 6:22 PM]

---

## Agenda

* Terraform 匯入
* K8S 匯入
* GUI 匯入

---

## Terraform 匯入

----

需建立 API Key，首先登入 Grafana ，開啟 Configuration 的 API Keys

![](https://i.imgur.com/u5eHduG.png)

----

建立一把 API Key

![](https://i.imgur.com/vCLKq4o.png)

----

設定 Key 的名稱、權限與使用期限

![](https://i.imgur.com/S3aOwiK.png)

----

Add 後會出現一次性的私鑰，記得要記錄下來

![](https://i.imgur.com/Sgq6awP.png)

----

完成後會在 Existing Keys 看到

![](https://i.imgur.com/37IUB8u.png)

----

到 Terraform 腳本中 Variable.tf 變數 grafana_url ，改成要匯入 Dashboard 的 Grafana URL

```
variable "grafana_url" {
  type    = "string"
  default = "https://xsgtest.grafana.net/"
}
```

----

接著繼續修改 Variable.tf 變數 grafana_auth ，把剛剛產出的私鑰取代掉變數

```
variable "grafana_auth" {
  type    = "string"
  default = "eyJrIjoibGVKb2RBb0Q3d0d5cDJEOEZNRDVVMEJOTmxGdjFoZXoiLCJuIjoiYWJjIiwiaWQiOjF9"
}

```

----

執行 Terraform 腳本

---

## K8S 匯入

----

需要在 Deployment 內新增 dashboards.yaml 的路徑

```
volumeMounts:
- name: grafana-dashboards
  mountPath: /etc/grafana/provisioning/dashboards/dashboards.yaml
  subPath: dashboards.yaml

volumes:
- name: grafana-dashboards
  configMap:
    name: grafana-dashboards
```

----

接著新增一個 ConfigMap 設定 Dashboard 的路徑

```
data:
  dashboards.yaml: |-
    apiVersion: 1
    providers:
    - name: default
      org_id: 1
      folder: ''
      type: file
      disableDeletion: true
      editable: true
      updateIntervalSeconds: 10
      allowUiUpdates: false
      options:
        path: /etc/grafana/provisioning/dashboards
```

----

最後把要匯入的 Dashboard 放入 Dashboard 路徑中，需設定 Deployment 與 ConfigMap

----

首先在 Deployment 內新增 Dashboard 檔案

```
volumeMounts:
- name: grafana-dashboard-prometheus
 mountPath: /etc/grafana/provisioning/dashboards/prometheus-stats.json
 subPath: prometheus-stats.json

volumes:
- name: grafana-dashboard-prometheus
 configMap:
   name: grafana-dashboard-prometheus
```

----

再來設定 ConfigMap 來存放 Dashboard 的設定

 ```
 data:
  prometheus-stats.json: |-
    {
      "annotations": {
        "list": [
          {
            "builtIn": 1,
            "datasource": "-- Grafana --",
            "enable": true,
            "hide": true,
            "iconColor": "rgba(0, 211, 255, 1)",
            "name": "Annotations & Alerts",
            "type": "dashboard"
          }
        ]
      },
 ......
 ```

---

## GUI 匯入

----

首先開啟 Grafana 網頁，選擇左邊的＋，點選 Import

![](https://i.imgur.com/z2cDxEQ.png)

----

匯入的方式有兩種，一種是上傳檔案，另一種是 Copy && Paste ，做完後按下 Load 按鈕， Dashboard 就完成了

![](https://i.imgur.com/EcZ4jg0.png)

----

完成後的 Dashboard 會在 Home 的下拉式選單中看到

![](https://i.imgur.com/rgraprl.png)
