Kubernetes:如何更新实时 busybox 容器的 'command'

Kubernetes: How to update a live busybox container's 'command'

我有以下清单创建了名为 'test'

的 运行 pod
apiVersion: v1
kind: Pod
metadata:
  name: hello-world
  labels:
    app: blue
spec:
  containers:
  - name: funskies
    image: busybox
    command: ["/bin/sh", "-c", "echo 'Hello World'"]

我想更新 pod 以包含附加命令

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
  labels:
    app: blue
spec:
  containers:
  restartPolicy: Never
  - name: funskies
    image: busybox
    command: ["/bin/sh", "-c", "echo 'Hello World' > /home/my_user/logging.txt"]

我试过的

kubectl edit pod test

结果如何

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
# pods "test" was not valid:
# * spec: Forbidden: pod updates may not change fields other than `spec.containers[*].image`...

我尝试的其他事情:

更新了清单,然后 运行 应用 - 同样的问题

kubectl apply -f test.yaml 

问题:更新 运行 pod 的正确方法是什么?

我不确定整个要求是什么。

但您可以执行到 pod 并更新详细信息

$ kubectl exec <pod-name> -it -n <namespace> -- <command to execute>

like,
$ kubectl exec pod/hello-world-xxxx-xx -it -- /bin/bash

如果 tty 支持 shell,请使用“/bin/sh”更新内容或命令。

编辑运行ning pod,不会保留清单文件中的更改。所以在这种情况下,您必须 运行 一个包含更改的新 pod。

您无法修改 Pod 的大多数属性。通常你不想直接创建 Pods;使用更高级别的控制器,如 Deployment。

Kubernetes documentation for a PodSpec 注释(强调我的):

containers: List of containers belonging to the pod. Containers cannot currently be added or removed. There must be at least one container in a Pod. Cannot be updated.

在所有情况下,无论如何,容器 运行 都是一个命令,如果您想更改该命令的内容,则需要删除并重新创建容器。在 Kubernetes 中,这总是意味着删除并重新创建包含 Pod。通常你不应该使用 bare Pods,但如果你这样做,你可以使用新命令创建一个新的 Pod 并删除旧的。删除 Pods 是非常常规的事情,各种普通的事情都会导致它发生(更新 Deployments、Horizo​​ntalPodAutoscaler 缩减,...)。

如果您有 Deployment 而不是裸 Pod,您可以自由更改它创建的 Pods 的 template:。这包括更改他们的 command:。这将导致 Deployment 使用新命令创建一个新 Pod,并且一旦 运行ning,删除旧 Pod。

您在问题中展示的那种生命周期很短的单命令容器不一定非常适合 运行在 Kubernetes 中使用。如果 Pod 不打算停留 运行 并为请求提供服务,Job 可能是更好的匹配;但是 Job 认为它只会 运行 一次,如果您更改已完成 Job 的 pod 规范,我认为它不会启动新的 Pod。您需要为此案例创建一个新工作。