挂载本地路径

pod yaml

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 # 宿主机路径

挂载网络路径NFS

nfs配置(mac环境)

# 修改配置文件
cat /etc/exports 
/Users/joy/test -alldirs  -maproot=root:wheel -network=192.168.0.0 -mask=255.255.0.0

# 检查配置状态
$ sudo nfsd checkexports

# 修改NFS的一个配置文件/etc/nfs.conf并添加如下内容:
nfs.server.mount.require_resv_port = 0

# 重启服务
$ sudo nfsd restart

# 查看挂载状态
$ showmount -e

# 本地测试验证
mount -t nfs -o nolock,nfsvers=3,vers=3 192.168.0.101:/Users/joy/test /Users/joy/tmp

pod直接挂载nfs

pod yaml

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/host_src  # 容器内挂载路径
      name: nfs-vol
  volumes:
  - name: nfs-vol
    nfs:
      server: 192.168.0.101
      path: /Users/joy/test

k8s挂载nfs pvc

pv.yaml

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: /Users/joy/test
    server: 192.168.0.101
  • kubectl apply -f pv.yaml
  • kubectl get pv

pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: slow
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
  • kubectl apply -f pvc.yaml
  • kubectl get pvc

pvc-pod.ymal

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
  • kubectl apply -f pvc-pod.yaml
  • kubectl get pod

挂载本地路径 PVC

pv以本地路径的方式声明,pvc、pod yaml和网络方式挂载一样使用即可。
pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/Users/joy/src"

参考

pv and pvc视频教程
official pv持久卷
official pod使用pvc
k8s使用mac上自带的NFS

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