Pod 定义与基本操作
约 518 字大约 2 分钟
布欧-Lewyon
2026-05-15
首页 › K8s › Pod 与容器(在新窗口打开) › Pod 定义与基本操作
什么是 Pod
Pod 是 Kubernetes 中最小的调度单元。一个 Pod 包含一个或多个容器,这些容器共享网络栈(IP + 端口空间)和存储卷。
Pod YAML
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
env: prod
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
protocol: TCP创建 Pod
# 命令式(快速测试)
kubectl run nginx --image=nginx:alpine --port=80
# 声明式(推荐,YAML 文件)
kubectl apply -f nginx-pod.yaml
# 查看 Pod
kubectl get pods
kubectl get pods -o wide # 查看 IP 和所在节点
# 查看详情
kubectl describe pod nginx-pod标签与选择器
# Pod 带有标签
metadata:
labels:
app: nginx
version: v1# 按标签查找 Pod
kubectl get pods -l app=nginx
kubectl get pods -l 'app=nginx,version=v1'
# 添加标签
kubectl label pod nginx-pod version=v2
# 删除标签
kubectl label pod nginx-pod version-多容器 Pod
apiVersion: v1
kind: Pod
metadata:
name: two-containers
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
- name: busybox
image: busybox:latest
command: ["/bin/sh", "-c", "while true; do wget -q -O- http://localhost:80; sleep 5; done"]# 查看多容器 Pod 的日志
kubectl logs two-containers -c nginx
kubectl logs two-containers -c busybox
# 进入指定容器
kubectl exec -it two-containers -c nginx -- sh删除 Pod
# 删除指定 Pod
kubectl delete pod nginx-pod
# 按标签删除
kubectl delete pod -l app=nginx
# 删除当前 Namespace 所有 Pod
kubectl delete pod --all由 Deployment 管理的 Pod 被删除后会重新创建。要删除需要删 Deployment。
小结
- Pod 是 K8s 最小调度单元,一个 Pod 内多个容器共享网络和存储。
- 创建 Pod 推荐声明式
kubectl apply -f pod.yaml。 - 标签(Label)用于组织和选择资源,
-l参数按标签过滤。 - 多容器 Pod 通过
-c参数指定容器操作。 kubectl describe pod是排查 Pod 问题的首选命令。
上一节:kubectl 安装与配置 下一节:Pod 生命周期与探针
