helm启动mysql-ha

image.png

前言
本文使用helm启动mysql-ha
*我的其他相关文章:
《docker-compose启动mysql》
《k8s启动单点mysql》
《helm启动单节点mysql》
《k8s启动phpmyadmin》

1. 下载cart包

  • 查找可用chart
# helm search repo mysql
NAME                       CHART VERSION   APP VERSION     DESCRIPTION
aliyuncs/mysql              6.8.0           8.0.19          Chart to create a Highly available MySQL cluster
aliyuncs/mysqldump           2.6.0           2.4.1           A Helm chart to help backup MySQL databases usi...
aliyuncs/mysqlha             1.0.0           5.7.13          MySQL cluster with a single master and zero or ...

说明: aliyuncs/mysql 这个是单机的, aliyuncs/mysqlha 这个是HA的

  • 下载chart
# helm fetch aliyuncs/mysqlha --untar --untardir ./

说明:加了两个参数,不下载tar包,直接是目录。

2. 修改配置文件

按需求修改 values.yaml 即可。

2.1 修改镜像(离线部署需要)

如果不能连接公网,将镜像下载后上传私有仓库,在配置文件中修改如下两处为私有仓库镜像。

mysqlImage: harbocto.xxx.com.cn/public/mysql:5.7.13
xtraBackupImage: harbocto.xxx.com.cn/public/gcr-xtrabackup:1.0

2.2 用户名密码

本次只修改了root密码,其他见下文说明

mysqlha:
  replicaCount: 3

  ## Password for MySQL root user
  ##
  # mysqlRootPassword: ## Default: random 10 character string
  mysqlRootPassword: Bxx888888

  ## Username/password for MySQL replication user
  ##
  mysqlReplicationUser: repl
  # mysqlReplicationPassword:

  ## Create a database user
  ##
  # mysqlUser:
  # mysqlPassword: ## Default: random 10 character string

  ## Allow unauthenticated access, uncomment to enable
  ##
  # mysqlAllowEmptyPassword: true

  ## Create database with name and grant all permissions to user on startup, if needed
  # mysqlDatabase:

  ## Configuration files for the master and slaves

说明:上边配置文件可见

  • root密码: mysqlRootPassword:
  • 普通用户: mysqlUser:
  • 普通用户密码: mysqlPassword
  • 新建数据库:mysqlDatabase:

2.3 修改存储大小

默认10g,此处修改为300g

persistence:
  enabled: true
  ## If defined, storageClassName: 
  ## If set to "-", storageClassName: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClassName spec is
  ##   set, choosing the default provisioner.  (gp2 on AWS, azure-disk on
  ##   Azure, standard on GKE, AWS & OpenStack)
  ##
  # storageClass: "-"
  accessModes:
  - ReadWriteOnce
  size: 300Gi

说明:因为我的k8s集群设定了默认storageclass,所以此处不用指定

3. 创建msyql集群

3.1 创建

[root@DoM01 mysqlha]# kubectl create namespace mysql
[root@DoM01 mysqlha]# cd mysqlha
[root@DoM01 mysqlha]# helm install mysqlha -n mysql ./

3.2 输出说明

创建完成输出如下:

NAME: mysqlha
LAST DEPLOYED: Mon Aug 16 17:16:16 2021
NAMESPACE: mysql
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The MySQL cluster is comprised of 3 MySQL pods: 1 master and 2 slaves. Each instance is accessible within the cluster through:

    .mysqlha

`mysqlha-0.mysqlha` is designated as the master and where all writes should be executed against. Read queries can be executed against the `mysqlha-readonly` service which distributes connections across all MySQL pods.

To connect to your database:

1. Obtain the root password:

    kubectl get secret --namespace mysql mysqlha -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo

2. Run a pod to use as a client:

    kubectl run mysql-client --image=harbocto.boe.com.cn/public/mysql:5.7.13 -it --rm --restart='Never' --namespace mysql -- /bin/sh

3. To connect to Master service (read/write):

    mysql -h mysqlha-0.mysqlha -u root -p

4. To connect to slave service (read-only):

   mysql -h mysqlha-readonly -u root -p

从输出我么可以看到如下几点:

  • 查看密码使用这个命令
kubectl get secret --namespace mysql mysqlha -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo
  • 写连接mysqlha-0.mysqlha
    从“3.2 查看”中,我们可以知道这是一个Handless的service
    因此 "pod_name"."service_name"可以直接访问到后端的指定pod
    我们这里的mysql-ha-mysqlha-0 即是我们的主库(写库)

  • 读连接mysqlha-readonly

3.3 查看

  • 查看pod
[root@DoM01 ~]# kubectl get pod -n mysql
NAME                          READY   STATUS    RESTARTS   AGE
mysqlha-0                     2/2     Running   0          81m
mysqlha-1                     2/2     Running   0          80m
mysqlha-2                     2/2     Running   0          80m

从 3.2的输出可以看到,mysqlha0是我们的master,其它两个是slave。

  • 查看service
[root@DoM01 ~]# kubectl get service -n mysql
NAME               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
mysqlha            ClusterIP   None                   3306/TCP         81m
mysqlha-readonly   NodePort    10.1.61.20             3306:30006/TCP   81m
  • mysqlha-readonly: 连接读库
  • mysqlha:
    经测试,如果直接连接,会随机分给后边的任何一个pod(一会儿master一会slave)
  • 连接写库:
    同namespace中的服务,连接 mysqlha-0.mysqlha:3306
    如果是其它namespace中的服务,需要访问mysqlha-0.mysqlha.mysql:3306
  • 查看PVC
[root@DoM01 mysqlha]# kubectl get pvc -n mysql
NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-mysqlha-0   Bound    pvc-1b5b3e8d-3106-4f87-a33e-18c36342f2ef   300Gi      RWO            nfs-client     89m
data-mysqlha-1   Bound    pvc-5ab9d655-0f20-4d17-80df-b25e0b58aabd   300Gi      RWO            nfs-client     88m
data-mysqlha-2   Bound    pvc-002914c3-d55c-42fd-bde7-a5aed1da3417   300Gi      RWO            nfs-client     88m

4. 修改端口

如果一开始你在value.yaml里修改了,这里就不用修改了。

4.1 修改只读端口

用edit命令修改即可

# kubectl get service -n mysql
NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
mysql-ha-mysqlha            ClusterIP   None                    3306/TCP         74m
mysql-ha-mysqlha-readonly   NodePort    172.21.12.189           3306:31008/TCP   74m

4.2 映射写库端口

mysql-ha-mysqlha 是一个Headless类型的service,我们可以通过它直接映射到后端的master,但是他不可以直接修改成NodePort,因此需要自己重新写了一个service

apiVersion: v1
kind: Service
metadata:
  labels:
    app: mysql-ha-mysqlha-m
  name: mysql-ha-mysqlha-m
  namespace: mysql
spec:
  ports:
  - name: mysql-ha-mysqlha-m
    nodePort: 31009
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: mysql-ha-mysqlha
  type: NodePort

查看结果如下

# kubectl get service -n mysql
NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
mysql-ha-mysqlha            ClusterIP   None                    3306/TCP         74m
mysql-ha-mysqlha-m          NodePort    172.21.13.8             3306:31009/TCP   4m46s
mysql-ha-mysqlha-readonly   NodePort    172.21.12.189           3306:31008/TCP   74m

你可能感兴趣的:(helm启动mysql-ha)