Helm部署mysql k8s主从集群

helm是k8s的包管理器,使用helm部署能简化部署过程中需要编写的Yaml文件,本文使用bitnami的helm仓库部署mysql一主二从集群。

前提条件:一个harbor docker私服和一个k8s集群、一个可用的storage Class

添加bitnami仓库并查找

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
​
[kmning@k8s-register-node ~]$ helm search repo mysql
NAME                    CHART VERSION   APP VERSION     DESCRIPTION
bitnami/mysql           9.8.1           8.0.33          MySQL is a fast, reliable, scalable, and easy t...
bitnami/phpmyadmin      11.0.0          5.2.1           phpMyAdmin is a free software tool written in P...
bitnami/mariadb         12.1.2          10.11.2         MariaDB is an open source, community-developed ...
bitnami/mariadb-galera  8.1.1           10.11.2         MariaDB Galera is a multi-primary database clus...

选择合适的版本

软件 版本
chart 9.8.1
mysql 5.7.26

拉取chat到本地

helm pull bitnami/mysql --version 9.8.1
​
tar -zxvf mysql-9.8.1.tgz
cp mysql/values.yaml ./values-mysql.yaml

修改镜像地址等配置

global:
  imageRegistry: "k8s-register-node.com:443"
  imagePullSecrets: []
  storageClass: "managed-nfs-storage"
  
#所有用到image的地方改成私服
metrics:
  enabled: false
  image:
    registry: k8s-register-node.com:443
    repository: lib-proxy/bitnami/mysqld-exporter
    tag: 0.14.0-debian-11-r112

mysql版本设置

image:
  registry: k8s-register-node.com:443
  repository: lib-proxy/bitnami/mysql
  tag: 5.7.42-debian-11-r5

账号密码以及开启主从模式

architecture: replication
auth:
  rootPassword: "rootpwd"
  createDatabase: true
  database: "my_database"
  username: "kmning"
  password: "kmningpwd"
  replicationUser: kmning
  replicationPassword: "kmningpwd"

主节点

primary:
  name: primary
  command: []
  args: []
  lifecycleHooks: {}
  hostAliases: []
  configuration: |-
    [mysqld]
    default_authentication_plugin=mysql_native_password
    skip-name-resolve
    explicit_defaults_for_timestamp
    basedir=/opt/bitnami/mysql
    plugin_dir=/opt/bitnami/mysql/lib/plugin
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    datadir=/bitnami/mysql/data
    tmpdir=/opt/bitnami/mysql/tmp
    max_allowed_packet=16M
    bind-address=*
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
    log-error=/opt/bitnami/mysql/logs/mysqld.log
    character-set-server=UTF8
    collation-server=utf8_general_ci
    slow_query_log=0
    slow_query_log_file=/opt/bitnami/mysql/logs/mysqld.log
    long_query_time=10.0

    [client]
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    default-character-set=UTF8
    plugin_dir=/opt/bitnami/mysql/lib/plugin

    [manager]
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
  existingConfigmap: ""
  updateStrategy:
    type: RollingUpdate
  persistence:
    enabled: true
    existingClaim: ""
    subPath: ""
    storageClass: "managed-nfs-storage"
    annotations: {}
    accessModes:
      - ReadWriteOnce
    size: 200Gi
    selector: {}
  extraVolumes: []

从节点

secondary:
  name: secondary
  replicaCount: 2
  hostAliases: []
  command: []
  args: []
  lifecycleHooks: {}
  configuration: |-
    [mysqld]
    default_authentication_plugin=mysql_native_password
    skip-name-resolve
    explicit_defaults_for_timestamp
    basedir=/opt/bitnami/mysql
    plugin_dir=/opt/bitnami/mysql/lib/plugin
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    datadir=/bitnami/mysql/data
    tmpdir=/opt/bitnami/mysql/tmp
    max_allowed_packet=16M
    bind-address=*
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
    log-error=/opt/bitnami/mysql/logs/mysqld.log
    character-set-server=UTF8
    collation-server=utf8_general_ci
    slow_query_log=0
    slow_query_log_file=/opt/bitnami/mysql/logs/mysqld.log
    long_query_time=10.0

    [client]
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    default-character-set=UTF8
    plugin_dir=/opt/bitnami/mysql/lib/plugin

    [manager]
    port=3306
    socket=/opt/bitnami/mysql/tmp/mysql.sock
    pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
  existingConfigmap: ""
  persistence:
    enabled: true
    existingClaim: ""
    subPath: ""
    storageClass: "managed-nfs-storage"
    annotations: {}
    accessModes:
      - ReadWriteOnce
    size: 8Gi
    selector: {}
  extraVolumes: []

