Kubernetes hostPath卷不显示文件

时间:2020-04-27 08:34:26

标签: kubernetes

我正在尝试创建PersistentVolume以便与Pod共享主节点(而不是节点)上的目录,为此,我正在使用以下.yaml:

>
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/home/myhostuser/shared"

我有以下Deployment .yaml,我正尝试在其中使用以下共享资源:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  selector:
    matchLabels:
      app: my-app
      replicas: 1
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app-image 
        volumeMounts:
         - mountPath: /shared_host_path
           name: my-volume
        securityContext:
          privileged: true      
        ports:
        - containerPort: 5000
      volumes:
      - name: my-volume
        hostPath: 
        # directory location on host
          path: "/home/myhostuser/shared/"

当我尝试在主机上运行ls /home/myhostuser/shared时,除了看到文件之外,但是当我在 pod 中运行ls /shared_host_path时,我没有查看文件。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

如下定义PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 3Gi

并按如下所示在部署中使用PVC

  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/shared_host_path"
          name: task-pv-storage

https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/

答案 1 :(得分:2)

如果要在主服务器上使用该卷,则必须在persistentVolume规范中指定该卷,例如

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  local:
    path: /home/myhostuser/shared
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - master

这是Kubernetes调度程序如何理解此PersistentVolume绑定到特定节点的方式