【转】K8S(kubernetes)基于nfs实现持久化(PV+PVC)

发布时间 2023-06-08 13:20:53作者: 立志做一个好的程序员

 

原文:https://www.cnblogs.com/zeng666/p/16622610.html

------------------

 

k8s集群情况:
[21:56:22 root@k8s-master ~]#kubectl get nodes
NAME               STATUS   ROLES    AGE   VERSION
k8s-master         Ready    master   10d   v1.18.0
k8s-worker-node1   Ready    <none>   10d   v1.18.0
k8s-worker-node2   Ready    <none>   10d   v1.18.0

1. 部署nfs服务端

k8s-master 节点上搭建了 NFS 服务器

(1)安装nfs服务:
yum install -y nfs-utils rpcbind

[22:01:29 root@k8s-master ~]#cat /etc/exports
/nfsdata *(rw,no_root_squash,no_all_squash,sync)

2)保存配置文件后,执行如下操作:

在服务端创建对应的目录和赋予权限:
[22:02:02 root@k8s-master ~]#mkdir /nfsdata
[22:02:52 root@k8s-master ~]# chmod 777 /nfsdata

[22:03:24 root@k8s-master ~]#ll /nfsdata/ -d
drwxrwxrwx 2 root root 6 8月  23 22:02 /nfsdata/

(3) 启动rpcbind和nfs服务(并且设置为开机启动):
[22:03:27 root@k8s-master ~]#systemctl enable --now rpcbind nfs
(4)每个node安装nfs工具,并且启动:
[21:56:10 root@k8s-worker-node1 ~]#yum install -y nfs-utils

[21:56:10 root@k8s-worker-node2 ~]#yum install -y nfs-utils

[22:07:07 root@k8s-worker-node1 ~]#systemctl enable --now nfs
[22:06:09 root@k8s-worker-node2 ~]#systemctl enable --now nfs

(5) 每个node查询一下NFS服务器
showmount -e nfs-ip

[22:07:29 root@k8s-worker-node1 ~]#showmount -e 10.0.0.7
Export list for 10.0.0.7:
/nfsdata *

[22:07:38 root@k8s-worker-node2 ~]#showmount -e 10.0.0.7
Export list for 10.0.0.7:
/nfsdata *

NFS PersistentVolume :

创建 PV 

(1)下面创建一个 PV mypv1,配置文件 nfs-pv1.yaml 如下:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mypv1
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nfs
nfs:
  path: /nfsdata/pv1
  server: 192.168.56.105



#配置详解:
①# capacity 指定 PV 的容量为 1G。

②# accessModes 指定访问模式为 ReadWriteOnce,支持的访问模式有:

# ReadWriteOnce – PV 能以 read-write 模式 mount 到单个节点。

# ReadOnlyMany – PV 能以 read-only 模式 mount 到多个节点。

# ReadWriteMany – PV 能以 read-write 模式 mount 到多个节点。

③# persistentVolumeReclaimPolicy 指定当 PV 的回收策略为 Recycle,支持的策略有:

# Retain – 需要管理员手工回收。

# Recycle – 清除 PV 中的数据,效果相当于执行 rm -rf /thevolume/*。

#Delete – 删除 Storage Provider 上的对应存储资源,例如 AWS EBS、GCE PD、Azure Disk、OpenStack Cinder Volume 等。

④# storageClassName 指定 PV 的 class 为 nfs。相当于为 PV 设置了一个分类,PVC 可以指定 class 申请相应 class 的 PV。

⑤# 指定 PV 在 NFS 服务器上对应的目录。

*(特别注意)
注意要做storage-provider段要提前建好文件夹
[22:19:36 root@k8s-master nfsdata]#pwd
/nfsdata
(2)创建 mypv1:
[22:28:28 root@k8s-master ~]#kubectl apply -f nfs-pv1.yaml
persistentvolume/mypv1 created

[22:28:34 root@k8s-master ~]#kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
mypv1   1Gi        RWO            Recycle          Available           nfs                     37s

#STATUS 为 Available,表示 mypv1 就绪,可以被 PVC 申请:

创建 PVC

(1)接下来创建 PVC mypvc1,配置文件 nfs-pvc1.yaml 如下:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mypvc1
spec:
accessModes:
  - ReadWriteOnce
