Kubernetes——临时存储卷

临时存储卷

  Kubernetes 支持存储卷类型中,emptyDir 存储卷的生命周期与其所属的 Pod 对象相同,它无法脱离 Pod 对象的声明周期提供数据存储功能,因此 emptyDir 通常仅用数据缓存或临时存储。

  不过,基于 emptyDir 构建的 gitRepo 存储卷可以在 Pod 对象的生命周期起始时从相应的 Git 仓库中复制相应数据文件到底层 emptyDir 中,从而使得它具有一定意义上的持久性。

一、 emptyDir存储卷

  emptyDir 存储卷是 Pod 对象生命周期的一个临时目录,类似 Docker 上的 "docker 挂载卷",在 Pod 对象启动时即被创建,而在 Pod 对象被移除时会被一并删除。不具有持久能力的 emptyDir 存储卷只能用于某些特殊场景中,例如,同一个 Pod 内的多个容器间文件的共享,或者作为容器数据的存储目录用于数据缓存系统等。

  emptyDir 存储卷则定义于 .spec.volumes.emptyDir 嵌套字段中,字段定义如下:

apiVerson: v1
kind: Pod
metadata:
  name: vol-emptydir-pod
spec:
  volumes:
  - name: html
    emptyDir:{}
  containers:
  - name:nginx
    image:nginx:1.12-apline
	volumeMounts:
	- name: html
	  mountPath: /usr/share/nginx/html
	- name: pagengen
	  image: alpine
	volumeMounts:
	- name: html
	  mountPath: /html
	command: ["/bin/sh", "-c"]
	args:
	- while trure;do
	    echo $(hostname) $(date) >> /html/index.html;
      done

  上面的示例中定义的存储卷名称为 html,挂载于容器 nginx 的 /usr/share/nginx/html 目录,以及容器 pagegen 的 /html 目录。容器 pagegen 每隔 10秒 向存储卷上的 index.html 文件中追加一行信息,而容器 nginx 中 的 nginx 进程则以其为站点主页:

  Pod 资源的详细信息中会显示存储卷的相关状态,包括其是否创建成功(在 Events 字段中输出)、相关的类型及参数(在 Volumes 字段输出)以及容器中的挂载状态等信息(在 Containers 字段中输出)。

  emptyDir 存储也可以基于 RAM 创建 tmpfs 文件系统的存储卷,常用于容器的应用提高性能缓存,下面是一个配置示例:

volumes:
  - name: cache
    emptyDir:
      medium:Memory

二、gitRepo 存储卷

  gitRepo 存储卷可以看做是 emptyDir 存储卷的一种实际应用,使用该存储卷的 Pod 资源可以通过挂载目录访问指定的代码仓库中的数据。使用 gitRepo 存储卷的 Pod 资源在创建时,会首先创建一个空目录(emptyDir)并克隆(clone)一份指定的 Git 仓库中的数据至该目录,而后再创建容器并挂载该存储卷。

  定义 gitRepo 类型的存储卷时,其定义如下:

[root@mh-k8s-master-247-10 ~]# kubectl explain pod.spec.volumes.gitRepo
KIND:     Pod
VERSION:  v1

RESOURCE: gitRepo <Object>

DESCRIPTION:
     GitRepo represents a git repository at a particular revision. DEPRECATED:
     GitRepo is deprecated. To provision a container with a git repo, mount an
     EmptyDir into an InitContainer that clones the repo using git, then mount
     the EmptyDir into the Pod's container.

     Represents a volume that is populated with the contents of a git
     repository. Git repo volumes do not support ownership management. Git repo
     volumes support SELinux relabeling. DEPRECATED: GitRepo is deprecated. To
     provision a container with a git repo, mount an EmptyDir into an
     InitContainer that clones the repo using git, then mount the EmptyDir into
     the Pod's container.

FIELDS:
   directory	<string>
     Target directory name. Must not contain or start with '..'. If '.' is
     supplied, the volume directory will be the git repository. Otherwise, if
     specified, the volume will contain the git repository in the subdirectory
     with the given name.

   repository	<string> -required-
     Repository URL

   revision	<string>
     Commit hash for the specified revision.

[root@mh-k8s-master-247-10 ~]# 

  其可嵌套使用的字段具体包含如下三个:

    • repository <string> -required- :Git 仓库的 URL,必选字段。
    • directory <string> :目标目录名称,名称中不能包含 ".." 字符; "." 表示将仓库中的数据直接复制到卷目录中,否则,即为复制到卷目录中以用户指定的字符串为名称的子目录中。
    • revision <string> : 特定 version 的提交哈希码。

提示:

使用 gitRepo 存储卷的 Pod 资源运行的工作节点上必须安装有 Git 程序,否则克隆仓库的操作将无从完成。另外,自从 kubernetes 1.12 起,gitRepo 存储卷已经被废弃。
 
posted @ 2022-06-23 15:11  左扬  阅读(178)  评论(0编辑  收藏  举报
levels of contents