自动化监控运维(二) Grafana 构建

零、 说明

  • 三种部署方式
    • rpm 包部署 (简单, 不写)
    • 二进制部署
    • docker 构建

一、 二进制安装

1.1 下载

下载连接

1.2 解压并复制到指定目录

tar -zxvf grafana-enterprise-10.1.2.linux-amd64.tar.gz
cp -rf grafana-10.1.2/ /usr/local/grafana

1.3 创建用户/组

groupadd grafana
useradd -g grafana grafana

1.4 创建相关目录

mkdir -p /etc/grafana
mkdir -p /etc/grafana/provisioning
mkdir -p /var/log/grafana
mkdir -p /var/lib/grafana
mkdir -p /var/lib/grafana/plugins

1.5 修改目录权限

chown -R grafana:grafana /usr/local/grafana/
chown -R grafana:grafana /etc/grafana/
chown -R grafana:grafana /var/lib/grafana/
chown -R grafana:grafana /var/log/grafana/

1.6 复制 grafana 配置文件

cp /usr/local/grafana/conf/sample.ini /etc/grafana/grafana.ini

1.7 环境配置文件

vim /etc/sysconfig/grafana-server

GRAFANA_USER=grafana
GRAFANA_GROUP=grafana
GRAFANA_HOME=/usr/local/grafana
LOG_DIR=/var/log/grafana
DATA_DIR=/var/lib/grafana
MAX_OPEN_FILES=10000
CONF_DIR=/etc/grafana
CONF_FILE=/etc/grafana/grafana.ini
RESTART_ON_UPGRADE=true
PLUGINS_DIR=/var/lib/grafana/plugins
PROVISIONING_CFG_DIR=/etc/grafana/provisioning
# Only used on systemd systems
PID_FILE_DIR=/var/run/grafana

1.8 systemd 管理脚本

vim /usr/lib/systemd/system/grafana-server.service

# /usr/lib/systemd/system/grafana-server.service
[Unit]
Description=Grafana instance
Documentation=http://docs.grafana.org
Wants=network-online.target
After=network-online.target
After=postgresql.service mariadb.service mysqld.service influxdb.service

[Service]
EnvironmentFile=/etc/sysconfig/grafana-server
User=grafana
Group=grafana
Type=notify
Restart=on-failure
WorkingDirectory=/usr/local/grafana
RuntimeDirectory=grafana
RuntimeDirectoryMode=0750
ExecStart=/usr/local/grafana/bin/grafana server \
--config=${CONF_FILE} \
--pidfile=${PID_FILE_DIR}/grafana-server.pid \
--packaging=rpm \
cfg:default.paths.logs=${LOG_DIR} \
cfg:default.paths.data=${DATA_DIR} \
cfg:default.paths.plugins=${PLUGINS_DIR} \
cfg:default.paths.provisioning=${PROVISIONING_CFG_DIR}

LimitNOFILE=10000
TimeoutStopSec=20
CapabilityBoundingSet=
DeviceAllow=
LockPersonality=true
MemoryDenyWriteExecute=false
NoNewPrivileges=true
PrivateDevices=true
PrivateTmp=true
ProtectClock=true
ProtectControlGroups=true
ProtectHome=true
ProtectHostname=true
ProtectKernelLogs=true
ProtectKernelModules=true
ProtectKernelTunables=true
ProtectProc=invisible
ProtectSystem=full
RemoveIPC=true
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
RestrictNamespaces=true
RestrictRealtime=true
RestrictSUIDSGID=true
SystemCallArchitectures=native
UMask=0027

[Install]
WantedBy=multi-user.target

1.9 启动

systemctl daemon-reload
systemctl enable grafana-server --now

1.10 注意

如果启动报错, 注意配置文件还有相关目录的权限

二、 docker 构建

2.1 拉取镜像

docker pull grafana/grafana-enterprise

2.2 docker-compose

vim grafana.yaml

version: "3"
services:
grafana:
image: grafana/grafana-enterprise
container_name: grafana
restart: unless-stopped
user: "0"
ports:
- "3000:3000"
volumes:
- /var/lib/grafana:/var/lib/grafana

2.3 启动

docker-compose -f grafana.yaml up -d