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
Kubernetes在部署容器中运行shell命令_Kubernetes_Yaml_Kubernetes Pod - Fatal编程技术网

Kubernetes在部署容器中运行shell命令

Kubernetes在部署容器中运行shell命令,kubernetes,yaml,kubernetes-pod,Kubernetes,Yaml,Kubernetes Pod,可以在Kubernetes中为部署定义命令,这些命令将在创建部署后立即执行 例如: cd opt/ wget xxxxxx mkdir new/ 到目前为止,我还没有找到解决这个问题的办法 有没有其他方法可以达到这种效果?可以这样做: apiVersion: apps/v1 kind: Deployment metadata: labels: app: example name: example spec: replicas: 1 selector: match

可以在Kubernetes中为部署定义命令,这些命令将在创建部署后立即执行

例如:

cd opt/
wget xxxxxx
mkdir new/
到目前为止,我还没有找到解决这个问题的办法


有没有其他方法可以达到这种效果?

可以这样做:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: example
  name: example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example
  strategy: {}
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
        - image: busybox
          command: ["/bin/sh"]
          args: ["-c", "cd opt/ &&  wget xxxxxx && mkdir new/ && process-that-keeps-container-running"]
          name: busybox

这有点棘手,因为在命令参数的末尾,您必须放置保持容器运行的命令。如果您不知道这是哪一个,您必须查看您正在使用的Docker映像的
CMD
ENTRYPOINT

为什么不将此命令作为pod的一部分运行?您尝试过吗?如果可以,我可能会将其构建到映像中。如果您获取的大部分是静态数据,请在Dockerfile
RUN
Int指令中执行;如果它非常动态,或者确实无法内置到映像中(类似于每个安装的安全凭据),则使用
ENTRYPOINT
wrapper脚本。感谢您的回复,但此配置会使pod状态终止