k8s集群部署方案_docker redis集群

k8s集群部署方案_docker redis集群作者:颜博 青云科技数据库研发工程师 目前从事 PostgreSQL 产品开发工作,热衷于 PostgreSQL 数据库的学习和研究 上一期我们介绍了 PG 集群复制管理工具 repmgr,能够轻松的

高可用 | repmgr 构建 PostgreSQL 高可用集群部署文档【建议收藏】

作者:颜博 青云科技数据库研发工程师

目前从事 PostgreSQL 产品开发工作,热衷于 PostgreSQL 数据库的学习和研究

上一期我们介绍了 PG 集群复制管理工具 repmgr,能够轻松的搭建出 PostgreSQL 的高可用集群,在主节点宕机后,挑选备机提升为主节点,继续提供服务。

本文将详细介绍 repmgr 构建 PostgreSQL 高可用集群的部署过程。

准备工作

  1. 集群所有服务器安装 repmgr 工具
  2. 主服务器安装 PostgreSQL 数据库,初始化完成并正常启动数据库(Primary)

| 1 主库部分

1.1 修改 postgresql.conf 文件

$ vim postgresql.conf
max_wal_senders = 10
max_replication_slots = 10
wal_level = "hot_standby"
hot_standby = on
archive_mode = on   # repmgr 本身不需要 WAL 文件归档。
archive_command = "/bin/true"

在 PG9.6 之前的版本中,wal_level 允许设置为archivehot_standby。新版本中,仍然接受这些值,但是它们会被映射成replica

1.2 创建 repmgr 用户和库

为 repmgr 元数据信息创建 PostgreSQL 超级用户和数据库

# su - postgres 
$ /usr/lib/postgresql/11/bin/createuser -s repmgr
$ /usr/lib/postgresql/11/bin/createdb repmgr -O repmgr

alter user repmgr with password "test1234";

1.3 修改 pg_hba.conf 文件

repmgr 用户作为 repmgr 工具默认使用的数据库用户
$ vim pg_hba.conf
local   replication   repmgr                              trust
host    replication   repmgr      127.0.0.1/32            trust
host    replication   repmgr      0.0.0.0/0               trust

local   repmgr        repmgr                              trust
host    repmgr        repmgr      127.0.0.1/32            trust
host    repmgr        repmgr      0.0.0.0/0               trust

repmgr 免密登录设置

# 修改 pg_hba.conf 文件后 reload 数据库生效
$ su - postgres -c "/usr/lib/postgresql/11/bin/pg_ctl reload"

# su postgres
$ vim ~/.pgpass

# 添加以下内容到 ~/.pgpass 文件,用户、数据库和密码修改为自己的即可
*:*:repmgr:repmgr:test1234

# 修改 ~/.pgpass 文件权限
chmod 600 ~/.pgpass

1.4 创建 repmgr.conf 文件

在主服务器上创建一个 repmgr.conf 文件

node_id、node_name、conninfo 需要与从库不同

node_id=1                                     # 节点ID,高可用集群各节点标识
node_name="node1"                             # 节点名称,高可用集群各节点名称,对应集群中 select * from pg_stat_replication; 中查到的 application_name
conninfo="host=192.168.100.2 port=5432 user=repmgr dbname=repmgr connect_timeout=2"     # 集群中的所有服务器都必须能够使用此字符串连接到本地节点
data_directory="/data/pgsql/main"             # pg数据目录
replication_user="repmgr"                     # 流复制数据库用户,默认使用repmgr
repmgr_bindir="/usr/lib/postgresql/11/bin"    # repmgr软件目录
pg_bindir="/usr/lib/postgresql/11/bin"        # pg软件目录

# 日志管理
log_level=INFO
log_file="/data/pglog/repmgr/repmgrd.log"      # log 文件需要提前创建
log_status_interval=10                         # 此设置导致 repmgrd 以指定的时间间隔(以秒为单位,默认为 300)发出状态日志行,描述 repmgrd 的当前状态, 
              # 例如:  [2021-09-28 17:51:15] [INFO] monitoring primary node "node1" (ID: 1) in normal state

