KubeDB 体验在K8s中轻松管理DB

885次阅读
一条评论

共计 15485 个字符,预计需要花费 39 分钟才能阅读完成。

KubeDB 体验在K8s中轻松管理DB

KubeDB 是一个可以提供k8s集群中对数据库:升级、扩缩容、监控、备份、还原的工具,目前拥有两个版本:KubeDB Community Edition | KubeDB Enterprise Edition。博主简单体验下Community Edition,因为这个版本不支持数据库备份、还原、升级、扩缩容等,Enterprise Edition才支持

官方网站:https://kubedb.com/

KubeDB 安装

获取 License

1.获取集群UID

[root@node1 ~]# kubectl get ns kube-system -o=jsonpath='{.metadata.uid}'
47755ade-b2c6-4691-b00d-b63fbf9e1ae7

2.登录 https://license-issuer.appscode.com/?p=kubedb-community 获取

KubeDB 体验在K8s中轻松管理DB

Helm 方式安装 kubeDB

添加仓库

[root@node1 ~]# helm repo add appscode https://charts.appscode.com/stable/
[root@node1 ~]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "stable" chart repository
...Successfully got an update from the "incubator" chart repository
...Successfully got an update from the "appscode" chart repository
...Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈

安装KubeDB

[root@node1 ~]# helm install kubedb appscode/kubedb \
  --version v2022.03.28 \
  --namespace kubedb --create-namespace \
  --set-file global.license=/path/to/the/license.txt

[root@node1 ~]# kubectl get pods --all-namespaces -l "app.kubernetes.io/instance=kubedb"
NAMESPACE   NAME                                           READY   STATUS    RESTARTS   AGE
kubedb      kubedb-kubedb-provisioner-595766676d-zmrmt     1/1     Running   1          121m
kubedb      kubedb-kubedb-webhook-server-6dbc8b9c9-kpgr8   1/1     Running   1          121m

查看kubeDB支持的CRD资源

[root@node1 ~]# kubectl get crd -l app.kubernetes.io/name=kubedb
NAME                                           CREATED AT
elasticsearchdashboards.dashboard.kubedb.com   2022-05-07T04:33:22Z
elasticsearches.kubedb.com                     2022-05-07T03:47:06Z
elasticsearchversions.catalog.kubedb.com       2022-05-07T03:45:57Z
etcds.kubedb.com                               2022-05-07T03:47:06Z
etcdversions.catalog.kubedb.com                2022-05-07T03:45:57Z
mariadbdatabases.schema.kubedb.com             2022-05-07T04:32:05Z
mariadbs.kubedb.com                            2022-05-07T03:47:06Z
mariadbversions.catalog.kubedb.com             2022-05-07T03:45:57Z
memcacheds.kubedb.com                          2022-05-07T03:47:06Z
memcachedversions.catalog.kubedb.com           2022-05-07T03:45:57Z
mongodbdatabases.schema.kubedb.com             2022-05-07T04:32:04Z
mongodbs.kubedb.com                            2022-05-07T03:47:07Z
mongodbversions.catalog.kubedb.com             2022-05-07T03:45:57Z
mysqldatabases.schema.kubedb.com               2022-05-07T04:32:04Z
mysqls.kubedb.com                              2022-05-07T03:47:07Z
mysqlversions.catalog.kubedb.com               2022-05-07T03:45:57Z
perconaxtradbs.kubedb.com                      2022-05-07T03:47:07Z
perconaxtradbversions.catalog.kubedb.com       2022-05-07T03:45:57Z
pgbouncers.kubedb.com                          2022-05-07T03:47:07Z
pgbouncerversions.catalog.kubedb.com           2022-05-07T03:45:57Z
postgresdatabases.schema.kubedb.com            2022-05-07T04:32:05Z
postgreses.kubedb.com                          2022-05-07T03:47:07Z
postgresversions.catalog.kubedb.com            2022-05-07T03:45:57Z
proxysqls.kubedb.com                           2022-05-07T03:47:07Z
proxysqlversions.catalog.kubedb.com            2022-05-07T03:45:57Z
redises.kubedb.com                             2022-05-07T03:47:08Z
redissentinels.kubedb.com                      2022-05-07T03:47:08Z
redisversions.catalog.kubedb.com               2022-05-07T03:45:57Z

安装KubeDB插件

