PostgreSQL + CentOS7 + Patroni

背景

PostgreSQL 的高可用环境

环境

  • CentOS 7

    • pg01 (192.168.1.120)
    • pg02 (192.168.1.121)
    • pg03(192.168.1.122)
  • PostgreSQL 11.2
  • Patroni 1.6.0
  • Python 2.7
  • etcdctl version: 3.3.11,API version: 2
  • 有效的时钟同步服务

安装配置 etcd 服务

在每个节点上安装服务,配置成集群模式。

参考内链:ETCD 集群安装

参考外链:ETCD 集群安装

安装配置 patroni 服务

  • 注意:所有的操作都是在 root 用户下运行。
  • 注意:在三个节点上都需要安装并进行配置。

准备数据目录

这个目录就是数据库存放数据的位置,根据实际需要进行配置。

mkdir -p /opt/data/patroni
chown -R postgres:postgres /opt/data
chmod -R 700 /opt/data

安装软件包

yum install -y gcc python-devel epel-release
yum install -y python2-pip
pip install --upgrade setuptools
pip install psycopg2-binary
pip install python-etcd
pip install patroni

配置启动脚本

为 patroni 服务创建启动脚本:vi /etc/systemd/system/patroni.service 添加如下内容。

[Unit]
Description=Runners to orchestrate a high-availability PostgreSQL
After=syslog.target network.target [Service]
Type=simple
User=postgres
Group=postgres
ExecStart=/bin/patroni /etc/patroni.yml
KillMode=process
TimeoutSec=30
Restart=no [Install]
WantedBy=multi-user.targ

禁止 postgresql 的自启动,使用 patroni 来管理

# 关闭 postgresql 的自动启动配置
systemctl disable postgresql-11

配置启动文件

创建配置文件 vi /etc/patroni.yml ,这个文件控制 postgresql 实例的启动关闭等等。

  • 留意配置文件上:password 的值需要加上单引号。

第一个节点的配置:192.168.1.120 - pg01

scope: postgres
name: pg01 restapi:
listen: 192.168.1.120:8008
connect_address: 192.168.1.120:8008 etcd:
host: 192.168.1.120:2379 bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: true initdb:
- encoding: UTF8
- data-checksums pg_hba:
- host replication replica 127.0.0.1/32 md5
- host replication replica 192.168.1.120/0 md5
- host replication replica 192.168.1.121/0 md5
- host replication replica 192.168.1.122/0 md5
- host all all 0.0.0.0/0 md5 users:
admin:
password: admin
options:
- createrole
- createdb postgresql:
listen: 192.168.1.120:5432
bin_dir: /usr/pgsql-11/bin
connect_address: 192.168.1.120:5432
data_dir: /opt/data/patroni
pgpass: /tmp/pgpass
authentication:
replication:
username: replica
password: 'replica'
superuser:
username: postgres
password: '123456'
parameters:
unix_socket_directories: '.' tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false

第二个节点的配置:192.168.1.121 - pg02

scope: postgres
name: pg02 restapi:
listen: 192.168.1.121:8008
connect_address: 192.168.1.121:8008 etcd:
host: 192.168.1.121:2379 bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: true initdb:
- encoding: UTF8
- data-checksums pg_hba:
- host replication replica 127.0.0.1/32 md5
- host replication replica 192.168.1.120/0 md5
- host replication replica 192.168.1.121/0 md5
- host replication replica 192.168.1.122/0 md5
- host all all 0.0.0.0/0 md5 users:
admin:
password: admin
options:
- createrole
- createdb postgresql:
listen: 192.168.1.121:5432
bin_dir: /usr/pgsql-11/bin
connect_address: 192.168.1.121:5432
data_dir: /opt/data/patroni
pgpass: /tmp/pgpass
authentication:
replication:
username: replica
password: 'replica'
superuser:
username: postgres
password: '123456'
parameters:
unix_socket_directories: '.' tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false

第三个节点的配置:192.168.1.122 - pg03

