Getting the latest kbcli ... Downloading ... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 33.3M 100 33.3M 0 0 24.4M 0 0:00:01 0:00:01 --:--:-- 24.5M Password: kbcli installed successfully. kbcli: 0.8.2 Make sure your docker service is running and begin your journey with kbcli:
kbcli playground init
For more information on how to get started, please visit: https:// $ kbcli version kbcli: 0.8.2
$ kbcli kubeblocks install KubeBlocks will be installed to namespace 'kb-system' Kubernetes version 1.28.7 kbcli version 0.8.2 Collecting data from cluster OK Kubernetes cluster preflight OK Warn - The default storage class was not found. You can use option --set storageClass=<storageClassName> when creating cluster Create CRDs OK Add and update repo kubeblocks OK Install KubeBlocks 0.8.2 OK Wait for addons to be enabled apecloud-mysql OK clickhouse OK kafka OK mongodb OK postgresql OK pulsar OK redis OK snapshot-controller OK
KubeBlocks 0.8.2 installed to namespace kb-system SUCCESSFULLY!
-> Basic commands for cluster: kbcli cluster create -h # help information about creating a database cluster kbcli cluster list # list all database clusters kbcli cluster describe <cluster name> # get cluster information
KubeBlocks 部署完成后,我们就可以使用 kbcli 来创建数据库集群了,数据库在 Kubernetes 上以 Pod 的形式运行。比如我们这里以 MySQL 为例,创建一个 MySQL 集群。
创建 MySQL 集群
为保持隔离,这里我们创建一个名为 demo 的独立命名空间。
kubectl create namespace demo
KubeBlocks 支持创建两种类型的 MySQL 集群:单机版(Standalone)和集群版(RaftGroup)。单机版仅支持一个副本,适用于对可用性要求较低的场景。集群版包含三个副本,适用于对高可用性要求较高的场景。为了确保高可用性,所有的副本都默认分布在不同的节点上。
直接使用下面的命令即可创建一个 MySQL 单机版集群:
$ kbcli cluster create mysql demo --namespace demo Info: --version is not specified, ac-mysql-8.0.30 is applied by default. Cluster demo created
创建后可以使用 kbcli cluster list 命令查看创建的数据库集群列表:
$ kbcli cluster list -n demo NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME demo demo apecloud-mysql ac-mysql-8.0.30 Delete Creating May 11,2024 20:15 UTC+0800
从上面的输出可以看到,我们创建的 MySQL 集群名称为 demo,所属命名空间为 demo,使用的集群定义为 apecloud-mysql,版本为 ac-mysql-8.0.30,状态为 Creating。当然我们也可以查看 Pod 的状态:
$ kubectl get cluster -n demo NAME CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS AGE demo apecloud-mysql ac-mysql-8.0.30 Delete Creating 2m24s $ kubectl get sts -n demo NAME READY AGE demo-mysql 0/1 2m $ kubectl get pods -n demo NAME READY STATUS RESTARTS AGE demo-mysql-0 0/5 Pending 0 79s
由于我们并没有配置存储类,所以 Pod 一直处于 Pending 状态,我们可以设置一个默认的 StorageClass 来解决这个问题。
上面创建单机版的 MySQL 集群是 kbcli 内置的集群定义,如果需要创建集群版的 MySQL 集群,可以使用 --cluster-definition 参数指定集群定义,比如创建一个 MySQL 集群版,设置副本数为 3,
$ kbcli cluster create mycluster --cluster-definition=apecloud-mysql --set cpu=0.5,memory=512Mi,storage=10Gi,replicas=3 --set storageClass=nfs-client --namespace demo Info: --cluster-version is not specified, ClusterVersion ac-mysql-8.0.30 is applied by default Cluster mycluster created
创建后可以使用 kbcli cluster list 命令查看创建的数据库集群列表:
$ kbcli cluster list -n demo NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME mycluster demo apecloud-mysql ac-mysql-8.0.30 Delete Creating May 11,2024 20:34 UTC+0800
此外还可以使用 kbcli cluster describe 命令查看集群的详细信息:
$ kbcli cluster describe mycluster -n demo Name: mycluster Created Time: May 11,2024 20:39 UTC+0800 NAMESPACE CLUSTER-DEFINITION VERSION STATUS TERMINATION-POLICY demo apecloud-mysql ac-mysql-8.0.30 Running Delete
Endpoints: COMPONENT MODE INTERNAL EXTERNAL mysql ReadWrite mycluster-mysql.demo.uster.local:3306 <none>
Topology: COMPONENT INSTANCE ROLE STATUS AZ NODE CREATED-TIME mysql mycluster-mysql-0 follower Running <none> node2/192.168.0.118 May 11,2024 20:39 UTC+0800 mysql mycluster-mysql-1 follower Running <none> node1/192.168.0.116 May 11,2024 20:39 UTC+0800 mysql mycluster-mysql-2 leader Running <none> node1/192.168.0.116 May 11,2024 20:39 UTC+0800
Images: COMPONENT TYPE IMAGE mysql mysql infracreate-registry.cn-zhangjiakou.cr.aliyuncs.com/apecloud/apecloud-mysql-server:8.0.30-5.beta3.20231215.ge77d836.13
Data Protection: BACKUP-REPO AUTO-BACKUP BACKUP-SCHEDULE BACKUP-METHOD BACKUP-RETENTION
Show cluster events: kbcli cluster list-events -n demo mycluster
从上面的输出可以看到我们创建的 MySQL 集群的详细信息,包括状态、集群的访问地址等,Topology 显示了集群的拓扑结构,Resources Allocation 显示了集群的资源分配情况,Images 显示了集群使用的镜像,Data Protection 显示了集群的数据保护情况。
$ kbcli cluster list-events -n demo mycluster # ...... demo May 11,2024 20:42 UTC+0800 Normal Pulled Instance/mycluster-mysql-1 Successfully pulled image 'infracreate-registry.cn-zhangjiakou.cr.aliyuncs.com/apecloud/apecloud-mysql-scale:0.2.6'in 725ms (1m42.223s including waiting) demo May 11,2024 20:42 UTC+0800 Normal Started Instance/mycluster-mysql-1 Started container config-manager demo May 11,2024 20:42 UTC+0800 Normal Pulled Instance/mycluster-mysql-1 Container image 'infracreate-registry.cn-zhangjiakou.cr.aliyuncs.com/apecloud/kubeblocks-tools:0.8.2' already present on machine demo May 11,2024 20:42 UTC+0800 Normal Started Instance/mycluster-mysql-1 Started container lorry demo May 11,2024 20:42 UTC+0800 Normal checkRole Instance/mycluster-mysql-0 {'event':'Success','operation':'checkRole','originalRole':'','role':'Follower'} demo May 11,2024 20:42 UTC+0800 Normal checkRole Instance/mycluster-mysql-2 {'event':'Success','operation':'checkRole','originalRole':'','role':'Leader'} demo May 11,2024 20:42 UTC+0800 Normal checkRole Instance/mycluster-mysql-1 {'event':'Success','operation':'checkRole','originalRole':'','role':'Follower'} demo May 11,2024 20:42 UTC+0800 Normal ComponentPhaseTransition Cluster/mycluster component is Running demo May 11,2024 20:42 UTC+0800 Normal AllReplicasReady Cluster/mycluster all pods of components are ready, waiting for the probe detection successful demo May 11,2024 20:42 UTC+0800 Normal ClusterReady Cluster/mycluster Cluster: mycluster is ready, current phase is Running demo May 11,2024 20:42 UTC+0800 Normal Running Cluster/mycluster Cluster: mycluster is ready, current phase is Running
通过这些 Events 事件可以查看集群的创建过程,以及集群的状态变化,如果有问题也可以根据 Events 事件来排查问题。从上面的输出可以看到我们的 MySQL 集群已经创建成功,状态为 Running。
我们也可以查看集群的 Pod 状态:
$ kubectl get pods -n demo NAME READY STATUS RESTARTS AGE mycluster-mysql-0 5/5 Running 0 7m44s mycluster-mysql-1 5/5 Running 0 7m44s mycluster-mysql-2 5/5 Running 0 7m44s
连接 MySQL 集群
接下来我们就可以连接到 MySQL 集群中,进行一些操作了。我们这里可以使用 kbcli 提供的 cluster connect 命令来连接到 MySQL 集群中:
$ kbcli cluster connect mycluster --namespace demo Connect to instance mycluster-mysql-2: out of mycluster-mysql-2(leader), mycluster-mysql-0(follower), mycluster-mysql-1(follower) Defaulted container 'mysql' out of: mysql, metrics, vttablet, lorry, config-manager mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 46 Server version: 8.0.30 WeSQL Server - GPL, Release 5, Revision e77d836
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.
mysql> USE mydb Database changed mysql> CREATE TABLE students ( student_id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, gender VARCHAR(10) NOT NULL, birthday DATE NOT NULL, major VARCHAR(50) NOT NULL, grade INT NOT NULL ); Query OK, 0 rows affected (0.06 sec)
$ kubectl exec -ti -n demo mycluster-mysql-0 -- bash Defaulted container 'mysql' out of: mysql, metrics, vttablet, lorry, config-manager [root@mycluster-mysql-0 /]# mysql -uroot -pzcw4p54l mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2625 Server version: 8.0.30 WeSQL Server - GPL, Release 5, Revision e77d836
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement.
mysql> use mydb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
Database changed mysql> SELECT * FROM students; +------------+-----------------+--------+------------+---------------------------------+-------+ | student_id | name | gender | birthday | major | grade | +------------+-----------------+--------+------------+---------------------------------+-------+ | 1 | John Smith | Male | 2001-01-01 | Computer Science and Technology | 2020 | | 2 | Emily Brown | Female | 2002-02-15 | Software Engineering | 2021 | | 3 | Michael Johnson | Male | 2003-03-26 | Information Security | 2022 | +------------+-----------------+--------+------------+---------------------------------+-------+ 3 rows inset (0.00 sec)
mysql>
如果在本地计算机上安装了 MySQL 客户端,我们同样可以使用 kubectl port-forward 来进行端口转发,然后连接到数据库,当然也可以通过一些网关服务(比如 higress)来暴露数据库服务。
扩展
前面我们创建的 MySQL 集群配置了集群的 CPU、内存、存储等,当资源不足的时候,我们还可以使用 kbcli cluster vscale 命令来进行垂直扩展,比如我们将 MySQL 集群的 CPU 和内存都扩展为 1 核 1GB,使用如下命令:
$ kkbcli cluster vscale mycluster --components=mysql --cpu 1000m --memory 1Gi -n demo Please type the name again(separate with white space when more than one): mycluster OpsRequest mycluster-verticalscaling-7zhf2 created successfully, you can view the progress: kbcli cluster describe-ops mycluster-verticalscaling-7zhf2 -n demo
在垂直扩容时,所有的 Pod 将按照 Learner -> Follower -> Leader 的顺序重启。重启后,主节点可能会发生变化。
Last Configuration: COMPONENT REQUEST-CPU REQUEST-MEMORY LIMIT-CPU LIMIT-MEMORY mysql 500m 512Mi 500m 512Mi
Status: Start Time: May 12,2024 07:26 UTC+0800 Duration: 45s Status: Running Progress: 0/3 OBJECT-KEY STATUS DURATION MESSAGE Pod/mycluster-mysql-0 Processing 45s Start to vertical scale: Pod/mycluster-mysql-0 in Component: mysql Pod/mycluster-mysql-2 Pending <Unknown> Pod/mycluster-mysql-1 Pending <Unknown>
Conditions: LAST-TRANSITION-TIME TYPE REASON STATUS MESSAGE May 12,2024 07:26 UTC+0800 WaitForProgressing WaitForProgressing True waitfor the controller to process the OpsRequest: mycluster-verticalscaling-7zhf2 in Cluster: mycluster May 12,2024 07:26 UTC+0800 Validated ValidateOpsRequestPassed True OpsRequest: mycluster-verticalscaling-7zhf2 is validated May 12,2024 07:26 UTC+0800 VerticalScaling VerticalScalingStarted True Start to vertical scale resources in Cluster: mycluster
Warning Events: <none>
我们还可以使用 cluster list-ops 命令查看这些运维操作的状态:
$ kbcli cluster list-ops --status all -n demo NAME NAMESPACE TYPE CLUSTER COMPONENT STATUS PROGRESS CREATED-TIME mycluster-verticalscaling-7zhf2 demo VerticalScaling mycluster mysql Succeed 3/3 May 12,2024 07:26 UTC+0800
Status: Start Time: May 12,2024 07:44 UTC+0800 Completion Time: May 12,2024 07:44 UTC+0800 Duration: 30s Status: Succeed Progress: 3/3 OBJECT-KEY STATUS DURATION MESSAGE
Conditions: LAST-TRANSITION-TIME TYPE REASON STATUS MESSAGE May 12,2024 07:44 UTC+0800 WaitForProgressing WaitForProgressing True waitfor the controller to process the OpsRequest: mycluster-reconfiguring-bb6l8 in Cluster: mycluster May 12,2024 07:44 UTC+0800 Validated ValidateOpsRequestPassed True OpsRequest: mycluster-reconfiguring-bb6l8 is validated May 12,2024 07:44 UTC+0800 Reconfigure ReconfigureStarted True Start to reconfigure in Cluster: mycluster, Component: mysql May 12,2024 07:44 UTC+0800 Succeed OpsRequestProcessedSuccessfully True Successfully processed the OpsRequest: mycluster-reconfiguring-bb6l8 in Cluster: mycluster
用户可以创建多个 BackupRepo 以适应不同的场景。例如,根据不同的业务需求,可以把业务 A 的数据存储在 A 仓库,把业务 B 的数据存储在 B 仓库,或者可以按地区配置多个仓库以实现异地容灾。在创建备份时,你需要指定备份仓库。你也可以创建一个默认的备份仓库,如果在创建备份时未指定具体的仓库,KubeBlocks 将使用此默认仓库来存储备份数据。
$ kbcli cluster list-backups -n demo NAME NAMESPACE SOURCE-CLUSTER METHOD STATUS TOTAL-SIZE DURATION CREATE-TIME COMPLETION-TIME EXPIRATION mybackup demo mycluster xtrabackup Completed 4645133 59s May 12,2024 08:55 UTC+0800 May 12,2024 08:56 UTC+0800 $ kbcli cluster restore new-cluster --backup mybackup -n demo Cluster new-cluster created
恢复后我们可以校验新集群的数据是否和原集群一致,如果一致则说明恢复成功。
$ kbcli cluster connect new-cluster -n demo Connect to instance new-cluster-mysql-1: out of new-cluster-mysql-1(leader), new-cluster-mysql-0(follower), new-cluster-mysql-2(follower) mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 9961 Server version: 8.0.30 WeSQL Server - GPL, Release 5, Revision 28f261a
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or 'h'forhelp. Type 'c' to clear the current input statement. mysql> USE mydb mysql> SELECT * FROM students; +------------+-----------------+--------+------------+---------------------------------+-------+ | student_id | name | gender | birthday | major | grade | +------------+-----------------+--------+------------+---------------------------------+-------+ | 1 | John Smith | Male | 2001-01-01 | Computer Science and Technology | 2020 | | 2 | Emily Brown | Female | 2002-02-15 | Software Engineering | 2021 | | 3 | Michael Johnson | Male | 2003-03-26 | Information Security | 2022 | +------------+-----------------+--------+------------+---------------------------------+-------+ 3 rows inset (0.00 sec) mysql> exit Bye