Skip to Content
Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition
book

Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition

by Benjamin Muschko
January 2026
Intermediate to advanced
392 pages
4h 44m
Chinese
O'Reilly Media, Inc.
Content preview from Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition

第21章 应用程序故障排除

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在生产环境的Kubernetes集群中运行应用程序时,故障 几乎不可避免。作为Kubernetes管理员,您有责任能够排查由应用程序开发人员设计和部署的Kubernetes对象所引发的问题。

本章将探讨有助于定位问题根源的故障排除策略,使您能够采取行动并妥善修复故障。这些策略从Kubernetes对象的高层级视角切入,并根据需要逐步深入细节。

Pod 故障排除

大多数情况下,创建 Pod 不会遇到问题。 只需执行runcreateapply命令即可实例化 Pod。若 YAML 配置文件格式正确,Kubernetes 便会接受请求,此时可假设系统运行正常。为验证行为正确性,首先应检查 Pod 的高层次运行时信息。该操作可能涉及其他 Kubernetes 对象,例如负责滚动部署多个 Pod 副本的 Deployment。

检索高级信息

要检索信息,可运行以下命令:仅获取命名空间中运行的 Pod,请执行kubectl get pods命令 ;要获取命名空间中最主要的对象类型(包括部署),请执行kubectl get all命令 。 需重点关注READYSTATUS 和RESTARTS三列。理想情况下,就绪容器数量应与规范中定义的容器数量一致。对于单容器Pod,READY列应显示1/1

状态应显示为Running,表明Pod已进入正常生命周期状态。需注意:即使Pod显示Running状态,应用程序仍可能存在运行异常。若重启次数大于0,则需检查存活探针逻辑(若已定义)并排查触发重启的原因。

以下Pod监听状态ErrImagePull,并为传入流量提供0/1个可用容器。简而言之,该Pod存在问题:

$ kubectl get pods
NAME              READY   STATUS         RESTARTS   AGE
misbehaving-pod   0/1     ErrImagePull   0          2s

在使用Kubernetes一段时间后,您将能自动识别常见错误状态 。表21-1列出了部分错误状态并说明了修复方法。

表 21-1 常见 Pod 错误状态
状态 根本原因 潜在修复方案

ImagePullBackOffErrImagePull

无法从注册表中提取映像。

检查图像名称是否正确,检查图像名称是否存在于注册表中,验证节点到注册表的网络访问,确保身份验证正确。

CrashLoopBackOff

容器中运行的应用程序或命令发生崩溃。

检查容器中执行的命令,确保映像能正常运行(例如通过 Docker 创建容器)。

CreateContainerConfigError

容器引用的ConfigMap或Secret无法找到。

检查配置对象的名称是否正确,验证命名空间中是否存在该配置对象。

检查事件

即使未遇到上述错误状态,Pod仍可能存在配置问题。可通过kubectl describe pod命令获取Pod详细信息 ,并检查其事件日志 。

以下输出属于尝试挂载不存在的密钥的 Pod。该 Pod 未显示具体错误信息,而是卡在状态 ContainerCreating:

$ kubectl get pods NAME READY STATUS RESTARTS AGE secret-pod ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Kubernetes 上的生成式人工智能 (Chinese Edition)

Kubernetes 上的生成式人工智能 (Chinese Edition)

Roland Huß, Daniele Zonca
游戏化头脑风暴

游戏化头脑风暴

Dave Gray, Sunni Brown, James Macanufo

Publisher Resources

ISBN: 0642572314712