使用sh命令时Kubernetes Pod的容器未运行

问题描述 投票:1回答:3

Pod容器在运行sh命令(以及/ bin / sh)之后,还没有准备好并且每次都停留在Waiting状态。例如,在v1-17.docs.kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-with-data-from-multiple-中看到的所有pod容器configmaps在执行sh命令后仅处于“完成”状态,或者,如果我设置为“ restartPolicy:始终”,则由于CrashLoopBackOff的原因,它们处于“正在等待”状态。(如果我不对容器设置任何命令,则容器可以正常工作。如果我在容器中使用sh命令,则在创建它们之后,可以使用“ kubectl日志”读取env变量设置正确。

预期的行为是使pod的容器在执行sh命令后运行。

我找不到有关此特定问题的参考,如果需要,我几乎不需要帮助,非常感谢!

请忽略我尝试了其他图像,无论哪种方式都会发生问题。

环境:qemu VM上的Kubernetes v 1.17.1

yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
data:
  how: very
---
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: nginx
      ports:
      - containerPort: 88
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: special-config
              # Specify the key associated with the value
              key: how
  restartPolicy: Always

描述豆荚:

kubectl describe pod dapi-test-pod
Name:         dapi-test-pod
Namespace:    default
Priority:     0
Node:         kw1/10.1.10.31
Start Time:   Thu, 21 May 2020 01:02:17 +0000
Labels:       <none>
Annotations:  cni.projectcalico.org/podIP: 192.168.159.83/32
              kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"dapi-test-pod","namespace":"default"},"spec":{"containers":[{"command...
Status:       Running
IP:           192.168.159.83
IPs:
  IP:  192.168.159.83
Containers:
  test-container:
    Container ID:  docker://63040ec4d0a3e78639d831c26939f272b19f21574069c639c7bd4c89bb1328de
    Image:         nginx
    Image ID:      docker-pullable://nginx@sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097
    Port:          88/TCP
    Host Port:     0/TCP
    Command:
      /bin/sh
      -c
      env
    State:          Waiting
      Reason:       CrashLoopBackOff
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Thu, 21 May 2020 01:13:21 +0000
      Finished:     Thu, 21 May 2020 01:13:21 +0000
    Ready:          False
    Restart Count:  7
    Environment:
      SPECIAL_LEVEL_KEY:  <set to the key 'how' of config map 'special-config'>  Optional: false
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-zqbsw (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  default-token-zqbsw:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-zqbsw
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                   From               Message
  ----     ------     ----                  ----               -------
  Normal   Scheduled  13m                   default-scheduler  Successfully assigned default/dapi-test-pod to kw1
  Normal   Pulling    12m (x4 over 13m)     kubelet, kw1       Pulling image "nginx"
  Normal   Pulled     12m (x4 over 13m)     kubelet, kw1       Successfully pulled image "nginx"
  Normal   Created    12m (x4 over 13m)     kubelet, kw1       Created container test-container
  Normal   Started    12m (x4 over 13m)     kubelet, kw1       Started container test-container
  Warning  BackOff    3m16s (x49 over 13m)  kubelet, kw1       Back-off restarting failed container
kubernetes containers
3个回答
0
投票

您能否执行kubectl logs -p <podname>将有助于找到原因。


0
投票

发生这种情况是因为您正在运行的容器中的进程已完成,并且该容器已关闭,因此kubernetes将该容器标记为已完成。

如果在Docker映像中将命令定义为CMD的一部分,或者您已经添加了自己的命令,则在命令完成后容器将关闭。这是为什么当您使用普通docker运行Ubuntu时,它先启动然后又直接关闭的原因。

要使Pod及其底层Docker容器继续运行,您需要启动一个将继续运行的进程。就您而言,立即运行env命令即可。

如果您将Pod设置为始终重启,那么kubernetes将继续尝试重启它,直到达到它的退出阈值为止。

像您正在运行的一次性命令对于实用程序类型的东西很有用。即然后做一个然后摆脱豆荚。

例如:

kubectl run tester --generator run-pod/v1 --image nginx --restart Never --rm -it -- /bin/sh -c env

要运行更长的时间,请启动一个继续运行的进程。

例如:

kubectl run tester --generator run-pod/v1 --image nginx -- /bin/sh -c "sleep 30"

该命令将运行30秒,因此pod也将运行30秒。它还将使用默认的重启策略Always。因此,该过程完成30秒后,Kubernetes将Pod标记为已完成,然后重新启动它以再次执行相同的操作。

通常,pod会启动一个长期运行的过程,就像Web服务器一样。为了让Kubernetes知道该Pod是否健康,因此可以执行它的高可用性魔术操作,如果兑现了则重启它,可以使用readiness and liveness probes


0
投票

您可以使用此清单;命令["/bin/sh", "-c"]表示“运行外壳程序,并执行以下指令”。然后将args作为命令传递到外壳。多行args使其简单易读。您的广告连播会显示其环境变量,并在不停止的情况下启动NGINX进程:

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
data:
  how: very
---
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: nginx
      ports:
        - containerPort: 88
      command: ["/bin/sh", "-c"]
      args:
        - env;
          nginx -g 'daemon off;';
      env:
        # Define the environment variable
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: special-config
              # Specify the key associated with the value
              key: how
  restartPolicy: Always
© www.soinside.com 2019 - 2024. All rights reserved.