启动Grafana容器时导入数据源和仪表盘的方法
概说
在使用容器运行Grafana时,介绍了在容器启动时导入Datasource和Dashboard的方法,而不是在容器启动后创建它们。
环境中的动作
-
- Debian [v10.8]
- Grafana [v8.0.6]
准备的事物
-
- 各个Dashboard以json格式输出的dashboard.json文件
用于Dashboard配置的dashboard.yml文件
用于数据源配置的datasource.yml文件
用于Grafana容器的Dockerfile文件
1. 将各个仪表盘导出为 JSON 格式,保存为 dashboard.json。
【具体的例子如下】
{
"annotations": {
"list": [
{
"$$hashKey": "object:1058",
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
〜〜〜〜〜 省略 〜〜〜〜〜
],
"time_options": [
"5m",
"15m",
"1h",
"6h",
"12h",
"24h",
"2d",
"7d",
"30d"
]
},
"timezone": "browser",
"title": "Linux Server",
"uid": "sxXoxCW7z",
"version": 1
}
制作方法
点击仪表盘画面右上角的齿轮标志
点击左侧菜单上的JSON模型。
将JSON模型内的JSON复制到文件中。
2. 用于仪表板配置的 dashboard.yml 文件
【以下是具体的例子】
apiVersion: 1
providers:
# <string> an unique provider name. Required
- name: 'Linux Server'
# <int> Org id. Default to 1
orgId: 1
# <string> name of the dashboard folder.
folder: 'Server'
# <string> folder UID. will be automatically generated if not specified
folderUid: ''
# <string> provider type. Default to 'file'
type: file
# <bool> disable dashboard deletion
disableDeletion: false
# <int> how often Grafana will scan for changed dashboards
updateIntervalSeconds: 10
# <bool> allow updating provisioned dashboards from the UI
allowUiUpdates: false
options:
# <string, required> path to dashboard files on disk. Required when using the 'file' type
path: /var/lib/grafana/dashboards/Linux_Server.json
# <bool> use folder names from filesystem to create folders in Grafana
foldersFromFilesStructure: false
- name: 'Windows Server'
orgId: 1
folder: 'Server'
type: file
disableDeletion: false
updateIntervalSeconds: 10
allowUiUpdates: false
options:
path: /var/lib/grafana/dashboards/Windows_Server.json
foldersFromFilesStructure: false
- name: 'Web Service'
orgId: 1
folder: 'Service'
type: file
disableDeletion: false
updateIntervalSeconds: 10
allowUiUpdates: false
options:
path: /var/lib/grafana/dashboards/Web_Service.json
foldersFromFilesStructure: false
制作方法
将Grafana公式容器镜像中的/etc/grafana/provisioning/dashboards/dashboard.yml复制一份。
在每个仪表板上添加以下提供者设置。
- name: 'Web Service' # Dashboardの「名前」
orgId: 1 # デフォルトのままで
folder: 'Service' # Dashboardが格納される「フォルダ名」
type: file # デフォルトのままで
disableDeletion: false # Dashboardの「削除可否」
updateIntervalSeconds: 10 # Dashboardの「更新頻度」
allowUiUpdates: false # Dashboardの「UI変更可否」
options:
path: /var/lib/grafana/dashboards/Web_Service.json # dashboardの「jsonファイルパス」
foldersFromFilesStructure: false # デフォルトのままで
3. 用于数据源自动配置的 datasource.yml 文件。
【具体例子如下】
# config file version
apiVersion: 1
# # list of datasources that should be deleted from the database
# deleteDatasources:
# - name: Graphite
# orgId: 1
# list of datasources to insert/update depending
# what's available in the database
datasources:
# <string, required> name of the datasource. Required
- name: Prometheus
# <string, required> datasource type. Required
type: prometheus
# <string, required> access mode. proxy or direct (Server or Browser in the UI). Required
access: proxy
# <int> org id. will default to orgId 1 if not specified
orgId: 1
# <string> custom UID which can be used to reference this datasource in other parts of the configuration, if not specified will be generated automatically
uid: prometheus
# <string> url
url: http://localhost:9090
# <string> Deprecated, use secureJsonData.password
password:
# <string> database user, if used
user:
# <string> database name, if used
database:
# <bool> enable/disable basic auth
basicAuth:
# <string> basic auth username
basicAuthUser:
# <string> Deprecated, use secureJsonData.basicAuthPassword
basicAuthPassword:
# <bool> enable/disable with credentials headers
withCredentials:
# <bool> mark as default datasource. Max one per org
isDefault: true
# # <map> fields that will be converted to json and stored in jsonData
# jsonData:
# graphiteVersion: '1.1'
# tlsAuth: true
# tlsAuthWithCACert: true
# # <string> json object of data that will be encrypted.
# secureJsonData:
# tlsCACert: '...'
# tlsClientCert: '...'
# tlsClientKey: '...'
# # <string> database password, if used
# password:
# # <string> basic auth password
# basicAuthPassword:
version: 1
# <bool> allow users to edit datasources from the UI.
editable: false
- name: CloudWatch
type: cloudwatch
jsonData:
authType: default
defaultRegion: ap-northeast-1
制作方法
将Grafana公式容器镜像中的 /etc/grafana/provisioning/datasources/datasource.yml 文件复制一份。
②. 每个数据源都要添加以下的datasources设置。
### Prometheusの場合 ###
- name: Prometheus # Datasourceの「名前」
type: prometheus # ※[p]は小文字
access: proxy # デフォルトのままで
uid: prometheus # デフォルトのままで
url: http://localhost:9090 # Datasourceの「URL」
isDefault: true # 複数のDatasource内で「デフォルトに設定するか否か」
version: 1 # デフォルトのままで
editable: false # Datasourceの「設定変更可否」
### CloudWatchの場合(動作環境がEC2内の場合) ###
- name: CloudWatch # Datasourceの「名前」
type: cloudwatch # ※[c]は小文字
jsonData:
authType: default # デフォルトのままで
defaultRegion: ap-northeast-1 # CloudWatch の「リージョン」
4. 用于 Grafana 容器的 Dockerfile
【具体例子如下】
FROM grafana/grafana:8.0.6
COPY ./dashboards/ /var/lib/grafana/dashboards/
COPY ./dashboard.yml /etc/grafana/provisioning/dashboards/dashboard.yml
COPY ./datasource.yml /etc/grafana/provisioning/datasources/datasource.yml
阐述
请根据以下的目录结构创建Dockerfile。
.
├── Dockerfile
├── dashboard.yml
├── dashboards
│ ├── Linux_Server.json
│ ├── Web_Service.json
│ └── Windows_Server.json
└── datasource.yml
进行动作检查
在与Dockerfile相同的目录中执行以下操作。
docker image build -t grafana:8.0.6 .
docker run -d -p 8080:3000 grafana:8.0.6
echo "[Grafana] → http://localhost:8080/"