# pg、repmgr服务管理命令
service_start_command="/usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ start -o "-c config_file=/etc/postgresql/11/main/postgresql.conf" -l /data/pglog/start.log"
service_stop_command="/usr/lib/postgresql/11/bin/pg_ctl stop"
service_restart_command="/usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ restart -o "-c config_file=/etc/postgresql/11/main/postgresql.conf" -l /data/pglog/start.log"
service_reload_command="su - postgres -c "/usr/lib/postgresql/11/bin/pg_ctl reload" "

repmgrd_pid_file="/tmp/repmgrd.pid"              # repmgrd 运行时的 pid 文件
repmgrd_service_start_command="/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start"
repmgrd_service_stop_command="kill -9 `cat /tmp/repmgrd.pid`"

# failover设置
failover=automatic
promote_command="/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf standby promote  --log-to-file"        #当 repmgrd 确定当前节点将成为新的主节点时 ,将在故障转移情况下执行 promote_command 中定义的程序或脚本
follow_command="/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf  standby follow --log-to-file --upstream-node-id=%n"        # %n将被替换 repmgrd与新的主节点的ID, 如果没有提供, repmgr standby follow将尝试自行确定新的主repmgr standby follow节点,
                                                                                                           # 但如果在新主节点提升后原主节点重新上线,则存在导致节点继续跟随原主节点的风险 。
# 高可用参数设置
location="location1"                # 定义节点位置的任意字符串,在故障转移期间用于检查当前主节点的可见性
priority=100                        # 节点优先级,选主时可能使用到。(lsn > priority > node_id)
                                    # 0 代表该节点不会被提升为主节点
monitoring_history=yes              # 是否将监控数据写入“monitoring_history”表
reconnect_interval=10               # 故障转移之前,尝试重新连接的间隔(以秒为单位)
reconnect_attempts=6                # 故障转移之前,尝试重新连接的次数
connection_check_type=ping          # ping: repmg 使用PQPing() 方法测试连接
                                    # connection: 尝试与节点建立新的连接
                                    # query: 通过现有连接在节点上执行 SQL 语句
monitor_interval_secs=5             # 写入监控数据的间隔
use_replication_slots=true
# failover_validation_command=      # %n (node_id), %a (node_name)。
                                    # 自定义脚本,以验证 repmgrd 做出的故障转移决策
                                    # 此脚本必须返回退出代码 0 以指示节点应提升自身为主节点。

本次示例 repmgr.conf 文件放到以下位置:/etc/postgresql/11/main/repmgr.conf

【使用注意】

  • repmgr.conf不应存储在 PostgreSQL 数据目录中,因为在设置或重新初始化 PostgreSQL 服务器时它可能会被覆盖;
  • 如果将 repmgr 二进制文件放置在 PostgreSQL 安装目录以外的位置,指定 repmgr_bindir 以启用 repmgr 在其他节点上执行操作(例如:repmgr cluster crosscheck)。

1.5 注册主服务器

要使 repmgr 支持复制集群,必须使用 repmgr 注册主节点(repmgr primary register)。这将安装 repmgr扩展和元数据对象,并为主服务器添加元数据记录。

# su - postgres -c "/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf primary register"
INFO: connecting to primary database...
NOTICE: attempting to install extension "repmgr"
NOTICE: "repmgr" extension successfully installed
NOTICE: primary node record (ID: 1) registered
  • 查看集群信息
# su - postgres -c "/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf cluster show"
 ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string                                                                          
----+-------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------------------------------------
 1  | node1 | primary | * running |          | default  | 100      | 1        | host=127.0.0.1 port=5432 user=repmgr dbname=repmgr connect_timeout=2 password=test1234
  • 查看 repmgr 元数据表
repmgr=# SELECT * FROM repmgr.nodes;

 node_id | upstream_node_id | active | node_name |  type   | location | priority |                                          conninfo                                           | repluser | slot_name |             config_file             
---------+------------------+--------+-----------+---------+----------+----------+---------------------------------------------------------------------------------------------+----------+-----------+-------------------------------------
       1 |                  | t      | node1     | primary | default  |      100 | host=127.0.0.1 port=5432 user=repmgr dbname=repmgr connect_timeout=2 password=test1234 | repmgr   |           | /etc/postgresql/11/main/repmgr.conf
  • 配置文件发生改变,需要在每个节点执行
