k8s中有许多应用会产生大量的数据需要存储,比如MySQL应用和大量的应用日志等。但当pod被删除或重启后这些数据会随之消失,如果想要长久的保留这些数据就要用到数据持久化存储。
本次采用NFS作为k8s集群持久化存储的方案

安装nfs

master节点作为nfs服务端,node节点作为客户端
master节点安装nfs

[root@server1 ~]# yum install -y nfs-utils rpcbind
Total                                                                                                                   207 kB/s | 472 kB  00:00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : rpcbind-0.2.0-49.el7.x86_64                                                                                                          1/4 
  Updating   : 1:nfs-utils-1.3.0-0.66.el7_8.x86_64                                                                                                  2/4 
  Cleanup    : 1:nfs-utils-1.3.0-0.61.el7.x86_64                                                                                                    3/4 
  Cleanup    : rpcbind-0.2.0-47.el7.x86_64                                                                                                          4/4 
  Verifying  : 1:nfs-utils-1.3.0-0.66.el7_8.x86_64                                                                                                  1/4 
  Verifying  : rpcbind-0.2.0-49.el7.x86_64                                                                                                          2/4 
  Verifying  : rpcbind-0.2.0-47.el7.x86_64                                                                                                          3/4 
  Verifying  : 1:nfs-utils-1.3.0-0.61.el7.x86_64                                                                                                    4/4 

Updated:
  nfs-utils.x86_64 1:1.3.0-0.66.el7_8                                           rpcbind.x86_64 0:0.2.0-49.el7                                          

Complete!

node节点安装nfs

[root@server2 ~]# yum install -y nfs-utils
[root@server3 ~]# yum install -y nfs-utils

配置nfs

[root@server1 ~]# vim /etc/exports
#/share 192.168.56.0/24(ro)
/nfsdata *(rw,no_root_squash,no_all_squash,sync)

创建/nfsdata目录并赋权

[root@server1 ~]# mkdir /nfsdata
[root@server1 ~]# chmod 775 -R /nfsdata

启动nfs和rpcbind服务

[root@server1 ~]# systemctl start rpcbind
[root@server1 ~]# systemctl start nfs

node节点查看master共享目录

[root@server2 ~]# showmount -e 192.168.56.132
Export list for 192.168.56.132:
/nfsdata *
[root@server3 ~]# showmount -e 192.168.56.132
Export list for 192.168.56.132:
/nfsdata *

创建NFS PersistentVolume

创建顺序:后端存储—pv—pvc—pod
创建PV

PV :PersistentVolume(持久化卷),是对底层的共享存储的一种抽象,PV由管理员进行创建和配置,它和具体的底层的共享存储技术的实现方式有关,比如 Ceph、GlusterFS、NFS等,都是通过插件机制完成与共享存储的对接。

[root@server1 nfs_pv]# vim pv1.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv1
  labels:
    app: nfs
spec:
  capacity:	#指定 PV 的容量为 1G
    storage: 1Gi
  accessModes:	#指定访问模式
  - ReadWriteOnce	#PV 能以 read-write 模式 mount 到单个节点
  persistentVolumeReclaimPolicy: Recycle	#指定当 PV 的回收策略为 Recycle
  storageClassName: nfs	#指定 PV 的 class 为 nfs。相当于为 PV 设置了一个分类,PVC 可以指定 class 申请相应 class 的 PV
  nfs:
    path: /nfsdata/pv1
    server: 192.168.56.132

创建PV

[root@server1 nfs_pv]# kubectl apply -f pv1.yml 
persistentvolume/pv1 created

查看pv

[root@server1 nfs_pv]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv1    1Gi        RWO            Recycle          Available                                   5m9s

创建pvc

[root@server1 nfs_pv]# vim pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc1
spec:
  accessModes:  #指定访问模式
    - ReadWriteOnce       #PV 能以 read-write 模式 mount 到单个节点
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs

创建pvc

[root@server1 nfs_pv]# kubectl apply -f pvc.yml 
persistentvolumeclaim/pvc1 created

查看pvc

[root@server1 nfs_pv]# kubectl get pvc
NAME   STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc1   Bound    pv1      1Gi        RWO            nfs            3s

查看pv和pvc是否绑定

