1.创建数据持久卷目录 mkdir -p /opt/prometheus/data/ mkdir -p /opt/prometheus/conf/ mkdir -p /opt/grafana/data/ chmod 777 /opt/prometheus/data /opt/grafana/data docker ps -a | awk '{print $1}' | grep -v CONTAINER | xargs docker rm -f 2.编辑prometheus配置文件 cat > /opt/prometheus/conf/prometheus.yml << EOF # 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: ['192.168.200.170:9090','192.168.200.171:8080','192.168.200.171:9100'] EOF 3.运行prometheus容器 docker run -d --restart=always --name prometheus -p 9090:9090 \ -v /opt/prometheus/conf/prometheus.yml:/etc/prometheus/prometheus.yml \ -v /opt/prometheus/data/:/prometheus \ prom/prometheus 4.运行grafana容器 docker run -d --restart=always --name grafana -p 3000:3000 \ -v /opt/grafana/data/:/var/lib/grafana \ grafana/grafana 5.运行cadvisor容器 docker run -d --restart=always --name cadvisor -p 8080:8080 \ -v /:/rootfs:ro \ -v /var/run:/var/run:rw \ -v /sys:/sys:ro \ -v /var/lib/docker/:/var/lib/docker:ro \ -v /dev/disk/:/dev/disk:ro \ -v /etc/localtime:/etc/localtime:rw \ google/cadvisor 6.运行node-exporter容器 docker run -d --restart=always --name node-exporter -p 9100:9100 \ -v /proc:/host/proc:ro \ -v /sys:/host/sys:ro \ -v /:/rootfs:ro \ -v /etc/localtime:/etc/localtime:rw \ prom/node-exporter \ --path.procfs /host/proc \ --path.sysfs /host/sys \ --collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)" 7.运行业务容器(示例) docker run -d --restart=always -u root --name nginx \ --net=host --privileged nginx