$ repmgr primary register --force -f /path/to/repmgr.conf
$ repmgr standby register --force -f /path/to/repmgr.conf
$ repmgr witness register --force -f /path/to/repmgr.conf -h primary_host

【使用注意】

repmgr 不能以 root 用户运行。

1.6 启动 repmgrd

1、修改 postgresql.conf 文件

加入 repmgr 共享库(在之前的共享库中在加入 repmgr 即可)。

shared_preload_libraries = "passwordcheck, repmgr"

2、重启数据库

/usr/lib/postgresql/11/bin/pg_ctl restart

3、启动 repmgrd 服务

# 创建日志文件,repmgrd 的日志文件需要手动创建
su postgres
mkdir -p /data/pglog/repmgr/
touch /data/pglog/repmgr/repmgrd.log

# 启动 repmgrd 服务
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start

1.7 repmgrd 日志轮换

为确保当前的 repmgrd 日志文件(repmgr.conf配置文件中用参数log_file指定的文件)不会无限增长,请将您的系统配置logrotate为定期轮换它。

vim /etc/logrotate.d/repmgr
    /data/pglog/repmgr/repmgrd.log {
        missingok
        compress
        rotate 52
        maxsize 100M
        weekly
        create 0600 postgres postgres
        postrotate
            /usr/bin/killall -HUP repmgrd
        endscript
    }

1.8 repmgrd 重载配置

# 1、kill 旧进程
kill -9 `cat /tmp/repmgrd.pid`

# 2、start
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start

| 2 备库部分

【使用注意】

在备用数据库上,不要创建 PostgreSQL 实例(即不要执行 initdb 或任何包提供的数据库创建脚本),但要确保目标数据目录(以及您希望 PostgreSQL 使用的任何其他目录)存在并归其所有 postgres 系统用户。权限必须设置为 0700 (drwx——)。

2.1 创建 repmgr.com 文件

在备用服务器上创建一个 repmgr.conf 文件,repmgr 配置文件与主库相同,注意修改其中的 node_id、node_name、conninfo 为本节点即可。

2.2 检查备库是否可克隆

备服务器节点注册前,不需要对 PostgreSQL 数据库进行初始化,可通过 repmgr 工具“一键式”部署。在对备用服务器进行克隆前,可以使用以下命令测试是否可以克隆。

使用--dry-run选项来检查备库是否可以克隆

$ su - postgres -c "/usr/lib/postgresql/11/bin/repmgr -h 192.168.100.2 -U repmgr -d repmgr -f /etc/postgresql/11/main/repmgr.conf standby clone --dry-run"

NOTICE: destination directory "/data/pgsql/main" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.100.2 user=repmgr dbname=repmgr
DETAIL: current installation size is 38 MB
INFO: "repmgr" extension is installed in database "repmgr"
INFO: replication slot usage not requested;  no replication slot will be set up for this standby
INFO: parameter "max_wal_senders" set to 10
NOTICE: checking for available walsenders on the source node (2 required)
INFO: sufficient walsenders available on the source node
DETAIL: 2 required, 10 available
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: required number of replication connections could be made to the source server
DETAIL: 2 replication connections required
NOTICE: standby will attach to upstream node 1
HINT: consider using the -c/--fast-checkpoint option
INFO: would execute:
  pg_basebackup -l "repmgr base backup"  -D /data/pgsql/main -h 192.168.100.2 -p 5432 -U repmgr -X stream 
INFO: all prerequisites for "standby clone" are met

报错以下内容证明:primary 节点的免密登录未配置好!

NOTICE: destination directory "/data/pgsql/main" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.100.2 user=repmgr dbname=repmgr
ERROR: connection to database failed
DETAIL: 
fe_sendauth: no password supplied

2.3 克隆备库

$ su - postgres -c "/usr/lib/postgresql/11/bin/repmgr -h 192.168.100.2 -U repmgr -d repmgr -f /etc/postgresql/11/main/repmgr.conf standby clone"

