kubernetes生产实践之mysql

简介

kubedb mysql 生命周期及特性


image.png
Supported MySQL Features
Features    Availability
Clustering  ✓
Persistent Volume   ✓
Instant Backup  ✓
Scheduled Backup    ✓
Initialize using Snapshot   ✓
Initialize using Script (*.sql, *sql.gz and/or *.sh)    ✓
Custom Configuration    ✓
Using Custom docker image   ✓
Builtin Prometheus Discovery    ✓
Using Prometheus operator   ✓

查看kubedb支持的mysql版本

[root@qd01-stop-k8s-master001 mysql]# kubectl get mysqlversions
NAME        VERSION   DB_IMAGE                 DEPRECATED   AGE
5.7.25-v1   5.7.25    kubedb/mysql:5.7.25-v1                17h
5.7.29      5.7.29    kubedb/mysql:5.7.29                   17h
5.7.31      5.7.31    kubedb/mysql:5.7.31                   17h
8.0.14-v1   8.0.14    kubedb/mysql:8.0.14-v1                17h
8.0.20      8.0.20    kubedb/mysql:8.0.20                   17h
8.0.21      8.0.21    kubedb/mysql:8.0.21                   17h
8.0.3-v1    8.0.3     kubedb/mysql:8.0.3-v1                 17h

编写配置文件

mode: GroupReplication


group.name可以使用这个网站生成:https://www.uuidgenerator.net/version4

apiVersion: kubedb.com/v1alpha2
kind: MySQL
metadata:
  name: mysql-group-test
  namespace: op
spec:
  version: "8.0.21"
  replicas: 3
  topology:
    mode: GroupReplication
    group:
      name: "d7f38430-d9ee-464b-af43-da9efa26fe02"
      baseServerID: 100
  storageType: Durable
  storage:
    storageClassName: "rbd"
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 50Gi
  terminationPolicy: Halt

安装Mysql

[root@qd01-stop-k8s-master001 mysql]# kubectl apply -f mysql-cluster-install.yaml
mysql.kubedb.com/mysql-group-test created

[root@qd01-stop-k8s-master001 mysql]# kubectl get po,ep,svc -n op
NAME                     READY   STATUS    RESTARTS   AGE
pod/mysql-group-test-0   2/2     Running   0          14m
pod/mysql-group-test-1   2/2     Running   0          9m41s
pod/mysql-group-test-2   2/2     Running   0          4m19s

NAME                                 ENDPOINTS                                                     AGE
endpoints/mysql-group-test           100.64.147.157:3306                                           14m
endpoints/mysql-group-test-pods      100.64.122.198:3306,100.64.147.157:3306,100.98.174.219:3306   14m
endpoints/mysql-group-test-standby   100.98.174.219:3306                                           14m

NAME                               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/mysql-group-test           ClusterIP   10.108.25.179   <none>        3306/TCP   14m
service/mysql-group-test-pods      ClusterIP   None            <none>        3306/TCP   14m
service/mysql-group-test-standby   ClusterIP   10.101.164.49   <none>        3306/TCP   14m

可以使用kubectl describe查看mysql集群详细信息

[root@qd01-stop-k8s-master001 mysql]# kubectl describe mysql mysql-group-test -n op
Name:         mysql-group-test
Namespace:    op
Labels:       <none>
Annotations:  <none>
API Version:  kubedb.com/v1alpha2
Kind:         MySQL
Metadata:
  Creation Timestamp:  2021-03-11T02:03:38Z
  Finalizers:
    kubedb.com
  Generation:  2
  Managed Fields:
    API Version:  kubedb.com/v1alpha2
    Fields Type:  FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .:
          f:kubectl.kubernetes.io/last-applied-configuration:
      f:spec:
        .:
        f:replicas:
        f:storage:
          .:
          f:accessModes:
          f:resources:
            .:
            f:requests:
              .:
              f:storage:
          f:storageClassName:
        f:storageType:
        f:terminationPolicy:
        f:topology:
          .:
          f:group:
            .:
            f:baseServerID:
            f:name:
          f:mode:
        f:version:
    Manager:      kubectl-client-side-apply
    Operation:    Update
    Time:         2021-03-11T02:03:38Z
    API Version:  kubedb.com/v1alpha2
    Fields Type:  FieldsV1
    fieldsV1:
      f:metadata:
        f:finalizers:
      f:spec:
        f:authSecret:
          .:
          f:name:
      f:status:
        .:
        f:conditions:
        f:observedGeneration:
        f:phase:
    Manager:         operator
    Operation:       Update
    Time:            2021-03-11T02:03:39Z
  Resource Version:  7283091
  UID:               38d0b2c8-2d56-4fcf-ba3b-dff211d8a63d
