以下介绍两种方式,一种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"