k8s学习(18):volume-卷

容器磁盘上的文件的声明周期是短暂的,这就使得容器中运行重要应用时会出现一些问题。首先,当容器崩溃时,kubelet会重启它,但是容器中的文件将丢失–容器以干净的状态(镜像最初的状态)重新启动。其次,在pod中同时运行多个容器时,这些容器之间通常需要共享文件。kubernetes中的volume抽象就很好的解决了这些问题

背景

kubernetes中的卷有明确的寿命–与封装它的pod相同。所以,卷的生命比pod中的所有容器都长,当这个容器重启时数据仍然得以保存。当然,当pod不再存在时,卷也将不复存在。也许更重要的是,kubernetes支持多种类型的卷,pod可以同时使用任意数量的卷

卷的类型

kubernetes支持以下类型的卷:

image-20200203013115393

emptyDir

当pod被分配给节点时,首先创建emptyDir卷,并且只要该Pod在该节点上运行,该卷就会存在。正如卷的名字所述,它最初是空的。Pod中的容器可以读取和写入emptyDir卷中的相同文件,尽管该卷可以挂载到每个容器中的相同或不同路径上。当处于任何原因从节点中删除Pod时,emptyDir中的数据将被永久删除。

emptyDir的用法:

  • 暂存空间,例如用于基于磁盘的合并排序
  • 用作长时间计算崩溃恢复时的检查点
  • Web服务器容器提供数据时,保存内容管理器容器提取的文件

举个栗子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
apiVersion: v1
kind: Pod
metadata:
name: pod-vol
spec:
containers:
- name: c1
image: hub.test.com/library/myapp:v1
ports:
- name: http
containerPort: 80
volumeMounts:
- name: html-volume
mountPath: /usr/share/nginx/html
- name: c2
image: busybox
imagePullPolicy: IfNotPresent
command: ['/bin/sh', '-c', 'sleep 600']
volumeMounts:
- name: html-volume
mountPath: /html
volumes:
- name: html-volume
emptyDir: {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@k8s-master volume]# kubectl apply -f pod.yaml 
pod/pod-vol created
[root@k8s-master volume]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-registry-test 1/1 Running 0 53m
pod-vol 2/2 Running 0 3s
[root@k8s-master volume]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod-registry-test 1/1 Running 0 53m 10.244.2.189 k8s-node02 <none> <none>
pod-vol 2/2 Running 0 15s 10.244.2.190 k8s-node02 <none> <none>
[root@k8s-master volume]# curl 10.244.2.190
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
[root@k8s-master volume]# kubectl exec -it pod-vol -c c2 -- /bin/sh
/html # date > index.html
/html # exit
[root@k8s-master volume]# curl 10.244.2.190
Sun Feb 2 17:56:42 UTC 2020

hostPath

hostPath卷将主机节点的文件系统中的文件或目录挂载到集群中

hostPath用途:

  • 运行需要访问Docker内部的容器;使用/var/lib/dockerhostPath
  • 在容器中运行cAdvisor;使用/dev/cgroupshostPath

CAdvisor:谷歌的一个docker监控服务

除了所需的path属性之外,用户还可以为hostPath指定type

image-20200203020648565

使用这种卷类型时请注意:

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

举个栗子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: c1
image: hub.test.com/library/myapp:v1
volumeMounts:
- name: test-volume
mountPath: /test
volumes:
- name: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@k8s-master volume]# kubectl create -f pod-hostpath.yaml 
pod/pod created
[root@k8s-master volume]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod 1/1 Running 0 80s 10.244.2.191 k8s-node02 <none> <none>
[root@k8s-master volume]# curl 10.244.2.191
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
# 此时由于当前node02节点的目录为空,所以curl访问失败
# 在node02节点新建index.html文件
[root@k8s-node02 test]# echo hello > /opt/test/index.html
# 访问该pod
[root@k8s-master volume]# curl 10.244.2.191
hello