Kubernetes Volume

2018年8月16日17:00:26 发表评论 3,325 ℃

emptyDir

emptyDir 是最基础的 Volume 类型。

Kubernetes Volume

① 文件最底部 volumes 定义了一个 emptyDir 类型的 Volume shared-volume。

② producer 容器将 shared-volume mount 到 /producer_dir 目录。

③ producer 通过 echo 将数据写到文件 hello 里。

④ consumer 容器将 shared-volume mount 到 /consumer_dir 目录。

⑤ consumer 通过 cat 从文件 hello 读数据。

hostPath

hostPath Volume 的作用是将 Docker Host 文件系统中已经存在的目录 mount 给 Pod 的容器。大部分应用都不会使用 hostPath Volume,因为这实际上增加了 Pod 与节点的耦合,限制了 Pod 的使用。不过那些需要访问 Kubernetes 或 Docker 内部数据(配置文件和二进制库)的应用则需要使用 hostPath。

kubectl edit --namespace=kube-system pod kube-apiserver-master #查看 kube-apiserver Pod 的配置

外部 Storage Provider

AWS Elastic Block Store 示例

Kubernetes Volume

分布式存储Ceph示例

Kubernetes Volume

Ceph文件系统的/some/path/in/side/cephfs目录被mount到容器路径/test-ceph。

PersistentVolume (PV)和PersistentVolumeClaim (PVC) 

PersistentVolume (PV) 

是外部存储系统中的一块存储空间,由管理员创建和维护。与 Volume 一样,PV 具有持久性,生命周期独立于 Pod。

PersistentVolumeClaim (PVC) 

是对 PV 的申请 (Claim)。PVC 通常由普通用户创建和维护。需要为 Pod 分配存储资源时,用户可以创建一个PVC,指明存储资源的容量大小和访问模式(比如只读)等信息,Kubernetes 会查找并提供满足条件的 PV。

NFS PersistentVolume

每个node安装nfs-utils,并在nfs-server设置好共享。

[root@nfs-server ~]# cat /etc/exports

/nfsdata 192.168.159.0/24(rw,async,no_root_squash)

创建一个PV

Kubernetes Volume

① 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 服务器上对应的目录。

创建PVC

Kubernetes Volume

Kubernetes Volume

在pod中使用储存,在 volumes 中通过 persistentVolumeClaim 指定使用 mypvc1 申请的 Volume。

Kubernetes Volume

回收PV

kubectl delete pvc mypvc1 #删除PVC mypvc1

PV动态供给

StorageClass standard

Kubernetes Volume

StorageClass slow

Kubernetes Volume

这两个 StorageClass 都会动态创建 AWS EBS,不同在于 standard 创建的是 gp2 类型的 EBS,而 slow 创建的是 io1 类型的 EBS。

PVC 在申请 PV 时,只需要指定 StorageClass 和容量以及访问模式,比如:

Kubernetes Volume

Mysql数据库示例

1、创建PC和PVC

Kubernetes Volume

Kubernetes Volume

2、部署mysql

Kubernetes Volume

kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword #访问mysql server

【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: