【问题标题】:Getting "CrashLoopBackOff" as status of deployed pod将“CrashLoopBackOff”作为已部署 pod 的状态
【发布时间】:2023-03-16 21:25:01
【问题描述】:

如何调试为什么它的状态是CrashLoopBackOff?

我没有使用 minikube ,而是在 Aws Kubernetes 实例上工作。

我按照本教程进行操作。 https://github.com/mkjelland/spring-boot-postgres-on-k8s-sample

当我这样做时

  kubectl create -f specs/spring-boot-app.yml

并通过

检查状态
  kubectl get pods 

它给了

     spring-boot-postgres-sample-67f9cbc8c-qnkzg   0/1     CrashLoopBackOff   14         50m

下面的命令

 kubectl describe pods spring-boot-postgres-sample-67f9cbc8c-qnkzg

给了

Events:
  Type     Reason   Age                    From                      Message
  ----     ------   ----                   ----                      -------
  Warning  BackOff  3m18s (x350 over 78m)  kubelet, ip-172-31-11-87  Back-off restarting failed container

命令 kubectl get pods --all-namespaces 给出

NAMESPACE     NAME                                           READY   STATUS             RESTARTS   AGE
default       constraintpod                                  1/1     Running            1          88d
default       postgres-78f78bfbfc-72bgf                      1/1     Running            0          109m
default       rcsise-krbxg                                   1/1     Running            1          87d
default       spring-boot-postgres-sample-667f87cf4c-858rx   0/1     CrashLoopBackOff   4          110s
default       twocontainers                                  2/2     Running            479        89d
kube-system   coredns-86c58d9df4-kr4zj                       1/1     Running            1          89d
kube-system   coredns-86c58d9df4-qqq2p                       1/1     Running            1          89d
kube-system   etcd-ip-172-31-6-149                           1/1     Running            8          89d
kube-system   kube-apiserver-ip-172-31-6-149                 1/1     Running            1          89d
kube-system   kube-controller-manager-ip-172-31-6-149        1/1     Running            1          89d
kube-system   kube-flannel-ds-amd64-4h4x7                    1/1     Running            1          89d
kube-system   kube-flannel-ds-amd64-fcvf2                    1/1     Running            1          89d
kube-system   kube-proxy-5sgjb                               1/1     Running            1          89d
kube-system   kube-proxy-hd7tr                               1/1     Running            1          89d
kube-system   kube-scheduler-ip-172-31-6-149                 1/1     Running            1          89d

命令 kubectl 记录 spring-boot-postgres-sample-667f87cf4c-858rx 不打印任何东西。

【问题讨论】:

  • 你可以使用kubectl describe pods spring-boot-postgres-sample-67f9cbc8c-qnkzg检查pods
  • @ThanhNguyenVan 已编辑 ..我添加了事件 :.,你还想让我添加什么吗?
  • docker logs container_id 显示属于该 pod 的容器日志
  • 对不起,我不知道如何获取 container_id...我试过 kubectl logs /jarName 但没有这样的容器
  • 运行kubectl describe pods spring-boot-postgres-sample-67f9cbc8c-qnkzg它显示容器所属。

标签: kubernetes kubernetes-pod


【解决方案1】:

你为什么不...

  1. 运行一个虚拟容器(运行一个无限睡眠命令)

  2. kubectl exec -it bash

  3. 直接运行程序,直接查看日志。

它是在 K8s 上调试的一种更简单的形式。

【讨论】:

  • 我有spring boot jar,直接运行程序是什么意思..如何查看它们的日志?
  • 这不显示输出吗? bash kubectl logs <container-name>
【解决方案2】:

首先我通过 postgres 部署修复了“pod has unbound PersistentVolumeClaims”的一些错误,所以我通过这篇文章修复了该错误 pod has unbound PersistentVolumeClaims

所以现在我的 postgres 部署正在运行。

