kubernetes生產(chǎn)實(shí)踐之mysql

簡(jiǎn)介

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

編寫(xiě)配置文件

mode: GroupReplication


group.name可以使用這個(gè)網(wǎng)站生成: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集群詳細(xì)信息

[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

從以上輸出可以看到缸兔,集群部署完畢。

驗(yàn)證數(shù)據(jù)庫(kù)

1吹艇、獲取root用戶(hù)名密碼

[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、現(xiàn)在可以使用獲取到的信息連接數(shù)據(jù)庫(kù)

[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受神、檢查集群狀態(tài)

[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抛猖、故障測(cè)試
我現(xiàn)在把PRIMARY的mysql-group-test-0 pod刪除,然后再查看集群信息,mysql-group-test-1會(huì)提升為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         |
+---------------------------+--------------------------------------+---------------------------------------------+-------------+--------------+-------------+----------------+

等待一段時(shí)間 mysql-group-test-0起來(lái)以后财著,角色就變成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         |
+---------------------------+--------------------------------------+---------------------------------------------+-------------+--------------+-------------+----------------+

注意:因?yàn)楸疚牟渴鹗鞘褂玫氖荊roupReplication模式(目前kubed只支持這種模式)联四,只能從PRIMARY節(jié)點(diǎn)寫(xiě)入數(shù)據(jù),所有節(jié)點(diǎn)都可以讀取數(shù)據(jù)撑教;如下圖所示朝墩。

image.png

如果想使用集群模式,參見(jiàn)以前的文章

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末驮履,一起剝皮案震驚了整個(gè)濱河市鱼辙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌玫镐,老刑警劉巖倒戏,帶你破解...
    沈念sama閱讀 216,744評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異恐似,居然都是意外死亡杜跷,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)矫夷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)葛闷,“玉大人,你說(shuō)我怎么就攤上這事双藕∈缰海” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,105評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵忧陪,是天一觀的道長(zhǎng)扣泊。 經(jīng)常有香客問(wèn)我,道長(zhǎng)嘶摊,這世上最難降的妖魔是什么延蟹? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,242評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮叶堆,結(jié)果婚禮上阱飘,老公的妹妹穿的比我還像新娘。我一直安慰自己虱颗,他們只是感情好沥匈,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評(píng)論 6 389
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著忘渔,像睡著了一般高帖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上辨萍,一...
    開(kāi)封第一講書(shū)人閱讀 51,215評(píng)論 1 299
  • 那天棋恼,我揣著相機(jī)與錄音,去河邊找鬼锈玉。 笑死爪飘,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的拉背。 我是一名探鬼主播师崎,決...
    沈念sama閱讀 40,096評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼椅棺!你這毒婦竟也來(lái)了犁罩?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,939評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤两疚,失蹤者是張志新(化名)和其女友劉穎床估,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體诱渤,經(jīng)...
    沈念sama閱讀 45,354評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡丐巫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了勺美。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片递胧。...
    茶點(diǎn)故事閱讀 39,745評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖赡茸,靈堂內(nèi)的尸體忽然破棺而出缎脾,到底是詐尸還是另有隱情,我是刑警寧澤占卧,帶...
    沈念sama閱讀 35,448評(píng)論 5 344
  • 正文 年R本政府宣布遗菠,位于F島的核電站,受9級(jí)特大地震影響屉栓,放射性物質(zhì)發(fā)生泄漏舷蒲。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評(píng)論 3 327
  • 文/蒙蒙 一友多、第九天 我趴在偏房一處隱蔽的房頂上張望牲平。 院中可真熱鬧,春花似錦域滥、人聲如沸纵柿。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,683評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)昂儒。三九已至,卻和暖如春委可,著一層夾襖步出監(jiān)牢的瞬間渊跋,已是汗流浹背腊嗡。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,838評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拾酝,地道東北人燕少。 一個(gè)月前我還...
    沈念sama閱讀 47,776評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像蒿囤,于是被迫代替她去往敵國(guó)和親客们。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容