Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kubernetes,VolumeMounts是一个文件,不是一个目录_Kubernetes - Fatal编程技术网

Kubernetes,VolumeMounts是一个文件,不是一个目录

Kubernetes,VolumeMounts是一个文件,不是一个目录,kubernetes,Kubernetes,我将使用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-ser

我将使用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装载整个文件夹,因此该文件夹的所有内容都将对您的容器可见

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

参考链接

您可以使用hostPath将文件从主机装载到吊舱,我正在为我的elasticsearch群集执行此操作,我希望将我的elasticsearch.yml文件从主机装载到吊舱

您需要记住,文件是装入的(不是复制的),因此您在一个文件中所做的更改会反映在两个位置。请看下面的内容 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"
          }
        ]
      }
    }
  }
}

希望这对您的部署配置有所帮助,您需要将
挂载路径
目录文件名称一起使用,将
子路径
字段与文件名称一起使用。另外,重要的是,您需要在节点上按照您希望的方式对文件进行命名,因此,如果您希望装载到
/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