helm安装redis集群

kubectl create ns mysql
helm -n mysql install mysql-cluster  mysql-9.8.1.tgz  -f values-mysql.yaml \
--set useBundledSystemChart=true

安装后打印

kmning@k8s-master-1:~/mysql-k8s-cluster$ helm -n mysql install mysql-cluster  mysql-9.8.1.tgz  -f values-mysql.yaml \
> --set useBundledSystemChart=true
NAME: mysql-cluster
LAST DEPLOYED: Thu May  4 05:48:32 2023
NAMESPACE: mysql
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: mysql
CHART VERSION: 9.8.1
APP VERSION: 8.0.33

** Please be patient while the chart is being deployed **

Tip:

  Watch the deployment status using the command: kubectl get pods -w --namespace mysql

Services:

  echo Primary: mysql-cluster-primary.mysql.svc.cluster.local:3306
  echo Secondary: mysql-cluster-secondary.mysql.svc.cluster.local:3306

Execute the following to get the administrator credentials:

  echo Username: root
  MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace mysql mysql-cluster -o jsonpath="{.data.mysql-root-password}" | base64 -d)

To connect to your database:

  1. Run a pod that you can use as a client:

      kubectl run mysql-cluster-client --rm --tty -i --restart='Never' --image  k8s-register-node.com:443/lib-proxy/bitnami/mysql:5.7.42-debian-11-r5 --namespace mysql --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash

  2. To connect to primary service (read/write):

      mysql -h mysql-cluster-primary.mysql.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"

  3. To connect to secondary service (read-only):

      mysql -h mysql-cluster-secondary.mysql.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"

上述打印提示了如何获取root密码和如何连接k8s集群上mysql主从的信息。明显,使用helm部署mysql主从,我们避免了编辑有状态集配置文件,Service配置文件,连主从设置也不需要我们处理。

观察主节点和从节点的pod是否正确启动,如果发现从节点不能正确启动,应该是用户没有远程登录权限,进入主节点开启即可。

 kubectl exec -it mysql-cluster-primary-0 -n mysql -- bash
I have no name!@mysql-cluster-primary-0:/$ mysql -h mysql-cluster-primary.mysql.svc.cluster.local -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 311
Server version: 5.7.42-log MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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> use mysql;
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> GRANT ALL PRIVILEGES ON *.* TO 'kmning'@'%' IDENTIFIED BY 'kmningpwd' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'rootpwd' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

查看从节点主从同步状态

kubectl exec -it mysql-cluster-secondary-0 -n mysql -- bash
I have no name!@mysql-cluster-secondary-0:/$ mysql -uroot -h mysql-cluster-secondary.mysql -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1118
Server version: 5.7.42-log MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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 slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-cluster-primary
                  Master_User: kmning
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 2632
               Relay_Log_File: mysql-relay-bin.000019
                Relay_Log_Pos: 2845
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2632
              Relay_Log_Space: 3265
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 124
                  Master_UUID: 6f965f32-ea3f-11ed-869f-2a649e004961
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

可见,主从正常。应用连接mysql直接使用主节点即可。连接主节点使用服务名进行连接,如下

jdbc:mysql://mysql-cluster-primary.mysql.svc.cluster.local:3306/yourdb?rewriteBatchedStatements=true&characterEncoding=UTF-8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&characterSetResults=UTF-8&serverTimezone=Asia/Shanghai

你可能感兴趣的:(kubernetes,mysql,mysql,kubernetes,数据库,helm)