【问题标题】:How to mount volume for docker container via yaml manifest?如何通过 yaml 清单为 docker 容器挂载卷?
【发布时间】:2015-09-04 23:26:38
【问题描述】:

我尝试使用以下 yaml 启动 container-vm 机器:

version: v1
kind: Pod
spec:
  containers:
    - name: simple-echo
      image: gcr.io/google_containers/busybox
      command: ['nc', '-p', '8080', '-l', '-l', '-e', 'echo', 'hello world!']
      imagePullPolicy: Always
      ports:
        - containerPort: 8080
          hostPort: 8080
          protocol: TCP
      volumeMounts:
        - name: string
          mountPath: /home
          readOnly: false
  restartPolicy: Always
  dnsPolicy: Default
  volumes:
    - name: string
      source:
        # Either emptyDir for an empty directory
        # emptyDir: {}
        # Or hostDir for a pre-existing directory on the host
        hostDir:
          path: /home

我希望可以从容器访问主机主目录。

但是,容器无法启动:

E0619 05:02:09.477574    2212 http.go:54] Failed to read URL: invalid pod: 
[spec.volumes[0].source: invalid value '<*>(0xc2080b79e0){HostPath:<nil> EmptyDir:<nil> GCEPersistentDisk:<nil> AWSElasticBlockStore:<nil> 
GitRepo:<nil> Secret:<nil> NFS:<nil> ISCSI:<nil> Glusterfs:<nil> PersistentVolumeClaimVolumeSource:<nil> RBD:<nil>}': 
exactly 1 volume type is required spec.containers[0].volumeMounts[0].name: not found 'string']

为容器指定体积的正确方法是什么?

【问题讨论】:

    标签: docker google-cloud-platform google-kubernetes-engine


    【解决方案1】:

    尝试将hostDir 替换为hostPath,如v1beta3-conversion-tips-from-v1beta12 中所述。

    尝试替换

    volumes:
        - name: string
          source:
            # Either emptyDir for an empty directory
            # emptyDir: {}
            # Or hostDir for a pre-existing directory on the host
            hostDir:
              path: /home
    

    volumes:
        - name: string
          hostPath:
            path: /home
    

    在您的配置底部。

    【讨论】:

    • 好像没用。
    • source: 行也必须删除 - hostPath 不再嵌套在 source 下。
    【解决方案2】:

    有一个简单的方法:

    version: v1 kind: Pod spec: containers: - name: simple-echo image: gcr.io/google_containers/busybox command: ['nc', '-p', '8080', '-l', '-l', '-e', 'echo', 'hello world!'] imagePullPolicy: Always ports: - containerPort: 8080 hostPort: 8080 protocol: TCP volumeMounts: - name: string mountPath: /home readOnly: false restartPolicy: Always dnsPolicy: Default volumes: - /path/dir/from/host:/name/of/dir/in/container

    【讨论】:

    • 不行:couldn't parse as single (json: cannot unmarshal string into Go value of type api.Volume) or multiple pods (json: cannot unmarshal string into Go value of type api.Volume)
    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 2021-07-02
    • 2021-09-17
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 2017-01-01
    相关资源
    最近更新 更多