准备K8s部署MySQL的yaml文件

本文部署以概述中图1组网方式为例,3个MySQL Pod分别部署在3个计算节点上,外部client访问K8s集群内部的3个MySQL Pod有2种方式:

  • 本文部署Kube-OVN绑定使用enp3s0的网口名(10GE网卡)以提升网络带宽(避免使用默认网关的板载网卡),为实现该绑网口方式部署Kube-OVN,需要在安装Kube-OVN前修改install.sh的参数,指定IFACE参数为"enp3s0",详细可见安装Kube-OVN 1.2.1中install.sh文件的IFACE参数。
  • 由于不使用网络存储服务,选择使用物理机本地存储MySQL的数据及配置文件,所以要求MySQL Pod必须部署在指定的物理机上,避免MySQL Pod被K8s自动调度到没有数据和配置文件的其他计算节点上。

本文以部署3个MySQL Pod为例,在K8s的主节点物理机上编写部署yaml(例如文件名称为mysql_deployment.yaml)配置文件。

yaml文件下载链接:https://mirrors.huaweicloud.com/kunpeng/archive/kunpeng_solution/database/scripts/mysql_deployment.yaml

下面yaml文件编辑内容和上面链接中下载的一致,为了方便可以直接通过链接下载。

1
vim mysql_deployment.yaml

编辑内容如下:

  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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
apiVersion: v1
kind: Namespace
metadata:
   name: ns-mysql-test
   labels:
     name: ns-mysql-test
---
apiVersion: kubeovn.io/v1
kind: Subnet
metadata:
  name: ns-mysql-test
spec:
  protocol: IPv4
  cidrBlock: 10.99.0.0/16
  excludeIps:
  - 10.99.0.1..10.99.0.10
  gateway: 10.99.0.1
  namespaces:
  - ns-mysql-test
  private: false
  gatewayType: distributed
  natOutgoing: false
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: mysql-1
  name: mysql-1
  namespace: ns-mysql-test
  annotations:
    ovn.kubernetes.io/ip_address: 10.99.0.15
    ovn.kubernetes.io/mac_address: 00:00:00:53:6B:B6
spec:
  nodeSelector:
    test: "mysql-test-1"
  hostAliases:
  - ip: "10.99.0.15"
    hostnames:
    - "mysql-1"
  - ip: "10.99.0.16"
    hostnames:
    - "mysql-2"
  - ip: "10.99.0.17"
    hostnames:
    - "mysql-3"
  containers:
  - name: mysql-1
    image: mymysql/centos8-mysql-arm:8.0.19
    resources:
      limits:
        cpu: 16
        memory: 64Gi
    ports:
    - name: mysql-port
      containerPort: 3306
    - name: copy-port
      containerPort: 33061
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: test123
    - name: MYSQL_ROOT_HOST
      value: "%"
    volumeMounts:
    - name: mysql-data
      mountPath: "/data/mysql/data"
    - name: mysql-log
      mountPath: "/data/mysql/log"
    - name: mysql-run
      mountPath: "/data/mysql/run"
    - name: mysql-tmp
      mountPath: "/data/mysql/tmp"
    - name: mysql-cnf
      mountPath: "/etc/my.cnf"
  volumes:
  - name: mysql-data
    hostPath:
      path: "/data/mysql/mysql_1/data"
      type: DirectoryOrCreate
  - name: mysql-log
    hostPath:
      path: "/data/mysql/mysql_1/log"
      type: DirectoryOrCreate
  - name: mysql-run
    hostPath:
      path: "/data/mysql/mysql_1/run"
      type: DirectoryOrCreate
  - name: mysql-tmp
    hostPath:
      path: "/data/mysql/mysql_1/tmp"
      type: DirectoryOrCreate
  - name: mysql-cnf
    hostPath:
      path: "/data/mysql/mysql_1/mysql_arm.cnf"
      type: FileOrCreate
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: mysql-2
  name: mysql-2
  namespace: ns-mysql-test
  annotations:
    ovn.kubernetes.io/ip_address: 10.99.0.16
    ovn.kubernetes.io/mac_address: 00:00:00:53:6B:B7
