### 使用 旧的 StorageClass nfs-clientapiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mysql-pvc
spec:accessModes:- ReadWriteMany
resources:requests:storage: 5Gi
storageClassName: nfs-client # 指定使用 nfs-client StorageClass---apiVersion: apps/v1
kind: Deployment
metadata:name: mysql
spec:replicas:1selector:matchLabels:app: mysql
template:metadata:labels:app: mysql
spec:containers:-name: mysql
image: mysql:5.7env:-name: MYSQL_ROOT_PASSWORD
value: yourpassword
-name: MYSQL_DATABASE
value: yourdatabase
ports:-containerPort:3306volumeMounts:-mountPath: /var/lib/mysql
name: mysql-storage
volumes:-name: mysql-storage
persistentVolumeClaim:claimName: mysql-pvc
---apiVersion: v1
kind: Service
metadata:name: mysql
spec:type: ClusterIP
ports:-port:3306targetPort:3306selector:app: mysql
### 需要进行创建
核查数据是否可以用
[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl exec -it mysql-c5c499479-nwrr2 -n default -- /bin/bash
bash-4.2# mysql -uroot -pyourpassword
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 2
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c)2000, 2023, Oracle and/or its affiliates.
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 || yourdatabase |
+--------------------+
5 rows inset(0.00 sec)
mysql>
查看 存储是否创建成功
### k8s 主节点上查看使用的存储[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
mysql-pvc Bound pvc-105f0c81-3c5c-408e-8f55-1292997dfbcf 5Gi RWX nfs-client <unset> 3m42s
### 192.168.73.169 主机上查看目录是否存在cd /data/nfsdata
[root@iZt4n7s7eakyy49fr382f8Z nfsdata]# ls |grep pvc-105f0c81-3c5c-408e-8f55-1292997dfbcf
default-mysql-pvc-pvc-105f0c81-3c5c-408e-8f55-1292997dfbcf
# 存储名字命名规则# 命名空间 + pvc名字 + pv名字
### k8s 主节点上查看使用的存储[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
mysql-pvc Bound pvc-105f0c81-3c5c-408e-8f55-1292997dfbcf 5Gi RWX nfs-client <unset> 12m
mysql-pvc-new Bound pvc-5366a8fd-3f45-48ac-b684-5544fbc8c807 5Gi RWX nfs-client-new <unset> 46s
### 192.168.73.245 主机上查看目录是否存在cd /data/nfsdata
[root@iZt4n4ptzm3y7uq20ym3pwZ nfsdata]# ls |grep pvc-5366a8fd-3f45-48ac-b684-5544fbc8c807
default-mysql-pvc-new-pvc-5366a8fd-3f45-48ac-b684-5544fbc8c807
# 存储名字命名规则# 命名空间 + pvc名字 + pv名字
停止该容器服务
### k8s 主节点### 需要关注,是多少个pod,后面迁移完成以后,需要更改为原来的数量[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl get all -n default
NAME READY STATUS RESTARTS AGE
pod/mysql-c5c499479-nwrr2 1/1 Running 0 17m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 192.168.64.1 <none> 443/TCP 277d
service/mysql ClusterIP 192.168.116.73 <none> 3306/TCP 17m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql 1/1 1 1 17m
NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-c5c499479 1 1 1 17m
### 需要将相关的 deployment pod 数量调整为 0 [root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl scale deployment mysql --replicas=0 -n default
deployment.apps/mysql scaled
### 核查服务是否停止[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl get all -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 192.168.64.1 <none> 443/TCP 277d
service/mysql ClusterIP 192.168.116.73 <none> 3306/TCP 21m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql 0/0 0 0 21m
NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-c5c499479 0 0 0 21m
### k8s 主节点上操作[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl get all -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 192.168.64.1 <none> 443/TCP 277d
service/mysql ClusterIP 192.168.116.73 <none> 3306/TCP 41m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql 0/0 0 0 41m
NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-c5c499479 0 0 0 41m
replicaset.apps/mysql-cbb666cb4 0 0 0 3m26s
### 开始扩容[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl scale deployment mysql --replicas=1 -n default
deployment.apps/mysql scaled
### 核查是否扩容成功[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl get all -n default
NAME READY STATUS RESTARTS AGE
pod/mysql-cbb666cb4-mnwt5 1/1 Running 0 40s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 192.168.64.1 <none> 443/TCP 277d
service/mysql ClusterIP 192.168.116.73 <none> 3306/TCP 44m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql 1/1 1 1 44m
NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-c5c499479 0 0 0 44m
replicaset.apps/mysql-cbb666cb4 1 1 1 5m49s
核查数据是否可以用
[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl exec -it mysql-cbb666cb4-mnwt5 -n default -- /bin/bash
bash-4.2# mysql -uroot -pyourpassword
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 2
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c)2000, 2023, Oracle and/or its affiliates.
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 || yourdatabase |
+--------------------+
5 rows inset(0.00 sec)
mysql>