首页 > 其他分享 >Kubernetes Volumes 笔记

Kubernetes Volumes 笔记

时间:2022-12-15 00:44:40浏览次数:72  
标签:Kubernetes 笔记 hostpath Volumes hostPath test pod root Pod

Volumes

官方文档

介绍

Container 中的文件在磁盘上是临时存放的,这给 Container 中运行的较重要的应用程序带来一些问题。

  1. 当容器崩溃时文件丢失。 kubelet 会重新启动容器,但容器会以干净的状态重启。
  2. 当Pod中同时运行多个容器,容器之间需要共享文件时。

背景

Kubernetes 支持很多类型的卷。 Pod 可以同时使用任意数目的卷类型。 临时卷类型的生命周期与 Pod 相同,但持久卷可以比 Pod 的存活期长。 当 Pod 不再存在时,Kubernetes 也会销毁临时卷;不过 Kubernetes 不会销毁持久卷。 对于给定 Pod 中任何类型的卷,在容器重启期间数据都不会丢失。

卷的核心是一个目录,其中可能存有数据,Pod 中的容器可以访问该目录中的数据。 所采用的特定的卷类型将决定该目录如何形成的、使用何种介质保存数据以及目录中存放的内容。

使用卷时, 在 .spec.volumes 字段中设置为 Pod 提供的卷,并在 .spec.containers[*].volumeMounts 字段中声明卷在容器中的挂载位置。

容器中的进程看到的文件系统视图是由它们的容器镜像的初始内容以及挂载在容器中的卷(如果定义了的话)所组成的。

其中根文件系统同容器镜像的内容相吻合。 任何在该文件系统下的写入操作,如果被允许的话,都会影响接下来容器中进程访问文件系统时所看到的内容。

EmptyDir

使用emptyDir,当Pod分配到Node上时,将会创建emptyDir,并且只要Node上的Pod一直运行,Volume就会一直存。当Pod(不管任何原因)从Node上被删除时,emptyDir也同时会删除,存储的数据也将永久删除。注:删除容器不影响emptyDir。

pod_empydir.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-emptydir
  namespace: default
spec:
  containers:
  - name: busybox1
    image: busybox 
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh", "-c", "sleep 3600"]
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  - name: busybox2
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh", "-c", "sleep 3600"]
    volumeMounts:
    - mountPath: /test/cache
      name: cache-volume
  volumes:
  - name: cache-volume
    emptyDir: {}

创建并查看结果

[root@master01 volumes]# kubectl apply -f pod_emptydir.yaml
pod/pod-emptydir created

[root@master01 volumes]# kubectl describe pod pod-emptydir
Name:         pod-emptydir
Namespace:    default
Priority:     0
Node:         master02/192.168.44.11
Start Time:   Thu, 15 Dec 2022 00:14:10 +0800
Labels:       <none>
Annotations:  <none>
Status:       Pending
IP:           
IPs:          <none>
Containers:
  busybox1:
    Container ID:  
    Image:         busybox
    Image ID:      
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/sh
      -c
      sleep 3600
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /cache from cache-volume (rw)  #挂载
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro)
  busybox2:
    Container ID:  
    Image:         busybox
    Image ID:      
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/sh
      -c
      sleep 3600
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /test/cache from cache-volume (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  cache-volume:
    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)
    Medium:     
    SizeLimit:  <unset>
  default-token-c7jnm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-c7jnm
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  58s   default-scheduler  Successfully assigned default/pod-emptydir to master02
  Normal  Pulling    56s   kubelet            Pulling image "busybox"

测试Emptydir

[root@master01 volumes]# kubectl exec -it pod-emptydir -c busybox1 -- sh
/ # ls
bin    cache  dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var
/ # cd cache/
/cache # touch create_busybox
/cache # ^C
/cache # exit
command terminated with exit code 130
[root@master01 volumes]# kubectl exec -it pod-emptydir -c busybox2 -- sh
/ # ls
bin    dev    etc    home   lib    lib64  proc   root   sys    test   tmp    usr    var
/ # cd test/cache/
/test/cache # ls
create_busybox

Hostpath

hostPath允许挂载Node上的文件系统到Pod里面去。如果Pod需要使用Node上的文件,可以使用hostPath。

示例

  • 运行一个需要访问 Docker 引擎内部机制的容器;请使用 hostPath 挂载 /var/lib/docker 路径。
  • 在容器中运行 cAdvisor 时,以 hostPath 方式挂载 /sys。
  • 允许 Pod 指定给定的 hostPath 在运行 Pod 之前是否应该存在,是否应该创建以及应该以什么方式存在

支持类型

取值 行为
空字符串(默认)用于向后兼容,这意味着在安装 hostPath 卷之前不会执行任何检查
DirectoryOrCreate 如果指定的路径不存在,那么将根据需要创建空目录,权限设置为 0755,具有与 Kubelet 相同的组和所有权
Directory 给定的路径必须存在
FileOrCreate 如果给定路径的文件不存在,那么将在那里根据需要创建空文件,权限设置为 0644,具有与 Kubelet 相同的组和所有权【前提:文件所在目录必须存在;目录不存在则不能创建文件】
File 给定路径上的文件必须存在
Socket 在给定路径上必须存在的 UNIX 套接字
CharDevice 在给定路径上必须存在的字符设备
BlockDevice 在给定路径上必须存在的块设备

hostpath.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-hostpath
  namespace: default
spec:
  containers:
  - name: busybox3
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh", "-c", "sleep 3600"]
    volumeMounts:
    - name: hostpath-dir-volume
      mountPath: /test-k8s/hostpath-dir
  volumes:
  - name: hostpath-dir-volume
    hostPath:
      path: /root/test_hostPath  # 宿主机目录
      type: DirectoryOrCreate  # hostPath 卷指定 type,如果目录不存在则创建(可创建多层目录)

