【问题标题】:cp command in yaml fileyaml文件中的cp命令
【发布时间】:2018-11-18 09:04:00
【问题描述】:

我正在我的 Kubernetes 集群上启动一个 glassfish pod,并且我正在尝试从我的主机上的一个文件夹中复制一些 .war 文件,但命令 cp 似乎总是失败。

我的 yaml 文件:

apiVersion: apps/v1
kind: Deployment
metadata:
 name: glassfish
spec:
# replicas: 2
 selector:
  matchLabels:
   app: glassfish
 strategy:
  type: Recreate
 template:
  metadata:
   labels:
    app: glassfish
  spec:
   containers:
   - image: glassfish:latest
     name: glassfish
     ports:
     - containerPort: 8080
       name: glassfishhttp
     - containerPort: 4848
       name: glassfishadmin
     command: ["/bin/cp"]
     args: #["/mnt/apps/*","/usr/local/glassfish4/glassfish/domains/domain1/autodeploy/"]
     - /mnt/apps/
     - /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/
     volumeMounts:
     - name: glassfish-persistent-storage
       mountPath: /mount
     - name: app
       mountPath: /mnt/apps
   volumes:
   - name: glassfish-persistent-storage
     persistentVolumeClaim:
      claimName: fish-mysql-pvc
   - name: app
     hostPath:
      path: /mnt/nfs
      type: Directory

我正在尝试在我的容器中使用以下命令:

cp /mnt/apps/* /usr/local/glassfish4/glassfish/domains/domain1/autodeploy

我做错了什么?

到目前为止,我已经尝试使用 / 来实现,没有它,使用 /* 当我使用应用程序/时,我看到“找不到项目或目录”,当我使用应用程序/时,我得到“目录省略”,我只需要地图中的内容,而不需要地图本身,所以 -r 并不真正也可以帮忙。

【问题讨论】:

  • 什么是失败?您收到任何错误消息吗?
  • 当我不使用 * 时我得到“目录省略”,如果我使用 * 我得到“文件或目录不存在”
  • 好的。编辑您的问题以使其更具解释性,请添加您尝试执行的整个命令
  • 将其添加到问题中

标签: kubernetes yaml


【解决方案1】:

这里需要注意两点:

  1. 如果要使用cp 复制目录,则必须向cp 提供-a-R 标志:
 -R    If source_file designates a directory, cp copies the directory and the entire subtree connected at
       that point.  If the source_file ends in a /, the contents of the directory are copied rather than
       the directory itself.  This option also causes symbolic links to be copied, rather than indirected
       through, and for cp to create special files rather than copying them as normal files.  Created
       directories have the same mode as the corresponding source directory, unmodified by the process'
       umask.

       In -R mode, cp will continue copying even if errors are detected.
  1. 如果您在 pod 中使用 /bin/cp 作为入口点,则此命令不会在 shell 中执行。但是,/path/to/* 中的 * 是一个 shell 功能。

  2. initContainers 没有args,只有`command。

要完成这项工作,请改用 /bin/sh 作为命令:

command:
  - /bin/sh
  - -c
  - cp /mnt/apps/* /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/

【讨论】:

  • 当我输入上述版本的/bin/sh 时,我得到以下信息:/bin/sh: 0: cant open cp....
  • 在尝试了更多之后,我现在处于尝试 kubectl logs 时我的 pod 甚至不再提供日志的地步,但它一直与 /bin/sh 一起崩溃跨度>
  • 嗨,我的错! initContainers 没有commandargs,它们只有command。我相应地更新了我的答案
  • 我也不好,我们不能在这里使用 initContainers,因为它被复制到本地路径而不是 PV...
  • 我试过只使用命令,并复制了 cp 命令以确保我不会出错,但它会引发另一个崩溃循环
【解决方案2】:

我做错了什么?

这里是正确的执行命令:

command: ["sh", "-c", "cp -r /mnt/apps/* /usr/local/glassfish4/glassfish/domains/domain1/autodeploy/ && asadmin start-domain --verbose"]

使用您的 cp 命令,您可以有效地覆盖合法所需的命令以启动一切。如果您检查该容器,您可以看到原始容器(在没有 cp 命令的情况下运行正常)。最初,容器以:

...
"Cmd": [
    "/bin/sh", 
    "-c",
    "asadmin start-domain --verbose"
],
...

只需在复制命令解决您的问题后添加即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2021-06-14
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2017-05-01
    相关资源
    最近更新 更多