ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Kubernetes中pod的管理及优化

2026/9/3 15:46:58 拓冰建站 浏览量
Kubernetes中pod的管理及优化 kubernetes 中的资源1.1 资源管理介绍资源作用简写Pod最小调度单元一个或多个容器短暂实体pod/poDeployment无状态应用管理 ReplicaSet滚动更新、扩缩容deployReplicaSet维持 Pod 副本数一般不直接使用rsDaemonSet每个节点运行一个 Pod代理类flannel、metallb‑speakerdsStatefulSet有状态应用稳定网络标识、稳定存储MySQL 集群stsJob一次性任务执行完成就结束jobCronJob定时 Job类似 crontabcj1.2 资源管理方式命令式对象管理直接使用命令去操作kubernetes资源kubectl run nginx-pod --imagenginx:latest --port80命令式对象配置通过命令配置和配置文件去操作kubernetes资源kubectl create/patch -f nginx-pod.yaml声明式对象配置通过apply命令和配置文件去操作kubernetes资源kubectl create/patch -f nginx-pod.yaml类型适用环境优点缺点命令式对象管理测试简单只能操作活动对象无法审计、跟踪命令式对象配置开发可以审计、跟踪项目大时配置文件多操作麻烦声明式对象配置开发支持目录操作意外情况下难以调试1.2.1 命令式对象管理kubectl是kubernetes集群的命令行工具通过它能够对集群本身进行管理并能够在集群上进行容器化应用的安装部署kubectl命令的语法如下kubectl [command] [type] [name] [flags]comand**指定要对资源执行的操作例如create、get、deletetype指定资源类型比如deployment、pod、servicename指定资源的名称名称大小写敏感flags指定额外的可选参数命名空间管理rootk8s-master ~]# kubectl get namespaces #查看命名空间 NAME STATUS AGE default Active 4h20m kube-flannel Active 3h31m #创建命名空间 [rootk8s-master ~]# kubectl get namespaces timinglee NAME STATUS AGE default Active 4h22m kube-flannel Active 3h32m timinglee Active 5s #删除命名空间 [rootk8s-master ~]# kubectl delete namespaces timinglee namespace timinglee deletedpod管理#查看pod的运行情况和在哪里运行 [rootk8s-master ~]# kubectl get pods -o wide No resources found in default namespace. #创建pod [rootk8s-master ~]# kubectl run lee --image nginx:latest pod/lee created [rootk8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES lee 1/1 Running 0 25s 10.244.1.10 k8s-node1 none none #当pod创建出现问题 [rootk8s-master ~]# kubectl run error --image lee:v1 [rootk8s-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 none lee 1/1 Running 0 91s 10.244.1.10 k8s-node1 none none #查看pod运行的详细信息 [rootk8s-master ~]# kubectl describe pods error Name: error Namespace: default .......... #删除 [rootk8s-master ~]# kubectl delete pods error [rootk8s-master ~]# kubectl delete pods --all pod error deleted from default namespace pod lee deleted from default namespace1.2.2 资源类型kubernetes中所有的内容都抽象为资源#列出集群中全部 API 资源包含资源名称、简写、API 版本、是否命名空间级别、Kind 类型。 kubectl api-resources常用资源类型kubect 常见命令操作kubectl 命令实操生成实验所需yml文件[rootk8s-master ~]# vim replica.yml apiVersion: apps/v1 kind: ReplicaSet metadata: labels: app: replica #设定控制器标签 name: replica spec: replicas: 2 #启动pod数量 selector: matchLabels: app: replica #控制器标签选择器 template: metadata: labels: app: replica #开启pod的属性模板 spec: containers: - image: myapp:v1 name: myappcreate[rootk8s-master ~]# kubectl create deployment webcluster --replicas 2 --image myapp:v1 deployment.apps/webcluster created [rootk8s-master ~]# kubectl get deployments.apps NAME READY UP-TO-DATE AVAILABLE AGE webcluster 2/2 2 2 15s [rootk8s-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 [rootk8s-master ~]# kubectl delete deployments.apps webcluster deployment.apps webcluster deleted from default namespaceedit[rootk8s-master ~]# kubectl create deployment webcluster --image myapp:v1 [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE webcluster-77c87d9946-2cgr7 1/1 Running 0 36s ​ [rootk8s-master ~]# kubectl edit deployments.apps webcluster replicas: 2 ​ [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE webcluster-77c87d9946-2cgr7 1/1 Running 0 36s webcluster-77c87d9946-2wqn7 1/1 Running 0 103spatch[rootk8s-master ~]# kubectl patch deployments.apps webcluster -p {spec:{replicas:1}} deployment.apps/webcluster patched [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE webcluster-77c87d9946-2wqn7 1/1 Running 0 6m20sexpose[rootk8s-master ~]# kubectl expose deployment webcluster --port 80 --target-port 80 service/webcluster exposed [rootk8s-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 [rootk8s-master ~]# kubectl describe svc webcluster Name: webcluster Namespace: default Labels: appwebcluster [rootk8s-master ~]# curl 10.97.61.108/hostname.html webcluster-77c87d9946-gh9v7 [rootk8s-master ~]# curl 10.97.61.108/hostname.html webcluster-77c87d9946-gh9v7 [rootk8s-master ~]# curl 10.97.61.108/hostname.html webcluster-77c87d9946-m69wl [rootk8s-master ~]# curl 10.97.61.108/hostname.html webcluster-77c87d9946-4p4mzlogs[rootk8s-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[rootk8s-master ~]# docker load -i busybox-latest.tar.gz Loaded image: busybox:latest [rootk8s-master ~]# docker tag busybox:latest reg.timinglee.org/library/busybox:latest [rootk8s-master ~]# kubectl run -it testpod --image busybox:latest 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 dont see a command prompt, try pressing enter. / # / # / # ctrlpq [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE testpod 1/1 Running 0 55s [rootk8s-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 dont see a command prompt, try pressing enter. / # / # / # [rootk8s-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 dont see a command prompt, try pressing enter. / #exec[rootk8s-master ~]# kubectl run testpod --image nginx:latest pod/testpod created [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE testpod 1/1 Running 0 4s rootk8s-master ~]# kubectl exec -it pods/testpod -c testpod -- /bin/bash roottestpod:/#cp[rootk8s-master ~]# kubectl cp testpod:/usr/share/nginx/html/index.html /mnt/test tar: Removing leading / from member names [rootk8s-master ~]# kubectl cp testpod:/usr/share/nginx/html /mnt/ tar: Removing leading / from member names [rootk8s-master ~]# ls /mnt/ 50x.html docker.service file1 hgfs index.html test [rootk8s-master ~]# echo timinglee /mnt/index.html [rootk8s-master ~]# kubectl cp /mnt/index.html testpod:/usr/share/nginx/html/index.html [rootk8s-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 [rootk8s-master ~]# curl 10.244.1.12 timingleerollout[rootk8s-master pod]# kubectl create deployment webcluster --image myapp:v1 --replicas 2 --dry-runclient -o yaml webcluster.yml [rootk8s-master pod]# 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 [rootk8s-master pod]# kubectl apply -f webcluster.yml deployment.apps/webcluster created [rootk8s-master pod]# kubectl get deployments.apps NAME READY UP-TO-DATE AVAILABLE AGE webcluster 2/2 2 2 7s [rootk8s-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 [rootk8s-master pod]# kubectl rollout status deployment webcluster deployment webcluster successfully rolled out deployment.apps/webcluster resumed [rootk8s-master pod]# kubectl rollout restart deployment webcluster deployment.apps/webcluster restarted [rootk8s-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 [rootk8s-master pod]# kubectl rollout restart deployment webcluster deployment.apps/webcluster restarted [rootk8s-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 0sscale[rootk8s-master pod]# kubectl scale deployment webcluster --replicas 4 deployment.apps/webcluster scaled [rootk8s-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 [rootk8s-master pod]# kubectl scale deployment webcluster --replicas 1 deployment.apps/webcluster scaled [rootk8s-master pod]# kubectl get pods NAME READY STATUS RESTARTS AGE webcluster-9787d97f6-zl6q2 1/1 Running 0 93slabel[rootk8s-master pod]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS webcluster-9787d97f6-zl6q2 1/1 Running 0 6m57s appwebcluster,pod-template-hash9787d97f6 [rootk8s-master pod]# kubectl get deployments.apps webcluster --show-labels NAME READY UP-TO-DATE AVAILABLE AGE LABELS webcluster 1/1 1 1 12m appwebcluster [rootk8s-master pod]# kubectl label pods webcluster-9787d97f6-zl6q2 app- pod/webcluster-9787d97f6-zl6q2 unlabeled [rootk8s-master pod]# kubectl label pods webcluster-9787d97f6-zl6q2 appwebcluster pod/webcluster-9787d97f6-zl6q2 labeled什么是podPod是可以创建和管理Kubernetes计算的最小可部署单元一个Pod代表着集群中运行的一个进程每个pod都有一个唯一的ip。一个pod类似一个豌豆荚包含一个或多个容器通常是docker多个容器间共享IPC、Network和UTC namespace。2.1 创建自主式pod 生产不推荐自主 Podyaml 中kind: Pod直接创建没有控制器管理没有 ownerReference优点简单直接yaml 短小快速测试调试临时实验环境用。启动速度快没有 ReplicaSet 中间层。可以精细控制 Pod 全部字段不需要被控制器模板约束。适合一次性调试 pod缺点节点故障、Pod 崩溃退出不会自动重建Pod 异常退出、节点宕机Pod 直接消失不会拉起新实例。Deployment 管理的 Pod 挂了控制器立刻新建 Pod。不支持扩缩容不能kubectl scale想要多副本只能复制多份 Pod yaml 分别创建。没有滚动更新、回滚能力修改镜像只能删除 Pod 再重新 apply没有 rollout 历史无法回滚。没有自愈能力节点资源不足被驱逐Pod 直接销毁不会重建。不支持版本记录没有 kubectl rollout history / rollout undo。删除自主 Pod 就是彻底删除控制器管理 Pod删 Pod 只是重建新 Pod。#查看所有pods [rootk8s-master ~]# kubectl get pods No resources found in default namespace. #建立一个名为timinglee的pod [rootk8s-master ~]# kubectl run timinglee --image nginx pod/timinglee created [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE timinglee 1/1 Running 0 6s #显示pod的较为详细的信息 [rootk8s-master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES timinglee 1/1 Running 0 11s 10.244.1.17 k8s-node1.timinglee.org none none2.2 利用控制器管理pod推荐高可用性和可靠性自动故障恢复如果一个 Pod 失败或被删除控制器会自动创建新的 Pod 来维持期望的副本数量。确保应用始终处于可用状态减少因单个 Pod 故障导致的服务中断。健康检查和自愈可以配置控制器对 Pod 进行健康检查如存活探针和就绪探针。如果 Pod 不健康控制器会采取适当的行动如重启 Pod 或删除并重新创建它以保证应用的正常运行。可扩展性轻松扩缩容可以通过简单的命令或配置更改来增加或减少 Pod 的数量以满足不同的工作负载需求。例如在高流量期间可以快速扩展以处理更多请求在低流量期间可以缩容以节省资源。水平自动扩缩容HPA可以基于自定义指标如 CPU 利用率、内存使用情况或应用特定的指标自动调整 Pod 的数量实现动态的资源分配和成本优化。版本管理和更新滚动更新对于 Deployment 等控制器可以执行滚动更新来逐步替换旧版本的 Pod 为新版本确保应用在更新过程中始终保持可用。可以控制更新的速率和策略以减少对用户的影响。回滚如果更新出现问题可以轻松回滚到上一个稳定版本保证应用的稳定性和可靠性。声明式配置简洁的配置方式使用 YAML 或 JSON 格式的声明式配置文件来定义应用的部署需求。这种方式使得配置易于理解、维护和版本控制同时也方便团队协作。期望状态管理只需要定义应用的期望状态如副本数量、容器镜像等控制器会自动调整实际状态与期望状态保持一致。无需手动管理每个 Pod 的创建和删除提高了管理效率。服务发现和负载均衡自动注册和发现Kubernetes 中的服务Service可以自动发现由控制器管理的 Pod并将流量路由到它们。这使得应用的服务发现和负载均衡变得简单和可靠无需手动配置负载均衡器。流量分发可以根据不同的策略如轮询、随机等将请求分发到不同的 Pod提高应用的性能和可用性。多环境一致性一致的部署方式在不同的环境如开发、测试、生产中可以使用相同的控制器和配置来部署应用确保应用在不同环境中的行为一致。这有助于减少部署差异和错误提高开发和运维效率。#建立控制器并自动运行pod [rootk8s-master ~]# kubectl create deployment timinglee --image nginx [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE timinglee-859fbf84d6-mrjvx 1/1 Running 0 37m ​ #为timinglee扩容 [rootk8s-master ~]# kubectl scale deployment timinglee --replicas 6 deployment.apps/timinglee scaled [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE timinglee-859fbf84d6-8rgkz 0/1 ContainerCreating 0 1s timinglee-859fbf84d6-ddndl 0/1 ContainerCreating 0 1s timinglee-859fbf84d6-m4r9l 0/1 ContainerCreating 0 1s timinglee-859fbf84d6-mrjvx 1/1 Running 0 37m timinglee-859fbf84d6-tsn97 1/1 Running 0 20s timinglee-859fbf84d6-xgskk 0/1 ContainerCreating 0 1s ​ #为timinglee缩容 rootk8s-master ~]# kubectl scale deployment timinglee --replicas 2 deployment.apps/timinglee scaled [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE timinglee-859fbf84d6-mrjvx 1/1 Running 0 38m timinglee-859fbf84d6-tsn97 1/1 Running 0 73s利用控制器实现版本更替在做以下实验时harbor仓库中的library项目中必须有myapp:v1和myapp:v2两个镜像#利用控制器建立pod [rootk8s-master ~]# kubectl create deployment timinglee --image myapp:v1 --replicas 2 deployment.apps/timinglee created #暴漏端口 [rootk8s-master ~]# kubectl expose deployment timinglee --port 80 --target-port 80 service/timinglee exposed [rootk8s-master ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 none 443/TCP 2d17h timinglee ClusterIP 10.110.195.120 none 80/TCP 8s #访问服务 [rootk8s-master ~]# curl 10.110.195.120 Hello MyApp | Version: v1 | a hrefhostname.htmlPod Name/a [rootk8s-master ~]# curl 10.110.195.120 Hello MyApp | Version: v1 | a hrefhostname.htmlPod Name/a [rootk8s-master ~]# curl 10.110.195.120 #产看历史版本 [rootk8s-master ~]# kubectl rollout history deployment timinglee deployment.apps/timinglee REVISION CHANGE-CAUSE 1 none #更新控制器镜像版本 [rootk8s-master ~]# kubectl set image deployments/timinglee myappmyapp:v2 deployment.apps/timinglee image updated #查看历史版本 [rootk8s-master ~]# kubectl rollout history deployment timinglee deployment.apps/timinglee REVISION CHANGE-CAUSE 1 none 2 none #访问内容测试 [rootk8s-master ~]# curl 10.110.195.120 Hello MyApp | Version: v2 | a hrefhostname.htmlPod Name/a [rootk8s-master ~]# curl 10.110.195.120 #版本回滚 [rootk8s-master ~]# kubectl rollout undo deployment timinglee --to-revision 1 deployment.apps/timinglee rolled back [rootk8s-master ~]# curl 10.110.195.120 Hello MyApp | Version: v1 | a hrefhostname.htmlPod Name/a2.3 利用yaml文件部署应用用yaml文件部署应用有以下优点声明式配置清晰表达期望状态以声明式的方式描述应用的部署需求包括副本数量、容器配置、网络设置等。这使得配置易于理解和维护并且可以方便地查看应用的预期状态。可重复性和版本控制配置文件可以被版本控制确保在不同环境中的部署一致性。可以轻松回滚到以前的版本或在不同环境中重复使用相同的配置。团队协作便于团队成员之间共享和协作大家可以对配置文件进行审查和修改提高部署的可靠性和稳定性灵活性和可扩展性丰富的配置选项可以通过 YAML 文件详细地配置各种 Kubernetes 资源如 Deployment、Service、ConfigMap、Secret 等。可以根据应用的特定需求进行高度定制化。组合和扩展可以将多个资源的配置组合在一个或多个 YAML 文件中实现复杂的应用部署架构。同时可以轻松地添加新的资源或修改现有资源以满足不断变化的需求。与工具集成与 CI/CD 流程集成可以将 YAML 配置文件与持续集成和持续部署CI/CD工具集成实现自动化的应用部署。例如可以在代码提交后自动触发部署流程使用配置文件来部署应用到不同的环境。命令行工具支持Kubernetes 的命令行工具 kubectl 对 YAML 配置文件有很好的支持可以方便地应用、更新和删除配置。同时还可以使用其他工具来验证和分析 YAML 配置文件确保其正确性和安全性。资源清单参数如何获得资源帮助kubectl explain pod.spec.containers在pod中运行多容器[rootk8s-master ~]# kubectl run testpod --image myapp:v1 --dry-runclient -o yaml testpod.yaml [rootk8s-master ~]# vim testpod.yaml apiVersion: v1 kind: Pod metadata: labels: run: testpod name: testpod spec: containers: - image: myapp:v1 name: myapp1 - image: busyboxplus:latest name: busybox command: - /bin/sh - -c - sleep 10000 [rootk8s-master ~]# kubectl apply -f testpod.yaml [rootk8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE testpod 2/2 Running 0 2m41s [rootk8s-master ~]# kubectl exec -it pods/testpod -c busybox -- /bin/sh [rootk8s-master ~]# kubectl exec -it pods/testpod -c busybox -- /bin/sh / # curl 127.0.0.1 Hello MyApp | Version: v1 | a hrefhostname.htmlPod Name/a在pod运行主机中暴漏端口[rootk8s-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 #端口所用协议 [rootk8s-master pod]# kubectl apply -f testpod.yaml rootk8s-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 [rootk8s-master pod]# curl k8s-node2 Hello MyApp | Version: v1 | a hrefhostname.htmlPod Name/a在pod中指定变量[rootk8s-master pod]# vim mysql.yml apiVersion: v1 kind: Pod metadata: labels: run: mysql name: mysql spec: 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 [rootk8s-master pod]# kubectl apply -f mysql.yml [rootk8s-master pod]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mysql 2/2 Running 0 36s 10.244.1.44 k8s-node1 none none #在浏览器中访问 node下面看到的主机ip选择运行节点rootk8s-master ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS k8s-master Ready control-plane 29h v1.35.7 beta.kubernetes.io/archamd64,beta.kubernetes.io/oslinux,kubernetes.io/archamd64,kubernetes.io/hostnamek8s-master,kubernetes.io/oslinux,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/archamd64,beta.kubernetes.io/oslinux,kubernetes.io/archamd64,kubernetes.io/hostnamek8s-node1,kubernetes.io/oslinux k8s-node2 Ready none 24h v1.35.7 beta.kubernetes.io/archamd64,beta.kubernetes.io/oslinux,kubernetes.io/archamd64,kubernetes.io/hostnamek8s-node2,kubernetes.io/oslinux [rootk8s-master pod]# vim mysql.yml apiVersion: v1 kind: Pod metadata: labels: run: mysql name: mysql spec: nodeSelector: 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共享宿主机网络[rootk8s-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 [rootk8s-master pod]# kubectl apply -f testpod.yaml [rootk8s-master pod]# kubectl exec -it pods/testpod -c busybox -- /bin/sh / # ifconfig资源优先级#BestEffort没有做任何资源限制资源使用优先级最低 [rootk8s-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 [rootk8s-master pod]# kubectl describe pods testpod | grep QoS Class: QoS Class: BestEffort #Burstable 设定了资源限制但是期望值和限制值不同资源使用优先级次之 [rootk8s-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: limits: cpu: 700m memory: 200M requests: cpu: 500m memory: 100M [rootk8s-master pod]# kubectl apply -f testpod.yaml pod/testpod unchanged [rootk8s-master pod]# kubectl describe pods testpod | grep QoS Class: QoS Class: Burstable #Guaranteed期望值和最大使用限制相同优[rootk8s-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: limits: cpu: 500m memory: 100M requests: cpu: 500m memory: 100M [rootk8s-master pod]# kubectl apply -f testpod.yaml pod/testpod created [rootk8s-master pod]# kubectl describe pods testpod | grep QoS Class: QoS Class: Guaranteed先级最高容器重启规则#Always 无论什么原因都会从新运行pod [rootk8s-master pod]# vim testpod.yaml apiVersion: v1 kind: Pod metadata: labels: run: testpod name: testpod spec: hostNetwork: true restartPolicy: Always containers: - image: busybox:latest name: busybox command: - /bin/sh - -c - sleep 60 [rootk8s-master pod]# kubectl get pods -o wide -w [rootk8s-node2 ~]# docker rm -f 3051d9de4c36 #OnFailure 非正常管关闭会从其pod 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 [rootk8s-master pod]# kubectl get pods -o wide -w [rootk8s-node2 ~]# docker rm -f 3051d9de4c36 #等30秒后让容器中的命令运行完成后再次观察 #Never pod关闭后不重启 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 [rootk8s-master pod]# kubectl get pods -o wide -w [rootk8s-node2 ~]# docker rm -f 3051d9de4c362.4.pod的生命周期init容器[rootk8s-master pod]# kubectl run webserver --image myapp:v1 --dry-runclient -o yaml init-example.yml [rootk8s-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 - until test -e /testfile;do echo wating for myservice; sleep 2;done containers: - image: myapp:v1 name: webserver restartPolicy: Always [rootk8s-master pod]# watch -n 1 kubectl get pods -o wide [rootk8s-master pod]# kubectl apply -f init-example.yml pod/webserver created [rootk8s-master pod]# kubectl exec -it pods/webserver -c busybox -- /bin/sh / # / # touch /testfileLivness存活探针[rootk8s-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 initialDelaySeconds: 3 periodSeconds: 1 timeoutSeconds: 1 restartPolicy: Always [rootk8s-master pod]# kubectl get pods -o wide -w [rootk8s-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 [rootk8s-master pod]# curl 10.244.5.66 curl: (7) Failed to connect to 10.244.5.66 port 80: 拒绝连接 [rootk8s-master pod]# curl 10.244.5.66 Hello MyApp | Version: v1 | a hrefhostname.htmlPod Name/areadness 就绪探针#[rootk8s-master pod]# vim readness-example.yml apiVersion: v1 kind: Pod metadata: labels: run: webserver name: webserver spec: containers: - image: myapp:v1 name: webserver restartPolicy: Always --- apiVersion: v1 kind: Service metadata: labels: run: webserver name: webserver spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: webserver [rootk8s-master pod]# kubectl apply -f readness-example.yml pod/webserver unchanged service/webserver created [rootk8s-master pod]# kubectl describe svc webserver Name: webserver Namespace: default Labels: runwebserver Annotations: none Selector: runwebserver Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.102.162.217 IPs: 10.102.162.217 Port: unset 80/TCP TargetPort: 80/TCP Endpoints: 10.244.5.67:80 Session Affinity: None Internal Traffic Policy: Cluster Events: none #删除默认发布文件 [rootk8s-master ~]# kubectl exec -it pods/webserver -c webserver -- /bin/sh / # cd /usr/share/nginx/ /usr/share/nginx # ls html /usr/share/nginx # cd html/ /usr/share/nginx/html # ls 50x.html index.html /usr/share/nginx/html # rm -fr index.html /usr/share/nginx/html # ls 50x.html /usr/share/nginx/html # #验证是否在访问service时endpoints中被下架 [rootk8s-master pod]# kubectl describe svc webserver Name: webserver Namespace: default Labels: runwebserver Annotations: none Selector: runwebserver Type: ClusterIP IP Family Policy: SingleStack IP Families: IPv4 IP: 10.102.162.217 IPs: 10.102.162.217 Port: unset 80/TCP TargetPort: 80/TCP Endpoints: 10.244.5.67:80 #还在 Session Affinity: None Internal Traffic Policy: Cluster Events: none #业务问题 [rootk8s-master pod]# curl 10.102.162.217 html headtitle403 Forbidden/title/head body bgcolorwhite centerh1403 Forbidden/h1/center hrcenternginx/1.12.2/center /body /html [rootk8s-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: path: /index.html port: 80 initialDelaySeconds: 3 periodSeconds: 2 timeoutSeconds: 1 restartPolicy: Always --- apiVersion: v1 kind: Service metadata: labels: run: webserver name: webserver spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: webserver [rootk8s-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 [rootk8s-master pod]# kubectl describe svc webserver Name: webserver Namespace: default Labels: runwebserver Annotations: none Selector: runwebserver 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 [rootk8s-master ~]# / # rm -fr /usr/share/nginx/html/index.html / # echo timinglee /usr/share/nginx/html/index.html / #