openGauss1.0.1支持docker主备搭建

作者:奔跑的数据库2024.04.28 17:30浏览量:40

简介:openGauss开源数据库技术

openGauss1.0.1 支持 docker 主备搭建

目前已经支持 x86-64 和 ARM64 两种架构。

x86-64 架构的 openGauss 运行在 CentOS 7.6 操作系统中。

ARM64 架构的 openGauss 运行在 openEuler 20.03 LTS 操作系统中。

快速搭建环境

pull 镜像文件

docker pull enmotech/opengauss:1.0.1

创建自定义网络,创建容器便于使用固定 IP

docker network create --subnet=***.***.***.***/16 myNetwork

创建主库容器并运行

docker run --name op_master \
--network myNetwork --ip ***.***.***.*** --privileged=true \
--hostname op_master --detach \
--env GS_PORT=6432 \
--env OG_SUBNET=***.***.***.***/16 \
--env GS_PASSWORD=Enmotech@2020 \
--env NODE_NAME=op_master \
--env REPL_CONN_INFO="replconninfo1 = 'localhost=***.***.***.*** localport=6439 localservice=6432 remotehost=***.***.***.*** remoteport=6439 remoteservice=6432 '\n" \
--cpuset-cpus="1,3" \
opengauss:1.0.1 -M primary

创建备库容器并运行

docker run --name op_slave_one \
--network myNetwork --ip ***.***.***.*** --privileged=true \
--hostname op_slave_one --detach \
--env GS_PORT=6432 \
--env OG_SUBNET=***.***.***.***/16 \
--env GS_PASSWORD=Enmotech@2020 \
--env NODE_NAME=op_slave_one \
--env REPL_CONN_INFO="replconninfo1 = 'localhost=***.***.***.*** localport=6439 localservice=6432 remotehost=***.***.***.*** remoteport=6439 remoteservice=6432 '\n" \
--cpuset-cpus="2,4" \
opengauss:1.0.1 -M standby

说明:可以参考官方镜像脚本来创建 localport 与 localservice 端口间隔要大于 2,对外映射端口为 localservice。

查询主备角色状态

查询命令

gs_ctl query -D /var/lib/opengauss/data/

主库 op_master 查询

docker exec -it op_master bash

[omm@op_master ~]$ gs_ctl query -D /var/lib/opengauss/data/
[2020-10-20 13:50:39.163][666][][gs_ctl]: gs_ctl query ,datadir is -D "/var/lib/opengauss/data"
 HA state:
    local_role                     : Primary
    static_connections             : 1
    db_state                       : Normal
    detail_information             : Normal

 Senders info:
    sender_pid                     : 637
    local_role                     : Primary
    peer_role                      : Standby
    peer_state                     : Normal
    state                          : Streaming
    sender_sent_location           : 0/5000238
    sender_write_location          : 0/5000238
    sender_flush_location          : 0/5000238
    sender_replay_location         : 0/5000238
    receiver_received_location     : 0/5000238
    receiver_write_location        : 0/5000238
    receiver_flush_location        : 0/5000238
    receiver_replay_location       : 0/5000238
    sync_percent                   : 100%
    sync_state                     : Sync
    sync_priority                  : 1
    sync_most_available            : On
    channel                        : ***.***.***.***:6439-->***.***.***.***:55440

 Receiver info:
No information

备库 op_slave_one 查询

docker exec -it op_slave_one bash

[omm@op_slave_one ~]$ gs_ctl query -D /var/lib/opengauss/data/
[2020-10-20 13:51:24.640][460][][gs_ctl]: gs_ctl query ,datadir is -D "/var/lib/opengauss/data"
 HA state:
    local_role                     : Standby
    static_connections             : 1
    db_state                       : Normal
    detail_information             : Normal

 Senders info:
No information
 Receiver info:
    receiver_pid                   : 401
    local_role                     : Standby
    peer_role                      : Primary
    peer_state                     : Normal
    state                          : Normal
    sender_sent_location           : 0/5000238
    sender_write_location          : 0/5000238
    sender_flush_location          : 0/5000238
    sender_replay_location         : 0/5000238
    receiver_received_location     : 0/5000238
    receiver_write_location        : 0/5000238
    receiver_flush_location        : 0/5000238
    receiver_replay_location       : 0/5000238
    sync_percent                   : 100%
    channel                        : ***.***.***.***:55440<--***.***.***.***:6439

说明:从上面主库 Senders 信息和备库 Receiver 可以看到主备状态正常。

读写及切换测试

