在Debian系统上部署PostgreSQL集群,通常指的是设置一个高可用性的PostgreSQL集群,例如使用Patroni和Keepalived来实现。以下是使用Patroni和Keepalived在Debian上部署PostgreSQL集群的基本步骤:
安装必要的软件包: 在所有的节点上安装PostgreSQL、Patroni和Keepalived。
sudo apt-get update
sudo apt-get install -y postgresql postgresql-contrib patroni keepalived
配置PostgreSQL:
在每个节点上配置postgresql.conf
和pg_hba.conf
文件,确保它们允许来自其他节点的连接。
初始化数据库集群: 在主节点上初始化一个新的PostgreSQL数据库集群。
sudo pg_createcluster --start --name <cluster_name> <version> main --start
替换<cluster_name>
为你的集群名称,<version>
为你的PostgreSQL版本。
配置Patroni:
在每个节点上创建Patroni配置文件/etc/patroni.yml
,并确保配置正确指向所有节点的信息,包括它们的角色(主或备)、数据目录、连接字符串等。
scope: postgresql
name: <node_name>
namespace: /db/
restapi:
listen: 0.0.0.0:8008
connect_address: <master_ip>
etcd:
host: <etcd_ip>
port: 2379
scheme: http
bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: true
use_slots: true
parameters:
wal_level: replica
max_wal_senders: 4
wal_keep_segments: 8
hot_standby: on
postgresql:
listen: 0.0.0.0:5432
connect_address: 127.0.0.1
data_dir: /var/lib/postgresql/<version>/<cluster_name>
pg_hba:
- host replication replicator <replica_ip>/32 md5
- host all all 0.0.0.0/0 md5
authentication:
replication:
username: replicator
password: <replicator_password>
parameters:
max_connections: 100
unix_socket_directories: '/var/run/postgresql'
替换<node_name>
为节点名称,<master_ip>
为主节点的IP地址,<etcd_ip>
为etcd集群的IP地址,<replica_ip>
为当前节点的IP地址。
启动Patroni: 在每个节点上启动Patroni服务。
sudo systemctl start patroni
sudo systemctl enable patroni
配置Keepalived: 在每个节点上配置Keepalived以确保虚拟IP地址的管理和故障转移。
编辑/etc/keepalived/keepalived.conf
文件,添加如下配置:
vrrp_script chk_psql {
script "pg_isready -h <virtual_ip> -U replicator -d <database_name>"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass <keepalived_password>
}
virtual_ipaddress {
<virtual_ip>
}
track_script {
chk_psql
}
}
替换<virtual_ip>
为虚拟IP地址,<database_name>
为数据库名称,<keepalived_password>
为Keepalived的认证密码。
启动Keepalived: 在每个节点上启动Keepalived服务。
sudo systemctl start keepalived
sudo systemctl enable keepalived
验证集群状态: 使用Patroni提供的API或者直接连接到虚拟IP地址来验证集群的状态。
请注意,这些步骤提供了一个基本的指南,实际的部署可能会更复杂,取决于你的具体需求和环境。在生产环境中部署之前,请确保你已经充分测试并理解了所有的配置和步骤。此外,你可能还需要配置防火墙规则以允许必要的网络流量。