【问题标题】:Kubernetes, VolumeMounts a file, not a directoryKubernetes,VolumeMounts 一个文件,而不是一个目录
【发布时间】:2019-04-28 17:13:03
【问题描述】:

我将使用 K8S 来编排 docker 容器。在 k8s 中,我需要将文件从主机目录(/configs/nginx/cas-server.conf)复制到 pod 容器目录(/etc/nginx/nginx.conf),但是当前的 k8s 只允许挂载目录,不能挂载/复制文件。如何解决这个问题呢?

下面是我的 nginx-cas-server-deply.yaml 文件。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-cas-server-depl
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-cas-server-pod
    spec:
      containers:
      - name: nginx-cas-server-pod
        image: nginx
        imagePullPolicy: Never
        ports:
          - containerPort: 100
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: nginx-cas-server-conf
        - mountPath: /app/cas-server/public
          name: nginx-cas-server-public
      volumes:
      - name: nginx-cas-server-conf
        hostPath:
          path: /configs/nginx/cas-server.conf
      - name: nginx-cas-server-public
        hostPath:
          path: /cas-server/public

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    在您的 Deployment 配置中,您需要使用带有 directoryfile 名称的 mountPath 以及带有 filesubPath 字段姓名。另外,重要的是,您需要将文件命名为与您希望挂载的完全相同的节点,因此如果要挂载到/etc/nginx/nginx.conf,文件应命名为nginx.conf

    示例如下:

    节点上目录的内容:

    # ls /config/
    nginx.conf  some_dir
    

    Nginx 部署的配置文件

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      labels:
        run: nginx
      name: nginx
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          run: nginx
      template:
        metadata:
          labels:
            run: nginx
        spec:
          containers:
          - image: nginx
            name: nginx
            volumeMounts:
            - mountPath: /etc/nginx/nginx.conf 
              name: test
              subPath: nginx.conf
          volumes:
          - hostPath:
              path: /config
            name: test
    

    【讨论】:

      【解决方案2】:

      您可以使用 hostPath 将文件从主机挂载到 pod,我正在为我的 elasticsearch 集群执行此操作,我想将我的 elasticsearch.yml 文件从主机挂载到 pod。

      您需要记住该文件已挂载(而不是复制),因此您在一个文件中所做的更改会在两个位置反映出来。请看以下 yaml 文件:

      {
        "kind": "StatefulSet",
        "apiVersion": "apps/v1beta1",
        "metadata": {
          "name": "ES",
          "labels": {
            "state": "es"
          }
        },
        "spec": {
            "spec": {
              "containers": [
                {
                  "name": "es",
                  "image": "",
                  "imagePullPolicy": "IfNotPresent",
                  "command": [
                    "/bin/sh",
                    "-c"
                  ],
                  "volumeMounts": [
                    {
                      "mountPath":"/data/elasticsearch/conf/elasticsearch.yml",
                      "name":"esconf"
                    }
                  ]
                }
              ],
              "volumes": [
                {
                  "name": "esconf",
                  "hostPath": {
                    "path": "/prafull/data/md_elasticsearch.yml",
                    "type": "FileOrCreate"
                  }
                }
              ],
              "restartPolicy": "Always",
              "imagePullSecrets": [
                {
                  "name": "gcr-imagepull-json-key"
                }
              ]
            }
          }
        }
      }
      

      希望对你有帮助

      【讨论】:

        【解决方案3】:

        Kubernetes 会挂载整个文件夹,因此该文件夹的所有内容都将对您的容器可见。

        您可以创建指向文件/etc/nginx/nginx.conf 的符号链接,而不是复制文件,该链接可以指向/configs/nginx/cas-server.conf。但要做到这一点,理想的方法是更新 docker 镜像的入口点

        参考链接 https://www.cyberciti.biz/faq/creating-soft-link-or-symbolic-link/

        【讨论】:

          猜你喜欢
          • 2023-03-22
          • 2021-10-14
          • 2020-09-28
          • 2011-07-26
          • 2018-06-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-25
          相关资源
          最近更新 更多