数据库
首页 > 数据库> > K8S中部署mysql-ha高可用集群

K8S中部署mysql-ha高可用集群

作者:互联网

作者:李毓

约定:
k8s:1.18
helm:v3
mysql:5.7.13
ceph:rbd模式

按照之前的教程,先添加好仓库

[root@adm-master ~]# helm repo list
NAME        URL                                                   
charts      https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
stable      http://mirror.azure.cn/kubernetes/charts              
aliyuncs    https://apphub.aliyuncs.com        
[root@adm-master ~]# helm pull aliyuncs/mysqlha
[root@adm-master ~]# tar -zxvf mysqlha-1.0.0.tgz
[root@adm-master mysqlha]# ls
Chart.yaml  OWNERS  README.md  templates  values.yaml

官方的模板里面有个坑

K8S中部署mysql-ha高可用集群

这里原本是没有的,要给他加上。

[root@adm-master mysqlha]# vim templates/statefulset.yaml 

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "fullname" . }}
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ template "mysqlha.chart" . }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  serviceName: {{ template "fullname" . }}
  replicas: {{ .Values.mysqlha.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "fullname" . }}

执行命令

[root@adm-master mysqlha]# helm install mysql . -f ./values.yaml
NAME: mysql
LAST DEPLOYED: Sat Mar 20 20:54:20 2021
NAMESPACE: default
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>.mysql-mysqlha

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

To connect to your database:

1. Obtain the root password: 

    kubectl get secret --namespace default 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=mysql:5.7.13 -it --rm --restart='Never' --namespace default -- /bin/sh

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

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

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

   mysql -h mysql-mysqlha-readonly -u root -p

K8S中部署mysql-ha高可用集群

有必要验证一下读写分离

先获取密码
K8S中部署mysql-ha高可用集群

进行base64反编码

[root@adm-master mysqlha]# echo -n MjIzRjBNTkFFV2hh | base64 --decode
223F0MNAEWha
[root@adm-master mysqlha]# echo -n a3FOdzZUbktGVjNq | base64 --decode
kqNw6TnKFV3j

进入容器

[root@adm-master mysqlha]# kubectl get pods -o wide
NAME                                     READY   STATUS    RESTARTS   AGE    IP             NODE        NOMINATED NODE   READINESS GATES
mysql-mysqlha-0                          2/2     Running   0          31m    10.244.2.227   adm-node2   <none>           <none>
mysql-mysqlha-1                          2/2     Running   0          30m    10.244.1.193   adm-node1   <none>           <none>
mysql-mysqlha-2                          2/2     Running   0          29m    10.244.2.228   adm-node2   <none>           <none>
nfs-client-provisioner-cc544b949-k8n2s   1/1     Running   29         109d   10.244.1.186   adm-node1   <none>           <none>
rbd-provisioner-c968dcb4b-wbhlc          1/1     Running   1          26h    10.244.1.187   adm-node1   <none>           <none>

[root@adm-master mysqlha]# kubectl exec -it mysql-mysqlha-0 sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
Defaulting container name to mysql.
Use 'kubectl describe pod/mysql-mysqlha-0 -n default' to see all of the containers in this pod.
# 
# mysql -h10.244.2.227 -uroot -pkqNw6TnKFV3j
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 203
Server version: 5.7.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> 

创建数据

mysql> show databases;
+------------------------+
| Database               |
+------------------------+
| information_schema     |
| mysql                  |
| performance_schema     |
| sys                    |
| xtrabackup_backupfiles |
+------------------------+
5 rows in set (0.00 sec)

mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.02 sec)

mysql> CREATE TABLE test.messages (message VARCHAR(250));
Query OK, 0 rows affected (0.06 sec)

mysql> INSERT INTO test.messages VALUES ('hello');
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+------------------------+
| Database               |
+------------------------+
| information_schema     |
| mysql                  |
| performance_schema     |
| sys                    |
| test                   |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.00 sec)

mysql> use test;
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> select * from messages;
+---------+
| message |
+---------+
| hello   |
+---------+
1 row in set (0.00 sec)

K8S中部署mysql-ha高可用集群

# mysql -uroot -h10.1.195.242 -pkqNw6TnKFV3j
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 265
Server version: 5.7.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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 databases ;
+------------------------+
| Database               |
+------------------------+
| information_schema     |
| mysql                  |
| performance_schema     |
| sys                    |
| test                   |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.01 sec)

标签:ha,--,root,master,mysql,adm,mysqlha,K8S
来源: https://blog.51cto.com/14783669/2667194