容器和 pod 的生命周期是短暂的,会被频繁的创建好销毁,其内部数据都会被清除。
Kubernetes 中也有 Volume 的概念,与 docker volume 类似,可以实现数据的持久化、数据共享。
volume 有多种类型,emptyDir 是最简单的一种,是 host 上的一个空目录。
对于 pod 来说,emptyDir 是持久的,容器被销毁而 emptyDir 不会被影响,但对于 pod 来说,emptyDir 是短暂的,pod 被销毁,emptyDir 也会被销毁,pod 与 emptyDir 的生命周期相同。
pod 中的所有容器都可以共享 volume,可以指定各自的 mount 路径。
示例
配置文件
emptyDir.yml :
apiVersion: v1
kind: Pod
metadata:
name: producer-consumer
spec:
containers:
- image: busybox
name: producer
volumeMounts:
- mountPath: /producer_dir
name: shared-volume
args:
- /bin/sh
- -c
- echo "hi " > /producer_dir/hello ; sleep 30000
- image: busybox
name: consumer
volumeMounts:
- mountPath: /consumer_dir
name: shared-volume
args:
- /bin/sh
- -c
- cat /consumer_dir/hello ; sleep 30000
volumes:
- name: shared-volume
emptyDir: {}
- 底部
volumes
定义了一个emptyDir
类型的 volume,名字为 shared-volume。 - producer 容器把 shared-volume 挂载到自己的 /producer_dir。
- producer 向 /producer_dir/hello 写数据。
- consumer 容器把 shared-volume 挂载到自己的 /consumer_dir。
- consumer 读取 /consumer_dir/hello 内容。
启动
kubectl apply -f emptyDir.yml
查看日志:
kubectl logs producer-consumer consumer
hi
输出了 producer 写入的内容,验证了 pod 中2个容器共享 emptyDir volume。
下面看下 pod 详细信息,可以更清晰的了解容器volume挂载情况:
kubectl describe pod producer-consumer
// 返回信息
Name: producer-consumer
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: microk8s/10.0.2.15
Start Time: Thu, 13 Dec 2018 02:51:21 +0000
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"producer-consumer","namespace":"default"},"spec":{"containers":[{"arg...
Status: Running
IP: 10.1.1.207
Containers:
producer:
Container ID: docker://549e3a366cc1310d4806c4ed80a48472bf2787dac50bfe670d32745af138653c
Image: busybox
Image ID: docker-pullable://busybox@sha256:2a03a6059f21e150ae84b0973863609494aad70f0a80eaeb64bddd8d92465812
Port: <none>
Host Port: <none>
Args:
/bin/sh
-c
echo "hi " > /producer_dir/hello ; sleep 30000
State: Running
Started: Thu, 13 Dec 2018 02:51:26 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/producer_dir from shared-volume (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-n44dv (ro)
consumer:
Container ID: docker://33ea87c34907cf535ddb32114c8b1a0d856f34343f11f6648bcc75e6f3534250
Image: busybox
Image ID: docker-pullable://busybox@sha256:2a03a6059f21e150ae84b0973863609494aad70f0a80eaeb64bddd8d92465812
Port: <none>
Host Port: <none>
Args:
/bin/sh
-c
cat /consumer_dir/hello ; sleep 30000
State: Running
Started: Thu, 13 Dec 2018 02:51:31 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/consumer_dir from shared-volume (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-n44dv (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
shared-volume:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
Medium:
default-token-n44dv:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-n44dv
Optional: false
...