kubectl get pods 显示 CrashLoopBackoff

kubectl get pods shows CrashLoopBackoff

我正在尝试使用我的本地 docker 图像创建一个 pod,如下所示。

1.First 我在终端 运行 这个命令

eval $(minikube docker-env)

2.I 创建了一个 docker 图像如下

sudo docker image build -t my-first-image:3.0.0 .

3.I 创建了 pod.yml 如下所示,我 运行 这个命令

kubectl -f create pod.yml.

4.then 我试过 运行 这个命令

kubectl get pods

但它显示以下错误


NAME                                  READY   STATUS             RESTARTS   AGE
multiplication-6b6d99554-d62kk        0/1     CrashLoopBackOff   9          22m
multiplication2019-5b4555bcf4-nsgkm   0/1     CrashLoopBackOff   8          17m
my-first-pod                          0/1     CrashLoopBackOff   4          2m51

5.i 获取 pods 日志

kubectl describe pod my-first-pod 

Events:
  Type     Reason     Age                    From               Message
  ----     ------     ----                   ----               -------
  Normal   Scheduled  6m22s                  default-scheduler  Successfully assigned default/my-first-pod to minikube
  Normal   Pulled     5m20s (x4 over 6m17s)  kubelet, minikube  Successfully pulled image "docker77nira/myfirstimage:latest"
  Normal   Created    5m20s (x4 over 6m17s)  kubelet, minikube  Created container
  Normal   Started    5m20s (x4 over 6m17s)  kubelet, minikube  Started container
  Normal   Pulling    4m39s (x5 over 6m21s)  kubelet, minikube  pulling image "docker77nira/myfirstimage:latest"
  Warning  BackOff    71s (x26 over 6m12s)   kubelet, minikube  Back-off restarting failed container


Dockerfile

    FROM node:carbon
    WORKDIR /app
    COPY . .
    CMD [ "node", "index.js" ]

pods.yml

    kind: Pod
    apiVersion: v1
    metadata:
     name: my-first-pod
    spec:
     containers:
     - name: my-first-container
       image: my-first-image:3.0.0

index.js

    var http = require('http');
    var server = http.createServer(function(request, response) {
     response.statusCode = 200;
     response.setHeader('Content-Type', 'text/plain');
     response.end('Welcome to the Golden Guide to Kubernetes
    Application Development!');
    });
    server.listen(3000, function() {
     console.log('Server running on port 3000');
    });

尝试使用命令kubectl logs -f my-first-pod

检查日志
kind: Pod
    apiVersion: v1
    metadata:
     name: my-first-pod
    spec:
     containers:
     - name: my-first-container
       image: my-first-image:3.0.0
       command: [ "sleep" ]
       args: [ "infinity" ]

我认为您的 pod 在 index.js

中执行脚本后被终止

我通过执行以下步骤成功 运行 你的图像:

docker build -t foo .

然后检查容器是否正常工作docker run -it foo

/app/index.js:5
response.end('Welcome to the Golden Guide to Kubernetes
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3

不确定这是否是您想要看到的结果,容器本身在运行。但是在 Kubernetes 中它进入 ErrImagePull

然后在受@Harsh Manvar 启发编辑您的Pod.yaml 之后,它可以正常工作。因此,完成命令后退出的问题只是问题的一部分。

apiVersion: v1
kind: Pod
metadata:
  name: hello-pod
spec:
  restartPolicy: Never
  containers:
  - name: hello
    image: "foo"
    imagePullPolicy: Never
    command: [ "sleep" ]
    args: [ "infinity" ]

这是 Minikube,因此您可以重复使用图像,但如果您有更多节点,这可能根本不起作用。您可以找到有关在 Kubernetes 中使用本地 docker 图像的很好的解释。