Helm 创建高可用mysql集群

helm 安装 MySQL 1主2从#

1. 添加 bitnami 的仓库#

1
Copy$ helm repo add kubegemsapp https://charts.kubegems.io/kubegemsapp

2. 查询 MySQL 资源#

1
2
3
4
5
6
7
8
$ helm repo update
$ helm search repo mysql
NAME CHART VERSION APP VERSION DESCRIPTION
bitnami/mysql 8.9.6 8.0.29 MySQL is a fast, reliable, scalable, and easy t...
bitnami/phpmyadmin 10.0.1 5.1.3 phpMyAdmin is a free software tool written in P...
bitnami/mariadb 11.0.2 10.6.7 MariaDB is an open source, community-developed ...
bitnami/mariadb-cluster 1.0.2 10.2.14 DEPRECATED Chart to create a Highly available M...
bitnami/mariadb-galera 7.1.8 10.6.7 MariaDB Galera is a multi-primary database clus...

3. 拉取 MySQL chart 到本地#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ mkdir /root/mysql && cd /root/mysql
# 拉取 chart 到本地 /root/mysql 目录
$ helm pull kubegemsapp/mysql --version 4.5.2
$ tar -xvf mysql-4.5.2.tgz
$ cp mysql/values.yaml ./values.yaml
# 查看当前目录层级
$ tree -L 2
.
├── mysql
│ ├── Chart.yaml
│ ├── files
│ ├── README.md
│ ├── templates
│ ├── values-production.yaml
│ └── values.yaml
├── mysql-4.5.2.tgz
└── values.yaml

4. 对本地 values-test.yaml 修改#

  • 查看集群 storageclasses
1
2
3
4
5
$ kubectl get storageclasses.storage.k8s.io
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
openebs-device openebs.io/local Delete WaitForFirstConsumer false 34d
openebs-hostpath openebs.io/local Delete WaitForFirstConsumer false 34d
openebs-jiva-default jiva.csi.openebs.io Delete Immediate true 33d
  • 修改配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
$ cat values.yaml
nameOverride: mysql
fullnameOverride: mysql
image:
registry: registry.cn-beijing.aliyuncs.com
repository: kubegemsapp/mysql
tag: 5.7.26
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 3306
securityContext:
enabled: true
fsGroup: 1001
runAsUser: 1001
root:
password: Huazhu@2021
forcePassword: true
db:
user: huazhu
password: Huazhu@2021
name: my_database
forcePassword: true
replication:
enabled: true
#user: huazhu_replicator
user: root
password: Huazhu@2021
forcePassword: true
master:
antiAffinity: soft
updateStrategy:
type: RollingUpdate
persistence:
enabled: true
storageClass: "managed-nfs-storage"
mountPath: /bitnami/mysql
annotations:
accessModes:
- ReadWriteOnce
size: 5Gi
config: |-
[mysqld]
default_authentication_plugin=mysql_native_password
skip-name-resolve
explicit_defaults_for_timestamp
basedir=/opt/bitnami/mysql
port=3306
socket=/opt/bitnami/mysql/tmp/mysql.sock
tmpdir=/opt/bitnami/mysql/tmp
max_allowed_packet=16M
bind-address=0.0.0.0
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
[client]
port=3306
socket=/opt/bitnami/mysql/tmp/mysql.sock
default-character-set=UTF8
[manager]
port=3306
socket=/opt/bitnami/mysql/tmp/mysql.sock
pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
resources: {}
livenessProbe:
enabled: true
initialDelaySeconds: 120
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe:
enabled: true
initialDelaySeconds: 15
periodSeconds: 10
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
slave:
replicas: 1
antiAffinity: soft
updateStrategy:
type: RollingUpdate
persistence:
enabled: true
storageClass: "managed-nfs-storage"
mountPath: /bitnami/mysql
annotations:
accessModes:
- ReadWriteOnce
size: 5Gi
config: |-
[mysqld]
default_authentication_plugin=mysql_native_password
skip-name-resolve
explicit_defaults_for_timestamp
basedir=/opt/bitnami/mysql
port=3306
socket=/opt/bitnami/mysql/tmp/mysql.sock
tmpdir=/opt/bitnami/mysql/tmp
max_allowed_packet=16M
bind-address=0.0.0.0
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
[client]
port=3306
socket=/opt/bitnami/mysql/tmp/mysql.sock
default-character-set=UTF8
[manager]
port=3306
socket=/opt/bitnami/mysql/tmp/mysql.sock
pid-file=/opt/bitnami/mysql/tmp/mysqld.pid
resources: {}
livenessProbe:
enabled: true
initialDelaySeconds: 120
periodSeconds: 30
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe:
enabled: true
initialDelaySeconds: 15
periodSeconds: 30
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
metrics:
enabled: false
image:
registry: registry.cn-beijing.aliyuncs.com
repository: kubegemsapp/mysqld-exporter
tag: v0.10.0
pullPolicy: IfNotPresent
resources: {}
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9104"

