安装prometheus并优雅的启动

1. 安装prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.2.1/prometheus-2.2.1.linux-amd64.tar.gz
tar -zxvf prometheus-2.2.1.linux-amd64.tar.gz
mv prometheus-2.2.1.linux-amd64 prometheus
cd prometheus/

------------------------------------------------------------
2.编辑配置文件
vim 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).

# 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']
      
--------------------------------------------------------------
3.测试能否启动:
./prometheus --config.file=/opt/prometheus/prometheus.yml 

4.使用netstat -ntpl 验证9090端口是否处于监听状态
  打开浏览器验证
  http://IP:9090
--------------------------------------------------------------
5.优雅的启动普罗米修斯

   创建日志文件:
   touch /var/log/prometheus.log
# 进入system目录,编写systemd服务脚本
cd /usr/lib/systemd/system
vim prometheus.service

#内容如下:
   [Unit]
   Description=Prometheus
   Documentation=https://prometheus.io/docs/introduction/overview/
   Wants=network-online.target
   After=network-online.target
   
   [Service]
   #User=prometheus  # 必须用该用户和相应的执行权限否则不能启动
   #Group=prometheus
   User=root
   Group=root
   Type=simple
   ExecStart=/opt/prometheus/prometheus.sh  # 启动脚本
   
   
   [Install]
   WantedBy=multi-user.target


------------------------------------------------------------------

编写启动脚本:
vim /opt/prometheus/prometheus.sh
内容如下:
#!/bin/bash
/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml &>> /var/log/prometheus.log

------------------------------------------------------------------

6. 验证能否优雅的启动:
 systemctl daemon-reload  # 重新加载daemon
 systemctl enable prometheus.service  # 配置开机加载
 systemctl start prometheus.service   # 启动普罗米修斯
 systemctl status prometheus          # 查看是启动状态
 
● prometheus.service - Prometheus
   Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2018-05-05 10:47:43 CST; 20min ago                         # runing
 Main PID: 38129 (prometheus.sh)
   CGroup: /system.slice/prometheus.service
           ├─38129 /bin/bash /opt/prometheus/prometheus.sh
           └─38130 /opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml

May 05 10:47:43 prometheus systemd[1]: Started Prometheus.
May 05 10:47:43 prometheus systemd[1]: Starting Prometheus...
 
7. 查看日志:(感觉有点问题,不知道是不是这么玩)
cat /var/log/prometheus.log
level=info ts=2018-05-05T02:47:43.171265453Z caller=main.go:220 msg="Starting Prometheus" version="(version=2.2.1, branch=HEAD, revision=bc6058c81272a8d938c05e75607371284236aadc)"
level=info ts=2018-05-05T02:47:43.171315413Z caller=main.go:221 build_context="(go=go1.10, user=root@149e5b3f0829, date=20180314-14:15:45)"
level=info ts=2018-05-05T02:47:43.171328257Z caller=main.go:222 host_details="(Linux 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 prometheus (none))"
level=info ts=2018-05-05T02:47:43.171339771Z caller=main.go:223 fd_limits="(soft=1024, hard=4096)"
level=info ts=2018-05-05T02:47:43.174048516Z caller=main.go:504 msg="Starting TSDB ..."
level=info ts=2018-05-05T02:47:43.191508356Z caller=web.go:382 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2018-05-05T02:47:43.19927128Z caller=main.go:514 msg="TSDB started"
level=info ts=2018-05-05T02:47:43.19931134Z caller=main.go:588 msg="Loading configuration file" filename=/opt/prometheus/prometheus.yml
level=info ts=2018-05-05T02:47:43.20034842Z caller=main.go:491 msg="Server is ready to receive web requests."

猜你喜欢

转载自my.oschina.net/jiaoyanli/blog/1807210