spec:
  nodeSelector:
    test: "mysql-test-2"
  hostAliases:
  - ip: "10.99.0.15"
    hostnames:
    - "mysql-1"
  - ip: "10.99.0.16"
    hostnames:
    - "mysql-2"
  - ip: "10.99.0.17"
    hostnames:
    - "mysql-3"
  containers:
  - name: mysql-2
    image: mymysql/centos8-mysql-arm:8.0.19
    resources:
      limits:
        cpu: 16
        memory: 64Gi
    ports:
    - name: mysql-port
      containerPort: 3306
    - name: copy-port
      containerPort: 33061
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: test123
    - name: MYSQL_ROOT_HOST
      value: "%"
    volumeMounts:
    - name: mysql-data
      mountPath: "/data/mysql/data"
    - name: mysql-log
      mountPath: "/data/mysql/log"
    - name: mysql-run
      mountPath: "/data/mysql/run"
    - name: mysql-tmp
      mountPath: "/data/mysql/tmp"
    - name: mysql-cnf
      mountPath: "/etc/my.cnf"
  volumes:
  - name: mysql-data
    hostPath:
      path: "/data/mysql/mysql_2/data"
      type: DirectoryOrCreate
  - name: mysql-log
    hostPath:
      path: "/data/mysql/mysql_2/log"
      type: DirectoryOrCreate
  - name: mysql-run
    hostPath:
      path: "/data/mysql/mysql_2/run"
      type: DirectoryOrCreate
  - name: mysql-tmp
    hostPath:
      path: "/data/mysql/mysql_2/tmp"
      type: DirectoryOrCreate
  - name: mysql-cnf
    hostPath:
      path: "/data/mysql/mysql_2/mysql_arm.cnf"
      type: FileOrCreate
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: mysql-3
  name: mysql-3
  namespace: ns-mysql-test
  annotations:
    ovn.kubernetes.io/ip_address: 10.99.0.17
    ovn.kubernetes.io/mac_address: 00:00:00:53:6B:B8
spec:
  nodeSelector:
    test: "mysql-test-3"
  hostAliases:
  - ip: "10.99.0.15"
    hostnames:
    - "mysql-1"
  - ip: "10.99.0.16"
    hostnames:
    - "mysql-2"
  - ip: "10.99.0.17"
    hostnames:
    - "mysql-3"
  containers:
  - name: mysql-3
    image: mymysql/centos8-mysql-arm:8.0.19
    resources:
      limits:
        cpu: 16
        memory: 64Gi
    ports:
    - name: mysql-port
      containerPort: 3306
    - name: copy-port
      containerPort: 33061
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: test123
    - name: MYSQL_ROOT_HOST
      value: "%"
    volumeMounts:
    - name: mysql-data
      mountPath: "/data/mysql/data"
    - name: mysql-log
      mountPath: "/data/mysql/log"
    - name: mysql-run
      mountPath: "/data/mysql/run"
    - name: mysql-tmp
      mountPath: "/data/mysql/tmp"
    - name: mysql-cnf
      mountPath: "/etc/my.cnf"
  volumes:
  - name: mysql-data
    hostPath:
      path: "/data/mysql/mysql_3/data"
      type: DirectoryOrCreate
  - name: mysql-log
    hostPath:
      path: "/data/mysql/mysql_3/log"
      type: DirectoryOrCreate
  - name: mysql-run
    hostPath:
      path: "/data/mysql/mysql_3/run"
      type: DirectoryOrCreate
  - name: mysql-tmp
    hostPath:
      path: "/data/mysql/mysql_3/tmp"
      type: DirectoryOrCreate
  - name: mysql-cnf
    hostPath:
      path: "/data/mysql/mysql_3/mysql_arm.cnf"
      type: FileOrCreate
---
apiVersion: v1
kind: Service
metadata: 
  name: mysql-1-service
  namespace: ns-mysql-test
spec:
  type: NodePort
  selector:
    app: mysql-1
  ports:
    - name: dbport
      protocol: TCP
      port: 3306
      targetPort: 3306
      nodePort: 30001
    - name: cpport
      protocol: TCP
      port: 33061
      targetPort: 33061
      nodePort: 30061
---
apiVersion: v1
kind: Service
metadata: 
  name: mysql-2-service
  namespace: ns-mysql-test
spec:
  type: NodePort
  selector:
    app: mysql-2
  ports:
    - name: dbport
      protocol: TCP
      port: 3306
      targetPort: 3306
      nodePort: 30002
    - name: cpport
      protocol: TCP
      port: 33061
      targetPort: 33061
      nodePort: 30062
---
apiVersion: v1
kind: Service
metadata: 
  name: mysql-3-service
  namespace: ns-mysql-test
spec:
  type: NodePort
  selector:
    app: mysql-3
  ports:
    - name: dbport
      protocol: TCP
      port: 3306
      targetPort: 3306
      nodePort: 30003
    - name: cpport
      protocol: TCP
      port: 33061
      targetPort: 33061
      nodePort: 30063

在该配置文件中配置了: