0x10 - PostgreSQL 安装之 CentOS7 + Patroni
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的更多相关文章
- CentOS7 PostgreSQL安装
CentOS7 PostgreSQL安装 CentOS7 PostgreSQL安装 Install 安装 使用yum安装 yum install http://yum.postgresql.org/9 ...
- 19.CentOS7下PostgreSQL安装过程
CentOS7下PostgreSQL安装过程 装包 sudo yum install postgresql-server postgresql-contrib 说明: 这种方式直接明了,其他方法也可以 ...
- CentOS7 PostgreSQL 安装
PostgreSQL安装 安装使用yum安装 (找源 http://yum.postgresql.org/) yum install https://download.postgresql.org/p ...
- centos7 postgresql安装配置
2021-07-15 1.添加用户 # 添加用户 postgres useradd postgres # 给用户 postgres 设置密码 passwd postgres 2.切换到该用户,下载 p ...
- postgresql安装及配置
目录 1. 安装 2. PostgrepSQL的简单配置 2.1 修改监听的ip和端口 2.2 修改数据库log相关的参数 2.3 内存参数 3. 数据库的基础操作 3.1 连接数据库控制台 3.2 ...
- Linux下apache+phppgadmin+postgresql安装配置
Linux下apache+phppgadmin+postgresql安装配置 操作系统:CentOS 安装包:httpd(首选yum), php(包括php以及php-pgsql,php-mbstri ...
- Ubuntu PostgreSQL安装和配置
一.安装 1.安装 使用如下命令,会自动安装最新版,这里为9.5 sudo apt-get install postgresql 安装完成后,默认会: (1)创建名为"postgres&qu ...
- PostgreSQL安装详细步骤(windows)
原文地址:http://blog.chinaunix.net/uid-354915-id-3498734.html PostgreSQL安装:一.windows下安装过程安装介质:postgresql ...
- 使用Xshell5连接虚拟机VMware中安装的CentOS7系统
使用Xshell5连接VMware中安装的CentOS7系统 准备材料 Xshell 下载地址 VMware Workstation 12 Pro 下载地址 CentOS 7 64位系统 下载地址 安 ...
随机推荐
- DataStructuresAndAlogorithm--红黑树
简介 为了理解红黑树(red-black tree)是什么,首先需要知道二叉树. 定义1:二叉树是结点的有限集合,该集合或者为空集,或者是由一个根和两棵互不相交的,称为该根的左子树和右子树的二叉树组成 ...
- Arduino --structure
The elements of Arduino (C++) code. Sketch loop() setup() Control Structure break continue do...whil ...
- bzoj 1009GT考试
做了上一道题,全程懵逼,再做这一道,,,23333继续 貌似那些东西都是差不多的.(参见cf621E) 这个为什么b[][]+=1很困惑,为什么不是从b[-1][??]转移的呢?想了一下,因为每一次都 ...
- cf 478D.Santa Claus and a Palindrome
原来set,priority_queue也可以映射..涨姿势2333 比较麻烦的应该就是判断自身回文的串是选2个还是选一个吧. #include<bits/stdc++.h> #defin ...
- DevOps专题|玩转Kubernetes网络
Kubernetes无疑是当前最火热的容器编排工具,网络是kubernetes中非常重要的一环, 本文主要介绍一些相应的网络原理及术语,以及kubernetes中的网络方案和对比. Kubernete ...
- flink和spark Streaming中的Back Pressure
Spark Streaming的back pressure 在讲flink的back pressure之前,我们先讲讲Spark Streaming的back pressure.Spark Strea ...
- git使用散记
1.从远程clone一个项目 git clone ‘项目地址’ //clone项目地 git checkout -b dev origin/dev //远程已有dev分支,新建本地dev分支与远程相对 ...
- 关于win10 使用eclipse如何配置环境变量
关于环境变量的配置,在百度上有很多教程,但对于我来说完成这步操作确实不简单,所以决定在这里分享一下配置方法. 1.安装好jdk/jre. 官网都有安装文件,仔细一些,就能安装成功,可以自定义安装路径 ...
- 【每日Scrum】第五天冲刺
一.计划会议内容 仍然在解决数据库问题 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图
- 四、python杂项
一.pycharm单行和多行注释快捷键 多行注释就一个组合键:选中+Ctrl+/