scope: postgres
name: pg03 restapi:
listen: 192.168.1.122:8008
connect_address: 192.168.1.122:8008 etcd:
host: 192.168.1.122:2379 bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: true initdb:
- encoding: UTF8
- data-checksums pg_hba:
- host replication replica 127.0.0.1/32 md5
- host replication replica 192.168.1.120/0 md5
- host replication replica 192.168.1.121/0 md5
- host replication replica 192.168.1.122/0 md5
- host all all 0.0.0.0/0 md5 users:
admin:
password: admin
options:
- createrole
- createdb postgresql:
listen: 192.168.1.122:5432
bin_dir: /usr/pgsql-11/bin
connect_address: 192.168.1.122:5432
data_dir: /opt/data/patroni
pgpass: /tmp/pgpass
authentication:
replication:
username: replica
password: 'replica'
superuser:
username: postgres
password: '123456'
parameters:
unix_socket_directories: '.' tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false

启动并验证服务

现在我们可以启动 patroni 服务,它会在后台创建新的数据库,根据配置文件( /etc/patroni.yml )里面的内容创建用户。

注意:这里建议不要设置开机自启动,因为不需要,手动调试的时候就手动启动,如果宕机了,需要手动处理之后再启动。

# 启动服务(会自动启动 pg 实例)
systemctl start patroni
# 关闭服务(会自动关闭 pg 实例)
systemctl stop patroni # 查看集群状态
patronictl -c /etc/patroni.yml list
[root@postgres0 patroni]# patronictl -c /etc/patroni.yml list
+----------+--------+---------------+--------+---------+----+-----------+
| Cluster | Member | Host | Role | State | TL | Lag in MB |
+----------+--------+---------------+--------+---------+----+-----------+
| postgres | pg01 | 192.168.1.120 | Leader | running | 1 | 0.0 |
| postgres | pg02 | 192.168.1.121 | | running | 1 | 0.0 |
| postgres | pg03 | 192.168.1.122 | | running | 1 | 0.0 |
+----------+--------+---------------+--------+---------+----+-----------+

你可以在 /var/log/messages 里面看到启动信息,也可以通过命令查看启动信息()。

JDBC连接

PG的 jdbc 中有一个targetServerType参数,设置这里,它会自动寻找 master 节点,也就是可以写入数据的节点。

jdbc:postgresql://192.168.1.120:5432,192.168.1.121:5432,192.168.1.122:5432/test1?targetServerType=master&user=postgres&password=123456

Patroni 命令

# 可以查看命令的使用说明
patronictl --help
# 查看版本号
patronictl -c /etc/patroni.yml version
# 查看所有成员信息
patronictl -c /etc/patroni.yml list
# 重新加载配置
patronictl -c /etc/patroni.yml reload
# 移除集群,重新配置的时候使用
patronictl -c /etc/patroni.yml remove postgres
# 重启数据库集群
patronictl -c /etc/patroni.yml restart postgres
# 切换 Leader,将一个 slave 切换成 leader。
patronictl -c /etc/patroni.yml switchover

参考文档

利用Restful API 动态修改数据库属性

https://patroni.readthedocs.io/en/latest/dynamic_configuration.html#dynamic-configuration

官方文档

https://patroni.readthedocs.io/en/latest/

JDBC参数,如何使用HA环境。

https://jdbc.postgresql.org/documentation/head/connect.html

