Rate This Document
Findability
Accuracy
Completeness
Readability

Preparing the YAML File for Deploying MySQL Pods in the K8s Cluster

This document uses the networking mode shown in Figure 1 as an example. Three MySQL pods are deployed on three compute nodes, respectively. External clients can access the three MySQL pods in the K8s cluster in either of the following methods:

  • Method 1:

    The MySQL pods can be accessed by external clients using nodePort of dbport in the three kind: Service parameters in the YAML file. That is, external clients can access the MySQL pods through the primary node. The destination IP address used by the external clients to access the three MySQL pods is the IP address (192.168.200.10) of the primary node, and the used port numbers are 30001, 30002, and 30003. For example, kind: Service, name: dbport, and nodePort: 30001 are configured in the YAML file.

  • Method 2:

    Route the pod IP address segment (10.99.0.0/16) of the MySQL pod to the IP address (192.168.200.10) of the primary node. The primary node also has an IP address in the 10.99.0.0/16 network segment. Therefore, external clients can directly access the pod IP address of the MySQL pod through the primary node. The destination IP addresses used by external clients to access the three MySQL pods are the pod IP addresses of the three MySQL pods. In addition, all the port numbers are 3306. For example, kind: Service, name: dbport, and port: 3306 are configured in the YAML file. In this method, you need to configure a static route on the gateway, for example, route add -net 10.99.0.0/16 gw 192.168.200.10. For details, see IP Addresses of Pods Exposed to the External Network in the Kube-OVN Component User Guide.

  • In this document, the network port name of enp3s0 (10GE NIC) is used to bind the Kube-OVN to improve the network bandwidth. Note that the onboard NIC of the default gateway is not recommended. To apply the network port binding mode to deploy the Kube-OVN, you need to modify the install.sh file and set the IFACE parameter to enp3s0 before installing the Kube-OVN. For details, see the IFACE parameter in the install.sh file in Installing Kube-OVN 1.2.1.
  • The data and configuration files of MySQL pods are stored on the local drive of a physical machine because no network storage service is used. Therefore, MySQL pods must be deployed on a specified physical machine to prevent them from being automatically scheduled by K8s to other compute nodes without the corresponding data and configuration files.

This document uses the deployment of three MySQL pods as an example to describe how to write a deployment YAML file (for example, mysql_deployment.yaml) on the physical machine of the K8s primary node.

Download URL of the YAML file: https://mirrors.huaweicloud.com/kunpeng/archive/kunpeng_solution/database/scripts/mysql_deployment.yaml

The content edited in the following YAML file is the same as that downloaded from the preceding link.

1
vim mysql_deployment.yaml

Edit the following content:

  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

The following information is set in the configuration file:

  • A namespace called ns-mysql-test
  • A subnet that functions in the ns-mysql-test namespace. The corresponding subnet segment is 10.99.0.0/16.
  • Three MySQL pods mysql-1, mysql-2, and mysql-3. The pod IP addresses and MAC addresses are fixed. The pod IP addresses are 10.99.0.15, 10.99.0.16, and 10.99.0.17.
  • With regard to the three MySQL pods, the configuration file directory is /etc/my.cnf, the data directory is /data/mysql/data, the log directory is /data/mysql/log, the run directory is /data/mysql/run, and the temporary directory is /data/mysql/tmp by default. All the directories are mapped to the file directories configured for volumes in the YAML file on the physical machine where the file is located. Note that the file directory information such as datadir, log-error, tmpdir, socket, and pid-file in the mysql_arm.cnf file must be the same as the configuration directories of volumeMounts in the YAML file. Otherwise, the MySQL pod data cannot be saved to the storage drive of the physical machine, leading to data loss.
  • To prevent configuration files from overlapping when mysql-1, mysql-2, and mysql-3 are deployed on the same physical machine, /data/mysql/mysql_1, /data/mysql/mysql_2, and /data/mysql/mysql_3 are used as the storage directories of the three pods, respectively.
  • The three MySQL pods use the self-built mymysql/centos8-mysql-arm:8.0.19 image to instantiate containers.
  • The root passwords of the three MySQL pods are set to test123 using the MYSQL_ROOT_PASSWORD environment variable.
  • The database service port 3306 is configured for each of the three MySQL pods and port 33061 is reserved for primary-secondary replication.
  • nodeSelector is used to configure the nodes of the target physical machines where mysql-1, mysql-2, and mysql-3 are to be deployed. The corresponding label needs to be configured for each node. In this example, three MySQL pods are deployed on the test="mysql-test-1", test="mysql-test-2", and test="mysql-test-3" nodes in the YAML configuration file. To deploy a pod on a specified node, you need to attach the test="mysql-test-1", test="mysql-test-2", or test="mysql-test-3" label to the node.

    Run the following command on the primary node to check the nodes in the current cluster.

    1
    kubectl get nodes
    
    NAME                  STATUS   ROLES    AGE   VERSION
    centos-10             Ready    master   42h   v1.18.5
    node-test-1           Ready    <none>   42h   v1.18.5
    node-test-2           Ready    <none>   42h   v1.18.5
    node-test-3           Ready    <none>   42h   v1.18.5

    In this example, three MySQL pods are deployed on the three nodes node-test-1, node-test-2, and node-test-3, respectively. Run the kubectl label nodes command to configure labels for the three nodes.

    1
    2
    3
    kubectl label nodes node-test-1 test=mysql-test-1
    kubectl label nodes node-test-2 test=mysql-test-2
    kubectl label nodes node-test-3 test=mysql-test-3
    

    By applying the deployment in the YAML file, you can deploy the three MySQL pods on the three nodes, respectively.

    • In network planning, the subnet segment of a pod cannot overlap with that of a node.
    • If three MySQL pods are deployed on the same node, their data storage directories on the physical machine must be different.
    • The namespace, subnet segment, data storage directory, IP address, MAC address, deployment node, MySQL root password, CPU, and memory usage restrictions must be configured for MySQL pods based on your requirements.
    • Ensure that the mymysql/centos8-mysql-arm:8.0.19 image has been imported to the node where a MySQL pod needs to be deployed.
  • Three services of the NodePort type map port 3306 of the three MySQL pods to ports 30001, 30002, and 30003 of the physical machine, and map port 33061 of the three MySQL pods to ports 30061, 30062, and 30063 of the physical machine.