当前位置:网站首页>kubernetes 的这几种存储卷,别再傻傻分不清了
kubernetes 的这几种存储卷,别再傻傻分不清了
2022-07-23 06:13:00 【CSDN云计算】

作者 | 江小南
来源 | 江小南和他的小伙伴们
存储卷类型
Kubernetes提供的存储卷(volume)属于Pod资源,共享于Pod内的所有容器,存储卷可在容器的文件系统之外存储相关的数据,也可以独立于Pod的生命周期实现数据持久化存储。
按照类型划分,大致分为三类:

如下是工作中使用到的存储卷,持久卷声明对应于持久化数据存储类型。

详细说明
1. EmptyDir
EmptyDir字面意思理解为空目录。在容器启动时被创建,在容器移除时一并删除,因此不具有持久化存储的能力,但是如果作为同一个Pod内的多个容器间共享文件,或者作为容器数据的临时存储目录是非常好用的选择。
关键词:生命周期同Pod生命周期;作用域为同一个Pod内。
emptyDir存储卷可用字段主要包含两个:
medium:存储介质的类型,可取值为"default"或"Memory","default"使用节点的默认存储介质,占用磁盘空间;"Memory"基于RAM的临时文件系统tmpfs,占用内存空间。
sizeLimit:当前存储卷的空间限额,默认值为nil,表示不限制;一般在medium字段值为"Memory"时需要限制。
2. HostPath
hostPath将目录从工作节点的文件系统挂载到pod中,它独立于Pod的生命周期,因而具有持久性,类似于docker中的-v。但是如果发生pod故障转移等情况,将不能获取到数据,除非数据在所有节点中都有。
关键词:某一工作节点的存储。
hostPath存储卷的嵌套字段共有两个:"path"(必选)和"type",它支持的存储卷类型(type)有如下几种:
DirectoryOrCreate:宿主机上不存在则创建此目录
Directory:宿主机上必须存在此目录
FileOrCreate:宿主机上不存在此文件则创建
File:宿主机上必须存在此文件
Socket:宿主机上必须存在的Socket文件路径
CharDevice:宿主机上必须存在的字符设备文件路径
BlockDevice:宿主机上必须存在的块设备文件路径
3. ConfigMap&Secret
ConfigMap和Secret作为配置集,以key-value的形式保存于etcd中,通过存储卷或者配置引用的形式在Pod中使用,区别在于Secret是经过加密的字段显示。示例如下:
# 新建configMap
# my-config.yaml
apiVersion: v1
data:
redis.conf: |
appendonly yes
nginx.conf: |
welcome nginx
project:
edsp
kind: ConfigMap
metadata:
name: my-conf
namespace: default[[email protected] test]# kubectl apply -f my-config.yaml
configmap/my-conf created
[[email protected] test]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 11d
my-conf 3 10s
[[email protected] test]#data是所有真正的数据,key:默认是文件名,value:配置文件的内容。
# 新建secret
[[email protected] secret]# echo -n "jiangxiaonan" > user
[[email protected] secret]# echo -n "xxcjxn12345" > password
[[email protected] secret]# ls
password user
[[email protected] secret]#kubectl create secret generic db-user-pass \
--from-file=user \
--from-file=password# 获取内容查看
[[email protected] secret]# kubectl get secrets
NAME TYPE DATA AGE
db-user-pass Opaque 2 7s
default-token-dp677 kubernetes.io/service-account-token 3 11d
[[email protected] secret]# kubectl get secrets db-user-pass -o=yaml
apiVersion: v1
data:
password: eHhjanhuMTIzNDU=
user: amlhbmd4aWFvbmFu
kind: Secret
metadata:
creationTimestamp: "2022-07-20T14:47:10Z"
managedFields:
name: db-user-pass
namespace: default
resourceVersion: "140362"
uid: 92095b33-385e-48a9-8418-ee5d86817cc3
type: Opaque
[[email protected] secret]# echo eHhjanhuMTIzNDU= | base64 -d
xxcjxn12345
[[email protected] secret]#data是所有真正的数据。
4. PV&PVC
PV&PVC是基于文件系统存在的,我们这里使用NFS文件系统,独立于Pod的生命周期实现数据的持久化存储。
NFS文件系统自行安装,这里只准备pv和pvc。
# pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv01-10m
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/01
server: 172.31.0.2# pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 500Mi
storageClassName: nfs[[email protected] test]# kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pv01-10m 1Gi RWX Retain Bound default/nginx-pvc nfs 11d
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/nginx-pvc Bound pv01-10m 1Gi RWX nfs 11d
[[email protected] test]#完整配置演示
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-deploy
name: nginx-deploy
spec:
replicas: 2
selector:
matchLabels:
app: nginx-deploy
template:
metadata:
labels:
app: nginx-deploy
spec:
restartPolicy: Always
containers:
- name: mynginx
image: nginx
imagePullPolicy: IfNotPresent
envFrom:
- configMapRef:
name: my-conf
- secretRef:
name: db-user-pass
volumeMounts:
- mountPath: /sams/redis.conf
name: redisconf
subPath: redis.conf
- mountPath: /sams/nginx.conf
name: nginxconf
subPath: nginx.conf
- mountPath: /sams/emptydir/test
name: emptydir-test
- mountPath: /sams/hostpaht/test
name: hostpaht-test
- mountPath: /sams/nginx
name: my-pvc
volumes:
- emptyDir:
sizeLimit: 300Mi
name: emptydir-test
- hostPath:
path: /sams/hostPaht/test
type: DirectoryOrCreate
name: hostpaht-test
- configMap:
name: my-conf
name: redisconf
- configMap:
name: my-conf
name: nginxconf
- name: my-pvc
persistentVolumeClaim:
claimName: nginx-pvc通过yaml可以看出,ConfigMap和Secret使用了配置引用的方式在Pod中使用,其余使用挂载。
[[email protected] test]# kubectl apply -f deployment.yaml
deployment.apps/nginx-deploy created
[[email protected] test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deploy-744dfb6ff-f4px5 1/1 Running 0 56s
nginx-deploy-744dfb6ff-zk5zf 1/1 Running 0 58s
[[email protected] test]#检查验证
进入到容器内部进行验证。
[[email protected] test]# kubectl exec -it nginx-deploy-744dfb6ff-f4px5 -c mynginx -- /bin/bash
[email protected]:/# cd /sams/
[email protected]:/sams# ls
emptydir hostpaht nginx nginx.conf redis.conf
[email protected]:/sams#EmptyDir
[email protected]:/sams# cd /sams/emptydir/test/
[email protected]:/sams/emptydir/test# pwd
/sams/emptydir/test
[email protected]:/sams/emptydir/test#EmptyDir挂载成功。
HostPaht
[email protected]:/sams/emptydir/test# cd /sams/hostpaht/test/
[email protected]:/sams/hostpaht/test# pwd
/sams/hostpaht/test
[email protected]:/sams/hostpaht/test# touch hostpaht.txt
[email protected]:/sams/hostpaht/test# ls
hostpaht.txt
[email protected]:/sams/hostpaht/test## 查看nginx-deploy-744dfb6ff-f4px5所在的节点为k8s-worker1
[[email protected] /]# kubectl describe pod nginx-deploy-744dfb6ff-f4px5
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 15m default-scheduler Successfully assigned default/nginx-deploy-744dfb6ff-f4px5 to k8s-worker1
Normal Pulled 15m kubelet Container image "nginx" already present on machine
Normal Created 15m kubelet Created container mynginx
Normal Started 15m kubelet Started container mynginx
[[email protected] /]## 到 k8s-worker1 验证
[[email protected] test]# pwd
/sams/hostPaht/test
[[email protected] test]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 21 00:39 hostpaht.txt
[[email protected] test]#存在/sams/hostPaht/test文件夹,并且hostpaht.txt同样存在,HostPaht挂载成功。
ConfigMap&Secret
[email protected]:/sams# cat redis.conf
appendonly yes
[email protected]:/sams# cat nginx.conf
welcome nginx
[email protected]:/sams# echo $project
edsp
[email protected]:/sams# echo $user
jiangxiaonan
[email protected]:/sams#ConfigMap挂载成功,Secret配置引用成功。
PV&PVC
[email protected]:/sams# cd nginx
[email protected]:/sams/nginx# touch aaa.txt
[email protected]:/sams/nginx## 到NFS文件系统查看
[[email protected] test]# cd /nfs/data/01
[[email protected] 01]# ls
aaa.txt
[[email protected] 01]#pvc挂载成功。

往期推荐
掌握 Dowanward API 的妙用,轻松拿捏 kubernetes 环境变量

点分享

点收藏

点点赞

点在看
边栏推荐
- Beifu PLC and C transmit structure type variables through ads communication
- 编译与预处理
- Notes du jour 7
- 【JZOF】08 二叉树的下一个结点
- How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB
- UI automation
- Opencv image processing (Part 2): edge detection + template matching + Hough transform
- Beifu PLC and C # regularly refresh IO through ads communication
- High voltage MOS tube knx42150 1500v/3a is applied to frequency converter power supply inverter, etc
- 沟通绩效业绩不佳的 11 个提示
猜你喜欢

Complex networks - common drawing software and libraries

Opencv image processing (Part 2): edge detection + template matching + Hough transform

互联网时代下,如何精细化用户运营?

第八天笔记

信号完整性(SI)电源完整性(PI)学习笔记(三十二)电源分配网路(四)

信号完整性(SI)电源完整性(PI)学习笔记(三十一)电源分配网路(三)

Beifu PLC and C transmit string array type variables through ads communication

射击 第 1-01 课:入门

Outlook tutorial, how to switch calendar views and create meetings in outlook?

Beifu PLC and C # regularly refresh IO through ads communication
随机推荐
Notes du jour 7
Numpy: quick start to basic operations
太空射击 Part 2-3: 子弹与敌人碰撞处理
Outlook tutorial, how to switch calendar views and create meetings in outlook?
设计思维的“布道者”
Static routing principle and configuration
MySQL----复合查询 外连接
When using fastjson to parse and assign JSON data, the order of JSON fields is inconsistent
Is it safe to open an account with Guosen Securities software? Will the information be leaked?
CAN控制器的位同步过程
Redis distributed lock practice
[offline voice topic ④] Anxin VC offline voice development board secondary development voice control LED light
Are there any academic requirements for career transfer software testing? Is there really no way out below junior college?
Shooting lesson 1-3: image Sprite
Beifu PLC and C transmit int array type variables through ads communication
Beifu PLC and C # regularly refresh IO through ads communication
Redis如何实现持久化?详细讲解RDB的三种触发机制及其优缺点,带你快速掌握RDB
How to prevent repeated payment of orders?
Convert the specified seconds to minutes and seconds
Confused, work without motivation? Career development hopeless? It's enough to read this article