k8s之挂载
1.挂载本地路径
pod yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | apiVersion: v1 kind: Pod metadata: name: test spec: containers: - name: test image: library / busybox command: [ "/bin/sh" ] args: [ "-c" , "while true; do echo hello; sleep 10;done" ] volumeMounts: - mountPath: / mnt / host_src # 容器内挂载路径,会自动创建 name: local - vol volumes: - name: local - vol # 与volumeMounts对应 hostPath: path: / tmp # 宿主机路径 |
2.pod直接挂载nfs
2.1 配置nfs
2.2 pod直接挂载nfs
pod yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | apiVersion: v1 kind: Pod metadata: name: test -nfs spec: containers: - name: test -nfs image: library /busybox command : [ "/bin/sh" ] args: [ "-c" , "while true; do echo hello; sleep 10;done" ] volumeMounts: - mountPath: /mnt/data # 容器内挂载路径 name: nfs-vol volumes: - name: nfs-vol nfs: server: 192.168.185.6 path: /data |
3.pvc挂载nfs
pv.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | apiVersion: v1 kind: PersistentVolume metadata: name: pv3 spec: capacity: storage: 5Gi volumeMode: Filesystem accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle storageClassName: slow mountOptions: - nolock - nfsvers=3 - vers=3 nfs: path: /data server: 192.168.0.101 |
1 2 | kubectl apply -f pv.yaml kubectl get pv |
pvc.yaml
1 2 3 4 5 6 7 8 9 10 11 12 | apiVersion: v1 kind: PersistentVolumeClaim metadata: name: task-pv-claim spec: storageClassName: slow volumeMode: Filesystem accessModes: - ReadWriteOnce resources: requests: storage: 3Gi |
1 2 | kubectl apply -f pvc.yaml kubectl get pvc |
pvc-pod.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | apiVersion: v1 kind: Pod metadata: name: task-pv-pod spec: volumes: - name: task-pv-storage persistentVolumeClaim: claimName: task-pv-claim containers: - name: task-pv-container image: nginx ports: - containerPort: 80 hostPort: 8008 name: "http-server" volumeMounts: - mountPath: "/usr/share/nginx/html" name: task-pv-storage |
1 2 | kubectl apply -f pvc-pod.yaml kubectl get pod |
4.挂载本地pvc
pv.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | apiVersion: v1 kind: PersistentVolume metadata: name: task-pv-volume labels: type : local spec: storageClassName: manual capacity: storage: 10Gi accessModes: - ReadWriteOnce hostPath: path: "/data" |
1 2 | kubectl apply -f pv.yaml kubectl get pv |
pvc.yaml
1 2 3 4 5 6 7 8 9 10 11 12 | apiVersion: v1 kind: PersistentVolumeClaim metadata: name: task-pv-claim spec: storageClassName: slow volumeMode: Filesystem accessModes: - ReadWriteOnce resources: requests: storage: 3Gi |
1 2 | kubectl apply -f pvc.yaml kubectl get pvc |
pvc-pod.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | apiVersion: v1 kind: Pod metadata: name: task-pv-pod spec: volumes: - name: task-pv-storage persistentVolumeClaim: claimName: task-pv-claim containers: - name: task-pv-container image: nginx ports: - containerPort: 80 hostPort: 8008 name: "http-server" volumeMounts: - mountPath: "/usr/share/nginx/html" name: task-pv-storage |
1 2 | kubectl apply -f pvc-pod.yaml kubectl get pod |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)