pg_auto_failover 之二 postgresql 1 master 1 slave

2023-11-09 02:59

本文主要是介绍pg_auto_failover 之二 postgresql 1 master 1 slave,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

os: ubuntu 16.04
db: postgresql 10.9

pg_auto_failover 是 citus 开源的一款 postgresql 高可用软件,目前只支持 postgresql 10 及以上.

pg_auto_failover is an extension and service for PostgreSQL that monitors and manages automated failover for a Postgres cluster. It is optimized for simplicity and correctness and supports Postgres 10 and newer.

ip 规划

192.168.56.101 node1
192.168.56.102 node2
192.168.56.103 node3

node1 为 monitor 节点
node2 node3 为 postgresql 主从

在这里插入图片描述
上一篇blog <<pg_auto_failover 之一 apt install / make make install>> 介绍了安装两种方式安装pg_auto_failover

这一篇介绍下初步的使用.

Using pg_auto_failover

1. Install and start a pg_auto_failover monitor on your monitor machine:
node1 上运行,创建 monitor

$ pg_autoctl create monitor --pgdata /data/pg10/main --pgport 5432 --nodename node114:48:40 INFO  Initialising a PostgreSQL cluster at "/data/pg10/main"
14:48:40 INFO   /usr/bin/pg_ctl --pgdata /data/pg10/main --options "-p 5432" --options "-h *" --wait start
14:48:41 INFO  Granting connection privileges on 192.168.56.0/24
14:48:41 INFO  Your pg_auto_failover monitor instance is now ready on port 5432.
14:48:41 INFO  pg_auto_failover monitor is ready at postgres://autoctl_node@node1:5432/pg_auto_failover
14:48:41 INFO  Monitor has been succesfully initialized.

发现对pg_hba.conf追加了一条纪录

host "pg_auto_failover" "autoctl_node" 192.168.56.0/24 trust # Auto-generated by pg_auto_failover

发现 pg_auto_failover 相关的进程

$ ps -ef|grep -i postgres |grep -v greppostgres  7616     1  0 14:48 ?        00:00:00 /usr/lib/postgresql/10/bin/postgres -D /data/pg10/main -p 5432 -h *
postgres  7620  7616  0 14:48 ?        00:00:00 postgres: checkpointer process   
postgres  7621  7616  0 14:48 ?        00:00:00 postgres: writer process   
postgres  7622  7616  0 14:48 ?        00:00:00 postgres: wal writer process   
postgres  7623  7616  0 14:48 ?        00:00:00 postgres: autovacuum launcher process   
postgres  7625  7616  0 14:48 ?        00:00:00 postgres: stats collector process   
postgres  7627  7616  0 14:48 ?        00:00:00 postgres: bgworker: pg_auto_failover monitor   
postgres  7628  7616  0 14:48 ?        00:00:00 postgres: bgworker: logical replication launcher   
postgres  7629  7616  0 14:48 ?        00:00:00 postgres: bgworker: pg_auto_failover monitor worker   
postgres  7635  7616  0 14:48 ?        00:00:00 postgres: bgworker: pg_auto_failover monitor worker $ ls -l /data/pg10/main
total 128
drwx------ 6 postgres postgres  4096 Jul  3 14:48 base
drwx------ 2 postgres postgres  4096 Jul  3 14:48 global
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_commit_ts
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_dynshmem
-rw------- 1 postgres postgres  4611 Jul  3 14:48 pg_hba.conf
-rw------- 1 postgres postgres  1636 Jul  3 14:48 pg_ident.conf
drwx------ 4 postgres postgres  4096 Jul  3 14:48 pg_logical
drwx------ 4 postgres postgres  4096 Jul  3 14:48 pg_multixact
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_notify
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_replslot
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_serial
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_snapshots
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_stat
drwx------ 2 postgres postgres  4096 Jul  3 14:49 pg_stat_tmp
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_subtrans
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_tblspc
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_twophase
-rw------- 1 postgres postgres     3 Jul  3 14:48 PG_VERSION
drwx------ 3 postgres postgres  4096 Jul  3 14:48 pg_wal
drwx------ 2 postgres postgres  4096 Jul  3 14:48 pg_xact
-rw------- 1 postgres postgres    88 Jul  3 14:48 postgresql.auto.conf
-rw-rw-r-- 1 postgres postgres   110 Jul  3 14:48 postgresql-auto-failover.conf
-rw------- 1 postgres postgres 22962 Jul  3 14:48 postgresql.conf
-rw------- 1 postgres postgres    76 Jul  3 14:48 postmaster.opts
-rw------- 1 postgres postgres    88 Jul  3 14:48 postmaster.pid
-rw-rw-r-- 1 postgres postgres   511 Jul  3 14:48 startup.log
$ psql
psql (10.9 (Ubuntu 10.9-1.pgdg16.04+1))
Type "help" for help.postgres=# \duList of rolesRole name   |                         Attributes                         | Member of 
--------------+------------------------------------------------------------+-----------autoctl      |                                                            | {}autoctl_node |                                                            | {}postgres     | Superuser, Create role, Create DB, Replication, Bypass RLS | {}postgres=# \lList of databasesName       |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
------------------+----------+----------+-------------+-------------+-----------------------pg_auto_failover | autoctl  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | postgres         | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | template0        | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgrestemplate1        | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgres
(4 rows)

