Oceanbase学习之---docker部署Oceanbase集群
概要:
OceanBase 社区版是一款开源分布式 HTAP(Hybrid Transactional/Analytical Processing)数据库管理系统,具有原生分布式架构,支持金融级高可用、透明水平扩展、分布式事务、多租户和语法兼容等企业级特性。
一、OceanBase 数据库基础概念
OceanBase相关概念
- OBServer
OBServer :一个集群由若干个 OBServer 节点组成,这些 OBServer 节点分属于若干个区(Zone),每个节点属于一个区。 - 区域(zone)
区域(zone):区是一个逻辑概念,表示集群内具有相似硬件可用性的一组节点,它在不同的部署模式下代表不同的含义。例如,当整个集群部署在同一个数据中心(IDC)内的时候,一个区的节点可以属于同一个机架,同一个交换机等。当集群分布在多个数据中心的时候,每个区可以对应于一个数据中心。 - 资源池
资源池:集群的多个服务器组成了一个大的资源池,管理员会根据各个租户的要求,创建与之对应的虚拟资源池给租户使用,资源池包括指定规格的 CPU、内存、存储等。为了避免租户之间争抢资源,租户之间的资源相互隔离,内存是物理隔离、CPU 是逻辑隔离。 - 租户
租户:租户是一个逻辑概念。在 OceanBase 数据库中,租户是资源分配的单位,是数据库对象管理和资源管理的基础,对于系统运维,尤其是对于云数据库的运维有着重要的影响。 - 数据分区
数据分区:OceanBase 数据库参考传统数据库分区表的概念,把一张表格的数据划分成不同的分区(Partition)。 - 副本
副本:在分布式环境下,为保证数据读写服务的高可用,OceanBase 数据库会把同一个分区的数据拷贝到多个机器。不同机器同一个分区的数据拷贝称为副本(Replica)。
二、宿主主机环境
1、CPU信息
[root@node103 shdata]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 6
2、内存信息(物理memory不小于12G)
[root@node103 shdata]# free -m
total used free shared buff/cache available
Mem: 13575 355 12679 9 541 12306
Swap: 2815 0 2815
3、操作系统信息
[root@node103 ~]# cat /etc/centos-release
CentOS Linux release 7.2.1511 (Core)
4、安装docker相关软件
[root@node103 shdata]# yum install -y yum-utils
[root@node103 shdata]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@node103 shdata]# yum install docker-ce docker-ce-cli containerd.io -y
# 查看docker相关软件
[root@node103 shdata]# rpm -qa |grep docker
docker-buildx-plugin-0.11.2-1.el7.x86_64
docker-compose-plugin-2.20.2-1.el7.x86_64
docker-ce-24.0.5-1.el7.x86_64
docker-ce-cli-24.0.5-1.el7.x86_64
docker-ce-rootless-extras-24.0.5-1.el7.x86_64
二、部署Oceanbase docker
1、启动docker进程
Tips:启动docker前应关闭主机防火墙(firewalld)
[root@node103 shdata]# systemctl start docker
[root@node103 shdata]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2023-08-15 17:51:23 CST; 4s ago
Docs: https://docs.docker.com
Main PID: 5072 (dockerd)
Memory: 31.5M
CGroup: /system.slice/docker.service
└─5072 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
2、搜索oceanbase images
[root@node103 shdata]# docker search oceanbase
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
oceanbase/oceanbase-ce OceanBase is an open-source, distributed HTA… 19
oceanbasedev/ob-operator 0
oceanbasedev/obagent 0
oceanbasedev/oceanbase-cn 0
oceanbase/obce-mini obce-mini is a mini standalone test image fo… 3
oceanbase/oceanbase-xe OceanBase Database 2.2 Express Edition 4
oceanbase/ob-operator 0
oceanbase/obce-operator obce-operator 1
oceanbase/miniob miniob database competition 2
oceanbase/obagent 0
oceanbase/obproxy-ce 0
oceanbasedev/sealer-ob 0
oceanbasedev/obproxy-ce 0
oceanbase/oceanbase-cloud-native 0
oceanbase/centos7 0
obpilot/oceanbase-ce 3 steps to run an OceanBase-CE docker in you… 4
oceanbasedev/obproxy 0
oceanbasedev/create_tenant 0
datatomdev/oceanbase 0
zibuyu886/oceanbase-ce-cluster OceanBase ce cluster 1
huweijie/oceanbase-ce-deploy 0
oceanbasedev/vim-with-go 0
hnwyllmm/oceanbase-ce 0
oceanbasedev/ob-configserver 0
superbigfu/oceanbase 0
3、下载oceanbase images
[root@node103 shdata]# docker pull obpilot/oceanbase-ce:latest
latest: Pulling from obpilot/oceanbase-ce
a1d0c7532777: Pull complete
42e2faadf654: Pull complete
b888a9404e21: Pull complete
426c12926548: Pull complete
a66beb305b92: Pull complete
2d1899456ae1: Pull complete
Digest: sha256:4c2d16106cdc4bd610baf155e43e048d896de4d6bfc777534e75bfa0497bff49
Status: Downloaded newer image for obpilot/oceanbase-ce:latest
docker.io/obpilot/oceanbase-ce:latest
4、查看下载oceanbase images
[root@node103 shdata]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
obpilot/oceanbase-ce latest ed34156dd401 9 months ago 883MB
5、启动oceanbase image创建容器
[root@node103 shdata]# docker run -itd -m 10G --name oceanbase-ce obpilot/oceanbase-ce:latest
94e9e2ff541b883da7bc2fc2e28f84a5df9b58d8e4f20962569f1ea971c80b52
# 查看容器信息
[root@node103 shdata]# docker ps | grep oceanbase-ce
94e9e2ff541b obpilot/oceanbase-ce:latest "/bin/bash" 4 minutes ago Up 3 minutes 2881/tcp, 2883/tcp, 3000/tcp, 9090/tcp oceanbase-ce
宿主机关机后重新启动 container:
# docker启动 container冲突
[root@node103 ~]# docker run -itd -m 10G --name oceanbase-ce obpilot/oceanbase-ce:latest
docker: Error response from daemon: Conflict. The container name "/oceanbase-ce" is already in use by container "94e9e2ff541b883da7bc2fc2e28f84a5df9b58d8e4f20962569f1ea971c80b52".
You have to remove (or rename) that container to be able to reuse that name.
# 删除原有的 container
[root@node103 ~]# docker rm 94e9e2ff541b883da7bc2fc2e28f84a5df9b58d8e4f20962569f1ea971c80b52 -f
94e9e2ff541b883da7bc2fc2e28f84a5df9b58d8e4f20962569f1ea971c80b52
# 启动 container
[root@node103 ~]# docker run -itd -m 10G --name oceanbase-ce obpilot/oceanbase-ce:latest
f27e846fae998c1614908856a67076824b558e4c2f0f30669fb2c20b3f271c1a
[root@node103 ~]# docker ps | grep oceanbase-ce
f27e846fae99 obpilot/oceanbase-ce:latest "/bin/bash" 28 seconds ago Up 21 seconds 2881/tcp, 2883/tcp, 3000/tcp, 9090/tcp oceanbase-ce
224ba6482d9f obpilot/oceanbase-ce:latest "/bin/bash" 7 minutes ago Up 7 minutes 2881/tcp, 2883/tcp, 3000/tcp, 9090/tcp 94e9e2ff541b883da7bc2fc2e28f84a5df9b58d8e4f20962569f1ea971c80b52
[root@node103 ~]# docker exec -it oceanbase-ce bash
[admin@f27e846fae99 ~]$
三、Oceanbase docker下部署集群
1、登录docker内部访问
[root@node103 shdata]# docker exec -it oceanbase-ce bash
2、查看集群信息(本案例部署的是单节点的集群)
[admin@94e9e2ff541b ~]$ obd cluster list
+------------------------------------------------------------+
| Cluster List |
+--------+---------------------------------+-----------------+
| Name | Configuration Path | Status (Cached) |
+--------+---------------------------------+-----------------+
| obdemo | /home/admin/.obd/cluster/obdemo | deployed |
+--------+---------------------------------+-----------------+
3、启动cluster server
[admin@94e9e2ff541b ~]$ obd cluster start obdemo
Get local repositories ok
Search plugins ok
Open ssh connection ok
Load cluster param plugin ok
Check before start observer ok
[WARN] (127.0.0.1) clog and data use the same disk (/)
Check before start obproxy ok
Start observer ok
observer program health check ok
Connect to observer ok
Initialize cluster ok
Start obproxy ok
obproxy program health check ok
Connect to obproxy ok
Initialize cluster ok
Wait for observer init ok
+---------------------------------------------+
| observer |
+-----------+---------+------+-------+--------+
| ip | version | port | zone | status |
+-----------+---------+------+-------+--------+
| 127.0.0.1 | 4.0.0.0 | 2881 | zone1 | ACTIVE |
+-----------+---------+------+-------+--------+
obclient -h127.0.0.1 -P2881 -uroot -prootPWD123 -Doceanbase
+---------------------------------------------+
| obproxy |
+-----------+------+-----------------+--------+
| ip | port | prometheus_port | status |
+-----------+------+-----------------+--------+
| 127.0.0.1 | 2883 | 2884 | active |
+-----------+------+-----------------+--------+
obclient -h127.0.0.1 -P2883 -uroot -prootPWD123 -Doceanbase
obdemo running
# 查看cluster service端口信息
[admin@94e9e2ff541b ~]$ netstat -ntlp|grep 288
tcp 0 0 0.0.0.0:2881 0.0.0.0:* LISTEN 100/observer
tcp 0 0 0.0.0.0:2882 0.0.0.0:* LISTEN 100/observer
tcp 0 0 0.0.0.0:2883 0.0.0.0:* LISTEN 664/obproxy
tcp 0 0 0.0.0.0:2884 0.0.0.0:* LISTEN 664/obproxy
4、查看cluster信息
[admin@94e9e2ff541b ~]$ obd cluster display obdemo
Get local repositories and plugins ok
Open ssh connection ok
Cluster status check ok
Connect to observer ok
Wait for observer init ok
+---------------------------------------------+
| observer |
+-----------+---------+------+-------+--------+
| ip | version | port | zone | status |
+-----------+---------+------+-------+--------+
| 127.0.0.1 | 4.0.0.0 | 2881 | zone1 | ACTIVE |
+-----------+---------+------+-------+--------+
obclient -h127.0.0.1 -P2881 -uroot -prootPWD123 -Doceanbase
Connect to obproxy ok
+---------------------------------------------+
| obproxy |
+-----------+------+-----------------+--------+
| ip | port | prometheus_port | status |
+-----------+------+-----------------+--------+
| 127.0.0.1 | 2883 | 2884 | active |
+-----------+------+-----------------+--------+
obclient -h127.0.0.1 -P2883 -uroot -prootPWD123 -Doceanbase
5、连接cluster访问数据库
Tips: docker部署后默认数据库是MySQL模式:
[admin@94e9e2ff541b ~]$ obclient -h127.0.0.1 -P2883 -uroot -prootPWD123 -Doceanbase
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 10
Server version: OceanBase_CE 4.0.0.0 (r100000272022110114-6af7f9ae79cd0ecbafd4b1b88e2886ccdba0c3be) (Built Nov 1 2022 14:56:31)
Copyright (c) 2000, 2018, OB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient [oceanbase]>
# 查看数据库信息
obclient [oceanbase]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| LBACSYS |
| mysql |
| oceanbase |
| ORAAUDITOR |
| SYS |
| test |
+--------------------+
7 rows in set (0.002 sec)
# 查看当前数据库表信息
obclient [oceanbase]> use test;
Database changed
obclient [test]> show tables;
Empty set (0.002 sec)
# 查看集群配置信息
obclient [test]> show parameters like 'cluster';
+-------+----------+-----------+----------+---------+-----------+--------+---------------------+----------+---------+---------+-------------------+
| zone | svr_type | svr_ip | svr_port | name | data_type | value | info | section | scope | source | edit_level |
+-------+----------+-----------+----------+---------+-----------+--------+---------------------+----------+---------+---------+-------------------+
| zone1 | observer | 127.0.0.1 | 2882 | cluster | NULL | obdemo | Name of the cluster | OBSERVER | CLUSTER | DEFAULT | DYNAMIC_EFFECTIVE |
+-------+----------+-----------+----------+---------+-----------+--------+---------------------+----------+---------+---------+-------------------+
1 row in set (0.007 sec)
四、创建资源和租户及业务数据
1、创建资源和租户
[admin@f27e846fae99 ~]$ obclient -h127.0.0.1 -P2883 -uroot -prootPWD123 -Doceanbase
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3
Server version: OceanBase_CE 4.0.0.0 (r100000272022110114-6af7f9ae79cd0ecbafd4b1b88e2886ccdba0c3be) (Built Nov 1 2022 14:56:31)
Copyright (c) 2000, 2018, OB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient [oceanbase]>
# 查看数据库版本信息
obclient [oceanbase]> select version();
+------------------------------+
| version() |
+------------------------------+
| 5.7.25-OceanBase_CE-v4.0.0.0 |
+------------------------------+
1 row in set (0.001 sec)
# 创建resource unit
obclient [oceanbase]> CREATE RESOURCE UNIT unit2 MAX_CPU 2, MIN_CPU 2, MEMORY_SIZE '2G',
-> MAX_IOPS 1024, MIN_IOPS 1024, IOPS_WEIGHT 0, LOG_DISK_SIZE '2G';
Query OK, 0 rows affected (0.007 sec)
# 创建resource pool
obclient [oceanbase]> CREATE RESOURCE POOL pool1 UNIT='unit2',UNIT_NUM=1,ZONE_LIST=('zone1');
Query OK, 0 rows affected (0.013 sec)
# 创建租户
obclient [oceanbase]> create tenant ob_mysql resource_pool_list=('pool1'), primary_zone='RANDOM',comment 'mysql tenant/instance',
-> charset='utf8' set ob_tcp_invited_nodes='%', ob_compatibility_mode='mysql';
Query OK, 0 rows affected (16.116 sec)
obclient [oceanbase]> SELECT * FROM DBA_OB_TENANTS;
+-----------+-------------+-------------+----------------------------+----------------------------+--------------+---------------+-------------------+--------------------+--------+---------------+--------+
| TENANT_ID | TENANT_NAME | TENANT_TYPE | CREATE_TIME | MODIFY_TIME | PRIMARY_ZONE | LOCALITY | PREVIOUS_LOCALITY | COMPATIBILITY_MODE | STATUS | IN_RECYCLEBIN | LOCKED |
+-----------+-------------+-------------+----------------------------+----------------------------+--------------+---------------+-------------------+--------------------+--------+---------------+--------+
| 1 | sys | SYS | 2023-08-22 14:55:42.334481 | 2023-08-22 14:55:42.334481 | RANDOM | FULL{1}@zone1 | NULL | MYSQL | NORMAL | NO | NO |
| 1001 | META$1002 | META | 2023-08-22 16:42:56.163501 | 2023-08-22 16:43:09.987177 | RANDOM | FULL{1}@zone1 | NULL | MYSQL | NORMAL | NO | NO |
| 1002 | ob_mysql | USER | 2023-08-22 16:42:56.175235 | 2023-08-22 16:43:10.015437 | RANDOM | FULL{1}@zone1 | NULL | MYSQL | NORMAL | NO | NO |
+-----------+-------------+-------------+----------------------------+----------------------------+--------------+---------------+-------------------+--------------------+--------+---------------+--------+
3 rows in set (0.003 sec)
obclient [oceanbase]> select * from oceanbase.__all_unit_config;
+----------------------------+----------------------------+----------------+-----------------+---------+---------+-------------+---------------+----------+----------+-------------+
| gmt_create | gmt_modified | unit_config_id | name | max_cpu | min_cpu | memory_size | log_disk_size | max_iops | min_iops | iops_weight |
+----------------------------+----------------------------+----------------+-----------------+---------+---------+-------------+---------------+----------+----------+-------------+
| 2023-08-22 14:55:42.308468 | 2023-08-22 14:55:42.308468 | 1 | sys_unit_config | 1 | 1 | 2147483648 | 2147483648 | 10000 | 10000 | 1 |
| 2023-08-22 16:41:31.028764 | 2023-08-22 16:41:31.028764 | 1007 | unit2 | 2 | 2 | 2147483648 | 2147483648 | 1024 | 1024 | 0 |
+----------------------------+----------------------------+----------------+-----------------+---------+---------+-------------+---------------+----------+----------+-------------+
2 rows in set (0.002 sec)
2、创建业务数据
[admin@c1888fd997d9 ~]$ obclient -h127.0.0.1 -P2883 -uroot@ob_mysql -Doceanbase
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 13
Server version: OceanBase_CE 4.0.0.0 (r100000272022110114-6af7f9ae79cd0ecbafd4b1b88e2886ccdba0c3be) (Built Nov 1 2022 14:56:31)
Copyright (c) 2000, 2018, OB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 创建业务库
obclient [oceanbase]> create database db1;
Query OK, 1 row affected (0.055 sec)
obclient [oceanbase]> show databases;
+--------------------+
| Database |
+--------------------+
| db1 |
| information_schema |
| mysql |
| oceanbase |
| test |
+--------------------+
5 rows in set (0.003 sec)
# 创建业务用户
obclient [oceanbase]> create user tom@'%' identified by 'beijing';
Query OK, 0 rows affected (0.034 sec)
obclient [oceanbase]> grant all privileges on *.* to tom@'%';
Query OK, 0 rows affected (0.020 sec)
# 通过业务用户访问数据库
[admin@c1888fd997d9 ~]$ obclient -h127.0.0.1 -P2883 -utom@ob_mysql -pbeijing -Doceanbase
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 15
Server version: OceanBase_CE 4.0.0.0 (r100000272022110114-6af7f9ae79cd0ecbafd4b1b88e2886ccdba0c3be) (Built Nov 1 2022 14:56:31)
Copyright (c) 2000, 2018, OB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
obclient [oceanbase]>
# 创建业务表
obclient [oceanbase]> use db1;
Database changed
obclient [db1]> create table t1 (id int,name varchar(10));
Query OK, 0 rows affected (0.158 sec)
# 插入业务数据
obclient [db1]> insert into t1 values (10,'tom'),(20,'jerry'),(30,'rose');
Query OK, 3 rows affected (0.025 sec)
Records: 3 Duplicates: 0 Warnings: 0
obclient [db1]> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
+------+-------+
3 rows in set (0.003 sec)
# 查看SQL执行计划
obclient [db1]> explain select * from t1\G
*************************** 1. row ***************************
Query Plan: ===================================
|ID|OPERATOR |NAME|EST. ROWS|COST|
-----------------------------------
|0 |TABLE SCAN|t1 |3 |2 |
===================================
Outputs & filters:
-------------------------------------
0 - output([t1.id], [t1.name]), filter(nil), rowset=256,
access([t1.id], [t1.name]), partitions(p0)
1 row in set (0.002 sec)
五、总结
通过docker方式可以快速创建一个单节点的Oceanbase的集群环境,可用于Oceanbase的学习和测试。