k8s使用的外部存储迁移

发布于:2025-07-04 ⋅ 阅读:(17) ⋅ 点赞:(0)
数据迁移指向
### 将 192.168.73.169 主机上 /data/nfsdata/default-mysql-pvc-pvc-105f0c81-3c5c-408e-8f55-1292997dfbcf/ 目录
### 的所有数据,迁移到 
### 192.168.73.245 主机上 /data/nfsdata/default-mysql-pvc-new-pvc-5366a8fd-3f45-48ac-b684-5544fbc8c807/

创建测试msql

### 使用 旧的 StorageClass nfs-client
apiVersion: 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: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: yourpassword
        - name: MYSQL_DATABASE
          value: yourdatabase
        ports:
        - containerPort: 3306
        volumeMounts:
        - 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: 3306
    targetPort: 3306
  selector:
    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 in set (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名字

创建新的 pvc 存储

### 使用 新的 StorageClass nfs-client-new
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc-new # 这个名称需要与上面使用的pvc进行区分,统一在后面加 -new
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
  storageClassName: nfs-client-new  # 指定使用新的 nfs-client-new StorageClass

### 需要进行创建

查看 存储是否创建成功

### 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

进行数据迁移

### 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


### 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

### 将 192.168.73.169 主机上 /data/nfsdata/default-mysql-pvc-pvc-105f0c81-3c5c-408e-8f55-1292997dfbcf/ 目录
### 的所有数据,迁移到 
### 192.168.73.245 主机上 /data/nfsdata/default-mysql-pvc-new-pvc-5366a8fd-3f45-48ac-b684-5544fbc8c807/

### 已经配置了免密登录
### 注意:是迁移目录的数据,不是目录,需要目录后面添加 / 
### 192.168.73.169 主机上操作
rsync -av -e "ssh" /data/nfsdata/default-mysql-pvc-pvc-105f0c81-3c5c-408e-8f55-1292997dfbcf/ \
root@192.168.73.245:/data/nfsdata/default-mysql-pvc-new-pvc-5366a8fd-3f45-48ac-b684-5544fbc8c807/

迁移命令输出如下

[root@iZt4n7s7eakyy49fr382f8Z nfsdata]# rsync -av -e "ssh" /data/nfsdata/default-mysql-pvc-pvc-105f0c81-3c5c-408e-8f55-1292997dfbcf/ \
> root@192.168.73.245:/data/nfsdata/default-mysql-pvc-new-pvc-5366a8fd-3f45-48ac-b684-5544fbc8c807/
sending incremental file list
./
auto.cnf
ca-key.pem
ca.pem

..............

yourdatabase/
yourdatabase/db.opt

sent 206,994,273 bytes  received 5,450 bytes  137,999,815.33 bytes/sec
total size is 206,920,794  speedup is 1.00

查看迁移以后的数据

### 192.168.73.245 上查看
[root@iZt4n4ptzm3y7uq20ym3pwZ nfsdata]# ll -h default-mysql-pvc-new-pvc-5366a8fd-3f45-48ac-b684-5544fbc8c807/
total 173M
-rw-r----- 1 polkitd input   56 Jul  2 19:17 auto.cnf
-rw------- 1 polkitd input 1.7K Jul  2 19:17 ca-key.pem
-rw-r--r-- 1 polkitd input 1.1K Jul  2 19:17 ca.pem
-rw-r--r-- 1 polkitd input 1.1K Jul  2 19:17 client-cert.pem
-rw------- 1 polkitd input 1.7K Jul  2 19:17 client-key.pem
-rw-r----- 1 polkitd input  679 Jul  2 19:37 ib_buffer_pool
-rw-r----- 1 polkitd input  76M Jul  2 19:37 ibdata1
-rw-r----- 1 polkitd input  48M Jul  2 19:37 ib_logfile0
-rw-r----- 1 polkitd input  48M Jul  2 19:17 ib_logfile1
drwxr-x--- 2 polkitd input 4.0K Jul  2 19:17 mysql
lrwxrwxrwx 1 polkitd input   27 Jul  2 19:17 mysql.sock -> /var/run/mysqld/mysqld.sock
drwxr-x--- 2 polkitd input 4.0K Jul  2 19:17 performance_schema
-rw------- 1 polkitd input 1.7K Jul  2 19:17 private_key.pem
-rw-r--r-- 1 polkitd input  452 Jul  2 19:17 public_key.pem
-rw-r--r-- 1 polkitd input 1.1K Jul  2 19:17 server-cert.pem
-rw------- 1 polkitd input 1.7K Jul  2 19:17 server-key.pem
drwxr-x--- 2 polkitd input  12K Jul  2 19:17 sys
drwxr-x--- 2 polkitd input 4.0K Jul  2 19:17 yourdatabase

更改 k8s deployment 数据使用的pvc

### k8s 主节点上操作
[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl edit deployment mysql -n default
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-storage
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - name: mysql-storage
        persistentVolumeClaim:
          claimName: mysql-pvc-new  # 更改为新的 pvc
          
### 核查是否更改成功
[root@iZt4nhyrqem8xubsznerpwZ grata]# kubectl get  deployment mysql -n default -o yaml |grep claimName:
          claimName: mysql-pvc-new # 已经更改为新的pvc

扩容 deployment 中的pod 到原始的数量

### 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 in set (0.00 sec)

mysql> 

网站公告

今日签到

点亮在社区的每一天
去签到