Spec:
  Auth Secret:
    Name:  mysql-group-test-auth
  Pod Template:
    Controller:
    Metadata:
    Spec:
      Affinity:
        Pod Anti Affinity:
          Preferred During Scheduling Ignored During Execution:
            Pod Affinity Term:
              Label Selector:
                Match Labels:
                  app.kubernetes.io/instance:    mysql-group-test
                  app.kubernetes.io/managed-by:  kubedb.com
                  app.kubernetes.io/name:        mysqls.kubedb.com
              Namespaces:
                op
              Topology Key:  kubernetes.io/hostname
            Weight:          100
            Pod Affinity Term:
              Label Selector:
                Match Labels:
                  app.kubernetes.io/instance:    mysql-group-test
                  app.kubernetes.io/managed-by:  kubedb.com
                  app.kubernetes.io/name:        mysqls.kubedb.com
              Namespaces:
                op
              Topology Key:  failure-domain.beta.kubernetes.io/zone
            Weight:          50
      Resources:
        Limits:
          Cpu:     500m
          Memory:  1Gi
        Requests:
          Cpu:               500m
          Memory:            1Gi
      Service Account Name:  mysql-group-test
  Replicas:                  3
  Storage:
    Access Modes:
      ReadWriteOnce
    Resources:
      Requests:
        Storage:         50Gi
    Storage Class Name:  rbd
  Storage Type:          Durable
  Termination Policy:    Halt
  Topology:
    Group:
      Base Server ID:  100
      Name:            d7f38430-d9ee-464b-af43-da9efa26fe02
    Mode:              GroupReplication
  Version:             8.0.21
Status:
  Conditions:
    Last Transition Time:  2021-03-11T02:03:39Z
    Message:               The KubeDB operator has started the provisioning of MySQL: op/mysql-group-test
    Reason:                DatabaseProvisioningStartedSuccessfully
    Status:                True
    Type:                  ProvisioningStarted
    Last Transition Time:  2021-03-11T02:21:17Z
    Message:               All desired replicas are ready.
    Reason:                AllReplicasReady
    Status:                True
    Type:                  ReplicaReady
    Last Transition Time:  2021-03-11T02:03:49Z
    Message:               The MySQL: op/mysql-group-test is accepting client requests.
    Observed Generation:   2
    Reason:                DatabaseAcceptingConnectionRequest
    Status:                True
    Type:                  AcceptingConnection
    Last Transition Time:  2021-03-11T02:21:17Z
    Message:               The MySQL: op/mysql-group-test is ready.
    Observed Generation:   2
    Reason:                ReadinessCheckSucceeded
    Status:                True
    Type:                  Ready
    Last Transition Time:  2021-03-11T02:21:18Z
    Message:               The MySQL: op/mysql-group-test is successfully provisioned.
    Observed Generation:   2
    Reason:                DatabaseSuccessfullyProvisioned
    Status:                True
    Type:                  Provisioned
  Observed Generation:     2
  Phase:                   Ready
Events:
  Type    Reason      Age    From             Message
  ----    ------      ----   ----             -------
  Normal  Successful  17m    KubeDB Operator  Successfully created governing service
  Normal  Successful  17m    KubeDB Operator  Successfully created service for primary/standalone
  Normal  Successful  17m    KubeDB Operator  Successfully created service for secondary replicas
  Normal  Successful  17m    KubeDB Operator  Successfully created database auth secret
  Normal  Successful  17m    KubeDB Operator  Successfully created StatefulSet
  Normal  Successful  17m    KubeDB Operator  Successfully created appbinding
  Normal  Successful  17m    KubeDB Operator  Successfully patched StatefulSet
  Normal  Successful  17m    KubeDB Operator  Successfully patched StatefulSet
  Normal  Successful  13m    KubeDB Operator  Successfully patched StatefulSet
  Normal  Successful  11m    KubeDB Operator  Successfully patched StatefulSet
  Normal  Successful  7m39s  KubeDB Operator  Successfully patched StatefulSet
  Normal  Successful  102s   KubeDB Operator  Successfully patched StatefulSet
  Normal  Successful  16s    KubeDB Operator  Successfully patched StatefulSet
  Normal  Successful  16s    KubeDB Operator  Successfully patched StatefulSet
  Normal  Successful  16s    KubeDB Operator  Successfully patched StatefulSet

从以上输出可以看到,集群部署完毕。

验证数据库

1、获取root用户名密码