[root@server1 nfs_pv]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM          STORAGECLASS   REASON   AGE
pv1    1Gi        RWO            Recycle          Bound    default/pvc1   nfs                     2m22s

pod使用pvc

[root@server1 nfs_pv]# vim pod1.yml 
apiVersion: v1
kind: Pod
metadata:
  name: nfs-pod
spec:
  containers:
    - name: nfs-pod
      image: nginx
      args:
      - /bin/sh
      - -c
      - sleep 30000
      volumeMounts:
      - mountPath: "/nfsdata"
        name: nfsdata
  volumes:
      - name: nfsdata
        persistentVolumeClaim:
          claimName: pvc1        #第2步中创建的pvc

创建pod

[root@server1 nfs_pv]# kubectl apply -f pod1.yml 
pod/nfs-pod created
[root@server1 nfs_pv]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nfs-pod                             1/1     Running   0          41s
nginx-deployment-6f4bbf5684-fqp9w   1/1     Running   0          28d
nginx-deployment-6f4bbf5684-gqht5   1/1     Running   0          28d
nginx-deployment-6f4bbf5684-wl84l   1/1     Running   0          28d
[root@server1 ~]# kubectl describe pod nfs-pod
State:          Running
      Started:      Wed, 26 Aug 2020 22:53:14 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /nfsdata from nfsdata (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-8kls9 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True

验证pv是否可以使用

root@server1 ~]# kubectl exec nfs-pod touch /nfsdata/hello
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.

可以看到在 Pod 中创建的文件 /nfsdata/hello 已经保存到了 NFS 服务器目录 /nfsdata 中

[root@server1 ~]# cd /nfsdata/
[root@server1 nfsdata]# ls
hello  

回收pv

当 PV 不再需要时,可通过删除 PVC 回收
[root@server1 nfsdata]# kubectl delete pvc pvc1

当 PVC被删除后,发现集群启动了一个新 Pod,这个 Pod 的作用就是清除 PV的数据。这个时候再看 NFS 服务器目录 /nfsdata中已经没有数据了

[root@server1 /]# cd nfsdata/
[root@server1 nfsdata]# ls

pv1的状态重新变为 Available,此时则可以被新的 PVC 申请。

[root@server1 nfsdata]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv1    1Gi        RWO            Recycle          Available           nfs                     23h

更改pv回收模式

之前因为是recycle模式导致删除pvc数据丢失,如果我们希望保留数据,可以将策略设置为 Retain。
修改pv的回收策略

[root@server1 nfs_pv]# vim pv1.yml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv1
  labels:
    app: nfs
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain #修改为Retain模式
  storageClassName: nfs
  nfs:
    path: /nfsdata
    server: 192.168.56.132

更新pv

[root@server1 nfs_pv]# kubectl apply -f pv1.yml 
persistentvolume/pv1 configured
[root@server1 nfs_pv]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv1    1Gi        RWO            Retain           Available           nfs                     23h

再次创建pvc

[root@server1 nfs_pv]# kubectl apply -f pvc.yml 
persistentvolumeclaim/pvc1 created

再次创建nfs-pod

[root@server1 nfs_pv]# kubectl apply -f pod1.yml 
pod/nfs-pod created

在pv1中创建文件
[root@server1 nfs_pv]# kubectl exec nfs-pod touch /nfsdata/hello.1
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
[root@server1 nfs_pv]# ll /nfsdata/
total 0
-rw-r--r-- 1 root root 0 Aug 27 22:44 hello.1

删掉nfs-pod和pvc验证hello.1文件是否还存在

[root@server1 nfs_pv]# kubectl delete pod nfs-pod --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nfs-pod" force deleted
[root@server1 nfs_pv]# kubectl delete pvc pvc1
persistentvolumeclaim "pvc1" deleted
[root@server1 nfs_pv]# ll /nfsdata
total 0
-rw-r--r-- 1 root root 0 Aug 27 22:44 hello.1

可以看到即便是删掉了pvc,数据依然被保留了下来。

虽然pv1中的数据得到了保留,但其 PV 状态会一直处于 Released,不能被其他PVC使用

[root@server1 nfs_pv]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM          STORAGECLASS   REASON   AGE
pv1    1Gi        RWO            Retain           Released   default/pvc1   nfs                     23h
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