Promethues(二)安装
二进制方式
-
下载安装包 最新安装包
$ mkdir -p /opt/k8s/prometheus $ cd /opt/k8s/prometheus $ wget https://github.com/prometheus/prometheus/releases/download/v2.16.0/prometheus-2.16.0.linux-amd64.tar.gz $ tar -xzvf prometheus-2.16.0.linux-amd64.tar.gz
-
查看解压后的文件目录
$ cd /opt/k8s/prometheus/prometheus-2.16.0.linux-amd64 $ ls -al 总用量 141000 drwxr-xr-x 4 3434 3434 4096 2月 14 09:54 . drwxr-xr-x 3 root root 4096 3月 18 12:05 .. drwxr-xr-x 2 3434 3434 4096 2月 14 09:52 console_libraries drwxr-xr-x 2 3434 3434 4096 2月 14 09:52 consoles -rw-r--r-- 1 3434 3434 11357 2月 14 09:52 LICENSE -rw-r--r-- 1 3434 3434 3184 2月 14 09:52 NOTICE -rwxr-xr-x 1 3434 3434 82329106 2月 14 07:52 prometheus -rw-r--r-- 1 3434 3434 926 2月 14 09:52 prometheus.yml -rwxr-xr-x 1 3434 3434 48417809 2月 14 07:54 promtool -rwxr-xr-x 1 3434 3434 13595766 2月 14 07:54 tsdb
- consoles: prometheus自带的一些view对应的html文件
- prometheus: 一个可执行文件,是Prometheus server的启动文件
- prometheus.yml: 默认的配置文件
- data: Prometheus 抓取的指标数据存放目录
- promtool: 一个工具集,可以用来对配置文件、规则文件进行检查,也可以利用PromQl进行查询操作
- tsdb: prometheus 自身的时间序列数据库TSDB的客户端工具
运行prometheus
-
使用默认的配置文件启动prometheus
$ cd /opt/k8s/prometheus/prometheus-2.16.0.linux-amd64 $ ./prometheus --config.file=prometheus.yml level=info ts=2020-03-18T05:41:13.719Z caller=main.go:295 msg="no time or size retention was set so using the default time retention" duration=15d level=info ts=2020-03-18T05:41:13.719Z caller=main.go:331 msg="Starting Prometheus" version="(version=2.16.0, branch=HEAD, revision=b90be6f32a33c03163d700e1452b54454ddce0ec)" level=info ts=2020-03-18T05:41:13.719Z caller=main.go:332 build_context="(go=go1.13.8, user=root@7ea0ae865f12, date=20200213-23:50:02)" level=info ts=2020-03-18T05:41:13.719Z caller=main.go:333 host_details="(Linux 5.3.0-40-generic #32~18.04.1-Ubuntu SMP Mon Feb 3 14:05:59 UTC 2020 x86_64 master (none))" level=info ts=2020-03-18T05:41:13.719Z caller=main.go:334 fd_limits="(soft=1024, hard=1048576)" level=info ts=2020-03-18T05:41:13.719Z caller=main.go:335 vm_limits="(soft=unlimited, hard=unlimited)" level=info ts=2020-03-18T05:41:13.720Z caller=main.go:661 msg="Starting TSDB ..." level=info ts=2020-03-18T05:41:13.720Z caller=web.go:508 component=web msg="Start listening for connections" address=0.0.0.0:9090 level=info ts=2020-03-18T05:41:13.723Z caller=head.go:577 component=tsdb msg="replaying WAL, this may take awhile" level=info ts=2020-03-18T05:41:13.723Z caller=head.go:625 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0 level=info ts=2020-03-18T05:41:13.724Z caller=main.go:676 fs_type=EXT4_SUPER_MAGIC level=info ts=2020-03-18T05:41:13.724Z caller=main.go:677 msg="TSDB started" level=info ts=2020-03-18T05:41:13.724Z caller=main.go:747 msg="Loading configuration file" filename=prometheus.yml level=info ts=2020-03-18T05:41:13.725Z caller=main.go:775 msg="Completed loading of configuration file" filename=prometheus.yml level=info ts=2020-03-18T05:41:13.725Z caller=main.go:630 msg="Server is ready to receive web requests."
-
默认配置文件
$ cat 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']
-
global 配置prometheus拉取指标数据的频率和进行计算的频率,都是15s
-
scrape_configs 配置段中,通过静态配置方式指定从localhost:9090/metrics拉取指标信息,其中9090是prometheus server启动后监听的端口
-
通过访问 http://localhost:9090/metrics,可以观察prometheus自身提供的各种监控指标
-
-
通过界面访问
-
将Promethues配置成系统服务,随系统启动
-
编辑启动文件
$ cat > /etc/systemd/system/prometheus.service<< EOF [Unit] Description=Prometheus Server Documentation=https://prometheus.io/docs/introduction/overview/ After=network.target [Service] Restart=on-failure WorkingDirectory=/opt/k8s/prometheus/prometheus-2.16.0.linux-amd64 ExecStart=/opt/k8s/prometheus/prometheus-2.16.0.linux-amd64/prometheus \ --config.file=/opt/k8s/prometheus/prometheus-2.16.0.linux-amd64/prometheus.yml \ --storage.tsdb.path=/opt/k8s/prometheus/prometheus-2.16.0.linux-amd64/data \ --storage.tsdb.retention.time=30d ExecReload=/bin/kill -s HUP $MAINPID [Install] WantedBy=multi-user.target EOF
- config.file指定配置文件地址
- storage.tsdb.path指定数据文件目录
- storage.tsdb.retention.time指定数据文件保留时间
Promethues提供了很多的命令行配置选项,可通过./prometheus -h进行查看
-
通过systemctl启动prometheus
$ systemctl daemon-reload $ systemctl start prometheus $ systemctl enable prometheus
-
docker 容器方式
前提是服务器上已经装上了docker环境
-
下载镜像
$ docker pull prom/prometheus:v2.16.0
-
启动镜像
$ docker run -p 9090:9090 prom/prometheus:v2.16.0
- 以上命令采用默认配置启动prometheus
-
通过挂载方式将配置文件挂载到容器内来自定义配置项
docker run \ -p 9090:9090 \ -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml \ prom/prometheus:v2.16.0
或者将配置目录直接挂载进去
docker run \ -p 9090:9090 \ -v /path/to/config:/etc/prometheus \ prom/prometheus:v2.16.0
如果监控目标以及rule等信息都稳定下来,可以基于prometheus的镜像,将配置文件等信息追加到容器中,Dockerfile如下
FROM prom/prometheus:v2.16.0 ADD prometheus.yml /etc/prometheus/