启动 monitor

$ pg_autoctl run --pgdata /data/pg10/main
15:12:53 INFO  Managing PostgreSQL installation at "/data/pg10/main"
15:12:53 INFO  Postgres is not running, starting postgres
15:12:53 INFO   /usr/bin/pg_ctl --pgdata /data/pg10/main --options "-p 5432" --options "-h *" --wait start
15:12:53 INFO  PostgreSQL is running in "/data/pg10/main" on port 5432
15:12:53 INFO  pg_auto_failover monitor is ready at postgres://autoctl_node@node1:5432/pg_auto_failover
15:12:53 INFO  Contacting the monitor to LISTEN to its events.

2. Get the Postgres URI (connection string) for the monitor node:
node1 上运行,获取 monitor 的 uri

$ pg_autoctl show uri --pgdata /data/pg10/main
postgres://autoctl_node@node1:5432/pg_auto_failover

The following two steps are going to use the option --monitor which expects that connection string. So copy/paste your actual Postgres URI for the monitor in the next steps.

3. Install and start a primary PostgreSQL instance:
node2 上运行,初始化一个 master 节点,并向 monitor 注册

$ pg_autoctl create postgres --pgdata /data/pg10/main --pgport 5432 --nodename node2 \
--monitor postgres://autoctl_node@node1:5432/pg_auto_failover15:22:44 INFO  Registered node node2:5432 with id 1 in formation "default", group 0.
15:22:44 INFO  Writing keeper init state file at "/var/lib/postgresql/.local/share/pg_autoctl/data/pg10/main/pg_autoctl.init"
15:22:44 INFO  Successfully registered as "single" to the monitor.
15:22:46 INFO  Initialising a PostgreSQL cluster at "/data/pg10/main"
15:22:46 INFO  Postgres is not running, starting postgres
15:22:46 INFO   /usr/bin/pg_ctl --pgdata /data/pg10/main --options "-p 5432" --options "-h *" --wait start
15:22:46 INFO  CREATE DATABASE postgres;
15:22:46 INFO  The database "postgres" already exists, skipping.
15:22:46 INFO  FSM transition from "init" to "single": Start as a single node
15:22:46 INFO  Initialising postgres as a primary
15:22:46 INFO  Transition complete: current state is now "single"
15:22:46 INFO  Keeper has been succesfully initialized.
$ ps -ef|grep -i postgres  |grep -v greppostgres 31958     1  0 15:22 pts/1    00:00:00 /usr/lib/postgresql/10/bin/postgres -D /data/pg10/main -p 5432 -h *
postgres 31959 31958  0 15:22 ?        00:00:00 postgres: logger process   
postgres 31961 31958  0 15:22 ?        00:00:00 postgres: checkpointer process   
postgres 31962 31958  0 15:22 ?        00:00:00 postgres: writer process   
postgres 31963 31958  0 15:22 ?        00:00:00 postgres: wal writer process   
postgres 31964 31958  0 15:22 ?        00:00:00 postgres: autovacuum launcher process   
postgres 31965 31958  0 15:22 ?        00:00:00 postgres: stats collector process   
postgres 31966 31958  0 15:22 ?        00:00:00 postgres: bgworker: logical replication launcher

Now that the installation is ready we can run the keeper service, which connects to the pg_auto_failover monitor every 5 seconds and implement the state transitions when needed:
运行 keeper

