数据卷和pvc存储持久化案例实战

本文介绍了Kubernetes中两种常见数据卷类型:emptyDir和hostPath。emptyDir数据卷用于Pod内容器间的数据共享,当Pod删除时数据随之消失。hostPath数据卷则将节点本地目录挂载到Pod中,使得容器可以直接访问主机文件系统。通过案例展示了如何创建和使用这两种数据卷。
摘要由CSDN通过智能技术生成

数据卷

  • 容器中的文件时临时存放在磁盘上的。
    • 当容器崩溃和升级时,kubectl会进行容器的重建,会导致容器内的文件会丢失。
    • 一个pod中运行多个容器需要共享文件。

常见的数据卷

  • 节点本地(hostPath,emptyDir)
  • 网络 (NFS,Ceph,GlusterFS)
  • 共有云 (AWS,EBS)
  • K8S资源 (configMap,secret)

emptyDir 数据卷

  • emptyDir卷是一个临时存储卷(Pod所在的节点),与Pod生命周期捆绑在一起,当pod被删除,卷也会跟着被删除
  • Pod中容器之间数据共享(应用场景)

emptyDir 案例

1、创建2个容器,将2个容器间的/usr/share/nginx/html和/tmp 目录做共享

[root@k8s-master ~]# vim emptydir.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - name: nginx1
    image: nginx
    volumeMounts:
      - mountPath: "/usr/share/nginx/html"
        name: share

  - name: tomcat2
    image: tomcat
    volumeMounts:
      - mountPath: "/tmp"
        name: share

  volumes:
  - name: share
    emptyDir: {}

[root@k8s-master ~]# kubect apply -f emptydir.yaml

[root@k8s-master ~]# kubectl exec -it test -c nginx1 -- bash 

root@test:/usr/share/nginx/html# touch 88888888

root@test:/usr/share/nginx/html# exit

[root@k8s-master ~]# kubectl exec -it test -c tomcat2 -- bash

root@test:/tmp# ls
88888888

root@test:/tmp# mkdir ./test

[root@k8s-master ~]# kubectl exec -it test -c nginx1 -- bash 

root@test:/# ls /usr/share/nginx/html/
88888888  test

hostPath 案例

本地tmp目录下的文件
在这里插入图片描述

[root@k8s-master tmp]# vim /root/hostpath.yaml              
apiVersion: v1
kind: Pod
metadata:
  name: feng
spec:
  containers:
  - name: http
    image: nginx
    volumeMounts:
    - name: mount
      mountPath: /usr/local
  volumes:
  - name: mount
    hostPath:
      path: /tmp
      type: Directory

[root@k8s-master tmp]# kubectl apply -f /root/hostpath.yaml 

[root@k8s-master tmp]# kubectl exec -it feng --  bash

root@feng:/usr/local# ls
vmware-root_699-3979839557  vmware-root_700-2730627996  vmware-root_701-3979708482  vmware-root_709-4248287236  vmware-root_712-2957059153

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值