Kubernetes 部分 Volume 类型介绍及 yaml 示例 --emptyDir(本地数据卷)
说明
EmptyDir 类型的 volume 创建于 pod 被调度到某个宿主机上的时候, 而同一个 pod 内的容器都能读写 EmptyDir 中的同一个文件. 一旦这个 pod 离开了这个宿主机, EmptyDir 中的数据就会被永久删除. 所以目前 EmptyDir 类型的 volume 主要用作临时空间, 比如 web 服务器写日志或者 tmp 文件需要的临时目录.
实战使用共享卷的标准单容器 POD
- # 创建 yaml 文件
- cat>> emptyDir.yaml <<EOF
- apiVersion: v1
- kind: Pod
- metadata:
- labels:
- name: test-emptypath
- role: master
- name: test-emptypath
- namespace: test
- spec:
- containers:
- - name: test-emptypath
- image: nginx:1.7.9
- volumeMounts:
- - name: log-storage
- mountPath: /tmp/
- volumes:
- - name: log-storage
- emptyDir: {}
- # 启动 emptyDir.yaml
- kubectl create -f ./emptyDir.yaml
- # 查看 Pod 运行状态
- kubectl get po -n test
NAME READY STATUS RESTARTS AGE
- test-emptypath 1/1 Running 0 3h
- ## 说明: 当 Pod 被分配给节点时, 首先创建 emptyDir 卷, 并且只要该 Pod
- ## 在该节点上运行, 该卷就会存在. 正如卷的名字所述, 它最初是空的.
实战使用共享卷的标准多容器 POD,
- # 创建 yaml 文件
- cat>> emptyDir2.yaml <<EOF
- apiVersion: v1
- kind: Pod
- metadata:
- name: datagrand
- namespace: test
- spec:
- containers:
- - name: test1
- image: nginx:1.7.9
- volumeMounts:
- - name: log-storage
- mountPath: /usr/share/nginx/html
- - name: test2
- image: centos
- volumeMounts:
- - name: log-storage
- mountPath: /html
- command: ["/bin/sh","-c"]
- args:
- - while true;do
- data>> /html/index.html;
- sleep 1;
- done
- volumes:
- - name: log-storage
- emptyDir: {}
- ## 说明: 在这个例子中, 我们定义了一个名为 HTML 的卷. 它的类型是 emptyDir,
- ## 这意味着当一个 POD 被分配到一个节点时, 卷先被创建, 并只要 Pod 在节点上
- ## 运行时, 这个卷仍存在. 正如名字所说, 它最初是空的. 第一容器运行 nginx 的
- ## 服务器并将共享卷挂载到目录 / usr /share/ nginx /html. 第二容器使用 centos
- ## 的镜像, 并将共享卷挂载到目录 / HTML. 每一秒, 第二容器添加当前日期和时
- ## 间到 index.html 文件中, 它位于共享卷. 当用户发出一个 HTTP 请求到 POD,
- ##nginx 的服务器读取该文件并将其传递给响应请求的用户.
- # 运行 yaml
- kubectl create -f ./emptyDir2.yaml
- # 查看 Pod 运行状态
- kubectl get po -n test
NAME READY STATUS RESTARTS AGE
datagrand 2/2 Running 0 22m
- # 进入容器 test1
- kubectl exec -it datagrand -c test1 /bin/bash -n test
- root@datagrand:/# cd /usr/share/nginx/html
- root@datagrand:/usr/share/nginx/html# ls
- index.html
- ## 添加内容
- root@datagrand:/usr/share/nginx/html# echo "this is a test">> index.html
- # 进入容器 test2
- kubectl exec -it datagrand -c test2 /bin/bash -n test
- [root@datagrand /]# cd html
- [root@datagrand html]# ls
- index.html
- [root@datagrand html]# cat index.html
- this is a test
- ##emptyDir 卷是两个容器 (test1 和 test2) 共享的
参考文档
https://www.kubernetes.org.cn/2767.html
来源: http://www.bubuko.com/infodetail-2631962.html