- Pod是可以创建和管理Kubernetes计算的最小可部署单元
- 一个Pod代表着集群中运行的一个进程,每个pod都有一个唯一的ip。
- 一个pod类似一个豌豆荚,包含一个或多个容器(通常是docker)
- 多个容器间共享IPC、Network和UTC namespace。
一.命令式对象管理
1.命名空间管理
kubectl是kubernetes集群的命令行工具,通过它能够对集群本身进行管理,并能够在集群上进行容器 化应用的安装部署。
bash
#查看命名空间
[root@k8s-master ~]# kubectl get namespaces
#创建命名空间
[root@k8s-master ~]# kubectl get namespaces
#删除命名空间
[root@k8s-master ~]# kubectl delete namespaces timinglee
2.pod管理
bash
#查看pod的运行情况和在哪里运行
[root@k8s-master ~]# kubectl get pods -o wide
No resources found in default namespace.
#创建pod
[root@k8s-master ~]# kubectl run lee --image nginx:latest
pod/lee created
#当pod创建出现问题
[root@k8s-master ~]# kubectl run error --image lee:v1
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
error 0/1 ImagePullBackOff 0 38s 10.244.2.3 k8s-node2 <none>
#查看pod运行的详细信息
[root@k8s-master ~]# kubectl describe pods error
#删除
[root@k8s-master ~]# kubectl delete pods error
[root@k8s-master ~]# kubectl delete pods --all
pod "error" deleted from default namespace
pod "lee" deleted from default namespace
3.创建自主式pod
优点:
- 灵活性高:可以精确控制 Pod 的各种配置参数,包括容器的镜像、资源限制、环境变量、命令和参数等,满足特定的应用需求。
- 学习和调试方便:对于学习 Kubernetes 的原理和机制非常有帮助,通过手动创建 Pod 可以深入了解 Pod 的结构和配置方式。在调试问题时,可以更直接地观察和调整 Pod 的设置。
- 适用于特殊场景:在一些特殊情况下,如进行一次性任务、快速验证概念或在资源受限的环境中进行特定配置时,手动创建 Pod 可能是一种有效的方式。
缺点:
- 管理复杂:如果需要管理大量的 Pod,手动创建和维护会变得非常繁琐和耗时。难以实现自动化的扩缩容、故障恢复等操作。
- 缺乏高级功能:无法自动享受 Kubernetes 提供的高级功能,如自动部署、滚动更新、服务发现等。这可能导致应用的部署和管理效率低下。
- 可维护性差:手动创建的 Pod 在更新应用版本或修改配置时需要手动干预,容易出现错误,并且难以保证一致性。相比之下,通过声明式配置或使用 Kubernetes 的部署工具可以更方便地进行应用的维护和更新。
4.利用控制器管理pod
高可用性和可靠性:
- 自动故障恢复:如果一个 Pod 失败或被删除,控制器会自动创建新的 Pod 来维持期望的副本数量。确保应用始终处于可用状态,减少因单个 Pod 故障导致的服务中断。
- 健康检查和自愈:可以配置控制器对 Pod 进行健康检查(如存活探针和就绪探针)。如果 Pod 不健康,控制器会采取适当的行动,如重启 Pod 或删除并重新创建它,以保证应用的正常运行。
可扩展性:
- 轻松扩缩容:可以通过简单的命令或配置更改来增加或减少 Pod 的数量,以满足不同的工作负载需求。例如,在高流量期间可以快速扩展以处理更多请求,在低流量期间可以缩容以节省资源。
- 水平自动扩缩容(HPA):可以基于自定义指标(如 CPU 利用率、内存使用情况或应用特定的指标)自动调整 Pod 的数量,实现动态的资源分配和成本优化。
版本管理和更新:
- 滚动更新:对于 Deployment 等控制器,可以执行滚动更新来逐步替换旧版本的 Pod 为新版本,确保应用在更新过程中始终保持可用。可以控制更新的速率和策略,以减少对用户的影响。
- 回滚:如果更新出现问题,可以轻松回滚到上一个稳定版本,保证应用的稳定性和可靠性。
声明式配置:
- 简洁的配置方式:使用 YAML 或 JSON 格式的声明式配置文件来定义应用的部署需求。这种方式使得配置易于理解、维护和版本控制,同时也方便团队协作。
- 期望状态管理:只需要定义应用的期望状态(如副本数量、容器镜像等),控制器会自动调整实际状态与期望状态保持一致。无需手动管理每个 Pod 的创建和删除,提高了管理效率。
服务发现和负载均衡:
- 自动注册和发现:Kubernetes 中的服务(Service)可以自动发现由控制器管理的 Pod,并将流量路由到它们。这使得应用的服务发现和负载均衡变得简单和可靠,无需手动配置负载均衡器。
- 流量分发:可以根据不同的策略(如轮询、随机等)将请求分发到不同的 Pod,提高应用的性能和可用性。
多环境一致性:
- 一致的部署方式:在不同的环境(如开发、测试、生产)中,可以使用相同的控制器和配置来部署应用,确保应用在不同环境中的行为一致。这有助于减少部署差异和错误,提高开发和运维效率。
二.kubectl 命令实操
1.上传实验镜像到仓库library中
bash
[root@k8s-master ~]# docker load -i myapp.tar.gz
[root@k8s-master ~]# docker tag timinglee/myapp:v1 reg.timinglee.org/library/myapp:v1
[root@k8s-master ~]# docker push reg.timinglee.org/library/myapp:v1
[root@k8s-master ~]# docker tag timinglee/myapp:v2 reg.timinglee.org/library/myapp:v2
[root@k8s-master ~]# docker push reg.timinglee.org/library/myapp:v2

