获取 "CrashLoopBackOff" 作为已部署 pod 的状态

Getting "CrashLoopBackOff" as status of deployed pod

如何调试为什么它的状态是 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 不打印任何东西。

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

$ 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]

首先我通过 postgres 部署修复了 "pod has unbound PersistentVolumeClaims" 的一些错误,所以我通过这个 post 修复了那个错误

所以现在我的 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"]}}'

所以我得到 external-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

这是我如何得到 host-config 地图

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

你为什么不...

  1. 运行 虚拟容器(运行 无限休眠命令)

  2. kubectl exec -it bash

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

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