Kubernetes部分Volume类型介绍及yaml示例--emptyDir

Kubernetes部分Volume类型介绍及yaml示例--emptyDir(本地数据卷)

  1. 说明
    EmptyDir类型的volume创建于pod被调度到某个宿主机上的时候,而同一个pod内的容器都能读写EmptyDir中的同一个文件。一旦这个pod离开了这个宿主机,EmptyDir中的数据就会被永久删除。所以目前EmptyDir类型的volume主要用作临时空间,比如Web服务器写日志或者tmp文件需要的临时目录。
  2. 实战使用共享卷的标准单容器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
    ##在该节点上运行,该卷就会存在。正如卷的名字所述,它最初是空的。
  3. 实战使用共享卷的标准多容器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)共享的
  4. 参考文档
    https://www.kubernetes.org.cn/2767.html

转载于:https://blog.51cto.com/wutengfei/2125069

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值