k8s volume mysql_Kubernetes K8S之存储Volume详解

17270ce95af85a4a2d33846cd5831d99.png

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

1. 主机配置规划

d3e38cbcc6efb071cd0f3aa10949d758.png

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 值如下:

a2881ad83ad77c05c995b04b26c14e76.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值