前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >k8s--kubernetes存储之Volume

k8s--kubernetes存储之Volume

作者头像
eadela
发布2019-12-11 14:12:19
6090
发布2019-12-11 14:12:19
举报
文章被收录于专栏:eadelaeadela

Volume

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

背景

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

卷的类型

Kubernetes支持以下类型的卷:

  • awsElasticBlockstore azureDisk azureFile cephfs csi downwardAPI emptyDin
  • fc flocker gcePersistentDisk gitRepo glusterfs hostPath iscsi local nfs
  • persistentVolumeClaim projected portworxVolume quobyte rbd scalelo secret
  • storageos vsphereVolume

emptyDir

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

emptyoir 的用法有:

  • 暂存空间,例如用于基于磁盘的合并排序
  • 用作长时间计算崩溃恢复时的检查点
  • Web服务器容器提供数据时,保存内容管理器容器提取的文件
代码语言:javascript
复制
apiVersion: v1 
kind: Pod 
metadata: 
  name: test-pd 
spec:
  containers:
    - image: k8s.gcr.io/test-webserver
      name; test-container 
      volumeMounts: 
        - mountPath: /cache 
          name: cache-volume 
  volumes:
    - name: cache-volume 
      emptyDir: {}
代码语言:javascript
复制
apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
    - image: wangyanglinux/myapp:v1
      name: test-container
      volumeMounts:
        - mountPath: /cache
          name: cache-volume
    - image: busybox
      name: liveness-httpget-container
      imagePullPolicy: IfNotPresent
      command: ["/bin/sh","-c","sleep 3600s"]
      volumeMounts:
        - mountPath: /test
          name: cache-volume
  volumes:
    - name: cache-volume
      emptyDir: {}
​
代码语言:javascript
复制
kubectl exec test-pd -c test-container -it -- /bin/sh
kubectl exec test-pd -c liveness-httpget-container  -it -- /bin/sh
代码语言:javascript
复制
[root@k8s-master01 ~]# kubectl exec test-pd -c test-container -it -- /bin/sh
/ # ls
bin    dev    home   media  proc   run    srv    tmp    var
cache  etc    lib    mnt    root   sbin   sys    usr
/ # cd test
/bin/sh: cd: can't cd to test
/ # cd cache/
/cache # ls
index.html
/cache # date >> index.html 
/cache # cat index.html 
Tue Dec  3 20:20:59 UTC 2019
Tue Dec  3 20:21:49 UTC 2019
​

hostPath

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

hostpath的用途如下:

  • 运行需要访问Docker内部的容器;使用/var/lib/docker的hostpath
  • 在容器中运行cAdvisor;使用/dev/cgroups的hostPath
  • 允许pod指定给定的hostPath是否应该在pod运行之前存在,是否应该创建,以及它应该以什么形式存在

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

使用这种卷类型是请注意,因为:

  • 由于每个节点上的文件都不同,具有相同配置(例如从podTemplate创建的)的pod在不同节点上的行为可能会有所不同
  • 当Kubernetes按照计划添加资源感知调度时,将无法考虑nostPath使用的资源
  • 在底层主机上创建的文件或目录只能由root写入。您需要在特权容器中以root身份运行进程,或修改主机上的文件权限以便写入hostPath卷
代码语言:javascript
复制
apiVersion: v1
kind: Pod
metadata:
  name: test-pd2
spec:
  containers:
    - image: wangyanglinux/myapp:v1
      name: test-container
      volumeMounts:
        - mountPath: /test-pd
          name: test-volume
  volumes:
    - name: test-volume
      hostPath:
        # directory location on host 
        path: /data
        # this field is optional 
        type: Directory
代码语言:javascript
复制
[root@k8s-master01 volume]# kubectl exec test-pd2 -c test-container  -it -- /bin/sh
/ # cd /test-pd/
/test-pd # date > index.html
/test-pd # cat index.html 
Tue Dec  3 20:50:55 UTC 2019
2019年 12月 04日 星期三 04:51:17 CST
/test-pd # exit
​
代码语言:javascript
复制
[root@k8s-node01 ~]# mkdir /data
[root@k8s-node01 ~]# cd /data
[root@k8s-node01 data]# ls
index.html
[root@k8s-node01 data]# cat index.html 
Tue Dec  3 20:50:55 UTC 2019
[root@k8s-node01 data]# date >> index.html 
[root@k8s-node01 data]# cat index.html 
Tue Dec  3 20:50:55 UTC 2019
2019年 12月 04日 星期三 04:51:17 CST
代码语言:javascript
复制
CrashLoopBackOff:
running:
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Volume
    • 背景
      • 卷的类型
        • emptyDir
          • hostPath
          相关产品与服务
          容器服务
          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档