0x10 - PostgreSQL 安装之 CentOS7 + Patroni的更多相关文章

  1. CentOS7 PostgreSQL安装

    CentOS7 PostgreSQL安装 CentOS7 PostgreSQL安装 Install 安装 使用yum安装 yum install http://yum.postgresql.org/9 ...

  2. 19.CentOS7下PostgreSQL安装过程

    CentOS7下PostgreSQL安装过程 装包 sudo yum install postgresql-server postgresql-contrib 说明: 这种方式直接明了,其他方法也可以 ...

  3. CentOS7 PostgreSQL 安装

    PostgreSQL安装 安装使用yum安装 (找源 http://yum.postgresql.org/) yum install https://download.postgresql.org/p ...

  4. centos7 postgresql安装配置

    2021-07-15 1.添加用户 # 添加用户 postgres useradd postgres # 给用户 postgres 设置密码 passwd postgres 2.切换到该用户,下载 p ...

  5. postgresql安装及配置

    目录 1. 安装 2. PostgrepSQL的简单配置 2.1 修改监听的ip和端口 2.2 修改数据库log相关的参数 2.3 内存参数 3. 数据库的基础操作 3.1 连接数据库控制台 3.2 ...

  6. Linux下apache+phppgadmin+postgresql安装配置

    Linux下apache+phppgadmin+postgresql安装配置 操作系统:CentOS 安装包:httpd(首选yum), php(包括php以及php-pgsql,php-mbstri ...

  7. Ubuntu PostgreSQL安装和配置

    一.安装 1.安装 使用如下命令,会自动安装最新版,这里为9.5 sudo apt-get install postgresql 安装完成后,默认会: (1)创建名为"postgres&qu ...

  8. PostgreSQL安装详细步骤(windows)

    原文地址:http://blog.chinaunix.net/uid-354915-id-3498734.html PostgreSQL安装:一.windows下安装过程安装介质:postgresql ...

  9. 使用Xshell5连接虚拟机VMware中安装的CentOS7系统

    使用Xshell5连接VMware中安装的CentOS7系统 准备材料 Xshell 下载地址 VMware Workstation 12 Pro 下载地址 CentOS 7 64位系统 下载地址 安 ...

随机推荐

  1. (转) Windows如何区分鼠标双击和两次单击

    Windows如何区分鼠标双击和两次单击 http://lbsloveldm.blog.hexun.com/12212875_d.html 在Windows平台上,鼠标左键的按下.松开.快速的两次点击 ...

  2. js实现ctrl+v粘贴上传图片(兼容chrome,firefox,ie11)

    背景 我们或多或少都使用过各式各样的富文本编辑器,其中有一个很方便功能,复制一张图片然后粘贴进文本框,这张图片就被上传了,那么这个方便的功能是如何实现的呢? 原理分析 提取操作:复制=>粘贴=& ...

  3. swift 录音 AVAudioRecorder

    2018年05月16日 15:22:44 msmwncx阅读数:548 https://blog.csdn.net/msmwncx/article/details/80336973 版权声明:本文为博 ...

  4. 干货分享|Law Essay写作高分攻略

    很多法学院的留学生对于Law Essay写作不是特别擅长,理论知识都了解,但是写出来的essay分数就是不高.同学们要从哪些方面入手呢?Law Essay写作要怎么拿高分?具体就跟小编一起来看看吧! ...

  5. SQL约束攻击

    本文转载自https://blog.csdn.net/kkr3584/article/details/69223010 目前值得高兴的是,开发者在建立网站时,已经开始关注安全问题了--几乎每个开发者都 ...

  6. hihocoder:#1082 : 然而沼跃鱼早就看穿了一切(用string)

    题目是这样的: 描述 fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽所有句子中的沼跃鱼("marshtomp",不区分大小写).为了使句子不缺少成分, ...

  7. PyMySQL学习笔记

    一些常用函数及解释 db = pymysql.connect('host','user','password','database') # 连接数据库 cursor = db.cursor() # 创 ...

  8. 51nod 1284:2 3 5 7的倍数 容斥原理

    1284 2 3 5 7的倍数 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 给出一个数N,求1至N中,有多少个数不是2 3 5 7的倍数. 例如N ...

  9. HDU 5311:Hidden String

    Hidden String  Accepts: 437  Submissions: 2174  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit ...

  10. 关于Oracle中job定时器(通过create_job创建的)配置示例

    begin dbms_scheduler.create_job(job_name => 'JOB_BASIC_STATISTIC', job_type => 'STORED_PROCEDU ...