Pod常见状态分析

常见状态和原因

kube-scheduler
Pending:调度不成功

kubelet
ImagePullBackOff:镜像拉取失败
Running:容器已创建并且启动
Ready:容器可以提供服务
CrashLoopBackOff:容器退出后kubelet拉起新容器

如果没有配置livenessProbe或者readinessProbe,那么对应的检查默认成功。
通过livenessProbe和readinessProbe是容器Ready的必要条件。

实验1:Pod重启次数计算方式
创建初始化容器后Pod重启次数是初始化容器重启次数之和

创建业务容器后Pod重启次数是业务容器重启次数之和

apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
        - name: busybox1
          image: busybox:1.36
          command: ["sh", "-c", "sleep 10000"]
        - name: busybox2
          image: busybox:1.36
          command: ["sh", "-c", "sleep 10000"]
          readinessProbe:
            tcpSocket:
              port: 100
            initialDelaySeconds: 5
      initContainers:
        - name: check1
          image: busybox:1.36
          command: ["sh", "-c", "cat /test/abc"]
          volumeMounts:
            - mountPath: /test
              name: root
        - name: check2
          image: busybox:1.36
          command: ["sh", "-c", "cat /test/abc"]
          volumeMounts:
            - mountPath: /test
              name: root
      volumes:
      - name: root
        hostPath:
          path: /root

实验2:livenessProbe成功而readinessProbe失败进入不了Ready

apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
        - name: busybox1
          image: busybox:1.36
          command: ["sh", "-c", "sleep 10000"]
        - name: busybox2
          image: busybox:1.36
          command: ["sh", "-c", "sleep 10000"]
          readinessProbe:
            tcpSocket:
              port: 100
            initialDelaySeconds: 5

查询异常Pod

kubectl get pod -A | grep -v Completed | awk -F ' ' '{split($3,a,"/");if(a[1]!=a[2]||$4!="Running"){print}}'

删除驱逐的Pod

kubectl get pods -A | grep "Evicted" | awk '{print $1,$2}' | xargs -r kubectl delete pod -n

与截图相比增加了-r参数,目的是xargs没有拿到查询结果时不执行kubectl。

参考资料

pod常见状态及故障原因

posted on 2023-05-01 18:20  王景迁  阅读(144)  评论(0编辑  收藏  举报

导航