KubeBlocks 体验之旅

KubeBlocks 体验之旅配图

KubeBlocks 是基于 Kubernetes 的云原生数据基础设施,将顶级云服务提供商的大规模生产经验与增强的可用性和稳定性改进相结合,帮助用户轻松构建容器化、声明式的关系型、NoSQL、流计算和向量型数据库服务。

初始化 Playground

只需要执行一下命令,如何就会自动创建一个单机版 mysql 集群 kbcli playground init

执行成功的输出如下

Create k3d cluster: kb-playground                  OK
Merge kubeconfig to /home/hong/.kube/config        OK
Switch current context to k3d-kb-playground        OK
Write kubeconfig to /home/hong/.kbcli/playground/kubeconfig OK
KubeBlocks will be installed to namespace "kb-system"
Kubernetes version 1.23.8+k3s1
Kubernetes provider K3S
kbcli version 0.8.2
Create CRDs                                        OK
Add and update repo kubeblocks                     OK
Install KubeBlocks 0.8.2                           OK
Wait for addons to be enabled
  alertmanager-webhook-adaptor                     OK
  apecloud-mysql                                   OK
  apecloud-otel-collector                          OK
  clickhouse                                       OK
  csi-hostpath-driver                              OK
  grafana                                          OK
  kafka                                            OK
  mongodb                                          OK
  postgresql                                       OK
  prometheus                                       OK
  pulsar                                           OK
  redis                                            OK
  snapshot-controller                              OK
Create cluster mycluster (ClusterDefinition: apecloud-mysql) OK

KubeBlocks playground init SUCCESSFULLY!

Kubernetes cluster "kb-playground" has been created.
Cluster "mycluster" has been created.
Elapsed time: 4m50s

1. Basic commands for cluster:

  kbcli cluster list                     # list database cluster and check its status
  kbcli cluster describe mycluster       # get cluster information

2. Connect to database

  kbcli cluster connect mycluster
  
3. View the Grafana:

  kbcli dashboard open kubeblocks-grafana

4. Destroy Playground:

  kbcli playground destroy

--------------------------------------------------------------------
To get more help: kbcli help
Use "kbcli [command] --help" for more information about a command.

查看集群

通过这个命令查看 kbcli cluster list,若 STATUS 为非 Runnig 状态则需要反复执行查看

NAME        NAMESPACE   CLUSTER-DEFINITION   VERSION           TERMINATION-POLICY   STATUS    CREATED-TIME                 
mycluster   default     apecloud-mysql       ac-mysql-8.0.30   WipeOut              Running   Mar 29,2024 15:00 UTC+0800

访问集群

  1. 获取访问凭证
kbcli cluster connect --show-example --show-password --client=cli mycluster
  1. 映射外部端口
kubectl port-forward service/mycluster-mysql 3306:3306
>
Forwarding from 127.0.0.1:3306 -> 3306
Forwarding from [::1]:3306 -> 3306
  1. 用 mysql 客户端正常链接数据库
mysql -h 127.0.0.1 -P 3306 -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 59
Server version: 8.0.30 WeSQL Server - GPL, Release 5, Revision e77d836

Copyright (c) 2000, 2024, 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' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| kubeblocks         |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

mysql> 

创建集群版 MySql

  1. 删除单机版 MySql 并创建 集群版 MySql
$ kbcli cluster delete mycluster
$ kbcli cluster create --cluster-definition='apecloud-mysql' --set replicas=3 mycluster
  1. 集群信息对比
  • 单机版 MySql 信息
kbcli cluster describe mycluster
Name: mycluster  Created Time: Mar 29,2024 15:00 UTC+0800
NAMESPACE   CLUSTER-DEFINITION   VERSION           STATUS    TERMINATION-POLICY   
default     apecloud-mysql       ac-mysql-8.0.30   Running   WipeOut              

Endpoints:
COMPONENT   MODE        INTERNAL                                         EXTERNAL   
mysql       ReadWrite   mycluster-mysql.default.svc.cluster.local:3306   <none>     

Topology:
COMPONENT   INSTANCE            ROLE     STATUS    AZ       NODE                                    CREATED-TIME                 
mysql       mycluster-mysql-0   leader   Running   <none>   k3d-kb-playground-server-0/172.25.0.2   Mar 29,2024 15:00 UTC+0800   

Resources Allocation:
COMPONENT   DEDICATED   CPU(REQUEST/LIMIT)   MEMORY(REQUEST/LIMIT)   STORAGE-SIZE   STORAGE-CLASS     
mysql       false       1 / 1                1Gi / 1Gi               data:20Gi      csi-hostpath-sc   

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 default mycluster
  • 集群版 MySql 信息
kbcli cluster describe mycluster
Name: mycluster  Created Time: Mar 29,2024 15:28 UTC+0800
NAMESPACE   CLUSTER-DEFINITION   VERSION           STATUS    TERMINATION-POLICY   
default     apecloud-mysql       ac-mysql-8.0.30   Running   Delete               

Endpoints:
COMPONENT   MODE        INTERNAL                                         EXTERNAL   
mysql       ReadWrite   mycluster-mysql.default.svc.cluster.local:3306   <none>     

Topology:
COMPONENT   INSTANCE            ROLE       STATUS    AZ       NODE                                    CREATED-TIME                 
mysql       mycluster-mysql-2   leader     Running   <none>   k3d-kb-playground-server-0/172.25.0.2   Mar 29,2024 15:28 UTC+0800   
mysql       mycluster-mysql-1   follower   Running   <none>   k3d-kb-playground-server-0/172.25.0.2   Mar 29,2024 15:28 UTC+0800   
mysql       mycluster-mysql-0   follower   Running   <none>   k3d-kb-playground-server-0/172.25.0.2   Mar 29,2024 15:28 UTC+0800   

Resources Allocation:
COMPONENT   DEDICATED   CPU(REQUEST/LIMIT)   MEMORY(REQUEST/LIMIT)   STORAGE-SIZE   STORAGE-CLASS     
mysql       false       1 / 1                1Gi / 1Gi               data:20Gi      csi-hostpath-sc   

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 default mycluster

参考文档

K8s
Publish on 2024-03-29,Update on 2025-02-10