2.kubectl命令使用方法
create:新建资源,不能更新
bash
# 创建 Deployment 资源:名称webcluster,副本数 2,镜像myapp:v1
[root@k8s-master ~]# kubectl create deployment webcluster --replicas 2 --image myapp:v1
deployment.apps/webcluster created
[root@k8s-master ~]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
webcluster 2/2 2 2 15s
#Deployment 创建成功,2 个 Pod 正常 Running
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-28thm 1/1 Running 0 24s
webcluster-77c87d9946-vwrsq 1/1 Running 0 24s
# 删除
[root@k8s-master ~]# kubectl delete deployments.apps webcluster
deployment.apps "webcluster" deleted from default namespace
[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.

edit:在线编辑完整 yaml
bash
[root@k8s-master ~]# kubectl create deployment webcluster --image myapp:v1
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-2cgr7 1/1 Running 0 36s
# 在线修改replicas参数
[root@k8s-master ~]# kubectl edit deployments.apps webcluster
replicas: 2
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-2cgr7 1/1 Running 0 36s
webcluster-77c87d9946-2wqn7 1/1 Running 0 103s

patch:局部补丁修改
bash
# 修改文件中局部内容
[root@k8s-master ~]# kubectl patch deployments.apps webcluster -p '{"spec":{"replicas":1}}'
deployment.apps/webcluster patched
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-2wqn7 1/1 Running 0 6m20s

expose:快速将 Deployment、Pod 等资源暴露为 Service
bash
[root@k8s-master ~]# kubectl expose deployment webcluster --port 80 --target-port 80
service/webcluster exposed
# 查看是否生效
[root@k8s-master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6h
webcluster ClusterIP 10.97.61.108 <none> 80/TCP 18s
[root@k8s-master ~]# kubectl describe svc webcluster

访问测试:

logs:查看容器日志
bash
[root@k8s-master ~]# kubectl logs pods/webcluster-77c87d9946-gh9v7
10.244.0.0 - - [20/Aug/2026:08:43:52 +0000] "GET /hostname.html HTTP/1.1" 200 28 "-" "curl/7.76.1" "-"
10.244.0.0 - - [20/Aug/2026:08:43:52 +0000] "GET /hostname.html HTTP/1.1" 200 28 "-" "curl/7.76.1" "-"

attach:缠绕,进入运行中的容器
bash
# 导入并上传镜像
[root@k8s-master ~]# docker load -i busybox-latest.tar.gz
Loaded image: busybox:latest
[root@k8s-master ~]# docker tag busybox:latest reg.timinglee.org/library/busybox:latest
[root@k8s-master ~]# kubectl run -it testpod --image busybox:latest
# 不中断退出
/ #
/ #
/ # <ctrl+pq>
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 55s
# 使用attach重新进入容器
[root@k8s-master ~]# kubectl attach pods/testpod -it
All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt.
If you don't see a command prompt, try pressing enter.
/ #
/ #

exec:进入容器执行命令
bash
[root@k8s-master ~]# kubectl run testpod --image nginx:latest
pod/testpod created
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
testpod 1/1 Running 0 4s
root@k8s-master ~]# kubectl exec -it pods/testpod -c testpod -- /bin/bash
root@testpod:/#

cp:容器与宿主机互拷文件
bash
# 从容器往宿主机拷贝目录
[root@k8s-master ~]# kubectl cp testpod:/usr/share/nginx/html/index.html /mnt/test
tar: Removing leading `/' from member names
# 从容器往宿主机拷贝文件
[root@k8s-master ~]# kubectl cp testpod:/usr/share/nginx/html /mnt/
tar: Removing leading `/' from member names
[root@k8s-master ~]# ls /mnt/
50x.html docker.service file1 hgfs index.html test
# 从宿主机往容器拷贝文件
[root@k8s-master ~]# echo timinglee > /mnt/index.html
[root@k8s-master ~]# kubectl cp /mnt/index.html testpod:/usr/share/nginx/html/index.html
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testpod 1/1 Running 0 6m3s 10.244.1.12 k8s-node1 <none> <none>
#访问测试是否文件传入了容器
[root@k8s-master ~]# curl 10.244.1.12
timinglee

rollout:deployment 发布、回滚
bash
[root@k8s-master pod]# kubectl create deployment webcluster --image myapp:v1 --replicas 2 --dry-run=client -o yaml > webcluster.yml
[root@k8s-master pod]# kubectl apply -f webcluster.yml
deployment.apps/webcluster created
[root@k8s-master pod]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
webcluster 2/2 2 2 7s
[root@k8s-master pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-77c87d9946-5qbxt 1/1 Running 0 16s
webcluster-77c87d9946-95b2g 1/1 Running 0 16s
[root@k8s-master pod]# kubectl rollout status deployment webcluster
deployment "webcluster" successfully rolled out
deployment.apps/webcluster resumed
[root@k8s-master pod]# kubectl rollout restart deployment webcluster
deployment.apps/webcluster restarted
[root@k8s-master pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-7bfd865747-jmhwl 1/1 Running 0 6s
webcluster-7bfd865747-qv2xt 1/1 Running 0 8s
[root@k8s-master pod]# kubectl rollout restart deployment webcluster
deployment.apps/webcluster restarted
[root@k8s-master pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-7bfd865747-jmhwl 1/1 Running 0 19s
webcluster-7bfd865747-qv2xt 0/1 Completed 0 21s
webcluster-9787d97f6-z7xv5 1/1 Running 0 1s
webcluster-9787d97f6-zl6q2 0/1 ContainerCreating 0 0s

scale:扩缩副本数
bash
# 创建 指定pod有4个
[root@k8s-master pod]# kubectl scale deployment webcluster --replicas 4
deployment.apps/webcluster scaled
[root@k8s-master pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-9787d97f6-bh796 1/1 Running 0 2s
webcluster-9787d97f6-bh8jd 1/1 Running 0 2s
webcluster-9787d97f6-z7xv5 1/1 Running 0 89s
webcluster-9787d97f6-zl6q2 1/1 Running 0 88s
# 调整pod为1个
[root@k8s-master pod]# kubectl scale deployment webcluster --replicas 1
deployment.apps/webcluster scaled
[root@k8s-master pod]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-9787d97f6-zl6q2 1/1 Running 0 93s

label:管理标签
bash
[root@k8s-master pod]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
webcluster-9787d97f6-zl6q2 1/1 Running 0 6m57s app=webcluster,pod-template-hash=9787d97f6
[root@k8s-master pod]# kubectl get deployments.apps webcluster --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
webcluster 1/1 1 1 12m app=webcluster
# 去除标签
[root@k8s-master pod]# kubectl label pods webc
# 打回标签luster-9787d97f6-zl6q2 app-
pod/webcluster-9787d97f6-zl6q2 unlabeled
[root@k8s-master pod]# kubectl label pods webcluster-9787d97f6-zl6q2 app=webcluster
pod/webcluster-9787d97f6-zl6q2 labeled

三.利用控制器实现版本更替
1.建立控制器
harbor仓库中的library项目中须提前备好myapp:v1和myapp:v2两个镜像。
bash
# 生成 Deployment yaml 文件
[root@k8s-master ~]# kubectl create deployment webcluster --image myapp:v1 --replicas 2 --dry-run=client -o yaml > webcluster.yml
root@k8s-master ~]# vim webcluster.yml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: webcluster
name: webcluster
spec:
replicas: 2
selector:
matchLabels:
app: webcluster
template:
metadata:
labels:
app: webcluster
spec:
containers:
- image: myapp:v1
name: myapp
[root@k8s-master ~]# kubectl apply -f webcluster.yml
deployment.apps/webcluster created
[root@k8s-master ~]# kubectl get pods
#查看 Deployment 发布历史
[root@k8s-master ~]# kubectl rollout history deployment webcluster
deployment.apps/webcluster
REVISION CHANGE-CAUSE
1 <none>
# 暴露为 NodePort 类型 Service
[root@k8s-master ~]# kubectl expose deployment webcluster --port 80 --target-port 80 --type NodePort
[root@k8s-master ~]# kubectl get svc

暴漏端口,访问测试:

2.更新业务版本
bash
# 修改 Deployment 中容器myapp的镜像为myapp:v2,触发滚动更新。
[root@k8s-master ~]# kubectl set image deployments webcluster myapp=myapp:v2
deployment.apps/webcluster image updated
# 手动添加变更注解,为此次更新设定标签
[root@k8s-master ~]# kubectl annotate deployment webcluster kubernetes.io/change-cause="myappv2" --overwrite
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
webcluster-6c8b4bb9d7-hpxf7 1/1 Running 0 7s
webcluster-6c8b4bb9d7-kz46j 1/1 Running 0 8s
[root@k8s-master ~]# kubectl rollout history
daemonset deployment statefulset
# 查看版本历史
[root@k8s-master ~]# kubectl rollout history deployment webcluster
deployment.apps/webcluster
REVISION CHANGE-CAUSE
1 <none>
2 <none>

3.版本回退
bash
# 查看版本历史
[root@k8s-master ~]# kubectl rollout history deployment webcluster
deployment.apps/webcluster
REVISION CHANGE-CAUSE
1 <none>
2 <none>
# 版本回退
[root@k8s-master ~]# kubectl rollout undo deployment webcluster --to-revision 1
deployment.apps/webcluster rolled back

四.利用yaml文件声明资源
1.在pod中运行多容器
bash
# 生成 Pod yaml 模板
[root@k8s-master ~]# kubectl run testpod --image myapp:v1 --dry-run=client -o yaml > testpod.yaml
[root@k8s-master ~]# vim testpod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
containers:
- image: myapp:v1 # 容器1:业务应用myapp,监听80端口
name: myapp1
- image: busyboxplus:latest # 容器2:工具busybox
name: busybox
command:
- /bin/sh
- -c
- sleep 10000 # 休眠10000秒,保持容器不退出
[root@k8s-master ~]# kubectl apply -f testpod.yaml
# 进入指定容器执行命令
[root@k8s-master ~]# kubectl exec -it pods/testpod -c busybox -- /bin/sh
[root@k8s-master ~]# kubectl exec -it pods/testpod -c busybox -- /bin/sh
/ # curl 127.0.0.1
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

2.在pod运行主机中暴漏端口
bash
[root@k8s-master pod]# vim testpod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
containers:
- image: myapp:v1
name: myapp1
ports:
- name: http
containerPort: 80 #pod内部容器端口
hostPort: 80 #pod所在节点端口
protocol: TCP #端口所用协议
[root@k8s-master pod]# kubectl apply -f testpod.yaml
root@k8s-master pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
testpod 1/1 Running 0 3m33s 10.244.5.43 k8s-node2 <none> <none>
[root@k8s-master pod]# curl k8s-node2
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

3.在pod中指定变量
bash
[root@k8s-master pod]# vim mysql.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: mysql
name: mysql
spec:
containers:
- image: mysql:8.0
name: mysql8
env: # env环境变量:MYSQL_ROOT_PASSWORD=lee,设置mysql root用户密码为lee。
- name: MYSQL_ROOT_PASSWORD
value: lee # mysql 镜像会读取该环境变量初始化 root 密码。
- image: phpmyadmin:latest # mysqladmin容器:phpmyadmin 网页管理工具
name: mysqladmin
env:
- name: PMA_ARBITRARY #PMA_ARBITRARY="1":开启phpmyadmin允许连接任意mysql服务器
value: "1"
ports:
- name: phpadminport
containerPort: 80
hostPort: 80 #hostPort:80:把容器80端口直接映射到宿主机节点的80端口。
protocol: TCP
[root@k8s-master pod]# kubectl apply -f mysql.yml
[root@k8s-master pod]# kubectl get pods -o wide

访问测试:

4.选择运行节点
bash
# 查看当前标签
root@k8s-master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready control-plane 29h v1.35.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node1 Ready <none> 29h v1.35.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux
k8s-node2 Ready <none> 24h v1.35.7 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux
[root@k8s-master pod]# vim mysql.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: mysql
name: mysql
spec:
nodeSelector: # 节点选择器,强制 Pod 调度到标签匹配的节点。
kubernetes.io/hostname: k8s-node2
containers:
- image: mysql:8.0
name: mysql8
env:
- name: MYSQL_ROOT_PASSWORD
value: lee
- image: phpmyadmin:latest
name: mysqladmin
env:
- name: PMA_ARBITRARY
value: "1"
ports:
- name: phpadminport
containerPort: 80
hostPort: 80
protocol: TCP
[root@k8s-master pod]# kubectl apply -f mysql.yml
pod/mysql created
# Pod 已经分配到 k8s‑node2 节点
[root@k8s-master pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql 0/2 ContainerCreating 0 11s <none> k8s-node2 <none> <none>

5.共享宿主机网络
bash
[root@k8s-master pod]# vim testpod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true # 开启主机网络模式,Pod 直接使用宿主机节点的网络命名空间
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
[root@k8s-master pod]# kubectl apply -f testpod.yaml
[root@k8s-master pod]# kubectl exec -it pods/testpod -c busybox -- /bin/sh

6.资源优先级
优先级: Guaranteed > Burstable > BestEffort
BestEffort没有做任何资源限制,资源使用优先级最低
bash
[root@k8s-master pod]# vim testpod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
# 这个 Pod 没有设置resources.requests和resources.limits,所以QoS等级为BestEffort(尽力而为)。
[root@k8s-master pod]# kubectl describe pods testpod | grep "QoS Class:"
QoS Class: BestEffort

Burstable 设定了资源限制,但是期望值和限制值不同,资源使用优先级次之
bash
[root@k8s-master pod]# vim testpod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
resources: # 条件:requests < limits,请求值 ≠ 限制值
limits:
cpu: 700m
memory: 200M
requests:
cpu: 500m
memory: 100M
[root@k8s-master pod]# kubectl apply -f testpod.yaml
pod/testpod unchanged
[root@k8s-master pod]# kubectl describe pods testpod | grep "QoS Class:"
QoS Class: Burstable

Guaranteed期望值和最大使用限制相同,优先级最高
bash
[root@k8s-master pod]# vim testpod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 10000
resources: # 条件:CPU、内存的 requests 和 limits 全部相等
limits:
cpu: 500m
memory: 100M
requests:
cpu: 500m
memory: 100M
[root@k8s-master pod]# kubectl apply -f testpod.yaml
pod/testpod created
[root@k8s-master pod]# kubectl describe pods testpod | grep "QoS Class:"
QoS Class: Guaranteed

7.容器重启规则
Always 无论什么原因都会从新运行pod
无论容器退出是什么状态(正常退出 / 异常崩溃),K8s 都会重启容器 。 这是Deployment 默认策略。
bash
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
restartPolicy: Always # 重启策略:Always
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 60
[root@k8s-master pod]# kubectl get pods -o wide -w

OnFailure 非正常关闭会重启pod
OnFailure:失败才重启,正常完成不重启。
bash
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
restartPolicy: OnFailure
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 30
[root@k8s-master pod]# kubectl get pods -o wide -w
#等30秒后让容器中的命令运行完成后再次观察

Never pod关闭后不重启
无论容器是正常退出,还是异常崩溃被杀死,都不会重启容器。
bash
apiVersion: v1
kind: Pod
metadata:
labels:
run: testpod
name: testpod
spec:
hostNetwork: true
restartPolicy: Never # 永不重启
containers:
- image: busybox:latest
name: busybox
command:
- /bin/sh
- -c
- sleep 30
[root@k8s-master pod]# kubectl get pods -o wide -w
容器正常跑完 sleep 30s:

五.pod的生命周期
1.init容器
- initContainers 会在业务容器之前全部执行完成 ,全部成功退出后,才启动主业务容器。
- 如果任意一个 init 容器失败,Pod 会不断重启 init 容器,直到全部成功。
- init 容器不会持久运行,执行完成就退出;和普通容器不一样。
bash
[root@k8s-master pod]# kubectl run webserver --image myapp:v1 --dry-run=client -o yaml > init-example.yml
[root@k8s-master pod]# vim init-example.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: webserver
name: webserver
spec:
initContainers:
- name: busybox
image: busybox:latest
command:
- /bin/sh
- -c
# 持续检测是否存在文件/testfile;文件不存在就一直循环等待。
- "until test -e /testfile;do echo wating for myservice; sleep 2;done"
containers:
- image: myapp:v1
name: webserver
restartPolicy: Always
# 主容器webserver不会启动,Pod 状态为Init:0/1;
[root@k8s-master pod]# kubectl apply -f init-example.yml
pod/webserver created
[root@k8s-master pod]# kubectl exec -it pods/webserver -c busybox -- /bin/sh
# 当手动创建/testfile,init循环结束,init容器正常退出;此时才启动主业务容器myapp:v1,Pod变成Running。
/ # touch /testfile


2.Livness存活探针
作用:检测容器是否还活着;探测失败,kubelet 会重启容器。
bash
[root@k8s-master pod]# kubectl delete -f livness-example.yaml --force
kind: Pod
metadata:
labels:
run: webserver
name: webserver
spec:
containers:
- image: myapp:v1
name: testpod
command: ["/bin/sh", "-c"]
args:
- |
nginx -g "daemon off;"
sleep 10000
livenessProbe:
tcpSocket:
port: 80 # TCP 探测,尝试连接容器 80 端口,能连通代表健康
initialDelaySeconds: 3 # Pod 启动后,等待 3 秒才开始第一次探测。
periodSeconds: 1 # 每隔 1 秒执行一次探测。
timeoutSeconds: 1 # 探测超时时间 1 秒。
restartPolicy: Always
[root@k8s-master pod]# kubectl exec -it pods/webserver -c webserver -- /bin/sh
/ # nginx -s stop
2026/08/23 03:59:45 [notice] 15#15: signal process started
/ # exit
[root@k8s-master pod]# curl 10.244.5.66
curl: (7) Failed to connect to 10.244.5.66 port 80: 拒绝连接
[root@k8s-master pod]# curl 10.244.5.66
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

3.之后进行访问测试:

3.readness就绪探针
作用:判断 Pod 是否准备好接收业务流量;探测失败, 不会重启 Pod**,只会把 Pod 从 Service 的 Endpoints 后端列表中摘除,不再转发流量。**
bash
[root@k8s-master pod]# vim readness-example.yml
apiVersion: v1
kind: Pod
metadata:
labels:
run: webserver
name: webserver
spec:
containers:
- image: myapp:v1
name: webserver
readinessProbe:
httpGet: # 发送 HTTP GET请求访问/index.html,返回2xx状态码代表就绪。
path: /index.html
port: 80
initialDelaySeconds: 3 # Pod 启动后等待 3 秒才开始第一次就绪探测。
periodSeconds: 2 # 每 2 秒执行一次探测。
timeoutSeconds: 1 # 探测超时 1 秒。
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
labels:
run: webserver
name: webserver
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: webserver
# Pod 启动,index.html存在,就绪探针成功。
[root@k8s-master pod]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
webserver 1/1 Running 0 7s 10.244.5.69 k8s-node2 <none> <none>
[root@k8s-master pod]# kubectl describe svc webserver
Name: webserver
Namespace: default
Labels: run=webserver
Annotations: <none>
Selector: run=webserver
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.110.248.237
IPs: 10.110.248.237
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.5.69:80
Session Affinity: None
Internal Traffic Policy: Cluster
Events: <none>
# 进入容器执行:
/usr/share/nginx/html # command terminated with exit code 137
[root@k8s-master ~]#
# 删除index.html文件后http 探针访问/index.html返回404,就绪探测失败。
# service 的Endpoints列表中移除这个Pod的IP,Service不再把流量转发过来。
/ # rm -fr /usr/share/nginx/html/index.html
# 恢复文件后就绪探针恢复成功,Pod 重新加入 Service 的 Endpoints,流量恢复转发。
/ # echo timinglee > /usr/share/nginx/html/index.html
/ #



4.StartupProbe探针
用于慢启动应用,针对启动很慢的业务(比如数据库、大应用,需要几十秒甚至几分钟初始化);
在 startupProbe 没有探测成功之前,liveness、readiness 探针全部暂停执行。 一旦 startupProbe 成功,后续就交给 liveness/readiness 正常工作。
- Pod 启动后,先跑
startupProbe - 如果startupProbe 一直失败,超时之后,kubelet 会重启容器
- 只要 startupProbe 探测成功一次,就不再执行它,之后由存活、就绪探针接管。