$ pg_autoctl run --pgdata /data/pg10/main15:25:46 INFO  Managing PostgreSQL installation at "/data/pg10/main"
15:25:46 INFO  pg_autoctl service is starting
15:25:46 INFO  Calling node_active for node default/1/0 with current state: single, PostgreSQL is running, sync_state is "", WAL delta is -1.
15:25:51 INFO  Calling node_active for node default/1/0 with current state: single, PostgreSQL is running, sync_state is "", WAL delta is -1.
15:25:56 INFO  Calling node_active for node default/1/0 with current state: single, PostgreSQL is running, sync_state is "", WAL delta is -1.
15:26:01 INFO  Calling node_active for node default/1/0 with current state: single, PostgreSQL is running, sync_state is "", WAL delta is -1.$ pg_autoctl show state --pgdata /data/pg10/main/Name |   Port | Group |  Node |     Current State |    Assigned State
------+--------+-------+-------+-------------------+------------------
node2 |   5432 |     0 |     5 |            single |            single

4. Install and start a secondary PostgreSQL instance:
node3 节点执行,作为 node2 节点 postgresql 的 slave

$ cd /data/pg10;
mv main main.bak;$ pg_autoctl create postgres --pgdata /data/pg10/main --nodename node3 \
--monitor postgres://autoctl_node@node1:5432/pg_auto_failover16:38:06 INFO  Continuing from a previous `pg_autoctl create` failed attempt
16:38:06 INFO  PostgreSQL state at registration time was: PGDATA does not exists
16:38:06 INFO  FSM transition from "wait_standby" to "catchingup": The primary is now ready to accept a standby
16:38:06 INFO  The primary node returned by the monitor is node2:5432
16:38:06 INFO  Initialising PostgreSQL as a hot standby
16:38:06 INFO  Running /usr/bin/pg_basebackup -w -h node2 -p 5432 --pgdata /data/pg10/backup -U pgautofailover_replicator --write-recovery-conf --max-rate 100M --wal-method=stream --slot pgautofailover_standby ...
16:38:07 INFO  pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/2000028 on timeline 1
pg_basebackup: starting background WAL receiver
24050/24050 kB (100%), 1/1 tablespace                                         
pg_basebackup: write-ahead log end point: 0/20000F8
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed16:38:07 INFO  Postgres is not running, starting postgres
16:38:07 INFO   /usr/bin/pg_ctl --pgdata /data/pg10/main --options "-p 5432" --options "-h *" --wait start
16:38:08 INFO  PostgreSQL started on port 5432
16:38:08 INFO  Transition complete: current state is now "catchingup"
16:38:08 INFO  Keeper has been succesfully initialized.

Once the pg_autoctl create command is done, again, it’s important to run the keeper’s service:
运行 keeper

$ pg_autoctl show state --pgdata /data/pg10/main/Name |   Port | Group |  Node |     Current State |    Assigned State
------+--------+-------+-------+-------------------+------------------
node2 |   5432 |     0 |     5 |      wait_primary |      wait_primary
node3 |   5432 |     0 |     6 |        catchingup |        catchingup$ pg_autoctl run --pgdata /data/pg10/main16:43:40 INFO  Managing PostgreSQL installation at "/data/pg10/main"
16:43:40 INFO  pg_autoctl service is starting
16:43:40 INFO  Calling node_active for node default/6/0 with current state: catchingup, PostgreSQL is running, sync_state is "", WAL delta is -1.
16:43:40 INFO  FSM transition from "catchingup" to "secondary": Convinced the monitor that I'm up and running, and eligible for promotion again
16:43:40 INFO  Transition complete: current state is now "secondary"
16:43:40 INFO  Calling node_active for node default/6/0 with current state: secondary, PostgreSQL is running, sync_state is "", WAL delta is 0.
16:43:45 INFO  Calling node_active for node default/6/0 with current state: secondary, PostgreSQL is running, sync_state is "", WAL delta is 0.
16:43:50 INFO  Calling node_active for node default/6/0 with current state: secondary, PostgreSQL is running, sync_state is "", WAL delta is 0.

新开一个窗口查看

$ pg_autoctl show state --pgdata /data/pg10/main/Name |   Port | Group |  Node |     Current State |    Assigned State
------+--------+-------+-------+-------------------+------------------
node2 |   5432 |     0 |     5 |           primary |           primary
node3 |   5432 |     0 |     6 |         secondary |         secondary

