ubuntu14.04 Docker安装Prometheus,Grafana监控服务器性能数据

一、安装Prometheus

docker pull prom/prometheus

二、配置Prometheus

sudo mkdir /etc/prometheus/
sudo vim /etc/prometheus/prometheus.yml
  • 添加监控节点
# 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).

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'codelab-monitor'

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first.rules"
  # - "second.rules"

# 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=` to any timeseries scraped from this config.
  - job_name: 'prometheus'

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

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

  - job_name: 'mysql'
    static_configs:
      - targets: ['172.17.0.1:9104']
        labels:
          instance: mysql

  - job_name: 'monitor'
    static_configs:
      - targets: ['172.17.0.1:9100']
        labels:
          instance: monitor
  • 启动prometheus容器
# 因为目录/data用户组和用户是jtserver,docker执行命令是root用户,先将/data/lib/prometheus给其他组权限才能执行
sudo mkdir /data/lib/prometheus
sudo chmod 767 /data/lib/prometheus

docker run -d \
  -p 9090:9090 \
  --restart=always \
  --name prometheus \
  -v /data/lib/prometheus:/data/lib/prometheus \
  -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
  prom/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/data/lib/prometheus

三、添加服务器基本性能监控

  • 拉取node-exporter模块
docker pull prom/node-exporter
  • 启动收集参数docker
docker run -d \
  -v "/proc:/host/proc:ro" \
  -v "/sys:/host/sys:ro" \
  -v "/:/rootfs:ro" \
  --net="host" \
  --restart=always \
  prom/node-exporter \
  --path.procfs /host/proc \
  --path.sysfs /host/sys \
  --collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"
  • 新版本收集不到信息,使用一个旧版本
docker pull prom/node-exporter:v0.14.0
docker run -d -p 9100:9100 \
  -v "/proc:/host/proc:ro" \
  -v "/sys:/host/sys:ro" \
  -v "/:/rootfs:ro" \
  --net="host" \
  --restart=always \
  prom/node-exporter:v0.14.0 \
    --collector.procfs /host/proc \
    --collector.sysfs /host/sys \
    --collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"
docker pull prom/node-exporter
docker pull prom/mysqld-exporter

四、监控mysql信息

  • 下载mysql_exporter镜像
docker pull prom/mysqld-exporter
  • mysql_exporter需要链接数据库,mysql创建mysql_monitor用户,添加PROCESS, REPLICATION CLIENT权限
mysql> INSERT INTO mysql.user(Host,User,Password) VALUES("localhost", "mysql_monitor", password("mysql_monitor")); 
mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysql_monitor'@'localhost' IDENTIFIED BY 'mysql_monitor';
mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysql_monitor'@'%' IDENTIFIED BY 'mysql_monitor';
mysql> FLUSH PRIVILEGES;
  • docker启动mysql_exporter镜像
docker run -d -p 9104:9104 \
  --name=mysqld-exporter \
  --restart=always \
  -e DATA_SOURCE_NAME="mysql_monitor:mysql_monitor@(172.17.0.1:3306)/" \
  prom/mysqld-exporter

五、安装和配置grafana

wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.1.4_amd64.deb
sudo apt-get install -y adduser libfontconfig
sudo dpkg -i grafana_5.1.4_amd64.deb

echo "deb https://packagecloud.io/grafana/stable/debian/ stretch main" | sudo tee sources.list.d/grafana-stable.list
curl https://packagecloud.io/gpg.key | sudo apt-key add -
sudo apt-get install -y apt-transport-https
sudo apt-get update
sudo apt-get install grafana
sudo update-rc.d grafana-server defaults
  • 添加数据源
    ubuntu14.04 Docker安装Prometheus,Grafana监控服务器性能数据_第1张图片
  • 选择使用Prometheus数据源,填Prometheus数据请求url
    ubuntu14.04 Docker安装Prometheus,Grafana监控服务器性能数据_第2张图片

六、配置percona-grafana面板

  • grafana 5.+版本dashboars.json替换成dashboards
#################################### Dashboards History ##################
[dashboards]
# Number dashboard versions to keep (per dashboard). Default: 20, Minimum: 1
;versions_to_keep = 20
enabled = true
path = /var/lib/grafana/dashboards

ubuntu14.04 Docker安装Prometheus,Grafana监控服务器性能数据_第3张图片

  • 添加percona面板,但是重启后不生效,只能一个个导入面板
git clone https://github.com/percona/grafana-dashboards.git
cp -r grafana-dashboards/dashboards /var/lib/grafana/
sudo service grafana-server restart

ubuntu14.04 Docker安装Prometheus,Grafana监控服务器性能数据_第4张图片

七、直接使用grafana插件

添加percona-app插件

grafana-cli plugins install percona-percona-app

ubuntu14.04 Docker安装Prometheus,Grafana监控服务器性能数据_第5张图片

  • 打开使用percona插件
    ubuntu14.04 Docker安装Prometheus,Grafana监控服务器性能数据_第6张图片

  • 使用插件效果图
    ubuntu14.04 Docker安装Prometheus,Grafana监控服务器性能数据_第7张图片

八、参考

  • percona-grafana
  • percona-grafana-dashboards
  • http://www.ywnds.com/?p=9656
  • grafana安装
  • https://grafana.com/plugins/percona-percona-app/installation
  • https://grafana.com/dashboards/2428

你可能感兴趣的:(服务器,运维)