# 添加插件仓库
[root@node1 ~]# kubectl krew index add appscode https://github.com/appscode/krew-index.git
WARNING: You have added a new index from "https://github.com/appscode/krew-index.git"
The plugins in this index are not audited for security by the Krew maintainers.
Install them at your own risk.
[root@node1 ~]# kubectl krew index list
INDEX     URL
appscode  https://github.com/appscode/krew-index.git
default   https://github.com/kubernetes-sigs/krew-index.git
[root@node1 ~]# kubectl krew update
Updated the local copy of plugin index "appscode".
Updated the local copy of plugin index.

# 安装dba插件
[root@node1 ~]# kubectl krew install appscode/dba
[root@node1 ~]# kubectl dba version
Version = v0.26.0
VersionStrategy = tag
GitTag = v0.26.0
GitBranch = HEAD
CommitHash = 9df8f77740e65d70a8f7bc09f9d7970b285f72bb
CommitTimestamp = 2022-03-28T23:08:02
GoVersion = go1.18
Compiler = gcc
Platform = linux/amd64

使用Kube部署单实例Mysql

创建测试资源

[root@node1 test-kubedb]# cat >mysqldemo.yml<<'EOF'
apiVersion: kubedb.com/v1alpha2
kind: MySQL
metadata:
  name: mysql-quickstart
  namespace: demo
spec:
  version: "8.0.27"
  # Durable:需要提供storageclass | Ephemeral: 不需要提供storageclass,而是使用emptyDir
  storageType: Durable
  storage:

    # 填写自己的storageclass
    storageClassName: "nfs-storage"
    accessModes:
    - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
  terminationPolicy: DoNotTerminate
EOF

[root@node1 test-kubedb]# kubectl apply -f mysqldemo.yml 
mysql.kubedb.com/mysql-quickstart created

[root@node1 test-kubedb]# kubectl get -f mysqldemo.yml 
NAME               VERSION   STATUS   AGE
mysql-quickstart   8.0.27    Ready    126m

[root@node1 test-kubedb]# kubectl get statefulset -n demo
NAME               READY   AGE

mysql-quickstart   1/1     131m

[root@node1 test-kubedb]# kubectl get pvc -n demo
NAME                      STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE

data-mysql-quickstart-0   Bound    pvc-f92d2467-fcd5-42d2-a01c-7501dd70b292   1Gi        RWO            nfs-storage    131m

[root@node1 test-kubedb]# kubectl get pv -n demo
pvc-f92d2467-fcd5-42d2-a01c-7501dd70b292   1Gi        RWO            Delete           Bound       demo/data-mysql-quickstart-0   nfs-storage             132m

[root@node1 test-kubedb]# kubectl get pods mysql-quickstart-0 -n demo -o yaml | grep podIP:
    cni.projectcalico.org/podIP: 10.100.166.133/32
          f:cni.projectcalico.org/podIP: {}
        f:podIP: {}
  podIP: 10.100.166.133

# 查看用户
[root@node1 test-kubedb]# kubectl get secrets -n demo mysql-quickstart-auth -o jsonpath='{.data.\username}' | base64 -d
root
# 查看密码
[root@node1 test-kubedb]# kubectl get secrets -n demo mysql-quickstart-auth -o jsonpath='{.data.\password}' | base64 -d
YvzR6XCYG4WGHK8W

# 测试连接
[root@node1 test-kubedb]# kubectl exec -it -n demo mysql-quickstart-0 -- mysql -u root --password='YvzR6XCYG4WGHK8W' -e "show databases"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

[root@node1 test-kubedb]# kubectl exec -it -n demo mysql-quickstart-0 -- mysql -u root --password='YvzR6XCYG4WGHK8W' -e "select @@version"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+
| @@version |
+-----------+
| 8.0.27    |
+-----------+

清除资源

[root@node1 test-kubedb]# kubectl patch -n demo mysql/mysql-quickstart -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"
mysql.kubedb.com/mysql-quickstart patched
[root@node1 test-kubedb]# kubectl delete -n demo mysql/mysql-quickstart
mysql.kubedb.com "mysql-quickstart" deleted
[root@node1 test-kubedb]# 

创建Mysql MGR集群

[root@node1 test-kubedb]# cat >mysql-rc.yaml<<'EOF'
apiVersion: kubedb.com/v1alpha2
kind: MySQL
metadata:
  name: my-group
  namespace: demo
spec:
  version: "8.0.27"
  replicas: 3
  topology:
    mode: GroupReplication
    group:

      # 可在此处获得uuid: https://www.uuidgenerator.net/version1
      name: "dc002fc3-c412-4d18-b1d4-66c1fbfbbc9b"
  storageType: Durable
  storage:
    # 选择自己的storageclass
    storageClassName: "nfs-storage"
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
  terminationPolicy: WipeOut
