Pod因错误和退出代码原因而终止:1

时间:2020-05-16 11:32:28

标签: docker kubernetes

我对使用图像的吊舱终止有疑问:kodekloud / throw-dice

pod-defintion.yml

apiVersion: v1
kind: Pod
metadata:
  name: throw-dice-pod
spec:
  containers:
  -  image: kodekloud/throw-dice
     name: throw-dice
  restartPolicy: Never

我已经检查了DockerFile 中的步骤。它运行throw-dice.sh,它随机返回1到6之间的数字。

让我们考虑第一次容器返回3,那么下面的pod如何终止?如果脚本返回号为!6,则应该终止的Pod级别中定义的条件在哪里。

执行以下步骤执行pod-definition.yml

master $ kubectl create -f /root/throw-dice-pod.yaml
pod/throw-dice-pod created
master $ kubectl get pods
NAME             READY   STATUS              RESTARTS   AGE
throw-dice-pod   0/1     ContainerCreating   0          9s
master $ kubectl get pods
NAME             READY   STATUS   RESTARTS   AGE
throw-dice-pod   0/1     Error    0          12s
master $ kubectl describe pod throw-dice-pod
Name:         throw-dice-pod
Namespace:    default
Priority:     0
Node:         node01/172.17.0.83
Start Time:   Sat, 16 May 2020 11:20:01 +0000
Labels:       <none>
Annotations:  <none>
Status:       Failed
IP:           10.88.0.4
IPs:
  IP:  10.88.0.4
Containers:
  throw-dice:
    Container ID:   docker://4560c794b58cf8f3e3fad691b2292e37db4e84e20c9286321f026d1735272b5f
    Image:          kodekloud/throw-dice
    Image ID:       docker-pullable://kodekloud/throw-dice@sha256:9c70a0f907b99293885a9591b6162e9ec89e127937626a97ca7f9f6be2d98b01
    Port:           <none>
    Host Port:      <none>
    State:          Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Sat, 16 May 2020 11:20:10 +0000
      Finished:     Sat, 16 May 2020 11:20:10 +0000
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-nr5kl (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  default-token-nr5kl:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-nr5kl
    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  <unknown>  default-scheduler  Successfully assigned default/throw-dice-pod to node01
  Normal  Pulling    21s        kubelet, node01    Pulling image "kodekloud/throw-dice"
  Normal  Pulled     19s        kubelet, node01    Successfully pulled image "kodekloud/throw-dice"
  Normal  Created    19s        kubelet, node01    Created container throw-dice
  Normal  Started    18s        kubelet, node01    Started container throw-dice


 master $ kubectl logs throw-dice-pod
      2

2 个答案:

答案 0 :(得分:3)

dockerfile具有ENTRYPOINT sh throw-dice.sh,这意味着执行脚本,然后容器自动终止。如果希望容器继续运行,则需要启动一个长时间运行的进程,例如一个Java进程ENTRYPOINT ["java", "-jar", "/whatever/your.jar"]

答案 1 :(得分:2)

此处的退出代码1由应用程序设置。

看看throw-dice.sh。我们看到应用程序在0,1,2之间改组了退出代码。

  • 退出代码始终等于上述改组结果。
  • 如果退出代码为0,则会记录6
  • 如果退出代码是12,则记录了1-5中的整型转换结果

因此,在您的情况下,改组的退出代码为1,日志的改组结果为2。并且从应用程序退出代码1被认为是Kubernetes的Error原因。