K8S之存储Volume概述与说明,并详解常用Volume示例

主机配置规划

服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟)
k8s-master CentOS7.7 2C/4G/20G 172.16.1.110 10.0.0.110
k8s-node01 CentOS7.7 2C/4G/20G 172.16.1.111 10.0.0.111
k8s-node02 CentOS7.7 2C/4G/20G 172.16.1.112 10.0.0.112

Volume概述

在容器中的文件在磁盘上是临时存放的,当容器关闭时这些临时文件也会被一并清除。这给容器中运行的特殊应用程序带来一些问题。

首先,当容器崩溃时,kubelet 将重新启动容器,容器中的文件将会丢失——因为容器会以干净的状态重建。

其次,当在一个 Pod 中同时运行多个容器时,常常需要在这些容器之间共享文件。

Kubernetes 抽象出 Volume 对象来解决这两个问题。

Kubernetes Volume卷具有明确的生命周期——与包裹它的 Pod 相同。 因此,Volume比 Pod 中运行的任何容器的存活期都长,在容器重新启动时数据也会得到保留。 当然,当一个 Pod 不再存在时,Volume也将不再存在。更重要的是,Kubernetes 可以支持许多类型的Volume卷,Pod 也能同时使用任意数量的Volume卷。

使用卷时,Pod 声明中需要提供卷的类型 (.spec.volumes 字段)和卷挂载的位置 (.spec.containers.volumeMounts 字段).

Volume类型

Kubernetes 支持下列类型的卷:

awsElasticBlockStore
azureDisk
azureFile
cephfs
cinder
configMap
csi
downwardAPI
emptyDir
fc (fibre channel)
flexVolume
flocker
gcePersistentDisk
gitRepo (deprecated)
glusterfs
hostPath
iscsi
local
nfs
persistentVolumeClaim
projected
portworxVolume
quobyte
rbd
scaleIO
secret
storageos
vsphereVolume

这里我们只介绍常用的存储,包括:Secret、ConfigMap、emptyDir、hostPath。

其中Secret参考文章:「Kubernetes K8S之存储Secret详解」

ConfigMap参考文章:「Kubernetes K8S之存储ConfigMap详解」

本文只说emptyDir和hostPath存储。

emptyDir卷

当 Pod 指定到某个节点上时,首先创建的是一个 emptyDir 卷,并且只要 Pod 在该节点上运行,卷就一直存在。就像它的名称表示的那样,卷最初是空的。

尽管 Pod 中每个容器挂载 emptyDir 卷的路径可能相同也可能不同,但是这些容器都可以读写 emptyDir 卷中相同的文件。

如果Pod中有多个容器,其中某个容器重启,不会影响emptyDir 卷中的数据。当 Pod 因为某些原因被删除时,emptyDir 卷中的数据也会永久删除。

注意:容器崩溃并不会导致 Pod 被从节点上移除,因此容器崩溃时 emptyDir 卷中的数据是安全的。

emptyDir的一些用途:

  • 缓存空间,例如基于磁盘的归并排序
  • 为耗时较长的计算任务提供检查点,以便任务能方便地从崩溃前状态恢复执行
  • 在 Web 服务器容器服务数据时,保存内容管理器容器获取的文件

emptyDir示例

yaml文件

[root@k8s-master emptydir]# pwd
/root/k8s_practice/emptydir
[root@k8s-master emptydir]# cat pod_emptydir.yaml
apiVersion: v1
kind: Pod
metadata:name: pod-emptydirnamespace: default
spec:containers:- name: myapp-podimage: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1imagePullPolicy: IfNotPresentvolumeMounts:- mountPath: /cachename: cache-volume- name: busybox-podimage: registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24imagePullPolicy: IfNotPresentcommand: ["/bin/sh", "-c", "sleep 3600"]volumeMounts:- mountPath: /test/cachename: cache-volumevolumes:- name: cache-volumeemptyDir: {}

启动pod,并查看状态

