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

Kubernetes run shell command in deployment container

是否可以在 Kubernetes 中为 Deployment 定义在创建 Deployment 后立即执行的命令?

例如:

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 图片的 CMDENTRYPOINT