pod管理及优化

 一、k8s中的资源

1、资源介绍

[root@k8s-master ~]# kubectl --namespace timinglee get po
No resources found in timinglee namespace.

[root@k8s-master ~]# kubectl run testpod --image timinglee/nginx

[root@k8s-master ~]# kubectl get pods -w
NAME      READY   STATUS              RESTARTS   AGE
testpod   0/1     ContainerCreating   0          19s

[root@k8s-master ~]# kubectl  delete  pods  testpod --force

2、资源管理方式

2.1 命令式对象管理

2.2  资源类型

k8s中所有的内容都抽象为资源

kubectl api-resources

常用资源类型

kubect常见命令操作

2.3 基本命令示例

[root@k8s-master ~]# kubectl get deployments.apps  lee
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    2/2     2            2           7m10s
[root@k8s-master ~]# kubectl edit  deployments.apps lee
deployment.apps/lee edited

[root@k8s-master ~]# kubectl get deployments.apps  lee
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    4/4     4            4           8m44s

#####打补丁

[root@k8s-master ~]# kubectl  patch  deployments.apps lee -p '{"spec":{"replicas":2}}'
deployment.apps/lee patched
[root@k8s-master ~]# kubectl get deployments.apps  lee
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    2/2     2            2           11m
[root@k8s-master ~]# kubectl delete  deployments.apps  lee
deployment.apps "lee" deleted
[root@k8s-master ~]# kubectl get pods
NAME     READY   STATUS    RESTARTS      AGE
myapp1   1/1     Running   1 (16m ago)   18m

3、运行和调试命令

运行pod

[root@k8s-master ~]# kubectl run testpod --image nginx
pod/testpod created

[root@k8s-master ~]# kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
testpod   1/1     Running   0          5s
[root@k8s-master ~]# kubectl get pods -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP         NODE       NOMINATED NODE   READINESS GATES
testpod   1/1     Running   0          44s   10.244.1.3   k8s-node2.timinglee.org   <none>           <none>

端口暴露

####编写控制器配置

[root@k8s-master ~]# kubectl  edit service testpod
service/testpod edited

###利用补丁修改控制器配置

[root@k8s-master ~]# kubectl patch service testpod -p '{"spec":{"type":"ClusterIP"}}'
service/testpod patched

###删除

[root@k8s-master ~]# kubectl delete service testpod

查看资源详细信息 

查看日志 

 运行交互pod

 [root@reg ~]# docker push  reg.timinglee.org/library/busybox:latest
The push refers to repository [reg.timinglee.org/library/busybox]
d51af96cf93e: Layer already exists
latest: digest: sha256:28e01ab32c9dbcbaae96cf0d5b472f22e231d9e603811857b295e61197e40a9b size: 527

###ctrl+pq退出不停止pod

 运行非交互pod

[root@k8s-master ~]# kubectl run nginx --image nginx
pod/nginx created

##进入已经运行的容器,且容器有交互环境

###在已经运行的pod中运行指定命令

 ###日志文件到pod中

 复制pod文件到本机