主库 op_master 写测试

[omm@op_master ~]$ gsql -p6432
gsql ((openGauss 1.0.1 build e9da9fb9) compiled at 2020-10-01 20:21:42 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# create table t(id json);
CREATE TABLE
omm=# insert into t values('{"name":"Mr.D"}');
INSERT 0 1

备库 op_slave_one 读测试

[omm@op_slave_one ~]$ gsql -p6432
gsql ((openGauss 1.0.1 build e9da9fb9) compiled at 2020-10-01 20:21:42 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# select * from t;
       id
-----------------
 {"name":"Mr.D"}
(1 row)

omm=# delete from t;
ERROR:  cannot execute DELETE in a read-only transaction

切换测试:将 op_slave_one 切换为主库,op_master 切换为备库

op_slave_one 执行 switchover

[omm@op_slave_one ~]$ gs_ctl switchover -D /var/lib/opengauss/data/
[2020-10-20 13:57:02.877][504][][gs_ctl]: gs_ctl switchover ,datadir is -D "/var/lib/opengauss/data"
[2020-10-20 13:57:02.877][504][][gs_ctl]: switchover term (1)
[2020-10-20 13:57:02.888][504][][gs_ctl]: waiting for server to switchover.........
[2020-10-20 13:57:08.920][504][][gs_ctl]: done
[2020-10-20 13:57:08.920][504][][gs_ctl]: switchover completed (/var/lib/opengauss/data)

op_slave_one 查询状态

[omm@op_slave_one ~]$ gs_ctl query -D /var/lib/opengauss/data/
[2020-10-20 13:58:13.340][555][][gs_ctl]: gs_ctl query ,datadir is -D "/var/lib/opengauss/data"
 HA state:
    local_role                     : Primary
    static_connections             : 1
    db_state                       : Normal
    detail_information             : Normal

 Senders info:
    sender_pid                     : 523
    local_role                     : Primary
    peer_role                      : Standby
    peer_state                     : Normal
    state                          : Streaming
    sender_sent_location           : 0/5004A10
    sender_write_location          : 0/5004A10
    sender_flush_location          : 0/5004A10
    sender_replay_location         : 0/5004A10
    receiver_received_location     : 0/5004A10
    receiver_write_location        : 0/5004A10
    receiver_flush_location        : 0/5004A10
    receiver_replay_location       : 0/5004A10
    sync_percent                   : 100%
    sync_state                     : Sync
    sync_priority                  : 1
    sync_most_available            : On
    channel                        : ***.***.***.***:6439-->***.***.***.***:39314

 Receiver info:
No information

op_master 查询状态

[omm@op_master ~]$ gs_ctl query -D /var/lib/opengauss/data/
[2020-10-20 13:58:42.827][743][][gs_ctl]: gs_ctl query ,datadir is -D "/var/lib/opengauss/data"
 HA state:
    local_role                     : Standby
    static_connections             : 1
    db_state                       : Normal
    detail_information             : Normal

 Senders info:
No information
 Receiver info:
    receiver_pid                   : 739
    local_role                     : Standby
    peer_role                      : Primary
    peer_state                     : Normal
    state                          : Normal
    sender_sent_location           : 0/5004A10
    sender_write_location          : 0/5004A10
    sender_flush_location          : 0/5004A10
    sender_replay_location         : 0/5004A10
    receiver_received_location     : 0/5004A10
    receiver_write_location        : 0/5004A10
    receiver_flush_location        : 0/5004A10
    receiver_replay_location       : 0/5004A10
    sync_percent                   : 100%
    channel                        : ***.***.***.***:39314<--***.***.***.***:6439

可以看到 op_master 变为备库,op_slave_one 变为主库,切换成功。

数据读写验证

主库 op_slave_one 做写入验证。

[omm@op_slave_one ~]$ gsql -p6432
gsql ((openGauss 1.0.1 build e9da9fb9) compiled at 2020-10-01 20:21:42 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# select * from t;
       id
-----------------
 {"name":"Mr.D"}
(1 row)

omm=# insert into t values('{"name":"insert from op_slave_one "}');
INSERT 0 1

备库 op_master 做读取验证。

[omm@op_master ~]$ gsql -p6432
gsql ((openGauss 1.0.1 build e9da9fb9) compiled at 2020-10-01 20:21:42 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# select * from t;
                  id
--------------------------------------
 {"name":"Mr.D"}
 {"name":"insert from op_slave_one "}
(2 rows)

omm=# delete from t;
ERROR:  cannot execute DELETE in a read-only transaction