学习pv 的时候没有网络卷什么的,所以我把我的一台虚拟机当作网络存储节点,具体过程如下:

  1. 首先构建pv:
apiVersion: v1 
kind: PersistentVolume   # pv
metadata: 
 name: example-pv 
 labels:
   release: "stable"  # 为了让后续的pvc绑定到此pv上,所以加了labels
spec: 
 capacity: 
   storage: 10Gi 
 volumeMode: Filesystem 
 accessModes: 
 - ReadWriteOnce 
 persistentVolumeReclaimPolicy: Delete 
 storageClassName: local-storage 
 local: 
   path: /data/along  # 本地存放目录
 nodeAffinity:   # 调度选择
   required: 
     nodeSelectorTerms: 
     - matchExpressions: 
       - key: kubernetes.io/hostname 
         operator: In 
         values: 
         - 172.31.0.53
创建成功结果
[root@vm172-31-0-9 store_method]# kubectl get pv -o wide
NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                         STORAGECLASS    REASON   AGE     VOLUMEMODE
example-pv       10Gi       RWO            Delete           Bound       default/example-local-claim   local-storage            97m     Filesystem
描述如下
[root@vm172-31-0-9 store_method]# kubectl describe pv example-pv 
Name:              example-pv
Labels:            release=stable
Annotations:       pv.kubernetes.io/bound-by-controller: yes
Finalizers:        [kubernetes.io/pv-protection]
StorageClass:      local-storage
Status:            Bound
Claim:             default/example-local-claim
Reclaim Policy:    Delete
Access Modes:      RWO
VolumeMode:        Filesystem
Capacity:          10Gi
Node Affinity:     
  Required Terms:  
    Term 0:        kubernetes.io/hostname in [172.31.0.53]
Message:           
Source:
    Type:  LocalVolume (a persistent volume backed by local storage on a node)
    Path:  /data/along
Events:    <none>

  1. 创建pvc
kind: PersistentVolumeClaim 
apiVersion: v1 
metadata: 
 name: example-local-claim 
spec: 
 accessModes: 
 - ReadWriteOnce 
 resources: 
   requests: 
     storage: 50Mi 
 storageClassName: local-storage
 selector:
  matchLabels:
    release: "stable" # 为了于上面的pvc绑定
如下
[root@vm172-31-0-9 store_method]# kubectl get pvc
NAME                  STATUS   VOLUME       CAPACITY   ACCESS MODES   STORAGECLASS    AGE
example-local-claim   Bound    example-pv   10Gi       RWO            local-storage   101m
  1. 创建pod使用pvc
apiVersion: v1 
kind: Pod 
metadata: 
 name: local-pvc-pod 
spec: 
 containers: 
 - image: busybox 
   name: test-local-pvc 
   command: [ "sleep", "3600" ] 
   volumeMounts: 
   - mountPath: /data  # pod下的目录
     name: data-volume 
 volumes: 
 - name: data-volume  # 为挂在过来的pvc起名字
   persistentVolumeClaim: 
     claimName: example-local-claim  # 选中上面的pvc
结果是这样的
[root@vm172-31-0-9 store_method]# kubectl describe pod local-pvc-pod 
Name:         local-pvc-pod
...省略...
Volumes:
  data-volume:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  example-local-claim
    ReadOnly:   false
  default-token-cnjj9:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-cnjj9
    Optional:    false
...省略...
进入pod写内容
[root@vm172-31-0-9 store_method]# kubectl exec -it local-pvc-pod -- /bin/sh
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # echo "are you ok?">>/data/test.log 
[root@vm172-31-0-9 store_method]# ssh 172.31.0.53
Last login: Thu Sep  3 16:40:00 2020 from 172.31.0.9
[root@vm172-31-0-53 ~]# ls
[root@vm172-31-0-53 ~]# cat /data/along/test.log 
are you ok?

参考文章
3种存储傻傻分不清楚!
https://stor.51cto.com/art/201812/588905.htm###

Logo

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

更多推荐