细说KubernetesPod的驱逐
!!大家好,我是乔克,一个爱折腾的运维工程,一个睡觉都被自己丑醒的云原生爱好者。
作者:乔克
公众号:运维开发故事
博客:www。jokerbai。com
原文:https:sysdig。comblogkubernetespodevicted
作者:JAVIERMARTNEZ
KubernetesPods被驱逐是什么意思?它们被终止了,通常是由于没有足够的资源,但是为什么会发生这种情况呢?
驱逐是一个过程,分配给一个节点的Pod被要求终止。Kubernetes中最常见的情况之一是抢占,为了在资源有限的节点上安排一个新的Pod,通常需要终止另外一个Pod。
另外,Kubernetes会不断检查资源使用情况,当节点压力过大的时候,会触发节点压力驱逐。
每天,数以千计的Pod被驱逐出他们的家园。搁浅和迷茫,他们不得不放弃以前的生活方式。他们中的一些人甚至会无家可归。当前的社会,对CPU和内存的要求会越来越高。
本篇文章将从以下几个方面来展开介绍:Pod被驱逐的原因:抢占和节点压力抢占式驱逐Pod优先级类节点压力驱逐服务质量类其他类型的驱逐Prometheus中的KubernetesPod驱逐监控Pods被驱逐的原因:抢占和节点压力
Kubernetes中发生Pod驱逐的原因有几个,最重要的原因是:抢占节点压力驱逐抢占驱逐
抢占的过程如下:如果一个新的Pod需要被调度,但没有任何合适的节点有足够的资源,那么kubescheduler将检查是否通过驱逐(终止)一些优先级较低的Pod,用来保障新的Pod可以调度。
让我们先了解一下Kubernetes调度是如何工作的。Pod调度
Kubernetes调度是将Pod分配给节点的过程。
默认情况下,有一个负责调度的Kubernetes实体,称为kubescheduler,它将在控制平面上运行。Pod将在Pending状态下开始,直到找到一个匹配的节点。
将一个Pod分配给一个节点的过程遵循这个顺序。预选打分预选
在预选过程中,kubescheduler将选择当前Pod可能被放置的所有节点。这里将考虑到污点和容忍度等特征。一旦完成,它将有一个适合该Pod的节点列表。打分
在打分过程中,kubescheduler将从上一步得到的列表中,给每个节点分配一个分数。这样一来,候选节点就会从最合适到最不合适排序。如果两个节点有相同的分数,kubescheduler会将它们随机排序。image。png
但是,如果没有合适的节点让Pod运行,会发生什么?在这种情况下,Kubernetes将启动抢占程序,试图驱逐低优先级的Pod,以便分配新的Pod。PodPriorityClass
怎样才能防止某个特定的Pod在抢占过程中被驱逐?有时候,一个特定的Pod对你来说是至关重要的,不应该被终止。
这就是为什么Kubernetes具有PriorityClass。
PriorityClass是一个Kubernetes对象,允许我们将数字优先级值映射到特定的Pod。那些数值较高的被归类为更重要,不太可能被驱逐。
你可以通过以下方式查询当前的PriorityClass。kubectlgetpriorityclasses
kubectlgetpc
NAMEVALUEGLOBALDEFAULTAGE
systemclustercritical2000000000spanstylecolor:f92672;fontweight:bold;lineheight:26px;falsespan2d
systemnodecritical2000001000spanstylecolor:f92672;fontweight:bold;lineheight:26px;falsespan2d
测试PriorityClass
这里有三个Pod:blueberry,raspberry和strawberry。NAMEREADYSTATUSRESTARTSAGE
blueberry11Running04h41m
raspberry11Running058m
strawberry11Running05h22m
还有两个PriorityClass:trueberry和falseberry。其中trueberry拥有比较高的优先级。spanstylelineheight:26px;apiVersion:spanspanstylecolor:a6e22e;lineheight:26px;scheduling。k8s。iov1span
spanstylelineheight:26px;kind:spanspanstylecolor:a6e22e;lineheight:26px;PriorityClassspan
spanstylelineheight:26px;metadata:span
spanstylelineheight:26px;name:spanspanstylecolor:a6e22e;lineheight:26px;trueberryspan
spanstylelineheight:26px;value:spanspanstylelineheight:26px;1000000span
spanstylelineheight:26px;globalDefault:spanspanstylecolor:f92672;fontweight:bold;lineheight:26px;falsespan
spanstylelineheight:26px;description:spanspanstylecolor:a6e22e;lineheight:26px;Thisfruitisatrueberryspan
spanstylelineheight:26px;apiVersion:spanspanstylecolor:a6e22e;lineheight:26px;scheduling。k8s。iov1span
spanstylelineheight:26px;kind:spanspanstylecolor:a6e22e;lineheight:26px;PriorityClassspan
spanstylelineheight:26px;metadata:span
spanstylelineheight:26px;name:spanspanstylecolor:a6e22e;lineheight:26px;falseberryspan
spanstylelineheight:26px;value:spanspanstylelineheight:26px;5000span
spanstylelineheight:26px;globalDefault:spanspanstylecolor:f92672;fontweight:bold;lineheight:26px;falsespan
spanstylelineheight:26px;description:spanspanstylecolor:a6e22e;lineheight:26px;Thisfruitisafalseberryspan
blueberry将使用trueberryraspberry和strawberry将使用ffalseberry
这意味着在发生抢占的情况下,raspberry和strawberry更有可能被驱逐,以便为更高优先级的Pod腾出空间。
然后通过在Pod定义中加入优先级类别,将其分配给Pod。spanstylelineheight:26px;priorityClassName:spanspanstylecolor:a6e22e;lineheight:26px;trueberryspan
现在让我们试着再增加三种水果:所有的新水果将包含更高的优先级类,称为trueberry。
由于这三个新的水果对内存或CPU的要求是节点无法满足的,kubelet会驱逐所有比新水果优先级低的Pod。Blueberry保持运行,因为它有更高的优先级。spanstylecolor:a6e22e;lineheight:26px;NAMEspanspanstylecolor:a6e22e;lineheight:26px;READYspanspanstylecolor:a6e22e;lineheight:26px;STATUSspanspanstylecolor:a6e22e;lineheight:26px;RESTARTSspanspanstylecolor:a6e22e;lineheight:26px;AGEspan
spanstylecolor:a6e22e;lineheight:26px;bananaspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;ContainerCreatingspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;2sspan
spanstylecolor:a6e22e;lineheight:26px;blueberryspanspanstylelineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;Runningspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;4h42mspan
spanstylecolor:a6e22e;lineheight:26px;raspberryspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;Terminatingspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;59mspan
spanstylecolor:a6e22e;lineheight:26px;strawberryspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;Terminatingspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;5h23mspan
spanstylecolor:a6e22e;lineheight:26px;tomatospanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;ContainerCreatingspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;2sspan
spanstylecolor:a6e22e;lineheight:26px;watermelonspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;ContainerCreatingspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;2sspan
image。png
最终结果如下:spanstylecolor:a6e22e;lineheight:26px;NAMEspanspanstylecolor:a6e22e;lineheight:26px;READYspanspanstylecolor:a6e22e;lineheight:26px;STATUSspanspanstylecolor:a6e22e;lineheight:26px;RESTARTSspanspanstylecolor:a6e22e;lineheight:26px;AGEspan
spanstylecolor:a6e22e;lineheight:26px;bananaspanspanstylelineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;Runningspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;3sspan
spanstylecolor:a6e22e;lineheight:26px;blueberryspanspanstylelineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;Runningspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;4h43mspan
spanstylecolor:a6e22e;lineheight:26px;tomatospanspanstylelineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;Runningspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;3sspan
spanstylecolor:a6e22e;lineheight:26px;watermelonspanspanstylelineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;1spanspanstylecolor:a6e22e;lineheight:26px;Runningspanspanstylelineheight:26px;0spanspanstylecolor:a6e22e;lineheight:26px;3sspan
节点压力驱逐
除了抢占之外,Kubernetes还不断检查节点资源,如磁盘压力、CPU或内存不足(OOM)。
如果节点的资源(如CPU或内存)消耗达到一定的阈值,Kubelet将开始驱逐Pod,以释放资源。服务质量(QoS)将被纳入考虑范围,以确定驱逐顺序。服务质量QoS
在Kubernetes中,Pod被赋予三种QoS类别之一,这将定义它们在缺乏资源的情况下被驱逐的可能性。这三种QoS分别是:GuaranteedBurstableBestEffort
这些QoS类别是如何分配给Pod的?这是基于对CPU和内存的限制和请求。limits:一个容器可以使用的资源的最大数量。requests:容器运行所需的最小资源量。image。pngGuaranteed
如果一个Pod被分配了一个Guaranteed的QoS等级,它们的特征如下:Pod中的所有容器都为CPU和内存设置了限制和请求。在Pod中的所有容器都有相同的CPU限制和CPU请求的值。Pod中的所有容器都有相同的内存限制和内存请求值。
一个有保证的Pod在正常情况下不会被驱逐以分配给节点中的另一个Pod。Burstable
如果一个Pod的QoS等级为Burstable,那么它将被分配到一个QoS等级。它没有担保的QoS等级。为Pod中的一个容器设置了限制或请求。
一个BurstablePod可以被驱逐,但比下一个类别的可能性小。BestEffort
一个Pod将被分配一个BestEffort的QoS类别,它们将:没有为Pod中的任何容器设置限制和请求。
BestEffortPod在节点中发生节点压力过程的情况下具有最高的驱逐机会。
重要的是:在限制和请求中可能有其他可用的资源,如短暂的存储,但它们不用于QoS类的计算。image。png
如前所述,QoS类将被纳入节点压力驱逐的考虑范围。以下是内部发生的过程。
kubelet按照以下顺序排列要被驱逐的Pod。使用量超过请求的BestEffortPods或BurstablePods使用量低于请求的BurstablePods或GuaranteedPods
Kubernetes将尝试在第二组之前驱逐第一组的Pod。
从上述内容中得到的一些启示。如果在你的容器中添加了非常低的请求,他们的Pod可能会被分配到组1,这意味着它更有可能被驱逐。你无法知道哪个特定的Pod会被驱逐,只是Kubernetes会尝试在第2组之前驱逐第1组的Pod。有保证的Pod通常不会被驱逐:Kubelet不会为了安排其他Pod而驱逐它们。但是,如果一些系统服务需要更多的资源,kubelet将在必要时终止有保证的Pod,并且总是以最低的优先级。其他类型的驱逐
本文主要介绍抢占和节点压力驱逐,但Pod也可以通过其他方式被驱逐。例子包括。API发起的驱逐
你可以通过使用KubernetesEvictionAPI【1】请求对你的一个节点中的Pod进行按需驱逐。基于污点的驱逐
通过Kubernetes污点和容忍度,可以指导你的Pod应该如何分配给Node。但是,如果你将NoExecute污点应用于现有的Node,所有不容忍它的Pod将被立即驱逐。节点排水
有些时候,节点变得无法使用,或者你不想再在上面工作。命令kubectlcordon可以防止新的Pod被安排在它上面,但也有可能一次性完全清空所有当前Pod。如果你运行kubectldrainnodename,该节点中的所有Pod将被驱逐,尊重其优雅的终止期。KubernetesPod驱逐监控
在你的云解决方案中,你可以使用Prometheus来轻松监控Pod驱逐的做法。spanstylecolor:a6e22e;lineheight:26px;kubepodstatusreason{reasonEvicted}spanspanstylecolor:a6e22e;lineheight:26px;spanspanstylelineheight:26px;0span
image。png
这将显示你的集群中所有被驱逐的Pod。你也可以将其与kubepodstatusphase{phaseFailed}配对,以提醒那些在Pod发生故障后被驱逐的人。
如果你想深入了解,请查看以下关于Prometheus中监控资源的文章。
如何合理调整Kubernetes的资源限制【1】Kubernetes容量规划:如何合理安排你的集群的请求【2】总结
正如你所看到的,驱逐只是Kubernetes的另一个功能,它允许你控制有限的资源:在这种情况下,Pod将使用的节点。
在抢占期间,Kubernetes将试图通过驱逐优先级较低的Pod来释放资源,以安排一个新的Pod。通过优先级类,你可以控制哪些Pod更有可能在抢占后继续运行,因为它们被驱逐的可能性较小。
在执行过程中,Kubernetes将检查节点压力,并在需要时驱逐Pod。通过QoS类,你可以控制哪些Pod在节点压力的情况下更有可能被驱逐。
内存和CPU是节点中的重要资源,你需要配置你的Pod、容器和节点来使用它们的正确数量。如果你对这些资源进行相应的管理,不仅可以节省成本,而且还可以确保重要的进程无论如何都能继续运行。文档
【1】https:kubernetes。iodocsreferencegeneratedkubernetesapiv1。25createevictionpodv1core
【1】https:sysdig。comblogkubernetesresourcelimits
【2】https:sysdig。comblogkubernetescapacityplanning
最后,求关注。如果你还想看更多优质原创文章,欢迎关注我们的公众号运维开发故事。
如果我的文章对你有所帮助,还请帮忙一下,你的支持会激励我输出更高质量的文章,非常感谢!
你还可以把我的公众号设为星标,这样当公众号文章更新时,你会在第一时间收到推送消息,避免错过我的文章更新。
realme手机主题曲公布,国内首款新机将发布IT之家4月29日消息经过在海外市场的历练,realme近日正式宣布正式进入国内市场,今天realme手机官微放出了《realme主题曲》。realme是李炳忠于2018……
Realme国行新机入网,其中一部无刘海水滴全面屏设计〔下午6:49:23〕更新:新增RMX1901曲线调整后图片,需要注意本次介绍了最近入网的两台不同型号的手机RMX1901、RMX1851。IT之家4月26日消息手机品牌……
官方发声,Realme手机将重回国内市场感谢IT之家网友热衷的线索投递!IT之家4月22日消息Realme曾是OPPO的一个产品系列。去年Realme成为OPPO的独立子品牌,并在海外上线。先前有迹象显示,Re……
魅族官方回顾Flyme变迁IT之家8月28日消息今日下午,魅族在珠海大剧院举行魅族16sPro旗舰手机发布会,带来了全新的MEIZUUR配件产品,并将带来Flyme8全新系统与来魅族16sPro新品手机……
你多久没有旅行了?耶鲁图书馆正在上演一场关于旅行的旅行,带你你有多久没旅行了?我们对于旅行的迷恋或许并不仅仅因为对于某个未曾抵达的目的地的好奇,而更多的是期待旅行中全然未知的体验和思考。正如美国作家亨利米勒(HenryMiller……
短线量化主力控盘3。15热点榜交易思路1、择时:混沌期2、择板块:数字、科技3、择股(跟踪观察):4、今日操作:卫星割肉了,亏了4个点,早上拉高肯定不会出的剑……
劳动报150平方米巨幅国旗亮相环球港南广场摘要:150平方米巨幅国旗贺国庆今年是中华人民共和国成立73周年。10月1日,150平米的巨幅国旗在上海环球港南广场亮相,同时现场还邀请了近百位儿童共同唱响《我和我的祖国……
孙新生酒泉15出航空城不远,就进入了甘肃地域。再行100公里,到了一个叫天仓的小镇。这里是沙漠区和半沙漠区的分界。转S214公路,开始出现村庄,并日见稠密。达金塔县后,进入河西走廊,远方祁连……
曝三星GalaxyA90弹出式摄像头还可旋转IT之家2月28日消息三星最近推出了GalaxyA系列新机GalaxyA30和GalaxyA50,之后该公司还将推出入门级的GalaxyA10和高端的GalaxyA90。根据此……
三星S10屏幕指纹模块强光下可见位置不居中IT之家3月3日消息2月28日三星在乌镇举行了三星S10S10S10e、GalaxyFold折叠屏手机发布会,三星S10系列手机国行价格也正式公布。目前国内外有不少媒体已……
三星S10e,2019年的第一款小屏旗舰手机IT之家3月2日消息在过去的2018年,厂商们都在努力地把屏幕做大,把边框做窄,然而却忽视了部分用户所期待的,一款真正的小屏手机。2019年2月28日,IT之家小编参加了三星S……
防晒做太好,会缺钙?这种担心真的多余吗?1。(单选题)戴口罩,还需要涂防晒霜吗?A不需要B需要医用口罩对紫外线有一定的阻挡作用,真丝口罩等轻薄材质制成的口罩阻挡作用甚微。而且,口罩的遮挡面积有限,暴……