EOF

[root@node1 test-kubedb]# kubectl create -f mysql-rc.yaml
mysql.kubedb.com/my-group created

[root@node1 test-kubedb]# kubectl get statefulset -n demo
NAME       READY   AGE
my-group   3/3     24s

[root@node1 test-kubedb]# kubectl get pvc -n demo
NAME              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-my-group-0   Bound    pvc-26bd9e26-e99f-4f0d-b629-9448d5dd6fc3   1Gi        RWO            nfs-storage    32s
data-my-group-1   Bound    pvc-217f7d53-73a3-4476-99fc-f128277414d8   1Gi        RWO            nfs-storage    27s
data-my-group-2   Bound    pvc-cbc0b462-6dc6-4ffb-9876-b1a360099de8   1Gi        RWO            nfs-storage    21s

[root@node1 test-kubedb]# kubectl get pv -n demo
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                  STORAGECLASS   REASON   AGE
pv-nfs                                     10Gi       RWX            Retain           Available                                                  23h
pv-nfs2                                    10Gi       RWX            Retain           Available                                                  23h
pvc-217f7d53-73a3-4476-99fc-f128277414d8   1Gi        RWO            Delete           Bound       demo/data-my-group-1   nfs-storage             34s
pvc-26bd9e26-e99f-4f0d-b629-9448d5dd6fc3   1Gi        RWO            Delete           Bound       demo/data-my-group-0   nfs-storage             39s
pvc-34069799-30d7-4a8d-8926-1c88afcd1581   100Mi      RWO            Delete           Bound       default/www-web-4      nfs-storage             25h
pvc-4cf0a4c9-244b-4c9d-a436-8a59510db08f   100Mi      RWO            Delete           Bound       default/www-web-0      nfs-storage             25h
pvc-918b16b3-d233-40ba-acd7-66fe14c9f748   100Mi      RWO            Delete           Bound       default/test-pvc       nfs-storage             26h
pvc-bfd0d983-09c5-4f7a-ab6d-e38c1cd509f5   100Mi      RWO            Delete           Bound       default/www-web-3      nfs-storage             25h
pvc-cb096dc0-ee04-45d9-bbc2-96f81fe0810a   100Mi      RWO            Delete           Bound       default/www-web-1      nfs-storage             25h
pvc-cbc0b462-6dc6-4ffb-9876-b1a360099de8   1Gi        RWO            Delete           Bound       demo/data-my-group-2   nfs-storage             28s
pvc-f5b968f7-4c74-484a-8e98-7d077cd94933   100Mi      RWO            Delete           Bound       default/www-web-2      nfs-storage             25h

[root@node1 test-kubedb]# kubectl get service -n demo
NAME               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
my-group           ClusterIP   10.96.52.232   <none>        3306/TCP   46s
my-group-pods      ClusterIP   None           <none>        3306/TCP   46s
my-group-standby   ClusterIP   10.96.18.78    <none>        3306/TCP   46s

测试连接

[root@node1 test-kubedb]# kubectl get secrets -n demo my-group-auth -o jsonpath='{.data.\username}' | base64 -d
root
[root@node1 test-kubedb]# kubectl get secrets -n demo my-group-auth -o jsonpath='{.data.\password}' | base64 -d
LnQ4xtbdhpYQ4Ymt

