云容器引擎 CCE-本地磁盘存储:使用kubectl挂载主机路径

时间:2023-11-01 16:25:54

使用kubectl挂载主机路径

CCE支持使用kubectl将容器所在宿主机的文件目录挂载到容器指定的挂载点中。

  1. 请参见通过kubectl连接集群,使用kubectl连接集群。
  2. 执行如下命令,配置名为“hostPath-pod-example.yaml”的创建Pod的yaml文件。

    touch hostPath-pod-example.yaml

    vi hostPath-pod-example.yaml

    在无状态工作负载中挂载主机路径,示例如下:

    apiVersion: apps/v1 kind: Deployment metadata:   name: hostpath-pod-example   namespace: default spec:   replicas: 1   selector:     matchLabels:       app: hostpath-pod-example   template:     metadata:       labels:         app: hostpath-pod-example     spec:       containers:       - image: nginx        name: container-0         volumeMounts:         - mountPath: /tmp           name: hostpath-example       imagePullSecrets:        - name: default-secret      restartPolicy: Always       volumes:       - name: hostpath-example         hostPath:           path: /tmp/test
    表5 本地磁盘储存依赖参数说明

    参数

    描述

    mountPath

    容器内挂载路径,示例中挂载到“/tmp”路径。

    hostPath

    主机路径,示例中主机路径为“/tmp/test”。

    “spec.template.spec.containers.volumeMounts.name ”和 “spec.template.spec.volumes.name”有映射关系,必须保持一致。

  3. 执行如下命令创建Pod。

    kubectl create -f hostPath-pod-example.yaml

  4. 验证主机路径挂载

    1. 查询工作负载(以hostpath-pod-example为例)的实例名称
      kubectl get po|grep hostpath-pod-example

      期望输出:

      hostpath-pod-example-55c8d4dc59-md5d9   1/1     Running   0          35s
    2. 在容器挂载路径/tmp下创建文件test1
      kubectl exec hostpath-pod-example-55c8d4dc59-md5d9 -- touch /tmp/test1
    3. 在主机路径/tmp/test/上可以见到该文件被创建
      ll /tmp/test/

      预期输出:

      -rw-r--r--  1 root root    0 Jun  1 16:12 test1
    4. 在主机挂载路径/tmp/test/上创建文件test2
      touch /tmp/test/test2
    5. 在容器挂载路径内可以见到该文件被新建
      kubectl exec hostpath-pod-example-55c8d4dc59-md5d9 -- ls -l /tmp

      预期输出:

      -rw-r--r-- 1 root root 0 Jun  1 08:12 test1-rw-r--r-- 1 root root 0 Jun  1 08:14 test2

support.huaweicloud.com/usermanual-cce/cce_01_0053.html