11.解决 Pod 部署问题
发布: (2026年2月21日 GMT+8 10:30)
2 分钟阅读
原文: Dev.to
Source: Dev.to
实验信息
一名初级 DevOps 成员在 Kubernetes 集群上部署堆栈时遇到困难。Pod 启动失败,出现错误。让我们快速排查并纠正问题。
- Pod 名称:
webserver - 主容器:
nginx-container(镜像nginx:latest) - Sidecar 容器:
sidecar-container(镜像ubuntu:latest)
目标是让 Pod 进入 Running 状态,并使应用可访问。
注意:
jump_host上的kubectl已经配置好可与 Kubernetes 集群交互。
初始检查
kubectl get pods
kubectl describe pod webserver
如果 Pod 卡住,先删除再应用修正后的定义:
kubectl delete pod webserver
创建修正后的 Pod 定义
apiVersion: v1
kind: Pod
metadata:
name: webserver
labels:
app: web-app
spec:
containers:
- name: nginx-container
image: nginx:latest # Fixed: removed extra 's'
ports:
- containerPort: 80
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx
- name: sidecar-container
image: ubuntu:latest
command:
- sh
- -c
args:
- while true; do cat /var/log/nginx/access.log /var/log/nginx/error.log; sleep 30; done
volumeMounts:
- name: shared-logs
mountPath: /var/log/nginx
volumes:
- name: shared-logs
emptyDir: {}
应用清单:
kubectl apply -f webserver-fixed.yaml
验证部署
kubectl get pods
kubectl describe pod webserver
检查每个容器的日志:
kubectl logs webserver -c nginx-container
kubectl logs webserver -c sidecar-container
在 Pod 内部测试 Web 服务器:
kubectl exec webserver -c nginx-container -- curl -s localhost:80 | head -n 5
如果输出显示预期的 HTML 内容,说明 Pod 正常运行,应用已可访问。
资源与后续步骤
- 完整代码仓库: KodeKloud Learning Labs
- 更多深度阅读: Whispering Cloud Insights – 阅读其他技术文章
- 加入讨论: DEV Community – 分享你的想法和问题
- 联系: LinkedIn
致谢
- 所有实验均来自 KodeKloud。
- 感谢提供这些宝贵资源。