NOTICE: destination directory "/data/pgsql/main" provided
INFO: connecting to source node
DETAIL: connection string is: host=192.168.100.2 user=repmgr dbname=repmgr
DETAIL: current installation size is 38 MB
INFO: replication slot usage not requested;  no replication slot will be set up for this standby
NOTICE: checking for available walsenders on the source node (2 required)
NOTICE: checking replication connections can be made to the source server (2 required)
INFO: checking and correcting permissions on existing directory "/data/pgsql/main"
NOTICE: starting backup (using pg_basebackup)...
HINT: this may take some time; consider using the -c/--fast-checkpoint option
INFO: executing:
  pg_basebackup -l "repmgr base backup"  -D /data/pgsql/main -h 192.168.100.2 -p 5432 -U repmgr -X stream 
NOTICE: standby clone (using pg_basebackup) complete
NOTICE: you can now start your PostgreSQL server
HINT: for example: pg_ctl -D /data/pgsql/main start
HINT: after starting the server, you need to register this standby with "repmgr standby register"

这代表使用 PostgreSQL 的pg_basebackup工具从 192.168.100.2 克隆了PostgreSQL 数据目录文件。将自动创建包含从该主服务器开始流式传输的正确参数的 recovery.conf 文件。默认情况下,主数据目录中的任何配置文件都将复制到备用。通常这些将是 postgresql.conf、postgresql.auto.conf、pg_hba.conf 和 pg_ident.conf。这些可能需要在待机启动之前进行修改。

2.4 修改配置文件

修改 postgresql.conf、pg_hba.conf 配置文件,配置免密登录。

2.5 启动备库

# su postgres
$ /usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ start -o "-c config_file=/etc/postgresql/11/main/postgresql.conf" -l /data/pglog/start.log

2.6 注册从库为备用服务器

# su postgres
$ /usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf --upstream-node-id=1 standby register

2.7 启动 repmgrd

1、修改 postgresql.conf 文件,加入repmgr 共享库

shared_preload_libraries = "passwordcheck, repmgr"

2、重启数据库

/usr/lib/postgresql/11/bin/pg_ctl restart

3、启动 repmgrd

# 创建日志文件
su postgres
mkdir -p /data/pglog/repmgr/
touch /data/pglog/repmgr/repmgrd.log

# 启动 repmgrd 服务
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start

2.8 repmgrd 日志轮换

为确保当前的 repmgrd 日志文件(repmgr.conf配置文件中用参数log_file指定的文件)不会无限增长,请将您的系统配置logrotate为定期轮换它。

#  vim /etc/logrotate.d/repmgr
    /data/pglog/repmgr/repmgrd.log {
         missingok
        compress
        rotate 52
        maxsize 100M
        weekly
        create 0600 postgres postgres
        postrotate
            /usr/bin/killall -HUP repmgrd
        endscript
    }

2.9 repmgrd 重载配置

# 1、kill 旧进程
kill -9 `cat /tmp/repmgrd.pid`

# 2、start
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start

| 3 见证服务器(witness)

【使用注意】

  • 只有在使用 repmgrd 时,见证服务器才有用;
  • 发生故障转移的情况下,见证服务器提供证据表明是主服务器本身是不可用的,而不是例如不同的物理位置之间的网络分离(防止脑裂问题出现) ;
  • 请在与集群主服务器位于同一网段的服务器上设置一个普通 PostgreSQL 实例,并安装 repmgr、repmgrd,注册该实例为 witness(repmgr witness register)(见证服务器 Database system identifier 不能与集群主服务器相同)。

3.1 启动节点 postgres 服务

/usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ start

3.2 添加 repmgr.conf 配置

基本配置与主库相同,保持 node_id、node_name、conninfo 与主库不同即可。

3.3 启动 repmgrd

1、修改 postgresql.conf 文件,加入repmgr 共享库

shared_preload_libraries = "passwordcheck, repmgr"

2、重启数据库

/usr/lib/postgresql/11/bin/pg_ctl -D /data/pgsql/main/ restart -o "-c config_file=/etc/postgresql/11/main/postgresql.conf" -l /data/pglog/start.log

3、启动 repmgrd

# 创建日志文件
su postgres
mkdir -p /data/pglog/repmgr/
touch /data/pglog/repmgr/repmgrd.log

# 启动 repmgrd 服务
/usr/lib/postgresql/11/bin/repmgrd -f /etc/postgresql/11/main/repmgr.conf start

4、为确保当前的 repmgrd 日志文件(repmgr.conf配置文件中用参数log_file指定的文件)不会无限增长,请将您的系统配置logrotate为定期轮换它