[root@k8s-master ~]# kubectl cp nginx:/boot/ anaconda-ks.cfg
tar: Removing leading `/' from member names

4、高级命令示例

[root@k8s-master ~]# vim testpod1.yml 

[root@k8s-master ~]# docker tag timinglee/myapp:v1 reg.timinglee.org/library/myapp:v1
[root@k8s-master ~]# docker tag timinglee/myapp:v2 reg.timinglee.org/library/myapp:v2
[root@k8s-master ~]# docker tag busyboxplus:latest reg.timinglee.org/library/busyboxplus:latest
[root@k8s-master ~]# docker push reg.timinglee.org/library/myapp:v1
 [root@k8s-master ~]# docker push reg.timinglee.org/library/myapp:v2
[root@k8s-master ~]# docker push reg.timinglee.org/library/busyboxplus:latest

[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.
[root@k8s-master ~]# kubectl run testpod1 --image nginx --dry-run=client -o yaml > testpod1.yml
[root@k8s-master ~]# vim testpod1.yml

[root@k8s-master ~]# kubectl create -f testpod1.yml
pod/myapp1 created

[root@k8s-master ~]# vim testpod1.yml

[root@k8s-master ~]# kubectl create -f testpod1.yml
Error from server (AlreadyExists): error when creating "testpod1.yml": pods "myapp1" already exists

################################## 

[root@k8s-master ~]# kubectl get pods
NAME     READY   STATUS    RESTARTS      AGE
myapp1   1/1     Running   1 (16m ago)   18m
[root@k8s-master ~]# kubectl delete  pods myapp1
pod "myapp1" deleted
[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.


 ####生成控制器

[root@k8s-master ~]# kubectl create deployment  lee --image myapp:v1 --dry-run=client -o yaml > deployment.yml
[root@k8s-master ~]# vim deployment.yml

[root@k8s-master ~]# vim deployment.yml

[root@k8s-master ~]# kubectl  apply -f deployment.yml
deployment.apps/lee created
[root@k8s-master ~]# kubectl get deployments.apps  lee
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    2/2     2            2           12s
查看详细信息

删除

[root@k8s-master ~]# kubectl delete -f  deployment.yml
deployment.apps "lee" deleted
[root@k8s-master ~]# kubectl get deployments.apps
No resources found in default namespace.

资源标签

[root@k8s-master ~]# kubectl  apply -f deployment.yml
deployment.apps/lee created
[root@k8s-master ~]# kubectl get pods --show-labels
NAME                   READY   STATUS    RESTARTS   AGE   LABELS
lee-579c757b65-8lvlh   1/1     Running   0          20s   app=lee,pod-template-hash=579c757b65
lee-579c757b65-ptmkt   1/1     Running   0          20s   app=lee,pod-template-hash=579c757b65
[root@k8s-master ~]# watch -n 1 kubectl get pods --show-labels

二、什么是pod

  • Pod是可以创建和管理Kubernetes计算的最小可部署单元
  • 一个Pod代表着集群中运行的一个进程,每个pod都有一个唯一的ip
  • 一个pod类似一个豌豆荚,包含一个或多个容器(通常是docker)
  • 多个容器间共享IPC、Network和UTC namespace

1、创建自主式pod (生产不推荐)

优点:

灵活性高:

  • 可以精确控制 Pod 的各种配置参数,包括容器的镜像、资源限制、环境变量、命令和参数等,满足特定的应用需求。

学习和调试方便:

  •  对于学习 Kubernetes 的原理和机制非常有帮助,通过手动创建 Pod 可以深入了解 Pod 的结构和配置方式。在调试问题时,可以更直接地观察和调整Pod的设置。

适用于特殊场景:

  • 在一些特殊情况下,如进行一次性任务、快速验证概念或在资源受限的环境中进行特定配置时,手动创建 Pod 可能是一种有效的方式

缺点:

管理复杂:

  • 如果需要管理大量的 Pod,手动创建和维护会变得非常繁琐和耗时。难以实现自动化的扩缩容、故障恢复等操作。

缺乏高级功能:

  • 无法自动享受 Kubernetes 提供的高级功能,如自动部署、滚动更新、服务发现等。这可能导致应用的部署和管理效率低下。

可维护性差:

  • 手动创建的 Pod 在更新应用版本或修改配置时需要手动干预,容易出现错误,并且难以保证一致性。相比之下,通过声明式配置或使用 Kubernetes 的部署工具可以更方便地进行应用的维护和更新。 

2、利用控制器管理pod (推荐)

高可用性和可靠性:

  •  自动故障恢复:如果一个Pod 失败或被删除,控制器会自动创建新的 Pod 来维持期望的副本数量。确保应用始终处于可用状态,减少因单个Pod故障导致的服务中断。
  • 健康检查和自愈;可以配置控制器对 Pod 进行健康检查(如存活探针和就绪探针)。如果 Pod 不健康,控制器会采取适当的行动,如重启Pod 或删除并重新创建它,以保证应用的正常运行。

可扩展性:

  • 轻松扩缩容:可以通过简单的命令或配置更改来增加或减少 Pod 的数量,以满足不同的工作负载需求。例如,在高流量期间可以快速扩展以处理更多请求,在低流量期间可以缩容以节省资源。
  • 水平自动扩缩容(HPA):可以基于自定义指标(如CPU利用率、内存使用情况或应用特定的指标)自动调整Pod的数量,实现动态的资源分配和成本优化,

版本管理和更新:

  • 滚动更新:对于 Deployment 等控制器,可以执行滚动更新来逐步替换旧版本的 Pod 为新版本确保应用在更新过程中始终保持可用。可以控制更新的速率和策略,以减少对用户的影响。
  • 回滚:如果更新出现问题,可以轻松回滚到上一个稳定版本,保证应用的稳定性和可靠性

声明式配置:

  • 简洁的配置方式:使用YAML 或JSON格式的声明式配置文件来定义应用的部署需求。这种方式使得配置易于理解、维护和版本控制,同时也方便团队协作。
  • 期望状态管理;只需要定义应用的期望状态(如副本数量、容器镜像等),控制器会自动调整实际状态与期望状态保持一致。无需手动管理每个Pod的创建和删除,提高了管理效率。

服务发现和负载均衡:

  • 自动注册和发现:Kubernetes中的服务(Service)可以自动发现由控制器管理的Pod,并将流量路由到它们。这使得应用的服务发现和负载均衡变得简单和可靠,无需手动配置负载均衡器
  • 流量分发:可以根据不同的策略(如轮询、随机等)将请求分发到不同的Pod,提高应用的性能和可用性。

多环境一致性:

  • 一致的部署方式:在不同的环境(如开发、测试、生产)中,可以使用相同的控制器和配置来部署应用,确保应用在不同环境中的行为一致。这有助于减少部署差异和错误,提高开发和运维效率。

 

实验

[root@k8s-master ~]# vim testpod1.yml
 

[root@k8s-master ~]# kubectl logs pods/myapp1 web2

####web2的80端口被web1占用
[root@k8s-master ~]# kubectl delete -f testpod1.yml
pod "myapp1" deleted
[root@k8s-master ~]# vim testpod1.yml

[root@k8s-master ~]# kubectl delete -f testpod1.yml
pod "myapp1" deleted

自动注册和发现 

[root@k8s-master ~]# vim deployment.yml

[root@k8s-master ~]# kubectl apply -f deployment.yml
deployment.apps/lee created
[root@k8s-master ~]# kubectl get deployments.apps
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    2/2     2            2           16s

扩容

[root@k8s-master ~]# kubectl scale deployment lee --replicas 4
deployment.apps/lee scaled
[root@k8s-master ~]# kubectl get deployments.apps
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
lee    4/4     4            4           11m
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-jpb6g
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-hstck
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-j98ss
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-jpb6g
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-hstck
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-5mp22
[root@k8s-master ~]# curl 10.99.148.75/hostname.html
lee-579c757b65-5mp22

[root@k8s-master ~]# kubectl delete service lee
service "lee" deleted
[root@k8s-master ~]# kubectl delete -f deployment.yml
deployment.apps "lee" deleted

建立pod

 

3、k8s中pod更新及回滚 

[root@k8s-master ~]# curl  10.244.1.19
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

更新版本

[root@k8s-master ~]# kubectl rollout history deployment timinglee
deployment.apps/timinglee
REVISION  CHANGE-CAUSE
1         <none>

[root@k8s-master ~]# curl 10.108.221.114
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>

回滚

[root@k8s-master ~]# kubectl rollout undo deployment timinglee --to-revision 1
deployment.apps/timinglee rolled back
[root@k8s-master ~]# curl 10.108.221.114
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]# curl 10.108.221.114
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

4、利用yaml文件部署应用

4.1用yaml文件部署应用有以下优点

声明式配置:

  • 清晰表达期望状态:以声明式的方式描述应用的部署需求,包括副本数量、容器配置、网络设置等。这使得配置易于理解和维护,并且可以方便地查看应用的预期状态
  • 可重复性和版本控制:配置文件可以被版本控制,确保在不同环境中的部署一致性。可以轻松回滚到以前的版本或在不同环境中重复使用相同的配置,
  • 团队协作:便于团队成员之间共要和协作,大家可以对配置文件进行审查和修改,提高部署的可靠性和稳定性。
  • 灵活性和可扩展性:
  • 丰富的配置选项:可以通过 YAML 文件详细地配置各种 Kubernetes 资源,如 Deployment、Service、ConfigMap、Secret等。可以根据应用的特定需求进行高度定制化。
  • 组合和扩展:可以将多个资源的配置组合在一个或多个 YAML文件中,实现复杂的应用部署架构同时,可以轻松地添加新的资源或修改现有资源以满足不断变化的需求。

与工具集成:

  • 与CI/CD 流程集成:可以将 YAML 配置文件与持续集成和持续部署(CIVCD)工具集成,实现自动化的应用部署。例如,可以在代码提交后自动触发部署流程,使用配置文件来部署应用到不同的环境。
  • 命令行工具支持:Kubernetes 的命令行工具 kubect] 对 YAML 配置文件有很好的支持,可以方便地应用、更新和删除配置。同时,还可以使用其他工具来验证和分析 YAML 配置文件,确保其正确性和安全性。

 4.2 资源清单参数

实验

[root@k8s-master pod]# kubectl run lee --image myapp:v1 --dry-run=client -o yaml > lee.yml
[root@k8s-master pod]# vim lee.yml


[root@k8s-master pod]# kubectl apply -f lee.yml
pod/lee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
lee                         1/1     Running   0          5s
timinglee-c56f584cf-6jxb7   1/1     Running   0          23m
timinglee-c56f584cf-mwrx8   1/1     Running   0          23m
[root@k8s-master pod]# kubectl create namespace leespace
namespace/leespace created
[root@k8s-master pod]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   11h
kube-flannel      Active   11h
kube-node-lease   Active   11h
kube-public       Active   11h
kube-system       Active   11h
leespace          Active   15s

[root@k8s-master pod]# vim lee.yml

[root@k8s-master pod]# kubectl apply -f lee.yml
pod/lee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
lee                         1/1     Running   0          12m
timinglee-c56f584cf-6jxb7   1/1     Running   0          35m
timinglee-c56f584cf-mwrx8   1/1     Running   0          35m
[root@k8s-master pod]#  kubectl delete pods lee
pod "lee" deleted
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee-c56f584cf-6jxb7   1/1     Running   0          37m
timinglee-c56f584cf-mwrx8   1/1     Running   0          37m
[root@k8s-master pod]# kubectl -n leespace get pods
NAME   READY   STATUS    RESTARTS   AGE
lee    1/1     Running   0          2m2s
[root@k8s-master pod]# kubectl delete -f lee.yml
pod "lee" deleted

4.3 获取资源帮助

4.4 编写示例

k8s中pod的yml写法及资源详解

运行一个简单的单个容器pod

[root@k8s-master yaml]# kubectl run timinglee --image myapp:v1 --dry-run=client -o yaml > pod.yml
[root@k8s-master yaml]# vim pod.yml

运行多个容器pod-------共享一个网络站

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods timinglee
NAME        READY   STATUS    RESTARTS   AGE
timinglee   2/2     Running   0          36s
[root@k8s-master yaml]# kubectl describe pods timinglee

[root@k8s-master yaml]# kubectl delete -f pod.yml --force
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl  apply  -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods timinglee
NAME        READY   STATUS   RESTARTS     AGE
timinglee   2/3     Error    1 (4s ago)   10s
[root@k8s-master yaml]# kubectl describe pods timinglee

[root@k8s-master yaml]# kubectl delete  -f pod.yml
pod "timinglee" deleted

理解pod间的网络整合

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl  apply  -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl exec pods/timinglee -c busyboxplus -it -- /bin/sh
/ # curl localhost
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

[root@k8s-master yaml]# kubectl delete  -f pod.yml --force

端口映射

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl  apply  -f pod.yml
pod/timinglee created

若端口没有暴露,则不能访问

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted

如何设定环境变量

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   1/1     Running   0          62s
timinglee-c56f584cf-6jxb7   1/1     Running   0          11h
timinglee-c56f584cf-mwrx8   1/1     Running   0          11h
[root@k8s-master yaml]# kubectl logs pods/timinglee busybox
timinglee

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted

资源限制

[root@k8s-master yaml]# vim pod.yml
 

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods  timinglee
NAME        READY   STATUS    RESTARTS   AGE
timinglee   1/1     Running   0          22s

[root@k8s-master yaml]# kubectl describe pods timinglee

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted

资源敏感型

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl describe pods timinglee

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted

容器启动管理

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl describe pods timinglee

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created

####异常

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted
[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   0/1     Error     0          2s
timinglee-c56f584cf-6jxb7   1/1     Running   0          12h
timinglee-c56f584cf-mwrx8   1/1     Running   0          12h
[root@k8s-master yaml]# kubectl get pods
NAME                        READY   STATUS             RESTARTS      AGE
timinglee                   0/1     CrashLoopBackOff   2 (12s ago)   30s
timinglee-c56f584cf-6jxb7   1/1     Running            0             12h
timinglee-c56f584cf-mwrx8   1/1     Running            0             12h
[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted

选择运行节点

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted

共享宿主机网络 

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl delete -f pod.yml
pod "timinglee" deleted

宿主机网络

[root@k8s-master yaml]# vim pod.yml

[root@k8s-master yaml]# kubectl apply -f pod.yml
pod/timinglee created
[root@k8s-master yaml]# kubectl exec pods/timinglee -c busybox -it -- /bin/sh
/ #
/ # ifconfig

三、pod的生命周期

1、INIT容器的功能

  • Init容器可以包含一些安装过程中应用容器中不存在的实用工具或个性化代码。
  • Init容器可以安全地运行这些工具,避免这些工具导致应用镜像的安全性降低。
  • 应用镜像的创建者和部署者可以各自独立工作,而没有必要联合构建一个单独的应用镜像。
  • Init 容器能以不同于Pod内应用容器的文件系统视图运行。因此,Init容器可具有访问 Secrets 的权限,而应用容器不能够访问。
  • 由于 Init 容器必须在应用容器启动之前运行完成,因此 Init 容器提供了一种机制来阻塞或延迟应用容器的启动,直到满足了一组先决条件。一旦前置条件满足,Pod内的所有的应用容器会并行启动。

2、INIT容器示例

[root@k8s-master pod]# kubectl run timinglee --image myapp:v1 --dry-run=client -o yaml > timinglee.yml
[root@k8s-master pod]# vim timinglee.yml

[root@k8s-master pod]# kubectl apply -f timinglee.yml
pod/timinglee created
[root@k8s-master pod]# kubectl  get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   1/1     Running   0          43s
timinglee-c56f584cf-6jxb7   1/1     Running   0          12h
timinglee-c56f584cf-mwrx8   1/1     Running   0          12h

[root@k8s-master pod]# kubectl delete -f timinglee.yml --force
[root@k8s-master pod]# vim timinglee.yml

[root@k8s-master pod]# kubectl delete -f timinglee.yml
pod "timinglee" deleted

3、 探针

探针是由kubelet对容器执行的定期诊断

  • ExecAction:在容器内执行指定命令。如果命令退出时返回码为0则认为诊断成功
  • TCPSocketAction:对指定端口上的容器的 IP 地址进行 TCP 检查。如果端口打开,则诊断被认为是成功的。
  • HTTPGetAction:对指定的端口和路径上的容器的 IP 地址执行 HTTP Get 请求。如果响应的状态码大于等于200 且小于 400,则诊断被认为是成功的。

每次探测都将获得以下三种结果之一

  • 成功:容器通过了诊断,
  • 失败:容器未通过诊断。
  • 未知:一断失败, 因此不会采取任何行动

Kubelet 可以选择是否执行在容器上运行的三种探针执行和做出反应

  • livenessProbe:指示容器是否正在运行,如果存活探测失败,则 kubelet 会杀死容器,并且容器将受到其 重启策略 的影响。如果容器不提供存活探针,则默认状态为 Success,
  • readinessProbe:指示容器是否准备好服务请求。如果就绪探测失败,端点控制器将从与 Pod 匹配的所有 Service 的端点中删除该 Pod 的IP 地址。初始延迟之前的就绪状态默认为 Failure。如果容器不提供就绪探针,则默认状态为 Success.
  • startupProbe:指示容器中的应用是否已经启动。如果提供了启动探测(startup probe),则禁用所有其他探测,直到它成功为止。如果启动探测失败,kubelet 将杀死容器,容器服从其重启策略进行重启。如果容器没有提供启动探测,则默认状态为成功Success。

ReadinessProbe 与 LivenessProbe 的区别

  • ReadinessProbe当检测失败后,将Pod的IP:Port从对应的EndPoint列表中删除。
  • LivenessProbe 当检测失败后,将杀死容器并根据 Pod 的重启策略来决定作出对应的措施StartupProbe与

ReadinessProbe、LivenessProbe的区别

  • 如果三个探针同时存在,先执行 StartupProbe 探针,其他两个探针将会被暂时禁用,直到 pod 满足 StartupProbe 探针配置的条件,其他2个探针启动,如果不满足按照规则重启容器。
  • 另外两种探针在容器启动后,会按照配置,直到容器消亡才停止探测,而 StartupProbe 探针只是在容器启动后按照配置满足一次后,不在进行后续的探测。

3.1 探针实例

3.1.1存活探针示例:

[root@k8s-master pod]# ls
lee.yml  timinglee.yml
[root@k8s-master pod]# vim timinglee.yml

[root@k8s-master pod]# kubectl apply -f timinglee.yml
pod/timinglee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS     AGE
timinglee                   1/1     Running   2 (2s ago)   12s
timinglee-c56f584cf-6jxb7   1/1     Running   0            13h
timinglee-c56f584cf-mwrx8   1/1     Running   0            13h

[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS             RESTARTS     AGE
timinglee                   0/1     CrashLoopBackOff   4 (3s ago)   44s
timinglee-c56f584cf-6jxb7   1/1     Running            0            13h
timinglee-c56f584cf-mwrx8   1/1     Running            0            13h
[root@k8s-master pod]# kubectl describe pods

[root@k8s-master pod]# kubectl delete -f timinglee.yml
pod "timinglee" deleted
[root@k8s-master pod]# vim timinglee.yml

[root@k8s-master pod]# kubectl apply -f timinglee.yml
pod/timinglee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   1/1     Running   0          13s
timinglee-c56f584cf-6jxb7   1/1     Running   0          13h
timinglee-c56f584cf-mwrx8   1/1     Running   0          13h
[root@k8s-master pod]# kubectl describe pods

[root@k8s-master pod]# kubectl delete -f timinglee.yml
pod "timinglee" deleted

3.1.2就绪探针示例:

[root@k8s-master pod]# vim timinglee.yml

[root@k8s-master pod]# kubectl apply -f timinglee.yml
pod/timinglee created
[root@k8s-master pod]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
timinglee                   0/1     Running   0          12s
timinglee-c56f584cf-6jxb7   1/1     Running   0          13h
timinglee-c56f584cf-mwrx8   1/1     Running   0          13h
[root@k8s-master pod]# kubectl describe pods

[root@k8s-master pod]# kubectl exec pods/timinglee -c timinglee -- /bin/sh -c "echo hello test > /usr/share/nginx/html/test.html"

[root@k8s-master pod]# curl 10.244.1.34
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master pod]# curl 10.244.1.34/test.html
hello test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

보고.싶다

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值