kubectl logs spring-boot-postgres-sample-67f9cbc8c-qnkzg 没有打印任何内容,这意味着配置文件中有问题。 kubectl describe pod spring-boot-postgres-sample-67f9cbc8c-qnkzg 说明容器已终止,原因已完成, 我通过运行容器无限时间来修复它 通过添加

   # Just sleep forever
command: [ "sleep" ]
args: [ "infinity" ]

所以现在我的部署正在运行。 但现在我通过

公开了我的服务
kubectl expose deployment spring-boot-postgres-sample --type=LoadBalancer --port=8080

但无法获取 External-Ip ,所以我做了

kubectl patch svc <svc-name> -n <namespace> -p '{"spec": {"type": "LoadBalancer", "externalIPs":["172.31.71.218"]}}'

所以我的外部 IP 为“172.31.71.218”

但现在的问题是 curl http://172.31.71.218:8080/ 超时

我做错了什么?

这是我的deployment.yml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: spring-boot-postgres-sample
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      name: spring-boot-postgres-sample
      labels:
        app: spring-boot-postgres-sample
    spec:
      containers:
      - name: spring-boot-postgres-sample
        command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]
        env:
          - name: POSTGRES_USER
            valueFrom:
              configMapKeyRef:
                name: postgres-config
                key: postgres_user
          - name: POSTGRES_PASSWORD
            valueFrom:
              configMapKeyRef:
                name: postgres-config
                key: postgres_password
          - name: POSTGRES_HOST
            valueFrom:
              configMapKeyRef:
                name: hostname-config
                key: postgres_host
        image: <mydockerHUbaccount>/spring-boot-postgres-on-k8s:v1

这是我的 postgres.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  namespace: default
data:
  postgres_user: postgresuser
  postgres_password: password
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres
spec:
  template:
    metadata:
      labels:
        app: postgres
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-pv-claim
      containers:
        - image: postgres
          name: postgres
          env:
            - name: POSTGRES_USER
              valueFrom:
                configMapKeyRef:
                  name: postgres-config
                  key: postgres_user
            - name: POSTGRES_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: postgres-config
                  key: postgres_password
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          ports:
            - containerPort: 5432
              name: postgres
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  type: ClusterIP
  ports:
    - port: 5432
  selector:
    app: postgres

这里我是如何得到主机配置图的

kubectl create configmap hostname-config --from-literal=postgres_host=$(kubectl get svc postgres -o jsonpath="{.spec.clusterIP}")

【讨论】:

  • 无法获取日志..这是否意味着我的配置仍然错误?你能检查我的部署.yaml..@ThanhNguyenVan
  • 您创建了postgresconfigmap 吗?
  • 是的。我做到了......我还添加了我的 postgres.config 和 host-config
  • 两个服务状态都在运行
【解决方案3】:

我能够重现该场景。似乎应用程序和 Postgres DB 之间存在连接问题。所以应用程序无法启动。请在下面找到它可能对您有所帮助的日志。

$ kubectl get po
NAME                                           READY     STATUS             RESTARTS   AGE
spring-boot-postgres-sample-5d7c85d98b-qwvjr   0/1       CrashLoopBackOff   19         1h


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

2019-05-23 10:53:01.889 ERROR 1 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

org.postgresql.util.PSQLException: Connection to :5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:262) ~[postgresql-9.4.1212.jre7.jar!/:9.4.1212.jre7]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-9.4.1212.jre7.jar!/:9.4.1212.jre7]

【讨论】:

  • kubectl logs spring-boot-postgres-sample-5d7c85d98b-hl5z2 -f
  • 为什么在我的情况下它没有打印一些东西?
猜你喜欢
  • 1970-01-01
  • 2022-10-23
  • 2020-05-23
  • 2020-09-01
  • 1970-01-01
  • 2019-04-04
  • 2023-03-11
  • 1970-01-01
  • 2020-02-14
相关资源
最近更新 更多