赞
踩
前言
1)本文使用helm启动mysql-ha
2)生产高可用集群: 《helm启动mysql-ha》
3)其他部署方式:《docker-compose启动mysql》、《helm启动单节点mysql》
4))web管理工具推荐 :《k8s启动phpmyadmin》
# 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的
# helm fetch aliyuncs/mysqlha --untar --untardir ./
说明:加了两个参数,不下载tar包,直接是目录。
按需求修改 values.yaml
即可。
如果不能连接公网,将镜像下载后上传私有仓库,在配置文件中修改如下两处为私有仓库镜像。
mysqlImage: harbocto.xxx.com.cn/public/mysql:5.7.13
xtraBackupImage: harbocto.xxx.com.cn/public/gcr-xtrabackup:1.0
本次只修改了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
说明:上边配置文件可见
默认10g,此处修改为300g
persistence:
enabled: true
## If defined, storageClassName: <storageClass>
## 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,所以此处不用指定
[root@DoM01 mysqlha]# kubectl create namespace mysql
[root@DoM01 mysqlha]# cd mysqlha
[root@DoM01 mysqlha]# helm install mysqlha -n mysql ./
创建完成输出如下:
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: <pod-name>.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
[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。
[root@DoM01 ~]# kubectl get service -n mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysqlha ClusterIP None <none> 3306/TCP 81m
mysqlha-readonly NodePort 10.1.61.20 <none> 3306/TCP 81m
- mysqlha-readonly: 连接读库,一会儿我们可以修改成nodeport让它对外提供服务。
- mysqlha:
经测试,如果直接连接,会随机分给后边的任何一个pod(一会儿master一会slave)- 连接写库:
同namespace中的服务,连接mysqlha-0.mysqlha:3306
,
如果是其它namespace中的服务,需要访问mysqlha-0.mysqlha.mysql:3306
如果是集群外部要用,我们将在后文4.2中创建一个写库的service。
[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
如果一开始你在value.yaml里修改了,这里就不用修改了。
用edit命令修改即可
[root@DoM01 ~]# kubectl get service -n mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysqlha ClusterIP None <none> 3306/TCP 81m
mysqlha-readonly NodePort 10.1.61.20 <none> 3306:30006/TCP 81m
为什么自己写一个service
mysql-ha-mysqlha 是一个Headless类型的service,
在k8s集群内部,我们可以通过它直接找到只读实例或者读写实例,
但是外部不可以访问,且它是不可以修改成nodeport类型的。所以我们自己写一个service。
为service选择pod的标签
从安装时的输出信息(本文3.2 )可知,mysqlha-0
这个pod是我们的读写库,因此我们service的selector
唯一指向是它即可
用edit命令我们可以看下mysqlha-0
metadata:
creationTimestamp: "2021-08-16T09:16:16Z"
generateName: mysqlha-
labels:
app: mysqlha
controller-revision-hash: mysqlha-86f767d8bd
statefulset.kubernetes.io/pod-name: mysqlha-0
分析三个label,只有statefulset.kubernetes.io/pod-name: mysqlha-0
可以唯一指向读写库,且不发生变化。
apiVersion: v1 kind: Service metadata: labels: app: mysqlha-m name: mysqlha-m namespace: mysql spec: ports: - name: mysql-ha-mysqlha-m nodePort: 31009 port: 3306 protocol: TCP targetPort: 3306 selector: statefulset.kubernetes.io/pod-name: mysqlha-0 type: NodePort
创建service
查看结果如下
[root@DoM01 mysql]# kubectl get service -n mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysqlha ClusterIP None <none> 3306/TCP 21h
mysqlha-m NodePort 10.1.105.237 <none> 3306:30007/TCP 22m
mysqlha-readonly NodePort 10.1.61.20 <none> 3306:30006/TCP 21h
30007即时我们的读写端口。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。