通过Grafana展示nginx容器指标
目标
本测试由三部分组成:
1)Nginx指标收集,由nginx-prometheus-exporter转化为prometheus可以处理的指标格式,这部分我们上一节已经完成,本节将复用我们做好的nginx-exporter:v0.2镜像。
2)配置Prometheus对指标进行收集(镜像https://hub.docker.com/r/prom/prometheus
3)配置Grafana对指标进行展示(镜像https://hub.docker.com/r/grafana/grafana

测试1:配置Prometheus对指标进行收集

首先,在宿主机上创建prometheus.yml文件,内容如下:
1
global:
2
scrape_interval: 15s
3
evaluation_interval: 15s
4
5
scrape_configs:
6
- job_name: 'prometheus'
7
static_configs:
8
- targets: ['localhost:9090']
9
- job_name: 'nginx_exporter'
10
static_configs:
11
- targets: ['192.168.2.31:9113']
12
labels:
13
group: 'services'
Copied!
启动Prometheus容器:
1
docker run --name prometheus1 -d -p 9090:9090 -v $(pwd)/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
Copied!
然后访问宿主机 http://192.168.2.31:9090/targets,获得如下界面即代表Prometheus和Nginx指标均可获取:
使用Nginx指标名称能在Prometheus绘制出简单图表

测试2:配置Grafana对指标进行展示

1)准备dashboard.json
首先我们需要定制一个适合展示prometheus收集回来的nginx的指标参数的dashboards仪表板模板,最快的方式是上官网的选择现成的,参考https://grafana.com/grafana/dashboards/
实际工作中,我们通常使用的nginx-vts-exporter能获得更多的指标。
这里我们自己定义一个适合nginx-prometheus-exporter指标的json文件,并命名为dashboard.json:
1
{
2
"annotations": {
3
"list": [
4
{
5
"builtIn": 1,
6
"datasource": "-- Grafana --",
7
"enable": true,
8
"hide": true,
9
"iconColor": "rgba(0, 211, 255, 1)",
10
"name": "Annotations & Alerts",
11
"type": "dashboard"
12
}
13
]
14
},
15
"editable": true,
16
"gnetId": null,
17
"graphTooltip": 0,
18
"id": 7,
19
"links": [],
20
"panels": [
21
{
22
"cacheTimeout": null,
23
"colorBackground": false,
24
"colorPostfix": false,
25
"colorPrefix": false,
26
"colorValue": true,
27
"colors": [
28
"#299c46",
29
"rgba(237, 129, 40, 0.89)",
30
"#d44a3a"
31
],
32
"datasource": "Prometheus",
33
"format": "none",
34
"gauge": {
35
"maxValue": 100,
36
"minValue": 0,
37
"show": false,
38
"thresholdLabels": false,
39
"thresholdMarkers": true
40
},
41
"gridPos": {
42
"h": 4,
43
"w": 12,
44
"x": 0,
45
"y": 0
46
},
47
"id": 4,
48
"interval": null,
49
"links": [],
50
"mappingType": 1,
51
"mappingTypes": [
52
{
53
"name": "value to text",
54
"value": 1
55
},
56
{
57
"name": "range to text",
58
"value": 2
59
}
60
],
61
"maxDataPoints": 100,
62
"nullPointMode": "connected",
63
"nullText": null,
64
"postfix": "",
65
"postfixFontSize": "50%",
66
"prefix": "",
67
"prefixFontSize": "50%",
68
"rangeMaps": [
69
{
70
"from": "null",
71
"text": "N/A",
72
"to": "null"
73
}
74
],
75
"sparkline": {
76
"fillColor": "rgba(31, 118, 189, 0.18)",
77
"full": false,
78
"lineColor": "rgb(31, 120, 193)",
79
"show": false,
80
"ymax": null,
81
"ymin": null
82
},
83
"tableColumn": "",
84
"targets": [
85
{
86
"expr": "nginx_up",
87
"interval": "",
88
"legendFormat": "",
89
"refId": "A"
90
}
91
],
92
"thresholds": "",
93
"timeFrom": null,
94
"timeShift": null,
95
"title": "Nginx Status",
96
"transparent": true,
97
"type": "singlestat",
98
"valueFontSize": "200%",
99
"valueMaps": [
100
{
101
"op": "=",
102
"text": "Down",
103
"value": "0"
104
},
105
{
106
"op": "=",
107
"text": "Up",
108
"value": "1"
109
}
110
],
111
"valueName": "current"
112
},
113
{
114
"cacheTimeout": null,
115
"datasource": "Prometheus",
116
"gridPos": {
117
"h": 4,
118
"w": 12,
119
"x": 12,
120
"y": 0
121
},
122
"id": 7,
123
"links": [],
124
"options": {
125
"fieldOptions": {
126
"calcs": [
127
"mean"
128
],
129
"defaults": {
130
"mappings": [],
131
"thresholds": {
132
"mode": "absolute",
133
"steps": [
134
{
135
"color": "green",
136
"value": null
137
},
138
{
139
"color": "red",
140
"value": 80
141
}
142
]
143
}
144
},
145
"overrides": [],
146
"values": false
147
},
148
"orientation": "auto",
149
"showThresholdLabels": false,
150
"showThresholdMarkers": true
151
},
152
"pluginVersion": "6.7.3",
153
"targets": [
154
{
155
"expr": "nginx_http_requests_total",
156
"interval": "",
157
"legendFormat": "",
158
"refId": "A"
159
},
160
{
161
"refId": "B"
162
},
163
{
164
"refId": "C"
165
}
166
],
167
"timeFrom": null,
168
"timeShift": null,
169
"title": "Nginx Total Request",
170
"transparent": true,
171
"type": "gauge"
172
},
173
{
174
"aliasColors": {},
175
"bars": false,
176
"cacheTimeout": null,
177
"dashLength": 10,
178
"dashes": false,
179
"datasource": "Prometheus",
180
"fill": 1,
181
"fillGradient": 0,
182
"gridPos": {
183
"h": 9,
184
"w": 12,
185
"x": 0,
186
"y": 4
187
},
188
"hiddenSeries": false,
189
"id": 2,
190
"legend": {
191
"alignAsTable": false,
192
"avg": false,
193
"current": false,
194
"max": false,
195
"min": false,
196
"rightSide": false,
197
"show": true,
198
"total": false,
199
"values": false
200
},
201
"lines": true,
202
"linewidth": 1,
203
"links": [],
204
"nullPointMode": "null",
205
"options": {
206
"dataLinks": []
207
},
208
"percentage": false,
209
"pluginVersion": "6.7.3",
210
"pointradius": 2,
211
"points": false,
212
"renderer": "flot",
213
"seriesOverrides": [],
214
"spaceLength": 10,
215
"stack": false,
216
"steppedLine": false,
217
"targets": [
218
{
219
"expr": "rate(nginx_connections_accepted[5m])",
220
"interval": "",
221
"legendFormat": "accepted_rate",
222
"refId": "A"
223
},
224
{
225
"expr": "rate(nginx_connections_handled[5m])",
226
"interval": "",
227
"legendFormat": "handled_rate",
228
"refId": "B"
229
}
230
],
231
"thresholds": [],
232
"timeFrom": null,
233
"timeRegions": [],
234
"timeShift": null,
235
"title": "Connection Rate",
236
"tooltip": {
237
"shared": true,
238
"sort": 0,
239
"value_type": "individual"
240
},
241
"type": "graph",
242
"xaxis": {
243
"buckets": null,
244
"mode": "time",
245
"name": null,
246
"show": true,
247
"values": []
248
},
249
"yaxes": [
250
{
251
"format": "short",
252
"label": null,
253
"logBase": 1,
254
"max": null,
255
"min": null,
256
"show": true
257
},
258
{
259
"format": "short",
260
"label": null,
261
"logBase": 1,
262
"max": null,
263
"min": null,
264
"show": true
265
}
266
],
267
"yaxis": {
268
"align": false,
269
"alignLevel": null
270
}
271
},
272
{
273
"aliasColors": {},
274
"bars": false,
275
"cacheTimeout": null,
276
"dashLength": 10,
277
"dashes": false,
278
"datasource": "Prometheus",
279
"fill": 1,
280
"fillGradient": 0,
281
"gridPos": {
282
"h": 9,
283
"w": 12,
284
"x": 12,
285
"y": 4
286
},
287
"hiddenSeries": false,
288
"id": 6,
289
"legend": {
290
"avg": false,
291
"current": false,
292
"max": false,
293
"min": false,
294
"show": true,
295
"total": false,
296
"values": false
297
},
298
"lines": true,
299
"linewidth": 1,
300
"links": [],
301
"nullPointMode": "null",
302
"options": {
303
"dataLinks": []
304
},
305
"percentage": false,
306
"pluginVersion": "6.7.3",
307
"pointradius": 2,
308
"points": false,
309
"renderer": "flot",
310
"seriesOverrides": [],
311
"spaceLength": 10,
312
"stack": false,
313
"steppedLine": false,
314
"targets": [
315
{
316
"expr": "nginx_connections_reading",
317
"interval": "",
318
"legendFormat": "conn_reading",
319
"refId": "A"
320
},
321
{
322
"expr": "nginx_connections_waiting",
323
"interval": "",
324
"legendFormat": "conn_waiting",
325
"refId": "B"
326
},
327
{
328
"expr": "nginx_connections_writing",
329
"interval": "",
330
"legendFormat": "conn_writing",
331
"refId": "C"
332
}
333
],
334
"thresholds": [],
335
"timeFrom": null,
336
"timeRegions": [],
337
"timeShift": null,
338
"title": "Connection Status",
339
"tooltip": {
340
"shared": true,
341
"sort": 0,
342
"value_type": "individual"
343
},
344
"type": "graph",
345
"xaxis": {
346
"buckets": null,
347
"mode": "time",
348
"name": null,
349
"show": true,
350
"values": []
351
},
352
"yaxes": [
353
{
354
"format": "short",
355
"label": null,
356
"logBase": 1,
357
"max": null,
358
"min": null,
359
"show": true
360
},
361
{
362
"format": "short",
363
"label": null,
364
"logBase": 1,
365
"max": null,
366
"min": null,
367
"show": true
368
}
369
],
370
"yaxis": {
371
"align": false,
372
"alignLevel": null
373
}
374
}
375
],
376
"schemaVersion": 22,
377
"style": "dark",
378
"tags": [],
379
"templating": {
380
"list": []
381
},
382
"time": {
383
"from": "now-30m",
384
"to": "now"
385
},
386
"timepicker": {
387
"refresh_intervals": [
388
"5s",
389
"10s",
390
"30s",
391
"1m",
392
"5m",
393
"15m",
394
"30m",
395
"1h",
396
"2h",
397
"1d"
398
]
399
},
400
"timezone": "",
401
"title": "nginx-simon",
402
"uid": "b2yin83Zz",
403
"variables": {
404
"list": []
405
},
406
"version": 3
407
}
Copied!
2)准备provisioning文件
2-1)定义存放dashboard模板的目录
创建一个dashboard.yaml文件:
1
apiVersion: 1
2
3
providers:
4
- name: 'nginx'
5
orgId: 1
6
folder: 'dashboard1'
7
folderUid: ''
8
type: file
9
disableDeletion: false
10
editable: true
11
updateIntervalSeconds: 10
12
allowUiUpdates: false
13
options:
14
path: /var/lib/grafana/dashboards
Copied!
2-2)定义数据源
创建一个datasouce.yaml文件:
1
apiVersion: 1
2
3
datasources:
4
- name: Prometheus
5
type: prometheus
6
access: proxy
7
orgId: 1
8
url: http://192.168.2.31:9090
9
basicAuth: false
10
isDefault: true
11
version: 1
12
editable: true
Copied!
3)启动Grafana容器
1
docker run -d --name=grafana1 \
2
-v $(pwd)/grafana/dashboards/dashboard.json:/var/lib/grafana/dashboards/dashboard.json \
3
-v $(pwd)/grafana/provisioning/dashboard.yaml:/etc/grafana/provisioning/dashboards/dashboard.yaml \
4
-v $(pwd)/grafana/provisioning/datasource.yaml:/etc/grafana/provisioning/datasources/datasource.yaml \
5
-p 3000:3000 grafana/grafana
Copied!
现在宿主机上三个容器是:
1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2
04b5ee1a4128 grafana/grafana "/run.sh" 3 seconds ago Up 2 seconds 0.0.0.0:3000->3000/tcp grafana1
3
556ca4ec29f8 prom/prometheus "/bin/prometheus --c…" 12 hours ago Up 12 hours 0.0.0.0:9090->9090/tcp prometheus1
4
71dda6ff4612 nginx-exporter:v0.2 "/bin/sh /run.sh" 12 hours ago Up 12 hours 80/tcp, 0.0.0.0:9113->9113/tcp exporter2
Copied!
访问宿主机3000端口,打开Grafana,正常可以看到如下仪表板:
Last modified 1yr ago