#  vim /etc/logrotate.d/repmgr
    /data/pglog/repmgr/repmgrd.log {
        missingok
         compress
        rotate 52
        maxsize 100M
        weekly
        create 0600 postgres postgres
        postrotate
            /usr/bin/killall -HUP repmgrd
        endscript
    }

3.4 注册 witness

/usr/lib/postgresql/11/bin/repmgr -f /etc/postgresql/11/main/repmgr.conf witness register -h 192.168.100.2

| 总结

至此,基于 repmgr 搭建出了一个 PostgreSQL 高可用集群(repmgr 本身不提供虚拟 ip 服务,如果需要虚拟 ip 服务,请使用 keepalived 或其它工具)。它具有集群状态监控、故障检测、故障转移等功能。更多 repmgr 高级功能及原理,例如处理网络分裂、主要可见性共识、级联复制、监控连接数、事件通知等,请参照官方文档进一步学习。

参考

[1]. repmgr.conf 配置: https://raw.githubusercontent.com/EnterpriseDB/repmgr/master/repmgr.conf.sample

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/5652.html

(0)
上一篇 2023-04-30
下一篇 2023-04-30

相关推荐

  • 索引很难么?带你从头到尾捋一遍MySQL索引结构,不信你学不会!「建议收藏」

    索引很难么?带你从头到尾捋一遍MySQL索引结构,不信你学不会!「建议收藏」前言 Hello我又来了,快年底了,作为一个有抱负的码农,我想给自己攒一个年终总结。自上上篇写了手动搭建Redis集群和MySQL主从同步(非Docker)和上篇写了动手实现MySQL读写分离and故

    2022-12-26
    97
  • mysql安装教程-window操作系统

    mysql安装教程-window操作系统1、下载安装包(官网下载) 直达链接:https://dev.mysql.com/downloads/mysql/ 下载后放到指定目录下解压即可(给电脑新手忠告:注意不要放在C盘,养成好习惯,放C盘多

    2023-06-11
    98
  • Python起始参数:完整指南

    Python起始参数:完整指南Python脚本通常可以在运行时动态地获取参数。命令行参数是由程序在运行时传递的字段,以决定程序的运行方式。Python的sys模块提供了一个名为sys.argv的列表,其中包含了传递到Python脚本的所有参数。其中,sys.argv[0]表示脚本名称,sys.argv[1]表示第一个参数,以此类推。下面是一个示例:

    2024-03-27
    24
  • 使用Python绘制散点图

    使用Python绘制散点图散点图是一种以点的位置为坐标,以变量的值为数据的图表形式,用于观察两个变量之间的关系是否存在某种模式、趋势或异常值。在数据科学领域,散点图经常用于探索性数据分析和建模。

    2024-05-01
    15
  • 利用Python实现字符串操作

    利用Python实现字符串操作Python是一门简单易学、功能强大的编程语言,被广泛应用于各种领域,特别是在数据分析、人工智能等领域。字符串是编程中经常用到的一种数据类型,而Python的字符串操作非常简单高效。本文将从选取、拼接、格式化、查找、替换等多个方面,介绍在Python中如何进行字符串操作。

    2024-03-14
    30
  • Python实现内容优化技巧

    Python实现内容优化技巧关键词优化是SEO中最基础最重要的环节,也是内容优化的基础。在Python中,可以使用爬虫来获取到一些和产品相关的关键词。关键词的选择要求相关性强、搜索量大、竞争度低,可以使用a href=”https://ads.google.com/home/tools/keyword-planner/”Google Ads关键词规划师/a来进行筛选。

    2023-12-18
    53
  • 阿里时空数据库_阿里巴巴数据库是什么

    阿里时空数据库_阿里巴巴数据库是什么https://www.aliyun.com/product/hitsdb_spatialpre 产品概述 时空数据库 时空数据库能够存储、管理包括时间序列以及空间地理位置相关的数据。传感器网络、移…

    2023-01-30
    102
  • MYSQLl给用户授予数据库表权限

    MYSQLl给用户授予数据库表权限给targetUserName用户授予databaseName单个数据库权限 grant all privileges on databaseName.* to targetUserName@&quo

    2023-01-31
    165

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注