创建并查看结果

[root@master01 volumes]# kubectl apply -f hostpath.yaml 
pod/pod-hostpath created

[root@master01 volumes]# kubectl get po -owide 
NAME                                READY   STATUS        RESTARTS   AGE   IP               NODE       NOMINATED NODE   READINESS GATES
pod-hostpath                        1/1     Running       0          19s   172.20.59.237    master02   <none>           <none>

[root@master01 volumes]# kubectl describe po  pod-hostpath
Name:         pod-hostpath
Namespace:    default
Priority:     0
Node:         master02/192.168.44.11
Start Time:   Thu, 15 Dec 2022 00:29:49 +0800
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           172.20.59.237
IPs:
  IP:  172.20.59.237
Containers:
  busybox3:
    Container ID:  docker://b5c41f3d14da84558f6298bf7967c3bf825cce19898f5cbb308a9b8fc6c78da4
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:3b3128d9df6bbbcc92e2358e596c9fbd722a437a62bafbc51607970e9e3b8869
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/sh
      -c
      sleep 3600
    State:          Running
      Started:      Thu, 15 Dec 2022 00:29:50 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /test-k8s/hostpath-dir from hostpath-dir-volume (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  hostpath-dir-volume:
    Type:          HostPath (bare host directory volume)
    Path:          /root/test_hostPath
    HostPathType:  DirectoryOrCreate
  default-token-c7jnm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-c7jnm
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  94s   default-scheduler  Successfully assigned default/pod-hostpath to master02
  Normal  Pulled     93s   kubelet            Container image "busybox" already present on machine
  Normal  Created    93s   kubelet            Created container busybox3
  Normal  Started    93s   kubelet            Started container busybox3

[root@master01 volumes]# ssh master02
Last login: Wed Nov 23 04:15:58 2022 from 192.168.44.1
[root@master02 ~]# ls
test_hostPath
[root@master02 ~]# cd test_hostPath/
[root@master02 test_hostPath]# ls
[root@master02 test_hostPath]# pwd
/root/test_hostPath


测试hostpath

[root@master02 test_hostPath]# pwd
/root/test_hostPath
[root@master02 test_hostPath]# touch create_hostpath
[root@master02 test_hostPath]# 
[root@master02 test_hostPath]# kubectl exec -ti pod-hostpath -- sh
/ # ls
bin       dev       etc       home      lib       lib64     proc      root      sys       test-k8s  tmp       usr       var

[root@master02 test_hostPath]# ls
create_hostpath

[root@master02 test_hostPath]# kubectl exec -ti pod-hostpath -- sh
/ # ls
bin       dev       etc       home      lib       lib64     proc      root      sys       test-k8s  tmp       usr       var
/ # cd test-k8s/hostpath-dir/
/test-k8s/hostpath-dir # ls
create_hostpath

标签:Kubernetes,笔记,hostpath,Volumes,hostPath,test,pod,root,Pod
From: https://www.cnblogs.com/arvinhuang/p/16984092.html

相关文章

  • Wireshark使用笔记
    Wireshark使用笔记,转载&学习《超详细的Wireshark使用教程》,并结合自己操作写以下笔记。wireshark是什么?wireshark是非常流行的网络封包分析软件,简称小鲨鱼,功能十分强大......
  • Docker学习笔记十一:Docker安装Redis
    下载命令:dockerpullredis安装可参考Docker Hub官网说明的镜像的用法  安装 不挂载目录启动容器创建容器命令:dockerrun-d--name=myredis-p6380:637......
  • FreeSWITCH学习笔记:内部命令(API)
    本文更新于2022-12-14,使用FreeSWITCH1.10.7。官方文档见:https://freeswitch.org/confluence/display/FREESWITCH/mod_commands说明:下文中,部分大写为自定义变量,根据实际......
  • java学习笔记--类、函数重载、this、static、继承、重写、多态
    <5>类1)类和对象类是把一类事物的静态属性和动态操作组合在一起所得的概念,相当于模型,或者说一个设计图纸。对象是类的一个个体,是根据类这个设计图纸造出来的实物,会产生和......
  • python学习笔记整理03(函数)
    1语法:1.1基本语法:#1.定义函数#使用def(define)关键字定义函数,且函数命名方法要遵循标识符规则deffunc1():#定义函数内的代码称为函数体print('函......
  • 网络流学习笔记
    网络流初步一个网络\(G=(V,E)\)是一张有向图,图中每条有向边\((x,y)\inE\)都有一个给定的权值\(c(x,y)\),称为边的容量。图中还有两个节点\(S\)和\(T\),源点和汇点。网络......
  • 详解物理层-通信基础【王道计算机网络笔记】
    ![![[附件/Pastedimage20221120151810.png|100]]](https://img-blog.csdnimg.cn/6765bf898d2a41588eb9e60989ab40bf.png=x300)物理层接口特性物理层解决如何在连接各种......
  • Python笔记--字符串
    字符串的三种定义单引号定义法:双引号定义法:三引号定义法:结果:其中,三引号定义的话,不用变量接收的话,就相当于多行注释;用变量接收的话,就是对于字符串的定义了。字符串......
  • #yyds干货盘点# react笔记之学习之state注意事项
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • 【《硬件架构的艺术》读书笔记】05 低功耗设计(3)
    5.6在寄存器传输级降低功耗RTL完成时80%的功耗就已经确定,后端不能解决所有功耗问题。综合前RTL阶段就应讲与功耗有关的所有问题解决。5.6.1状态机编码与解码格雷码在......