So when pg_autoctl create postgres is used on an existing ${PGDATA} where Postgres is running as a secondary node, then you have the following error message:

ERROR pg_autoctl doesn't know how to register an already existing standby server at the moment

Installing pg_auto_failover on-top of an existing Postgres setup

已经有 postgresql 运行,需要使用 pg_auto_failover 做 ha 的

$ pg_autoctl create postgres --pgdata /data/pg10/main/ --nodename node2

Application and Connection Strings

To retrieve the connection string to use at the application level, use the following command:

$ pg_autoctl show uri --formation default --pgdata /data/pg10/main/
postgres://node2:5432,node3:5432/postgres?target_session_attrs=read-write

相关的一些文件

ls -l "/var/lib/postgresql/.config/pg_autoctl/data/pg10/main/pg_autoctl.cfg" \
"/var/lib/postgresql/.local/share/pg_autoctl/data/pg10/main/pg_autoctl.state" \
"/var/lib/postgresql/.local/share/pg_autoctl/data/pg10/main/pg_autoctl.init" \
"/tmp/pg_autoctl/data/pg10/main/pg_autoctl.pid"

参考:
https://github.com/citusdata/pg_auto_failover
https://pg-auto-failover.readthedocs.io/en/latest/quickstart.html

https://www.citusdata.com/blog/2019/05/30/introducing-pg-auto-failover/
https://cloudblogs.microsoft.com/opensource/2019/05/06/introducing-pg_auto_failover-postgresql-open-source-extension-automated-failover-high-availability/

这篇关于pg_auto_failover 之二 postgresql 1 master 1 slave的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/373717

相关文章

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

postgresql数据库基本操作及命令详解

《postgresql数据库基本操作及命令详解》本文介绍了PostgreSQL数据库的基础操作,包括连接、创建、查看数据库,表的增删改查、索引管理、备份恢复及退出命令,适用于数据库管理和开发实践,感兴... 目录1. 连接 PostgreSQL 数据库2. 创建数据库3. 查看当前数据库4. 查看所有数据库

PostgreSQL数据库密码被遗忘时的操作步骤

《PostgreSQL数据库密码被遗忘时的操作步骤》密码遗忘是常见的用户问题,因此提供一种安全的遗忘密码找回机制是十分必要的,:本文主要介绍PostgreSQL数据库密码被遗忘时的操作步骤的相关资... 目录前言一、背景知识二、Windows环境下的解决步骤1. 找到PostgreSQL安装目录2. 修改p

PostgreSQL 默认隔离级别的设置

《PostgreSQL默认隔离级别的设置》PostgreSQL的默认事务隔离级别是读已提交,这是其事务处理系统的基础行为模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一 默认隔离级别概述1.1 默认设置1.2 各版本一致性二 读已提交的特性2.1 行为特征2.2

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

一文详解PostgreSQL复制参数

《一文详解PostgreSQL复制参数》PostgreSQL作为一款功能强大的开源关系型数据库,其复制功能对于构建高可用性系统至关重要,本文给大家详细介绍了PostgreSQL的复制参数,需要的朋友可... 目录一、复制参数基础概念二、核心复制参数深度解析1. max_wal_seChina编程nders:WAL

PostgreSQL 序列(Sequence) 与 Oracle 序列对比差异分析

《PostgreSQL序列(Sequence)与Oracle序列对比差异分析》PostgreSQL和Oracle都提供了序列(Sequence)功能,但在实现细节和使用方式上存在一些重要差异,... 目录PostgreSQL 序列(Sequence) 与 oracle 序列对比一 基本语法对比1.1 创建序

全解析CSS Grid 的 auto-fill 和 auto-fit 内容自适应

《全解析CSSGrid的auto-fill和auto-fit内容自适应》:本文主要介绍了全解析CSSGrid的auto-fill和auto-fit内容自适应的相关资料,详细内容请阅读本文,希望能对你有所帮助... css  Grid 的 auto-fill 和 auto-fit/* 父元素 */.gri

最详细安装 PostgreSQL方法及常见问题解决

《最详细安装PostgreSQL方法及常见问题解决》:本文主要介绍最详细安装PostgreSQL方法及常见问题解决,介绍了在Windows系统上安装PostgreSQL及Linux系统上安装Po... 目录一、在 Windows 系统上安装 PostgreSQL1. 下载 PostgreSQL 安装包2.