[root@k8s-master emptydir]# kubectl apply -f pod_emptydir.yaml
pod/pod-emptydir created
[root@k8s-master emptydir]#
[root@k8s-master emptydir]# kubectl get pod -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP             NODE         NOMINATED NODE   READINESS GATES
pod-emptydir   2/2     Running   0          10s   10.244.2.166   k8s-node02   <none>           <none>
[root@k8s-master emptydir]#
[root@k8s-master emptydir]# kubectl describe pod pod-emptydir
Name:         pod-emptydir
Namespace:    default
Priority:     0
Node:         k8s-node02/172.16.1.112
Start Time:   Fri, 12 Jun 2020 22:49:11 +0800
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"pod-emptydir","namespace":"default"},"spec":{"containers":[{"image":"...
Status:       Running
IP:           10.244.2.166
IPs:IP:  10.244.2.166
Containers:myapp-pod:Container ID:   docker://d45663776b40a24e7cfc3cf46cb08cf3ed6b98b023a5d2cb5f42bee2234c7338Image:          registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1Image ID:       docker-pullable://10.0.0.110:5000/k8s-secret/myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870ePort:           <none>Host Port:      <none>State:          RunningStarted:      Fri, 12 Jun 2020 22:49:12 +0800Ready:          TrueRestart Count:  0Environment:    <none>Mounts:/cache from cache-volume (rw)  ##### 挂载信息/var/run/secrets/kubernetes.io/serviceaccount from default-token-v48g4 (ro)busybox-pod:Container ID:  docker://c2917ba30c3322fb0caead5d97476b341e691f9fb1990091264364b8cd340512Image:         registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24Image ID:      docker-pullable://registry.cn-beijing.aliyuncs.com/ducafe/busybox@sha256:f73ae051fae52945d92ee20d62c315306c593c59a429ccbbdcba4a488ee12269Port:          <none>Host Port:     <none>Command:/bin/sh-csleep 3600State:          RunningStarted:      Fri, 12 Jun 2020 22:49:12 +0800Ready:          TrueRestart Count:  0Environment:    <none>Mounts:/test/cache from cache-volume (rw)  ##### 挂载信息/var/run/secrets/kubernetes.io/serviceaccount from default-token-v48g4 (ro)
Conditions:Type              StatusInitialized       True Ready             True ContainersReady   True PodScheduled      True
Volumes:cache-volume:Type:       EmptyDir (a temporary directory that shares a pod's lifetime)Medium:     SizeLimit:  <unset>default-token-v48g4:Type:        Secret (a volume populated by a Secret)SecretName:  default-token-v48g4Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300snode.kubernetes.io/unreachable:NoExecute for 300s
Events:Type    Reason     Age   From                 Message----    ------     ----  ----                 -------Normal  Scheduled  3s    default-scheduler    Successfully assigned default/pod-emptydir to k8s-node02Normal  Pulled     2s    kubelet, k8s-node02  Container image "registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1" already present on machineNormal  Created    2s    kubelet, k8s-node02  Created container myapp-podNormal  Started    2s    kubelet, k8s-node02  Started container myapp-podNormal  Pulled     2s    kubelet, k8s-node02  Container image "registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24" already present on machineNormal  Created    2s    kubelet, k8s-node02  Created container busybox-podNormal  Started    2s    kubelet, k8s-node02  Started container busybox-pod

emptyDir验证

在pod中的myapp-pod容器内操作

[root@k8s-master emptydir]# kubectl exec -it pod-emptydir -c myapp-pod -- sh
/ # cd /cache
/cache #
/cache # pwd
/cache
/cache #
/cache # date >> data.info
/cache # ls -l
total 4
-rw-r--r--    1 root     root            29 Jun 12 14:53 data.info
/cache # cat data.info
Fri Jun 12 14:53:27 UTC 2020

在pod中的busybox-pod容器内操作

[root@k8s-master emptydir]# kubectl exec -it pod-emptydir -c busybox-pod -- sh
/ # cd /test/cache
/test/cache # ls -l
total 4
-rw-r--r--    1 root     root            29 Jun 12 14:53 data.info
/test/cache # cat data.info
Fri Jun 12 14:53:27 UTC 2020
/test/cache #
/test/cache # echo "===" >> data.info
/test/cache # date >> data.info
/test/cache # cat data.info
Fri Jun 12 14:53:27 UTC 2020
===
Fri Jun 12 14:56:05 UTC 2020

由上可见,一个Pod中多个容器可共享同一个emptyDir卷。

hostPath卷

hostPath 卷能将主机node节点文件系统上的文件或目录挂载到你的 Pod 中。 虽然这不是大多数 Pod 需要的,但是它为一些应用程序提供了强大的逃生舱。

hostPath 的一些用法有

  • 运行一个需要访问 Docker 引擎内部机制的容器;请使用 hostPath 挂载 /var/lib/docker 路径。
  • 在容器中运行 cAdvisor 时,以 hostPath 方式挂载 /sys。
  • 允许 Pod 指定给定的 hostPath 在运行 Pod 之前是否应该存在,是否应该创建以及应该以什么方式存在。

支持类型

除了必需的 path 属性之外,用户可以选择性地为 hostPath 卷指定 type。支持的 type 值如下:

取值 行为
空字符串(默认)用于向后兼容,这意味着在安装 hostPath 卷之前不会执行任何检查
DirectoryOrCreate 如果指定的路径不存在,那么将根据需要创建空目录,权限设置为 0755,具有与 Kubelet 相同的组和所有权
Directory 给定的路径必须存在
FileOrCreate 如果给定路径的文件不存在,那么将在那里根据需要创建空文件,权限设置为 0644,具有与 Kubelet 相同的组和所有权【前提:文件所在目录必须存在;目录不存在则不能创建文件】
File 给定路径上的文件必须存在
Socket 在给定路径上必须存在的 UNIX 套接字
CharDevice 在给定路径上必须存在的字符设备
BlockDevice 在给定路径上必须存在的块设备

注意事项

当使用这种类型的卷时要小心,因为:

  • 具有相同配置(例如从 podTemplate 创建)的多个 Pod 会由于节点上文件的不同而在不同节点上有不同的行为。
  • 当 Kubernetes 按照计划添加资源感知的调度时,这类调度机制将无法考虑由 hostPath 卷使用的资源。
  • 基础主机上创建的文件或目录只能由 root 用户写入。需要在 特权容器 中以 root 身份运行进程,或者修改主机上的文件权限以便容器能够写入 hostPath 卷。

hostPath示例

yaml文件

[root@k8s-master hostpath]# pwd
/root/k8s_practice/hostpath
[root@k8s-master hostpath]# cat pod_hostpath.yaml
apiVersion: v1
kind: Pod
metadata:name: pod-hostpathnamespace: default
spec:containers:- name: myapp-podimage: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1imagePullPolicy: IfNotPresentvolumeMounts:- name: hostpath-dir-volumemountPath: /test-k8s/hostpath-dir- name: hostpath-file-volumemountPath: /test/hostpath-file/test.confvolumes:- name: hostpath-dir-volumehostPath:# 宿主机目录path: /k8s/hostpath-dir# hostPath 卷指定 type,如果目录不存在则创建(可创建多层目录)type: DirectoryOrCreate- name: hostpath-file-volumehostPath:path: /k8s2/hostpath-file/test.conf# 如果文件不存在则创建。 前提:文件所在目录必须存在  目录不存在则不能创建文件type: FileOrCreate

启动pod,并查看状态

[root@k8s-master hostpath]# kubectl apply -f pod_hostpath.yaml
pod/pod-hostpath created
[root@k8s-master hostpath]#
[root@k8s-master hostpath]# kubectl get pod -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP             NODE         NOMINATED NODE   READINESS GATES
pod-hostpath   1/1     Running   0          17s   10.244.4.133   k8s-node01   <none>           <none>
[root@k8s-master hostpath]#
[root@k8s-master hostpath]# kubectl describe pod pod-hostpath
Name:         pod-hostpath
Namespace:    default
Priority:     0
Node:         k8s-node01/172.16.1.111
Start Time:   Sat, 13 Jun 2020 16:12:15 +0800
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"pod-hostpath","namespace":"default"},"spec":{"containers":[{"image":"...
Status:       Running
IP:           10.244.4.133
IPs:IP:  10.244.4.133
Containers:myapp-pod:Container ID:   docker://8cc87217fb483288067fb6d227c46aa890d02f75cae85c6d110646839435ab96Image:          registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1Image ID:       docker-pullable://registry.cn-beijing.aliyuncs.com/google_registry/myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870ePort:           <none>Host Port:      <none>State:          RunningStarted:      Sat, 13 Jun 2020 16:12:17 +0800Ready:          TrueRestart Count:  0Environment:    <none>Mounts:/test-k8s/hostpath-dir from hostpath-dir-volume (rw)/test/hostpath-file/test.conf from hostpath-file-volume (rw)/var/run/secrets/kubernetes.io/serviceaccount from default-token-v48g4 (ro)
Conditions:Type              StatusInitialized       True Ready             True ContainersReady   True PodScheduled      True
Volumes:hostpath-dir-volume:Type:          HostPath (bare host directory volume)Path:          /k8s/hostpath-dirHostPathType:  DirectoryOrCreatehostpath-file-volume:Type:          HostPath (bare host directory volume)Path:          /k8s2/hostpath-file/test.confHostPathType:  FileOrCreatedefault-token-v48g4:Type:        Secret (a volume populated by a Secret)SecretName:  default-token-v48g4Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300snode.kubernetes.io/unreachable:NoExecute for 300s
Events:Type    Reason     Age        From                 Message----    ------     ----       ----                 -------Normal  Scheduled  <unknown>  default-scheduler    Successfully assigned default/pod-hostpath to k8s-node01Normal  Pulled     12m        kubelet, k8s-node01  Container image "registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1" already present on machineNormal  Created    12m        kubelet, k8s-node01  Created container myapp-podNormal  Started    12m        kubelet, k8s-node01  Started container myapp-pod

hostPath验证

宿主机操作

根据pod,在k8s-node01节点宿主机操作【因为Pod分配到了该节点】

# 对挂载的目录操作
[root@k8s-node01 hostpath-dir]# pwd
/k8s/hostpath-dir
[root@k8s-node01 hostpath-dir]# echo "dir" >> info
[root@k8s-node01 hostpath-dir]# date >> info
[root@k8s-node01 hostpath-dir]# cat info
dir
Sat Jun 13 16:22:37 CST 2020
# 对挂载的文件操作
[root@k8s-node01 hostpath-file]# pwd
/k8s2/hostpath-file
[root@k8s-node01 hostpath-file]# echo "file" >> test.conf
[root@k8s-node01 hostpath-file]# date >> test.conf
[root@k8s-node01 hostpath-file]#
[root@k8s-node01 hostpath-file]# cat test.conf
file
Sat Jun 13 16:23:05 CST 2020
在Pod 容器中操作
# 进入pod 中的指定容器【如果只有一个容器,那么可以不指定容器】
[root@k8s-master hostpath]# kubectl exec -it pod-hostpath -c myapp-pod -- /bin/sh
##### 对挂载的目录操作
/ # cd /test-k8s/hostpath-dir
/test-k8s/hostpath-dir # ls -l
total 4
-rw-r--r--    1 root     root            33 Jun 13 08:22 info
/test-k8s/hostpath-dir # cat info
dir
Sat Jun 13 16:22:37 CST 2020
/test-k8s/hostpath-dir #
/test-k8s/hostpath-dir # date >> info
/test-k8s/hostpath-dir # cat info
dir
Sat Jun 13 16:22:37 CST 2020
Sat Jun 13 08:26:10 UTC 2020
##### 对挂载的文件操作
# cd /test/hostpath-file/
/test/hostpath-file # cat test.conf
file
Sat Jun 13 16:23:05 CST 2020
/test/hostpath-file # echo "file====" >> test.conf
/test/hostpath-file # cat test.conf
file
Sat Jun 13 16:23:05 CST 2020
file====

相关阅读

1、Kubernetes K8S之存储Secret详解

2、Kubernetes K8S之存储ConfigMap详解

3、官网Volume详解


———END——— 如果觉得不错就关注下呗 (-^O^-) !

Kubernetes K8S之存储Volume详解相关推荐

  1. Kubernetes K8S之存储Secret详解

    Kubernetes K8S之存储Secret详解 Secret概述 Secret类型 Service Account Opaque Secret 创建secret 将Secret挂载到Volume中 ...

  2. Kubernetes K8S之存储PV-PVC详解

    K8S之存储PV-PVC概述与说明,并详解常用PV-PVC示例 概述 与管理计算实例相比,管理存储是一个明显的问题.PersistentVolume子系统为用户和管理员提供了一个API,该API从如何 ...

  3. k8s volume mysql_Kubernetes K8S之存储Volume详解

    K8S之存储Volume概述与说明,并详解常用Volume示例 1. 主机配置规划 2. Volume概述 在容器中的文件在磁盘上是临时存放的,当容器关闭时这些临时文件也会被一并清除.这给容器中运行的 ...

  4. kubernetes系列10—存储卷详解

    kubernetes系列10-存储卷详解 1.认识存储卷 1.1 背景 默认情况下容器中的磁盘文件是非持久化的,容器中的磁盘的生命周期是短暂的,这就带来了一系列的问题:第一,当一个容器损坏之后,kub ...

  5. K8S之存储PV-PVC详解

    概述 与管理计算实例相比,管理存储是一个明显的问题.PersistentVolume子系统为用户和管理员提供了一个API,该API从如何使用存储中抽象出如何提供存储的详细信息.为此,我们引入了两个新的 ...

  6. k8s学习笔记(10)--- kubernetes核心组件之controller manager详解

    kubernetes核心组件之controller manager详解 一.Controller Manager简介 1.1 Replication Controller 1.2 Node Contr ...

  7. Kubernetes(K8s)基本概念:Volume(存储卷)、Persistent Volume

    Kubernetes(K8s)基本概念:Volume(存储卷) 一.Volume(存储卷) Volume是Pod中能够被多个容器访问的共享目录. K8s中的Volume定义在Pod上,然后被一个Pod ...

  8. k8s安装和部署详解

    k8s安装和部署详解 文章目录 k8s安装和部署详解 kubernetes官方提供的三种部署方式 minikube kubeadm 二进制包 使用kubeadm方式安装 1.准备环境 2.确认dock ...

  9. 云服务器存储扩容详解

    云服务器存储扩容详解 2020-01-20阅读 6640 环境说明 Linux操作系统:CentOS Linux release 7.6.1810 (Core) 平台环境:腾讯云CVM,CBS 本教程 ...

最新文章

  1. java基础学习(5)-continue与break
  2. redis集群扩容和缩容_Full-Stack-Notes
  3. GET和POST的真正区别
  4. 国防科技大学计算机学院教员,我国最好的四所科技大学,第一名排名世界前列,还有一所是铁饭碗...
  5. python 菜鸟-Python 运算符
  6. python和java选择哪个-Python与Java-你首选哪个?
  7. hadoop-执行mapreduce时主机名非法的处理
  8. java 权限管理都用什么_java权限管理框架有哪些?
  9. 一套数据中心弱电系统安全运维服务方案,完整版素材!
  10. python 小说爬虫_初次尝试python爬虫,爬取小说网站的小说。
  11. 在VB.NET中生成随机数
  12. 安装opensuse的笔记-重庆linux开源组织
  13. 华为鸿蒙系统穿戴app,华为应该如何盘活鸿蒙系统?
  14. 【CXY】JAVA基础 之 Collections
  15. htmllt;pregt;标签,聊一聊HTML pre标签
  16. 技术知识库——C1见习能力认证(应用软件开发的基础知识)
  17. 亚马逊、Facebook在印度遭遇的最大阻力竟是中国!
  18. 常用正则表达式爬取网页信息及分析HTML标签总结
  19. 计算机考试操作题怎么做,计算机考试操作题的做法.doc
  20. VLAN端口属性详解

热门文章

  1. Python制做动态图
  2. 产品经理之如何快速阐释产品价值(FABE模型)
  3. 虚拟桌面:VmwareVDI架构详解
  4. 查找并下载开放的音乐数据(.mp3)
  5. 线性表之线性表与数组的区别
  6. 途志传媒:直播电商如何维护消费者权益?抖音推出安心购
  7. IDEA新建项目配置tomcat
  8. 如何配置一台以机器学习、深度学习为用途的工作站?
  9. 使用百度云GPU主机做深度学习
  10. 利用闲置电脑安装虚拟机搭建hadoop集群