Prometheus远端数据库存储使用PostgreSQL

以下介绍两种方式,一种Docker方式,Postgresql运行于容器中,随容器生,随容器死。一种传统方式,运行于宿主机上,源码编译安装。

方式一:Docker方式,简单快速!

启动PostgreSQL, pg_prometheus, TimescaleDB容器

docker run --name pg_prometheus -d -e POSTGRES_PASSWORD=mypass -p 5432:5432 timescale/pg_prometheus:latest  postgres -csynchronous_commit=off

启动prometheus-postgreSQL storage adapter容器

docker run --name prometheus_postgresql_adapter --link pg_prometheus -d -p 9201:9201  timescale/prometheus-postgresql-adapter:latest  -pg-host=pg_prometheus  -pg-password=mypass  -pg-prometheus-log-samples

启动Prometheus容器

wget https://github.com/timescale/prometheus-postgresql-adapter/blob/master/sample-docker-prometheus.yml 

docker run -p 9090:9090 --link prometheus_postgresql_adapter -v sample-docker-prometheus.yml:/etc/prometheus/prometheus.yml   prom/prometheus

验证Prometheus

curl http://localhost:9090/graph

方式二:源码编译安装,进阶之选!

下载、编译、安装Postgresql

建议选择Postgresql 9.x 版本。部分Linux系统的组件不能很好的兼容Postgresql最新版本。

wget https://ftp.postgresql.org/pub/source/v9.6.16/postgresql-9.6.16.tar.gz
tar -zxvf postgresql-9.6.16.tar.gz
cd postgresql-9.6.16
./configure
make 
make install # root

初始化Postgresql数据库

adduser postgres
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

下载、编译、安装pg_prometheus

git clone https://github.com/timescale/pg_prometheus.git
cd pg_prometheus
make
make install #root

安装TimescaleDB

先安装cmake

要求cmake版本 >=3.4

wget https://cmake.org/files/v3.5/cmake-3.5.0-Linux-x86_64.sh
chmod a+x cmake-3.5.0-Linux-x86_64.sh
./cmake-3.5.0-Linux-x86_64.sh
cd cmake-3.5.0-Linux-x86_64
cp -r bin/* /usr/local/bin/
cp -r -f share/* /usr/local/share/

官方手册:https://cmake.org/download/

后安装TimescaleDB
git clone https://github.com/timescale/timescaledb.git
cd timescaledb
./bootstrap -DUSE_OPENSSL=0 -DREGRESS_CHECKS=OFF
cd build && make
make install

官网手册:https://docs.timescale.com/latest/getting-started/installation/rhel-centos/installation-source

配置Postgresql

修改postgresql.conf

添加下行:
shared_preload_libraries = ‘pg_prometheus, timescaledb’

修改监听地址:
listen_addresses=’*’

修改pg_hba.conf

赋权容器访问数据库:
host all admin 172.17.0.2/16 md5
或全部开启:
host all all 0.0.0.0/0 trust

启动Postgresql并配置pg_prometheus

/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/data/logfile start 
export PATH=/usr/local/pgsql/bin:$PATH
psql
CREATE EXTENSION pg_prometheus;
CREATE EXTENSION timescaledb;
CREATE ROLE prometheus WITH LOGIN PASSWORD 'secret';
GRANT ALL ON SCHEMA prometheus TO prometheus;

建表

启用TimescaleDB (推荐)
SELECT create_prometheus_table('metrics',use_timescaledb=>true);
不启用TimescaleDB
SELECT create_prometheus_table('metrics',use_timescaledb=>false);

官方手册: Github

修改Prometheus

修改prometheus.yml,添加下行:

remote_write:
    - url: "http://localhost:9201/write"
remote_read:
    - url: "http://localhost:9201/read"
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值