resources:
  requests:
    storage: 1Gi
storageClassName: nfs

#需要指定 PV 的容量,访问模式和 class。

(2)创建 mypvc1:
[22:43:10 root@k8s-master ~]#kubectl apply -f nfs-pvc1.yaml
persistentvolumeclaim/mypvc1 created

[22:43:12 root@k8s-master ~]#kubectl get pvc
NAME     STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mypvc1   Bound    mypv1    1Gi        RWO            nfs            106s

#从 kubectl get pvc 和 kubectl get pv 的输出可以看到 mypvc1 已经 Bound 到 mypv1,申请成功。

Pod使用pvc:

(1)接下来就可以在 Pod 中使用存储了,Pod 配置文件 pod1.yml 如下:

[22:59:02 root@k8s-master ~]#cat pod1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:
  containers:
    - name: mypod1
      image: busybox
      args:
      - /bin/sh
      - -c
      - sleep 30000
      volumeMounts:
      - mountPath: "/mydata"
        name: mydata
  volumes:
    - name: mydata
      persistentVolumeClaim:
        claimName: mypvc1

#与使用普通 Volume 的格式类似,在 volumes 中过 persistentVolumeClaim 指定使用 mypvc1 申请的 Volume。
创建 mypod1:
[22:59:00 root@k8s-master ~]#kubectl apply -f pod1.yaml
pod/mypod1 created

pod 内部:启动详细流程:

pod 内部:启动详细流程:
[23:12:39 root@k8s-master ~]#kubectl describe pod mypod1
Name:         mypod1
Namespace:    default
Priority:     0
Node:         k8s-worker-node1/10.0.0.17
Start Time:   Tue, 23 Aug 2022 23:12:22 +0800
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.244.1.2
IPs:
  IP:  10.244.1.2
Containers:
  mypod1:
    Container ID:  docker://fb0ca7dfb995d127d98dc451e20dbfccb63ef59d886e850119ea87820ca0d7d5
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
    Port:          <none>
    Host Port:     <none>
    Args:
      /bin/sh
      -c
      sleep 30000
    State:          Running
      Started:      Tue, 23 Aug 2022 23:12:40 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /mydata from mydata (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-xb5g7 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  mydata:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  mypvc1
    ReadOnly:   false
  default-token-xb5g7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-xb5g7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age        From                       Message
  ----    ------     ----       ----                       -------
  Normal  Scheduled  <unknown>  default-scheduler          Successfully assigned default/mypod1 to k8s-worker-node1
  Normal  Pulling    29s        kubelet, k8s-worker-node1  Pulling image "busybox"
  Normal  Pulled     12s        kubelet, k8s-worker-node1  Successfully pulled image "busybox"
  Normal  Created    12s        kubelet, k8s-worker-node1  Created container mypod1
  Normal  Started    12s        kubelet, k8s-worker-node1  Started container mypod1
[23:12:52 root@k8s-master ~]#kubectl get pod -o wide
NAME     READY   STATUS    RESTARTS   AGE   IP           NODE               NOMINATED NODE   READINESS GATES
mypod1   1/1     Running   0          51s   10.244.1.2   k8s-worker-node1   <none>           <none>

验证 PV 是否可用:

[23:13:14 root@k8s-master ~]#kubectl exec mypod1 touch /mydata/hello
[23:14:59 root@k8s-master ~]#kubectl exec -it  mypod1 --/bin/sh
Error: unknown flag: --/bin/sh
See 'kubectl exec --help' for usage.

[23:17:09 root@k8s-master ~]#kubectl exec -it  mypod1 -- /bin/sh
/ # cd /mydata/
/mydata # ls
hello
#此时看到master节点已经有文件创建了。已经实现持久化存储了!
[23:18:51 root@k8s-master pv1]#ls
hello

#继续测试:
[23:20:55 root@k8s-master pv1]#kubectl exec mypod1 -- touch /mydata/hello-2.0
[23:21:01 root@k8s-master pv1]#ls
hello  hello-2.0

#由此可见,在 Pod 中创建的文件 /mydata/hello 确实已经保存到了 NFS 服务器目录 /nfsdata/pv1 中。