k8s学习 - 存储1

为何需要存储卷

在这里插入图片描述
Pod需要设置卷来源(spec.volume)和挂载点(spec.containers.volumeMounts)两个信息后才可以使用相应的Volume。

本地(hostPath,emptyDir)
网络(NFS,Ceph,GlusterFS)
公有云(AWS EBS)
K8S资源(configmap,secret)

emptyDir

emptyDir:是一个临时存储卷,与Pod生命周期绑定一起,如果Pod删除了卷也会被删除
应用场景:Pod中容器之间数据共享

apiVersion: v1
kind: Pod
metadata: 
  name: producer-consumer
spec:
  containers:
    - name: producer
      image: busybox
      volumeMounts: 
      - name: shared-volume
        mountPath: /producer_dir
      args:
      - /bin/sh
      - -c
      - echo "hello this is producer" > /producer_dir/hello ; sleep 3600
    - name: consumer
      image: busybox
      volumeMounts:
      - name: shared-volume
        mountPath: /consumer_dir
      args:
      - /bin/sh
      - -c
      - cat  /consumer_dir/hello ; sleep 3600
  volumes:
  - name: shared-volume
    emptyDir: {}

验证

[root@master yaml]# kubectl logs producer-consumer consumer
hello this is producer

hostPath

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 36000
    volumeMounts:
    - name: data
      mountPath: /data
  volumes:
  - name: data
    hostPath:
      path: /tmp
      type: Directory

PV & PVC

准备工作NFS

yum install nfs-utils   所有节点上都安装
systemctl stop firewalld
systemctl disable firewalld 

systemctl start nfs
systemctl enable nfs


mkdir /data
vim /etc/exports
/data *(rw, no_root_squash)
systemctl reload nfs 

案例 pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
   storage: 5Gi
  accessModes:
  - ReadWriteMany
  nfs:
    path: /data
    server: 172.16.10.216

案例pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc0001
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: ""
  resources:
    requests:
      storage: 2Gi

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值