6. 安装 MySQL 集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
helm install --namespace mysql mysql-ha -f ./values.yaml ./mysql --set auth.rootPassword=Huazhu@2021
NAME: mysql-cluster
LAST DEPLOYED: Mon May 9 01:54:38 2022
NAMESPACE: test-middleware
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Please be patient while the chart is being deployed
Tip:
Watch the deployment status using the command: kubectl get pods -w --namespace test-middleware
Services:
echo Master: mysql-cluster-mysql.test-middleware.svc.cluster.local:3306
echo Slave: mysql-cluster-mysql-slave.test-middleware.svc.cluster.local:3306
Administrator credentials:
echo Username: root
echo Password : $(kubectl get secret --namespace test-middleware mysql-cluster-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode)
To connect to your database:
1. Run a pod that you can use as a client:
kubectl run mysql-cluster-mysql-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:5.7.26 --namespace test-middleware --command -- bash
2. To connect to master service (read/write):
mysql -h mysql-cluster-mysql.test-middleware.svc.cluster.local -uroot -p my_database
3. To connect to slave service (read-only):
mysql -h mysql-cluster-mysql-slave.test-middleware.svc.cluster.local -uroot -p my_database
To upgrade this helm chart:
1. Obtain the password as described on the 'Administrator credentials' section and set the 'root.password' parameter as shown below:
ROOT_PASSWORD=$(kubectl get secret --namespace test-middleware mysql-cluster-mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode)
helm upgrade mysql-cluster bitnami/mysql --set root.password=$ROOT_PASSWORD

7. 查看部署的 MySQL 集群#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ helm -n mysql list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
mysql-cluster test-middleware 1 2022-05-09 01:54:38.848559008 -0400 EDT deployed mysql-4.5.2 5.7.26
$ kubectl -n mysql get pods -l app=mysql
NAME READY STATUS RESTARTS AGE
mysql-cluster-mysql-master-0 1/1 Running 0 16m
mysql-cluster-mysql-slave-0 1/1 Running 0 16m
mysql-cluster-mysql-slave-1 1/1 Running 0 14m
> mysql-cluster-mysql-master-0 为主,mysql-cluster-mysql-slave-0 和 mysql-cluster-mysql-slave-1 为从
> default名称空间如何访问此 MySQL 集群
> MySQL主节点:mysql-cluster-mysql.test-middleware
> MySQL从节点0:mysql-cluster-mysql-slave-0.mysql-cluster-mysql-slave.test-middleware
> MySQL从节点1:mysql-cluster-mysql-slave-1.mysql-cluster-mysql-slave.test-middleware
  • 查看服务使用的 storageclass
1
2
3
4
5
6
7
8
9
10
11
12
13
Copy# 查看 pvc
$ kubectl -n test-middleware get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-mysql-cluster-mysql-master-0 Bound pvc-b9a1d1ca-44d3-4292-af45-e6f3b3063395 8Gi RWO openebs-jiva-default 31m
data-mysql-cluster-mysql-slave-0 Bound pvc-0d234b12-26eb-4e07-9dc0-ef9f0230e9fa 8Gi RWO openebs-jiva-default 31m
data-mysql-cluster-mysql-slave-1 Bound pvc-16531f4b-41ac-4a04-9d90-04b92aab7b49 8Gi RWO openebs-jiva-default 29m
# 查看 pv
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-b9a1d1ca-44d3-4292-af45-e6f3b3063395 8Gi RWO Delete Bound test-middleware/data-mysql-cluster-mysql-master-0 openebs-jiva-default 33m
pvc-0d234b12-26eb-4e07-9dc0-ef9f0230e9fa 8Gi RWO Delete Bound test-middleware/data-mysql-cluster-mysql-slave-0 openebs-jiva-default 33m
pvc-16531f4b-41ac-4a04-9d90-04b92aab7b49 8Gi RWO Delete Bound test-middleware/data-mysql-cluster-mysql-slave-1 openebs-jiva-default 31m

8. 连接 MySQL 集群 验证服务#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Copy# 获取 MySQL 集群的密码
$ kubectl get secret --namespace mysql mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode
Huazhu@2021
# 启动一个临时容器
$ kubectl run mysql-cluster-mysql-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:5.7.26 --namespace mysql --command -- bash
## 登陆 MySQL Master节点
$ mysql -h mysql.mysql -uroot -p
Enter password: # Huazhu@2021
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| my_database |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
# 查看主从状态
# 查看File和Position的值,在从库配置中会显示。
> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
## 登陆从库,查看主从同步状态
$ mysql -h mysql-mysql-slave.mysql -uroot -p
Enter password: # root123
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: mysql-cluster-mysql
Master_User: replicator
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000002 # File: mysql-bin.000002
Read_Master_Log_Pos: 154 # Position: 154
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
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: 154
Relay_Log_Space: 2236
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: 641
Master_UUID: aa7a516b-cf5c-11ec-b974-a2ee403fe88f
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