Pod 卡在“CrashLoopBackOff"上,即使它应该进入/bin/bash [英] Pod Stuck on `CrashLoopBackOff` even though it should go into /bin/bash

查看:58
本文介绍了Pod 卡在“CrashLoopBackOff"上,即使它应该进入/bin/bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 minikube 运行 Kubernetes 集群,即使我在 Dockerfile<中指定,我的部署(或单个 Pod)也不会继续运行/code> 应该保持终端打开(我也用 sh 尝试过).他们不断地重新启动,有时他们会在再次重新启动之前卡在 CrashLoopBackOff 状态:

来自 ubuntu曝光 8080CMD/bin/bash

我的部署文件:

apiVersion:apps/v1种类:部署元数据:名称:卧铺部署规格:复制品:10选择器:匹配标签:应用程序:卧铺世界minReadySeconds:10战略:类型:滚动更新滚动更新:最大不可用:1最大浪涌:1模板:元数据:标签:应用程序:卧铺世界规格:容器:- 名称:卧铺舱图片:kubelabimagePullPolicy: IfNotPresent端口:- 容器端口:8080

总而言之,我的工作流程如下(deploy.sh):

#!/bin/bash# 打扫kubectl 删除部署 --allkubectl 删除 pod --all# 构建图像须藤码头建设-t库贝拉.# 部署kubectl apply -f sleeper_deployment.yml

顺便说一句,我已经使用 sudo docker run -dt kubelab 单独测试了 Docker 容器,并且 它确实保持不变.为什么它不留在 Kubernetes 中?是否有参数(在 YAML 文件中)或我应该为这种特殊情况使用的标志?

解决方案

1.原始答案(但已编辑...)

如果您熟悉 Docker,请查看 this.

如果您正在寻找 docker run -dt kubelab 的等效项,请尝试 kubectl run -it kubelab --restart=Never --image=ubuntu/bin/bash.在您的情况下,使用 Docker -t 标志:分配一个伪-tty.这就是为什么您的 Docker 容器会一直运行的原因.

试试:

kubectl run kubelab --image=ubuntu--公开--端口 8080 --/bin/bash -c 'while true;do sleep 3600;done'

或者:

kubectl run kubelab --image=ubuntu--干运行-oyaml --公开--端口 8080 --/bin/bash -c 'while true;do sleep 3600;done'

2.解释发生了什么(Philippe Fanaro 添加):

正如@David Maze 所说,bash 进程将立即退出,因为人工终端不会有任何东西进入其中,这与 Docker 的行为略有不同.

如果更改restart Policy,它仍然会终止,不同的是Pod不会重新生成或重启.

一种方法是(注意restartPolicy的标签):

api版本:v1种类:豆荚元数据:名称:kubelab-pod标签:区域:产品版本:v1规格:容器:- 名称:kubelab-ctr图片:kubelabimagePullPolicy: IfNotPresent端口:- 容器端口:8080重启策略:从不

但是,如果在 deployment YAML 中指定,这将不起作用.这是因为部署会强制重新生成,试图始终达到所需状态.这可以在部署文档网页中确认:

<块引用>

只有一个 .spec.template.允许 spec.restartPolicy 等于 Always,如果未指定,则为默认值.

3.如果你真的想强制 Docker 容器继续运行

在这种情况下,您将需要一些不会退出的东西.类似服务器的进程就是一个例子.但您也可以尝试this StackOverflow answer中提到的内容:

CMD exec/bin/bash -c "trap : TERM INT; sleep infinity & wait"

<块引用>

这将使您的容器保持活动状态,直到它被告知停止.使用陷阱和等待将使您的容器立即响应停止请求.没有陷阱/等待停止将需要几秒钟.

I'm running a Kubernetes cluster with minikube and my deployment (or individual Pods) won't stay running even though I specify in the Dockerfile that it should stay leave a terminal open (I've also tried it with sh). They keep getting restarted and sometimes they get stuck on a CrashLoopBackOff status before restarting again:

FROM ubuntu

EXPOSE 8080

CMD /bin/bash

My deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sleeper-deploy
spec:
  replicas: 10
  selector:
    matchLabels:
      app: sleeper-world
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
    metadata:
      labels:
        app: sleeper-world
    spec:
      containers:
      - name: sleeper-pod
        image: kubelab
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080

All in all, my workflow follows (deploy.sh):

#!/bin/bash

# Cleaning

kubectl delete deployments --all
kubectl delete pods --all

# Building the Image

sudo docker build 
    -t kubelab 
    .

# Deploying

kubectl apply -f sleeper_deployment.yml

By the way, I've tested the Docker Container solo using sudo docker run -dt kubelab and it does stay up. Why doesn't it stay up within Kubernetes? Is there a parameter (in the YAML file) or a flag I should be using for this special case?

解决方案

1. Original Answer (but edited...)

If you are familiar with Docker, check this.

If you are looking for an equivalent of docker run -dt kubelab, try kubectl run -it kubelab --restart=Never --image=ubuntu /bin/bash. In your case, with the Docker -t flag: Allocate a pseudo-tty. That's why your Docker Container stays up.

Try:

kubectl run kubelab 
    --image=ubuntu 
    --expose 
    --port 8080 
    -- /bin/bash -c 'while true;do sleep 3600;done'

Or:

kubectl run kubelab 
    --image=ubuntu 
    --dry-run -oyaml 
    --expose 
    --port 8080 
    -- /bin/bash -c 'while true;do sleep 3600;done'

2. Explaining what's going on (Added by Philippe Fanaro):

As stated by @David Maze, the bash process is going to exit immediately because the artificial terminal won't have anything going into it, a slightly different behavior from Docker.

If you change the restart Policy, it will still terminate, the difference is that the Pod won't regenerate or restart.

One way of doing it is (pay attention to the tabs of restartPolicy):

apiVersion: v1
kind: Pod
metadata:
  name: kubelab-pod
  labels:
    zone: prod
    version: v1
spec:
  containers:
  - name: kubelab-ctr
    image: kubelab
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080
  restartPolicy: Never

However, this will not work if it is specified inside a deployment YAML. And that's because deployments force regeneration, trying to always get to the desired state. This can be confirmed in the Deployment Documentation Webpage:

Only a .spec.template.spec.restartPolicy equal to Always is allowed, which is the default if not specified.

3. If you really wish to force the Docker Container to Keep Running

In this case, you will need something that doesn't exit. A server-like process is one example. But you can also try something mentioned in this StackOverflow answer:

CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"

This will keep your container alive until it is told to stop. Using trap and wait will make your container react immediately to a stop request. Without trap/wait stopping will take a few seconds.

这篇关于Pod 卡在“CrashLoopBackOff"上,即使它应该进入/bin/bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