[root@qd01-stop-k8s-master001 mysql]# kubectl get secrets -n op mysql-group-test-auth  -o jsonpath='{.data.\username}' | base64 -d
root
[root@qd01-stop-k8s-master001 mysql]# kubectl get secrets -n op mysql-group-test-auth  -o jsonpath='{.data.\password}' | base64 -d
8(wVGGRfF4iQq1Zt

2、查看mysql pod 列表

[root@qd01-stop-k8s-master001 mysql]# kubectl get pods -n op -l app.kubernetes.io/instance=mysql-group-test  -o jsonpath='{range.items[*]}{.metadata.name} ........... {.status.podIP} ............ {.metadata.name}.my-group-gvr.{.metadata.namespace}{"\t\n"}{end}'
mysql-group-test-0 ........... 100.64.147.157 ............ mysql-group-test-0.my-group-gvr.op
mysql-group-test-1 ........... 100.98.174.219 ............ mysql-group-test-1.my-group-gvr.op
mysql-group-test-2 ........... 100.64.122.198 ............ mysql-group-test-2.my-group-gvr.op

[root@qd01-stop-k8s-master001 mysql]# kubectl get svc -n op
NAME                       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
mysql-group-test           ClusterIP   10.108.25.179   <none>        3306/TCP   43m
mysql-group-test-pods      ClusterIP   None            <none>        3306/TCP   43m
mysql-group-test-standby   ClusterIP   10.101.164.49   <none>        3306/TCP   43m

3、现在可以使用获取到的信息连接数据库

[root@qd01-stop-k8s-master001 mysql]# kubectl exec -it -n op mysql-group-test-0 -c mysql -- mysql -u root --password="8(wVGGRfF4iQq1Zt"  --host=10.108.25.179  -e "select 1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---+
| 1 |
+---+
| 1 |
+---+

4、检查集群状态

[root@qd01-stop-k8s-master001 mysql]# kubectl exec -it -n op mysql-group-test-0 -c mysql -- mysql -u root --password="8(wVGGRfF4iQq1Zt"  --host=10.108.25.179  -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 | 596744e1-820e-11eb-8425-f2f48f05462c |
+----------------------------------+--------------------------------------+

[root@qd01-stop-k8s-master001 mysql]# kubectl exec -it -n op mysql-group-test-0 -c mysql -- mysql -u root --password="8(wVGGRfF4iQq1Zt"  --host=10.108.25.179  -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 |
+---------------------------+--------------------------------------+---------------------------------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 18b45cbd-820f-11eb-8060-9a70fb141a1f | mysql-group-test-1.mysql-group-test-pods.op |        3306 | ONLINE       | SECONDARY   | 8.0.21         |
| group_replication_applier | 1dd5699c-8210-11eb-b13a-9a07d15a7e1f | mysql-group-test-2.mysql-group-test-pods.op |        3306 | ONLINE       | SECONDARY   | 8.0.21         |
| group_replication_applier | 596744e1-820e-11eb-8425-f2f48f05462c | mysql-group-test-0.mysql-group-test-pods.op |        3306 | ONLINE       | PRIMARY     | 8.0.21         |
+---------------------------+--------------------------------------+---------------------------------------------+-------------+--------------+-------------+----------------+

5、故障测试
我现在把PRIMARY的mysql-group-test-0 pod删除,然后再查看集群信息,mysql-group-test-1会提升为PRIMARY

[root@qd01-stop-k8s-master001 ~]# kubectl delete po  mysql-group-test-0 -n op
[root@qd01-stop-k8s-master001 ~]# kubectl exec -it -n op mysql-group-test-0 -n op -c mysql -- mysql -u root --password="8(wVGGRfF4iQq1Zt"  --host=10.108.25.179  -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 |
+---------------------------+--------------------------------------+---------------------------------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 18b45cbd-820f-11eb-8060-9a70fb141a1f | mysql-group-test-1.mysql-group-test-pods.op |        3306 | ONLINE       | PRIMARY     | 8.0.21         |
| group_replication_applier | 1dd5699c-8210-11eb-b13a-9a07d15a7e1f | mysql-group-test-2.mysql-group-test-pods.op |        3306 | ONLINE       | SECONDARY   | 8.0.21         |
+---------------------------+--------------------------------------+---------------------------------------------+-------------+--------------+-------------+----------------+

等待一段时间 mysql-group-test-0起来以后,角色就变成SECONDARY
[root@qd01-stop-k8s-master001 ~]# kubectl exec -it -n op mysql-group-test-0 -c mysql -- mysql -u root --password="8(wVGGRfF4iQq1Zt"  --host=10.108.25.179  -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 |
+---------------------------+--------------------------------------+---------------------------------------------+-------------+--------------+-------------+----------------+
| group_replication_applier | 18b45cbd-820f-11eb-8060-9a70fb141a1f | mysql-group-test-1.mysql-group-test-pods.op |        3306 | ONLINE       | PRIMARY     | 8.0.21         |
| group_replication_applier | 1dd5699c-8210-11eb-b13a-9a07d15a7e1f | mysql-group-test-2.mysql-group-test-pods.op |        3306 | ONLINE       | SECONDARY   | 8.0.21         |
| group_replication_applier | 596744e1-820e-11eb-8425-f2f48f05462c | mysql-group-test-0.mysql-group-test-pods.op |        3306 | ONLINE       | SECONDARY   | 8.0.21         |
+---------------------------+--------------------------------------+---------------------------------------------+-------------+--------------+-------------+----------------+

注意:因为本文部署是使用的是GroupReplication模式(目前kubed只支持这种模式),只能从PRIMARY节点写入数据,所有节点都可以读取数据;如下图所示。

image.png

如果想使用集群模式,参见以前的文章

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349

推荐阅读更多精彩内容