openGauss是一款开源关系型数据库管理系统,采用木兰宽松许可证v2发行。openGauss内核早期源自PostgreSQL,深度融合华为在数据库领域多年的经验,结合企业级场景需求,持续构建竞争力特性。同时openGauss也是一个开源、免费的数据库平台,鼓励社区贡献、合作。
今天无意看到恩墨开源团队发布了openGauss新的容器镜像,此版本镜像还支持创建主从复制的openGauss高可用集群。
本篇文件记录快速部署的主从复制的openGauss高可用集群的过程以及中间遇到的问题,希望对学习openGauss的同学有所帮助。
参考dockerhub网址:https://hub.docker.com/r/enmotech/opengauss
基础环境准备
[root@hhj ~]# cat /etc/system-release
Oracle Linux Server release 7.9
[root@hhj ~]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
scan: Docker Scan (Docker Inc., v0.8.0)
Server:
Containers: 48
Running: 0
Paused: 0
Stopped: 48
Images: 18
Server Version: 20.10.7
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: false
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc version: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 5.4.17-2102.201.3.el7uek.x86_64
Operating System: Oracle Linux Server 7.9
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 4.268GiB
Name: hhj
ID: HHHZ:DPVB:WA2L:J5BI:D4Q6:YMH4:NQEQ:WS4M:A6KP:R3JC:L6EW:LS6V
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
192.168.56.102:5000
127.0.0.0/8
Registry Mirrors:
https://1nj0zren.mirror.aliyuncs.com/
https://docker.mirrors.ustc.edu.cn/
http://f1361db2.m.daocloud.io/
https://registry.docker-cn.com/
Live Restore Enabled: false
1
2
3
4
快速拉取openGauss最新镜像
[root@hhj ~]# docker pull enmotech/opengauss:latest
latest: Pulling from enmotech/opengauss
Digest: sha256:d5a3e38fa2553a44e7fa1cd5cad0b4f0845a679858764067d7b0052a228578a0
Status: Image is up to date for enmotech/opengauss:latest
docker.io/enmotech/opengauss:latest
1
2
3
4
5
修改openGauss的镜像标签
注意:不修改标签在启动容器时会报找不到镜像
[root@hhj ~]# docker tag enmotech/opengauss:latest opengauss:latest
[root@hhj ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
enmotech/opengauss latest b4dd24d09223 2 months ago 383MB
opengauss latest b4dd24d09223 2 months ago 383MB
1
2
3
4
5
获取创建主备容器脚本并运行
[root@hhj ~]# # wget https://raw.githubusercontent.com/enmotech/enmotech-docker-opengauss/master/create_master_slave.sh
#vi create_master_slave.sh
#!/bin/bash -e
# Parameters
#!/bin/bash
#set OG_SUBNET,GS_PASSWORD,MASTER_IP,SLAVE_1_IP,MASTER_HOST_PORT,MASTER_LOCAL_PORT,SLAVE_1_HOST_PORT,SLAVE_1_LOCAL_PORT,MASTER_NODENAME,SLAVE_NODENAME
read -p "Please input OG_SUBNET (容器所在网段) [172.11.0.0/24]: " OG_SUBNET
OG_SUBNET=${OG_SUBNET:-172.11.0.0/24}
echo "OG_SUBNET set $OG_SUBNET"
read -p "Please input GS_PASSWORD (定义数据库密码)[Enmo@123]: " GS_PASSWORD
GS_PASSWORD=${GS_PASSWORD:-Enmo@123}
echo "GS_PASSWORD set $GS_PASSWORD"
read -p "Please input MASTER_IP (主库IP)[172.11.0.101]: " MASTER_IP
MASTER_IP=${MASTER_IP:-172.11.0.101}
echo "MASTER_IP set $MASTER_IP"
read -p "Please input SLAVE_1_IP (备库IP)[172.11.0.102]: " SLAVE_1_IP
SLAVE_1_IP=${SLAVE_1_IP:-172.11.0.102}
echo "SLAVE_1_IP set $SLAVE_1_IP"
read -p "Please input MASTER_HOST_PORT (主库数据库服务端口)[5432]: " MASTER_HOST_PORT
MASTER_HOST_PORT=${MASTER_HOST_PORT:-5432}
echo "MASTER_HOST_PORT set $MASTER_HOST_PORT"
read -p "Please input MASTER_LOCAL_PORT (主库通信端口)[5434]: " MASTER_LOCAL_PORT
MASTER_LOCAL_PORT=${MASTER_LOCAL_PORT:-5434}
echo "MASTER_LOCAL_PORT set $MASTER_LOCAL_PORT"
read -p "Please input SLAVE_1_HOST_PORT (备库数据库服务端口)[6432]: " SLAVE_1_HOST_PORT
SLAVE_1_HOST_PORT=${SLAVE_1_HOST_PORT:-6432}
echo "SLAVE_1_HOST_PORT set $SLAVE_1_HOST_PORT"
read -p "Please input SLAVE_1_LOCAL_PORT (备库通信端口)[6434]: " SLAVE_1_LOCAL_PORT
SLAVE_1_LOCAL_PORT=${SLAVE_1_LOCAL_PORT:-6434}
echo "SLAVE_1_LOCAL_PORT set $SLAVE_1_LOCAL_PORT"
read -p "Please input MASTER_NODENAME [opengauss_master]: " MASTER_NODENAME
MASTER_NODENAME=${MASTER_NODENAME:-opengauss_master}
echo "MASTER_NODENAME set $MASTER_NODENAME"
read -p "Please input SLAVE_NODENAME [opengauss_slave1]: " SLAVE_NODENAME
SLAVE_NODENAME=${SLAVE_NODENAME:-opengauss_slave1}
echo "SLAVE_NODENAME set $SLAVE_NODENAME"
read -p "Please input openGauss VERSION [1.0.1]: " VERSION
VERSION=${VERSION:-1.0.1}
echo "openGauss VERSION set $VERSION"
echo "starting "
docker network create --subnet=$OG_SUBNET opengaussnetwork \
|| {
echo ""
echo "ERROR: OpenGauss Database Network was NOT successfully created."
echo "HINT: opengaussnetwork Maybe Already Exsist Please Execute 'docker network rm opengaussnetwork' "
exit 1
}
echo "OpenGauss Database Network Created."
docker run --network opengaussnetwork --ip $MASTER_IP --privileged=true \
--name $MASTER_NODENAME -h $MASTER_NODENAME -p $MASTER_HOST_PORT:$MASTER_HOST_PORT -d \
-e GS_PORT=$MASTER_HOST_PORT \
-e OG_SUBNET=$OG_SUBNET \
-e GS_PASSWORD=$GS_PASSWORD \
-e NODE_NAME=$MASTER_NODENAME \
-e REPL_CONN_INFO="replconninfo1 = 'localhost=$MASTER_IP localport=$MASTER_LOCAL_PORT localservice=$MASTER_HOST_PORT remotehost=$SLAVE_1_IP remoteport=$SLAVE_1_LOCAL_PORT remoteservice=$SLAVE_1_HOST_PORT'\n" \
enmotech/opengauss:$VERSION -M primary \
|| {
echo ""
echo "ERROR: OpenGauss Database Master Docker Container was NOT successfully created."
exit 1
}
echo "OpenGauss Database Master Docker Container created."
sleep 30s
docker run --network opengaussnetwork --ip $SLAVE_1_IP --privileged=true \
--name $SLAVE_NODENAME -h $SLAVE_NODENAME -p $SLAVE_1_HOST_PORT:$SLAVE_1_HOST_PORT -d \
-e GS_PORT=$SLAVE_1_HOST_PORT \
-e OG_SUBNET=$OG_SUBNET \
-e GS_PASSWORD=$GS_PASSWORD \
-e NODE_NAME=$SLAVE_NODENAME \
-e REPL_CONN_INFO="replconninfo1 = 'localhost=$SLAVE_1_IP localport=$SLAVE_1_LOCAL_PORT localservice=$SLAVE_1_HOST_PORT remotehost=$MASTER_IP remoteport=$MASTER_LOCAL_PORT remoteservice=$MASTER_HOST_PORT'\n" \
enmotech/opengauss:$VERSION -M standby \
|| {
echo ""
echo "ERROR: OpenGauss Database Slave1 Docker Container was NOT successfully created."
exit 1
}
echo "OpenGauss Database Slave1 Docker Container created."
[root@hhj ~]## chmod +x create_master_slave.sh
[root@hhj ~]# ./create_master_slave.sh
Please input OG_SUBNET (容器所在网段) [172.11.0.0/24]:
OG_SUBNET set 172.11.0.0/24
Please input GS_PASSWORD (定义数据库密码)[Enmo@123]:
GS_PASSWORD set Enmo@123
Please input MASTER_IP (主库IP)[172.11.0.101]:
MASTER_IP set 172.11.0.101
Please input SLAVE_1_IP (备库IP)[172.11.0.102]:
SLAVE_1_IP set 172.11.0.102
Please input MASTER_HOST_PORT (主库数据库服务端口)[5432]:
MASTER_HOST_PORT set 5432
Please input MASTER_LOCAL_PORT (主库通信端口)[5434]:
MASTER_LOCAL_PORT set 5434
Please input SLAVE_1_HOST_PORT (备库数据库服务端口)[6432]:
SLAVE_1_HOST_PORT set 6432
Please input SLAVE_1_LOCAL_PORT (备库通信端口)[6434]:
SLAVE_1_LOCAL_PORT set 6434
Please input MASTER_NODENAME [opengauss_master]:
MASTER_NODENAME set opengauss_master
Please input SLAVE_NODENAME [opengauss_slave1]:
SLAVE_NODENAME set opengauss_slave1
Please input openGauss VERSION [1.0.1]: latest
openGauss VERSION set latest
starting
13b4cf3a545cdc51539b2f4e6b227536cacd5caa764a167701927d7a6cbd21e5
OpenGauss Database Network Created.
85c9894a167ca992c237900bb27ed50c8ffb323217de6c1011614f5a67f8dcd6
OpenGauss Database Master Docker Container created.
e3fbdd36d6203a34051eea9d6a756d404528a0dcdca169aa2be42502a45d3087
OpenGauss Database Slave1 Docker Container created.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
注意如果遇到容器存在或者网络存在可以如下删除
[root@hhj ~]# docker rm opengauss_master
opengauss_master
[root@hhj ~]# docker network rm opengaussnetwork
opengaussnetwork
1
2
3
4
验证openGauss主备集群状态
查看容器启动情况
[root@hhj ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3fbdd36d620 opengauss:latest "entrypoint.sh -M st…" 13 seconds ago Up 12 seconds 5432/tcp, 0.0.0.0:6432->6432/tcp, :::6432->6432/tcp opengauss_slave1
85c9894a167c opengauss:latest "entrypoint.sh -M pr…" 46 seconds ago Up 43 seconds 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp opengauss_master
1
2
3
4
进入openGauss主库
[root@hhj ~]# docker exec -it opengauss_master bash
root@opengauss_master:/# su - omm
omm@opengauss_master:~$ gsql
gsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
omm | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
(4 rows)
omm=# \q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
检查主从情况
[2021-12-19 04:29:35.624][350][][gs_ctl]: gs_ctl query ,datadir is /var/lib/opengauss/data
HA state:
local_role : Primary
static_connections : 1
db_state : Normal
detail_information : Normal
Senders info:
sender_pid : 320
local_role : Primary
peer_role : Standby
peer_state : Normal
state : Streaming
sender_sent_location : 0/4000268
sender_write_location : 0/4000268
sender_flush_location : 0/4000268
sender_replay_location : 0/4000268
receiver_received_location : 0/4000268
receiver_write_location : 0/4000268
receiver_flush_location : 0/4000268
receiver_replay_location : 0/4000268
sync_percent : 100%
sync_state : Sync
sync_priority : 1
sync_most_available : On
channel : 172.11.0.101:5434-->172.11.0.102:38096
Receiver info:
No information
omm@opengauss_master:~$
omm@opengauss_master:~$ exit
logout
root@opengauss_master:/# exit
exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
进入openGauss从库
root@opengauss_slave1:/# su - omm
omm@opengauss_slave1:~$ gsql -r -p6432
gsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
omm=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
omm | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
(4 rows)
omm=# \q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
检查主从情况
omm@opengauss_slave1:~$ gs_ctl query -D /var/lib/opengauss/data/
[2021-12-19 04:31:24.788][361][][gs_ctl]: gs_ctl query ,datadir is /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 : 338
local_role : Standby
peer_role : Primary
peer_state : Normal
state : Normal
sender_sent_location : 0/40004A8
sender_write_location : 0/40004A8
sender_flush_location : 0/40004A8
sender_replay_location : 0/40004A8
receiver_received_location : 0/40004A8
receiver_write_location : 0/40004A8
receiver_flush_location : 0/40004A8
receiver_replay_location : 0/40004A8
sync_percent : 100%
channel : 172.11.0.102:38096<--172.11.0.101:5434
omm@opengauss_slave1:~$ exit
logout
root@opengauss_slave1:/# exit
exit
[root@hhj ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3fbdd36d620 opengauss:latest "entrypoint.sh -M st…" 8 minutes ago Up 8 minutes 5432/tcp, 0.0.0.0:6432->6432/tcp, :::6432->6432/tcp opengauss_slave1
85c9894a167c opengauss:latest "entrypoint.sh -M pr…" 9 minutes ago Up 9 minutes 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp opengauss_master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
验证openGauss主备集群主从同步
进入主库创建表和插入数据
[root@hhj ~]# docker exec -it opengauss_master bash
root@opengauss_master:/# su - omm
omm@opengauss_master:~$ gsql -r
gsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
omm= create table test(id int);
CREATE TABLE
omm=# insert into test values(1);
INSERT 0 1
omm=# insert into test values(2);
INSERT 0 1
omm=# select * from test;
id
----
1
2
(2 rows)
omm=# exit
omm-# \q
omm@opengauss_master:~$ exit
logout
root@opengauss_master:/# exit
exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
进入从库验证数据同步
[root@hhj ~]# docker exec -it opengauss_slave1 bash
root@opengauss_slave1:/# su - omm
omm@opengauss_slave1:~$ gsql -r -p 6432
gsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
omm=# \d
List of relations
Schema | Name | Type | Owner | Storage
--------+------+-------+-------+----------------------------------
public | test | table | omm | {orientation=row,compression=no}
(1 row)
omm=# select * from test;
id
----
1
2
(2 rows)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
文章知识点与官方知识档案匹配,可进一步学习相关知识
————————————————
版权声明:本文为CSDN博主「Gauss松鼠会」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/GaussDB/article/details/122111657