首页 云原生

1. 安装 Docker 服务

curl https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
yum list docker-ce --showduplicates | sort -r    # 显示所有版本
yum install -y docker-ce-20.10.5    # 指定 docker 版本
systemctl start docker    # 启动 docker 服务
systemctl status docker    # 查看 docker 服务状态
systemctl enable docker    # 设置 docker 开机自启动

2. 安装启动 Prometheus 服务

创建 prom 用户,创建数据目录:

groupadd -g 2000 prom
useradd -u 2000 -g prom prom
mkdir /home/prom/data

创建配置文件:

vim /home/prom/prometheus.yaml
# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

启动容器服务:

docker pull prom/prometheus
docker run -d --net=host --user root -p 9090:9090 --name prometheus -v /home/prom/prometheus.yml:/etc/prometheus/prometheus.yml -v /home/prom/data:/data/prometheus prom/prometheus --config.file="/etc/prometheus/prometheus.yml" --storage.tsdb.path="/data/prometheus"

3. 安装启动 Grafana 服务

创建数据目录:

mkdir -p /home/prom/grafana && chmod 777 /home/prom/grafana

启动容器服务:

docker pull grafana/grafana:latest
docker run -d --net=host -p 3000:3000 -v /home/prom/grafana:/var/lib/grafana --name=grafana grafana/grafana:latest

安装插件:

下载Grafana插件

tar zxf /tmp/grafana-plugins.tar.gz -C /home/prom/grafana/plugins/
chown -R prom. /home/prom/grafana/plugins/*
chmod 777 /home/prom/grafana/plugins/*

重启容器:

docker restart grafana

4. 配置 Grafana 对接 Prometheus

访问 http://ip:3000,初始账号密码为 admin/admin,会要求更改密码。

按照如下截图顺序配置 Prometheus Dashboard:

5. Node_Exporter 示例

启动容器服务:

docker pull prom/node-exporter:latest
docker run -d --net=host -p 9100:9100 --name node-exporter prom/node-exporter:latest

修改 Prometheus 配置文件:

vim /home/prom/prometheus.yaml

  - job_name: 'linux-node'
    static_configs:
    - targets: ['localhost:9100']
      labels:
        instance: linux-node

重启 Prometheus 容器:

docker restart prometheus

创建Dashboard:

6. 多容器启动管理

多容器配置,需要修改端口、数据存储路径等信息,例如:

Prometheus:

docker run -d --net=host --user root -p 9091:9091 --name prometheus-poc -v /home/prom/poc/prometheus.yml:/etc/prometheus/prometheus.yml -v /home/prom/poc/data:/data/prometheus prom/prometheus --config.file="/etc/prometheus/prometheus.yml" --storage.tsdb.path="/data/prometheus" --web.listen-address="0.0.0.0:9091"

不同之处:

  • -p 9091:9091
  • --name prometheus-poc
  • -v /home/prom/poc/prometheus.yml
  • -v /home/prom/poc/data
  • --web.listen-address="0.0.0.0:9091"

Grafana:

docker run -d -p 3001:3000 -v /home/prom/poc/grafana:/var/lib/grafana --name=grafana-poc grafana/grafana:latest

不同之处:

  • -p 3001:3000
  • --name=grafana-poc
  • -v /home/prom/poc/grafana

7. 参考资料

Docker 部署 Prometheus+Grafana

Dashboard Download




文章评论

目录