目录
一 介绍
volume是pod中能被多个容器访问的共享目录,当容器被终止时volume中的数据也不会丢失
一 基本存储
1.1 HostPath
hostpath就是node主计中的实际目录股灾到pdo中,以供容器使用,pod销毁,但是数据依然在node主机上上
[root@master ~]# cat volumes.yaml apiVersion: v1 kind: Pod metadata: name: pod namespace: dev labels: version: v1 spec: containers: - name: nginx image: nginx:1 ports: - containerPort: 80 volumeMounts: - name: logs-volume mountPath: /var/log/nginx - name: busybox image: busybox command: ["/bin/sh","-c","tail -f /logs/access.log"] volumeMounts: - name: logs-volume mountPath: /logs volumes: - name: logs-volume hostPath: path: /root/logs type: DirectoryOrCreate ##目录不存在则创建后使用
测试和实验
1.测试发现 [root@server1 logs]# cat access.log 10.244.0.0 - - [16/Feb/2023:14:38:03 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-" 10.244.0.0 - - [16/Feb/2023:14:38:04 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-" 10.244.0.0 - - [16/Feb/2023:14:38:05 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/ ##此时在server1 生成 /root/log文件 目录底下有nginx的文件 ##此时就算删除pod 文件也依然存在
1.2 NFS
hostpath的问题在于只能存储在放置pod的节点,如果节点挂了数据就无法存储了
不做演示,和上述基本一样只是把路径改为了nfs的共享路径
volumes: - name: logs-volume nfs: server: 192.168.10.123 ##nfs共享的ip path: /root/data/nfs ##贡献路径
二 高级存储
- pv 是持久化卷的意思,一般是对底层共享存储的一种抽象,一般有k8s管理员进行创建和配置。
- pvc是持久卷声明的意思,一啊是用户对于存储需求的一种声明,pvc简单来说就是用户对k8s系统发出的一种资源需求申请
2.1 pv
测试
[root@master ~]# cat pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /public
server: 192.168.1.105
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /public
server: 192.168.1.105
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv3
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
nfs:
path: /public
server: 192.168.1.105
//查看pv空间 [root@master ~]# kubectl get pv -o wide NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE pv1 2Gi RWX Retain Available 42s Filesystem pv2 2Gi RWX Retain Available 42s Filesystem pv3 2Gi RWX Retain Available 42s Filesystem
2.2 pvc
pvc是资源的申请,用来声明对存储空间的,访问模式,存储类别需求信息
[root@master ~]# cat pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc1 spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi
观察实验
[root@master ~]# kubectl get pvc,pv NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE persistentvolumeclaim/pvc1 Bound pv1 2Gi RWX 4m34s NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE persistentvolume/pv1 2Gi RWX Retain Bound default/pvc1 40h persistentvolume/pv2 2Gi RWX Retain Available 40h persistentvolume/pv3 2Gi RWX Retain Available 40h ##发现自己已经调用了pv1 已经绑定到 default 下的pvc上面了
2.3 configmap
主要用于存储配置信息
如何调用configmap 在pod文件中 voulmeMounts: -name: config mountPath: /etc/psas //将config挂载到目录中 volumes: - name: config configMap: namez: configmap //对用configmap的名字
2.4 secret
和configmap用法基本一直,一般用于保存密钥和敏感信息及加密存储文件