[k8s]pod持久存储的几种方式图解

参考: https://kubernetes.io/docs/concepts/storage/volumes/

1.emptyDir

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

2.hostPath

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: gcr.io/google_containers/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data

3.pod持久存储

  • 方式1: pod直接挂载nfs-server
    [k8s]pod持久存储的几种方式图解_第1张图片
volumes:
  - name: nfs
    nfs:
      server: 192.168.1.1
      path:"/"

静态提供:管理员手动创建多个PV,供PVC使用。
动态提供:动态创建PVC特定的PV,并绑定。

  • 方式2: 手动创建pv
    [k8s]pod持久存储的几种方式图解_第2张图片
pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv003
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /somepath
    server: 192.168.1.1
$ kubectl get pv
NAME                                       CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM                             STORAGECLASS     REASON    AGE
nfs-pv-heketi                              300Mi      ROX           Retain          Bound     default/nfs-pvc-heketi                                       7d
pvc-02b8a30d-8e28-11e7-a07a-025622f1d9fa   50Gi       RWX           Retain          Bound     kube-public/jenkins-pvc           heketi-storage             5d

pv可以设置三种回收策略:保留(Retain),回收(Recycle)和删除(Delete)。

  • 保留策略:允许人工处理保留的数据。
  • 删除策略:将删除pv和外部关联的存储资源,需要插件支持。
  • 回收策略:将执行清除操作,之后可以被新的pvc使用,需要插件支持。

pv的状态:

  • Available – 资源尚未被claim使用
  • Bound – 已经绑定到某个pvc上
  • Released – 对应的pvc被删除,但是资源还没有被集群回收
  • Failed – 自动回收失败

访问权限

ReadWriteOnce – 被单个节点mount为读写rw模式
ReadOnlyMany – 被多个节点mount为只读ro模式
ReadWriteMany – 被多个节点mount为读写rw模式
参考: http://dockone.io/article/2290 没什么卵用
[k8s]pod持久存储的几种方式图解_第3张图片

pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: myclaim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
mypod.yaml

volumes:
  - name: mypod
    persistentVolumeClaim:
      claimName: myclaim
  • 方式3: 自动创建pv–statefulset
    参考: https://kubernetes.io/docs/concepts/storage/persistent-volumes/

逻辑关系图解1
[k8s]pod持久存储的几种方式图解_第4张图片

逻辑关系图解2
[k8s]pod持久存储的几种方式图解_第5张图片

为了说明一个class对应多个pvc.

$ kubectl get pvc
NAME             STATUS    VOLUME          CAPACITY   ACCESSMODES   STORAGECLASS   AGE
nfs-nginx        Bound     nfs-xx          300Mi      ROX                          7d
$ kubectl get pv
NAME                                       CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM                             STORAGECLASS     REASON    AGE
nfs-pv-heketi                              300Mi      ROX           Retain          Bound     default/nfs-pvc-heketi                                       7d
pvc-02b8a30d-8e28-11e7-a07a-025622f1d9fa   50Gi       RWX           Retain          Bound     kube-public/jenkins-pvc           heketi-storage             5d
pvc-05bbb05c-8edf-11e7-a07a-025622f1d9fa   30Gi       RWX           Retain          Bound     kube-public/mysql-develop-pvc     heketi-storage             5d
pvc-0b3ecd56-8ffb-11e7-a07a-025622f1d9fa   50Gi       RWX           Retain          Bound     kube-public/gitlab-pvc            heketi-storage             3d
pvc-4e657ad5-9205-11e7-a07a-025622f1d9fa   2Gi        RWX           Delete          Bound     kube-public/private-pvc           heketi-storage             1d
pvc-73dec4f4-8feb-11e7-a07a-025622f1d9fa   10Gi       RWX           Retain          Bound     kube-public/gitlab-postgres-pvc   heketi-storage             3d
pvc-d74aff4a-91fc-11e7-a07a-025622f1d9fa   2Gi        RWX           Delete          Bound     kube-public/spring-pvc            heketi-storage             1d
pvc-db821d79-8fb6-11e7-a07a-025622f1d9fa   3Gi        RWX           Retain          Bound     kube-public/gitlab-redis-pvc      heketi-storage             3d

参考nfs更能看清晰一些

代码配置逻辑关系图解

[k8s]pod持久存储的几种方式图解_第6张图片

实例

[k8s]pod持久存储的几种方式图解_第7张图片

你可能感兴趣的:([k8s]pod持久存储的几种方式图解)