|NO.Z.00189|——————————|CloudNative|——|KuberNetes&持久存储.V02|——|volume.v02|emptyDir卷|
一、持久化存储
### --- 创建nginx-deploy.yaml;使用emptyDir卷的示例,直接指定emptyDir为{}即可:
~~~ 创建nginx-deployment挂载volume配置参数
[root@k8s-master01 ~]# vim nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2020-09-19T02:41:11Z"
generation: 1
labels:
app: nginx
name: nginx
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx:1.15.2 # 第一个:容器下挂载
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /opt
name: share-volume
- image: nginx:1.15.2 # 第二个:容器下挂载,在同一个pod下不可以启动两个相同名称的容器
imagePullPolicy: IfNotPresent
name: nginx2
command:
- sh
- -c
- sleep 3600
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /mnt
name: share-volume
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- name: share-volume
emptyDir: {}
#medium: Memory
~~~ # 文件详解
~~~ 因为这个volumes是给多个container去使用的,所以说它和我们的containers是平级的,
~~~ volumes是切片形式的,我们也可以配置多个。
volumes:
- name: share-volume // 名称
emptyDir: {} // 类型:为空 ;若是使用Memory的话,需要把这两个括号去掉,若是直接进行文件共享,直接写花括号就可以。
#medium: Memory // 使用内存
二、创建nginx-deploy的volume共享卷容器:容器之间数据共享,持久化存储。
### --- 创建2个容器
[root@k8s-master01 ~]# kubectl create -f nginx-deploy.yaml
[root@k8s-master01 ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
busybox 1/1 Running 87 6d21h
nginx-67948c7d6b-9cnx8 2/2 Running 0 82s
nginx-67948c7d6b-fjdcr 2/2 Running 0 81s
三、验证pod之间是否可以实现数据共享
### --- 在容器中创建一个目录,验证效果
[root@k8s-master01 ~]# kubectl exec -ti nginx-67948c7d6b-9cnx8 -c nginx -- bash
root@nginx-67948c7d6b-9cnx8:/#
~~~ # 查看它挂载到那个目录下
root@nginx-67948c7d6b-9cnx8:/# cd /opt/
root@nginx-67948c7d6b-9cnx8:/opt# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 95G 5.9G 90G 7% /opt // 确认他是挂载到/opt目录之下
~~~ # 在nginx下touch一个文件
root@nginx-67948c7d6b-9cnx8:/opt# touch yanqi.volumes.test
~~~ # 在nginx2就可以看到这个文件
[root@k8s-master01 ~]# kubectl exec -ti nginx-67948c7d6b-9cnx8 -c nginx2 -- bash
root@nginx-67948c7d6b-9cnx8:/# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 95G 5.9G 90G 7% /mnt // nginx2的挂载目录是mnt目录
root@nginx-67948c7d6b-9cnx8:/# ls /mnt/
yanqi.volumes.test // 可以看到文件已经存在
~~~ # 在nginx2容器下写入数据,在nginx上查看数据的更新
root@nginx-67948c7d6b-9cnx8:/# echo "test yanqi.volumes.test" >> /mnt/yanqi.volumes.test
[root@k8s-master01 ~]# kubectl exec nginx-67948c7d6b-9cnx8 -c nginx -- cat /opt/yanqi.volumes.test
test yanqi.volumes.test
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了