Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包 。自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员和用户社区。现在,它是一个独立的开源项目,并且独立于任何公司进行维护。为了强调这一点并阐明项目的治理结构,Prometheus 在2016年加入了 Cloud Native Computing Foundation,这是继Kubernetes之后的第二个托管项目。总所周知Zabbix在监控界占有不可撼动的地位,功能强大。但是对容器监控显得力不从心。Prometheus则解决了容器的监控问题。
Prometheus采集的监控数据均以metric(指标)形式保存在时序数据库中(TSDB),每一条时间序列由 metric 和 labels 组成,每条时间序列按照时间的先后顺序存储它的样本值。
Prometheus通过HTTP协议周期性抓取被监控组件的状态,被监控组件只要提供对应的HTTP接口就可以接入监控。不需要任何SDK或者其他的集成过程。这样做非常适合做虚拟化环境监控系统,比如Vm、Docker、Kubernetes等。
Prometheus下载地址:https://prometheus.io/download
# 此处以CentOS 7为例
# 下载完成后上传到服务器并解压,或者直接wget下载
wget https://github.com/prometheus/prometheus/releases/download/v2.21.0/prometheus-2.21.0.linux-amd64.tar.gz
tar -zxvf prometheus-2.21.0.linux-amd64.tar.gz
# 由于解压后的文件夹名过长,重命名文件夹
mv prometheus-2.21.0.linux-amd64 prometheus
# 根据实际路径修改路径,并指定需要监听的端口,这样,简单的Prometheus服务端就搭建好了
nohup /root/prometheus/prometheus --web.listen-address=0.0.0.0:9090 --config.file=/root/prometheus/prometheus.yml --web.enable-lifecycle --storage.tsdb.path=/root/prometheus/data --storage.tsdb.retention.time=15d &
# 不要忘记防火墙放行端口,--permanent参数为永久生效,不加此参数重启失效
firewall-cmd --zone=public --add-port=9090/tcp --permanent
# 或者直接停用防火墙(不推荐)
systemctl stop firewalld
systemctl disable firewalld
完成上述操作并成功启动Prometheus服务后可打开浏览器尝试访问,http://ip:9090 (IP为部署的服务器ip,本机部署则直接127.0.0.1),如果顺利的话你将访问到如下页面
Alert:用于配置告警规则 Graph:用来运行PromQL语句的一个控制台,并且可以把运行出来的语句用用图形化进行展示 Status:包含系统信息,系统状态,配置信息,目标节点的状态,服务发现状态等元信息的查看
这张图是官方的整体架构图。米黄色部分是Prometheus自己的组件,绿色的为第三方的中间件和应用。
Prometheus主要是通过拉取的方式获取数据,那么我们模拟一个数据源,并让prometheus去拉取。 先新建一个SpringBoot项目,并添加如下pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
并在application.properties里加上
server.port=8080
spring.application.name=springboot2-prometheus
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
management.endpoints.web.exposure.include=* 配置为开启 Actuator 服务,Spring Boot Actuator 会自动配置一个 URL 为 /actuator/Prometheus 的 HTTP 服务来供 Prometheus 抓取数据,不过该服务默认关闭,该配置将打开所有的 Actuator 服务。
management.metrics.tags.application 配置会将该工程应用名称添加到计量器注册表的 tag 中去,方便后边 Prometheus 根据应用名称来区分不同的服务。
在工程启动类中添加如下Bean来监控JVM性能指标信息
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName){
return registry -> registry.config().commonTags("application", applicationName);
}
}
在启动SpringBoot工程后,在浏览器访问http://127.0.0.1:8080/actuator/prometheus ,如果服务正常,你将会看到如下内容
编辑Prometheus目录下的prometheus.yml文件,并添加如下内容(需要遵循yaml语法,注意缩进)
- job_name: 'test'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['192.168.80.52:8080']
labels:
instance: demo
上面的配置表示Prometheue每隔5秒钟从http://192.168.80.52:8080/actuator/prometheus 这个url拉取指标,并且为每个指标添加instance里设置的这个标签。
配置完成后重启Prometheue服务,并访问Prometheue Web控制台的Staus下的targets,查看Targets是否添加成功,如果数据拉取正常,则State为UP状态
#编辑Grafana yum源
vim /etc/yum.repos.d/grafana.repo
#将下面的内容粘贴进去并保存退出
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
#通过yum安装
yum install grafana
#启动Grafana
systemctl start grafana-server
systemctl enable grafana-server
安装完成后访问 http://ip:3000 即可打开Grafana登录页面 默认账号与密码均为admin,首次登录需要重设密码
导入JVM的Dashboard模板,模板编号为 4701
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。