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. Egret - EUI - 隐藏滚动条

    <e:Skin> <e:VScrollBar autoVisibility="false" visible="false"/> < ...

  2. 在MFC做DLL动态链接库时,使用boost,出现断言错误

    建立的MFC DLL工程中有使用boost::thread,就会发生compile正常但是一程式执行或者直接编辑就出現ASSERT错误. 错误位置:dllinit.cpp,Line: 587,ASSE ...

  3. 【LeetCode】验证二叉搜索树

    [问题]给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征:节点的左子树只包含小于当前节点的数.节点的右子树只包含大于当前节点的数.所有左子树和右子树自身必须也是二叉搜 ...

  4. 七牛云存储javascript-sdk和java-sdk的使用

    自己做项目使用的是一台阿里云最便宜的服务器,存储空间只有40G,静态资源和动态资源都放在一起.听说七牛云存储注册认证即送10G的免费存储,想着把静态资源放在七牛云上,分担一下阿里云服务器的存储压力. ...

  5. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring体系结构详解

    Spring 框架采用分层架构,根据不同的功能被划分成了多个模块,这些模块大体可分为 Data Access/Integration.Web.AOP.Aspects.Messaging.Instrum ...

  6. MVC通用仓储类

    原文链接:http://www.codeproject.com/Articles/1095323/Generic-Repository-Pattern-MVC 良好的架构师任何项目的核心,开发人员一直 ...

  7. Python logging模块 控制台、文件输出

    步骤 导入logging模块 设置level(此处是DEBUG) 添加文件handler和流handler import logging logger=logging.getLogger(__name ...

  8. UVA 11375 高精度Bign类

    求火柴的组成的数字最多能组成多少种数字,典型的递推问题 但是因为结果巨大,要用高精度运算 一开始手写高精度,不仅挫的要死,最后还WA了. 最后学了一下白书上面的bign类,相当方便啊. #includ ...

  9. import datetime

    import datetimenow = datetime.datetime.now()print('当前时间:',now) 当前时间: 2019-11-21 11:11:58.093122

  10. jquery鼠标键盘悬停事件,形变动画和淡入淡出

    鼠标和键盘悬停 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...