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

1. 主机配置规划

2. Volume概述

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

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

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

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

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

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

3. 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。

本文只说emptyDir和hostPath存储。

4. emptyDir卷

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

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

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

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

4.1 emptyDir的一些用途:

缓存空间,例如基于磁盘的归并排序

为耗时较长的计算任务提供检查点,以便任务能方便地从崩溃前状态恢复执行

在 Web 服务器容器服务数据时,保存内容管理器容器获取的文件

4.2 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-emptydir

namespace: default

spec:

containers:

- name: myapp-pod

image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1

imagePullPolicy: IfNotPresent

volumeMounts:

- mountPath: /cache

name: cache-volume

- name: busybox-pod

image: registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24

imagePullPolicy: IfNotPresent

command: ["/bin/sh", "-c", "sleep 3600"]

volumeMounts:

- mountPath: /test/cache

name: cache-volume

volumes:

- name: cache-volume

emptyDir: {}

启动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

[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:

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://d45663776b40a24e7cfc3cf46cb08cf3ed6b98b023a5d2cb5f42bee2234c7338

Image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1

Image ID: docker-pullable://10.0.0.110:5000/k8s-secret/myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e

Port:

Host Port:

State: Running

Started: Fri, 12 Jun 2020 22:49:12 +0800

Ready: True

Restart Count: 0

Environment:

Mounts:

/cache from cache-volume (rw) ##### 挂载信息

/var/run/secrets/kubernetes.io/serviceaccount from default-token-v48g4 (ro)

busybox-pod:

Container ID: docker://c2917ba30c3322fb0caead5d97476b341e691f9fb1990091264364b8cd340512

Image: registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24

Image ID: docker-pullable://registry.cn-beijing.aliyuncs.com/ducafe/busybox@sha256:f73ae051fae52945d92ee20d62c315306c593c59a429ccbbdcba4a488ee12269

Port:

Host Port:

Command:

/bin/sh

-c

sleep 3600

State: Running

Started: Fri, 12 Jun 2020 22:49:12 +0800

Ready: True

Restart Count: 0

Environment:

Mounts:

/test/cache from cache-volume (rw) ##### 挂载信息

/var/run/secrets/kubernetes.io/serviceaccount from default-token-v48g4 (ro)

Conditions:

Type Status

Initialized True

Ready True

ContainersReady True

PodScheduled True

Volumes:

cache-volume:

Type: EmptyDir (a temporary directory that shares a pod's lifetime)

Medium:

SizeLimit:

default-token-v48g4:

Type: Secret (a volume populated by a Secret)

SecretName: default-token-v48g4

Optional: false

QoS Class: BestEffort

Node-Selectors:

Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s

node.kubernetes.io/unreachable:NoExecute for 300s

Events:

Type Reason Age From Message

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

Normal Scheduled 3s default-scheduler Successfully assigned default/pod-emptydir to k8s-node02

Normal Pulled 2s kubelet, k8s-node02 Container image "registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1" already present on machine

Normal Created 2s kubelet, k8s-node02 Created container myapp-pod

Normal Started 2s kubelet, k8s-node02 Started container myapp-pod

Normal Pulled 2s kubelet, k8s-node02 Container image "registry.cn-beijing.aliyuncs.com/google_registry/busybox:1.24" already present on machine

Normal Created 2s kubelet, k8s-node02 Created container busybox-pod

Normal Started 2s kubelet, k8s-node02 Started container busybox-pod

4.3 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卷。

5. hostPath卷

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

5.1 hostPath 的一些用法有

运行一个需要访问 Docker 引擎内部机制的容器;请使用 hostPath 挂载 /var/lib/docker 路径。

在容器中运行 cAdvisor 时,以 hostPath 方式挂载 /sys。

允许 Pod 指定给定的 hostPath 在运行 Pod 之前是否应该存在,是否应该创建以及应该以什么方式存在。

5.2 支持类型

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

5.3 注意事项

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

具有相同配置(例如从 podTemplate 创建)的多个 Pod 会由于节点上文件的不同而在不同节点上有不同的行为。

当 Kubernetes 按照计划添加资源感知的调度时,这类调度机制将无法考虑由 hostPath 卷使用的资源。

基础主机上创建的文件或目录只能由 root 用户写入。需要在 特权容器 中以 root 身份运行进程,或者修改主机上的文件权限以便容器能够写入 hostPath 卷。

5.4 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-hostpath

namespace: default

spec:

containers:

- name: myapp-pod

image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1

imagePullPolicy: IfNotPresent

volumeMounts:

- name: hostpath-dir-volume

mountPath: /test-k8s/hostpath-dir

- name: hostpath-file-volume

mountPath: /test/hostpath-file/test.conf

volumes:

- name: hostpath-dir-volume

hostPath:

# 宿主机目录

path: /k8s/hostpath-dir

# hostPath 卷指定 type,如果目录不存在则创建(可创建多层目录)

type: DirectoryOrCreate

- name: hostpath-file-volume

hostPath:

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

[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:

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://8cc87217fb483288067fb6d227c46aa890d02f75cae85c6d110646839435ab96

Image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1

Image ID: docker-pullable://registry.cn-beijing.aliyuncs.com/google_registry/myapp@sha256:9eeca44ba2d410e54fccc54cbe9c021802aa8b9836a0bcf3d3229354e4c8870e

Port:

Host Port:

State: Running

Started: Sat, 13 Jun 2020 16:12:17 +0800

Ready: True

Restart Count: 0

Environment:

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 Status

Initialized True

Ready True

ContainersReady True

PodScheduled True

Volumes:

hostpath-dir-volume:

Type: HostPath (bare host directory volume)

Path: /k8s/hostpath-dir

HostPathType: DirectoryOrCreate

hostpath-file-volume:

Type: HostPath (bare host directory volume)

Path: /k8s2/hostpath-file/test.conf

HostPathType: FileOrCreate

default-token-v48g4:

Type: Secret (a volume populated by a Secret)

SecretName: default-token-v48g4

Optional: false

QoS Class: BestEffort

Node-Selectors:

Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s

node.kubernetes.io/unreachable:NoExecute for 300s

Events:

Type Reason Age From Message

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

Normal Scheduled default-scheduler Successfully assigned default/pod-hostpath to k8s-node01

Normal Pulled 12m kubelet, k8s-node01 Container image "registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1" already present on machine

Normal Created 12m kubelet, k8s-node01 Created container myapp-pod

Normal Started 12m kubelet, k8s-node01 Started container myapp-pod

5.5 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====

k8s volume mysql_Kubernetes K8S之存储Volume详解相关推荐

  1. Kubernetes K8S之存储Secret详解

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

  2. Kubernetes K8S之Taints污点与Tolerations容忍详解

    Kubernetes K8S之Taints污点与Tolerations容忍详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master C ...

  3. K8s系列之:kubectl子命令详解edit

    K8s系列之:kubectl子命令详解edit 在服务器上使用默认编辑器资源,使用这个命令可以编辑多个对象,然后一次性地应用更改. #编辑名为nginx的服务 kubectl edit svc ngi ...

  4. K8s系列之:kubectl子命令详解explain

    K8s系列之:kubectl子命令详解explain 资源记录.获取资源及其字段的文档. kubectl explain pods#获取资源的特定字段的文档 kubectl explain pods. ...

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

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

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

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

  7. Mysql存储引擎详解(MyISAM与InnoDB的区别)

    Mysql存储引擎详解(MyISAM与InnoDB的区别) 存储引擎     MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平 ...

  8. mysql数据库存储引擎和索引的描述_Mysql InnoDB引擎的索引与存储结构详解

    前言 在Oracle 和SQL Server等数据库中只有一种存储引擎,所有数据存储管理机制都是一样的. 而MySql数据库提供了多种存储引擎.用户可以根据不同的需求为数据表选择不同的存储引擎,用户也 ...

  9. 浏览器对象存储数据详解

    浏览器对象存储数据详解 前言 随着需求的发展,浏览器的功能正变的越来越强大,在本地存储数据可以极大的方便人们进行各种操作,如localStroage/sessionStroage等,下面我就记录在项目 ...

最新文章

  1. Android SQLite开发调试工具 Android Debug Database
  2. proto的介绍和基础使用
  3. SpringCloud个人笔记-01-Eureka初体验
  4. matlab 仿真钢琴,用Matlab模拟钢琴的声音
  5. [P2396] yyy loves Maths VII
  6. Python数据分析Numpy库方法简介(一)
  7. Softmax与Cross-entropy的求导
  8. ARM给服务器厂商更多创新机会
  9. Mp3原理及文件格式解析(from)
  10. 【Tkinter界面化小程序】用Python做一款免费音乐下载器、无广告无弹窗、清爽超流畅哦~
  11. Linux执行sql文件
  12. Java JSON转Excel工具类
  13. python爬取网易云音乐数据
  14. 解决“虚拟机使用的是此版本 VMware Workstation 不支持的硬件版本”的问题。
  15. 高一计算机课期中考试总结反思,信息技术期中考试试卷分析与反思
  16. 川蔚蓝:追求品质、创新发展;以诚为根,以客为本
  17. 网络wifi已连接显示不可上网的解决方法教程
  18. 动画多个元素过渡 多个组件过渡 以及过渡模式
  19. 身份证号第18位(效验码)计算方法 用于检测身份证号是否正确
  20. 网易易盾web端H5接入

热门文章

  1. php怎么做考勤行事例,PHP开发制作一个简单的活动日程表Calendar
  2. c++学习笔记:记在类前
  3. 小程序和App同时拥有?两者兼得的一种技术方案
  4. 福建师范大学计算机专业研究生,福建师范大学研究生专业排名
  5. 【Unity URP】手写PBR:从build-in转到URP
  6. java计算机毕业设计美容美发店会员管理系统源码+系统+数据库+lw文档+mybatis+运行部署
  7. Photoshop脚本 照片滤镜的使用
  8. 基数估计算法(一):Flajolet-Martin算法
  9. python财务人员有必要学吗_会计难学吗?我侄女不知道做什么工作好,我觉得会计找工作容易,打算叫他去学,可是就是怕证难考...
  10. Flutter Web在美团外卖的实践