[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-0.my-group-pods.demo -e "select @@version"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+
| @@version |
+-----------+
| 8.0.27    |
+-----------+
[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-1.my-group-pods.demo -e "select @@version"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+
| @@version |
+-----------+
| 8.0.27    |
+-----------+
[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-2.my-group-pods.demo -e "select @@version"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------+
| @@version |
+-----------+
| 8.0.27    |
+-----------+

查看集群节点状态

[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-2.my-group-pods.demo -e "show status like '%primary%'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------------------------+--------------------------------------+
| Variable_name                    | Value                                |
+----------------------------------+--------------------------------------+
| group_replication_primary_member | b75c9436-cdd4-11ec-bdb2-36483d845b22 |
+----------------------------------+--------------------------------------+

[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-2.my-group-pods.demo -e "select * from performance_schema.replication_group_members"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------------+--------------------------------------+-----------------------------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST                       | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-----------------------------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | b1993ed2-cdd4-11ec-bd5b-6a4d8dc13980 | my-group-2.my-group-pods.demo.svc |        3306 | ONLINE       | SECONDARY   | 8.0.27         | XCom                       |
| group_replication_applier | b48465a7-cdd4-11ec-87d3-de8d51d3ee7d | my-group-1.my-group-pods.demo.svc |        3306 | ONLINE       | SECONDARY   | 8.0.27         | XCom                       |
| group_replication_applier | b75c9436-cdd4-11ec-bdb2-36483d845b22 | my-group-0.my-group-pods.demo.svc |        3306 | ONLINE       | PRIMARY     | 8.0.27         | XCom                       |
+---------------------------+--------------------------------------+-----------------------------------+-------------+--------------+-------------+----------------+----------------------------+

测试集群同步

# 创建测试数据
[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-0.my-group-pods.demo -e "CREATE DATABASE playground;"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-1.my-group-pods.demo -e "CREATE DATABASE playground;"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1290 (HY000) at line 1: The MySQL server is running with the --super-read-only option so it cannot execute this statement
command terminated with exit code 1

[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-0.my-group-pods.demo -e "CREATE TABLE playground.equipment ( id INT NOT NULL AUTO_INCREMENT, type VARCHAR(50), quant INT, color VARCHAR(25), PRIMARY KEY(id));" 
mysql: [Warning] Using a password on the command line interface can be insecure.

[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-0.my-group-pods.demo -e "INSERT INTO playground.equipment (type, quant, color) VALUES ('slide', 2, 'blue');"
mysql: [Warning] Using a password on the command line interface can be insecure.

# 测试在不同实例获取数据
[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-0.my-group-pods.demo -e "SELECT * FROM playground.equipment;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+-------+-------+
| id | type  | quant | color |
+----+-------+-------+-------+
|  1 | slide |     2 | blue  |
+----+-------+-------+-------+

[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-1.my-group-pods.demo -e "SELECT * FROM playground.equipment;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+-------+-------+
| id | type  | quant | color |
+----+-------+-------+-------+
|  1 | slide |     2 | blue  |
+----+-------+-------+-------+

[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-2.my-group-pods.demo -e "SELECT * FROM playground.equipment;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+-------+-------+
| id | type  | quant | color |
+----+-------+-------+-------+
|  1 | slide |     2 | blue  |
+----+-------+-------+-------+

测试故障切换

# 删除primary角色实例
[root@node1 test-kubedb]# kubectl delete pod my-group-0 -n demo
pod "my-group-0" deleted

# 查看集群实例状态,发现primary已经自动转移
[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-2.my-group-pods.demo -e "select * from performance_schema.replication_group_members"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------------+--------------------------------------+-----------------------------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST                       | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-----------------------------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | b1993ed2-cdd4-11ec-bd5b-6a4d8dc13980 | my-group-2.my-group-pods.demo.svc |        3306 | ONLINE       | PRIMARY     | 8.0.27         | XCom                       |
| group_replication_applier | b48465a7-cdd4-11ec-87d3-de8d51d3ee7d | my-group-1.my-group-pods.demo.svc |        3306 | ONLINE       | SECONDARY   | 8.0.27         | XCom                       |
| group_replication_applier | b75c9436-cdd4-11ec-bdb2-36483d845b22 | my-group-0.my-group-pods.demo.svc |        3306 | ONLINE       | SECONDARY   | 8.0.27         | XCom                       |
+---------------------------+--------------------------------------+-----------------------------------+-------------+--------------+-------------+----------------+----------------------------+

# 测试数据查询
[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-0.my-group-pods.demo -e "SELECT * FROM playground.equipment;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+-------+-------+
| id | type  | quant | color |
+----+-------+-------+-------+
|  1 | slide |     2 | blue  |
+----+-------+-------+-------+

[root@node1 test-kubedb]# kubectl exec -it -n demo my-group-0 -c mysql -- mysql -u root --password=LnQ4xtbdhpYQ4Ymt --host=my-group-1.my-group-pods.demo -e "SELECT * FROM playground.equipment;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-------+-------+-------+
| id | type  | quant | color |
+----+-------+-------+-------+
|  1 | slide |     2 | blue  |
+----+-------+-------+-------+

正文完
 236
xadocker
版权声明:本站原创文章,由 xadocker 2022-05-07发表,共计15485字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(一条评论)
Top-one 评论达人 LV.1
2022-05-24 12:48:45 回复

用来自助,还是不错滴 :smile:

 Windows  Chrome  中国广东省广州市电信