您现在的位置是:热点 >>正文
彻底搞懂 Kubernetes 中的 Events
热点8304439人已围观
简介...
之前我写了一篇《更优雅的 Kubernetes 集群事件度量方案》,利用 Jaeger 利用 tracing 的方式来采集 Kubernetes 集群中的 events 并进行展示。最终效果如下:
写那篇文章的时候,立了个 flag 要详细介绍下其中的原理,鸽了很久,现在年底了,也该发出来了。
Eents 概览
我们先来做个简单的示例,来看看 Kubernetes 集群中的 events 是什么。
创建一个新的名叫 moelove 的 namespace ,然后在其中创建一个叫做 redis 的 deployment。接下来查看这个 namespace 中的所有 events。
(MoeLove) kubectl create ns moelove namespace/moelove created (MoeLove) kubectl -n moelove create deployment redis --image=ghcr.io/moelove/redis:alpine deployment.apps/redis created (MoeLove) kubectl -n moelove get deploy NAME READY UP-TO-DATE AVAILABLE AGE redis 1/1 1 1 11s (MoeLove) kubectl -n moelove get events LAST SEEN TYPE REASON OBJECT MESSAGE 21s Normal Scheduled pod/redis-687967dbc5-27vmr &nbs彻底p; Successfully assigned moelove/redis-687967dbc5-27vmr to kind-worker3 21s Normal Pulling pod/redis-687967dbc5-27vmr Pulling image "ghcr.io/moelove/redis:alpine" 15s Normal Pulled pod/redis-687967dbc5-27vmr Successfully pulled image "ghcr.io/moelove/redis:alpine" in 6.814310968s 14s Normal Created pod/redis-687967dbc5-27vmr Created container redis 14s Normal Started pod/redis-687967dbc5-27vmr Started container redis 22s Normal SuccessfulCreate replicaset/redis-687967dbc5 Created pod: redis-687967dbc5-27vmr 22s Normal ScalingReplicaSet 搞懂 deployment/redis Scaled up replica set redis-687967dbc5 to 1
但是我们会发现默认情况下 kubectl get events 并没有按照 events 发生的顺序进行排列,所以我们往往需要为其增加 --sort-by='{.metadata.creationTimestamp}' 参数来让其输出可以按时间进行排列。
这也是为何 Kubernetes v1.23 版本中会新增 kubectl alpha events 命令的原因。
按时间排序后可以看到如下结果:
(MoeLove) kubectl -n moelove get events --sort-by='{.metadata.creationTimestamp}' LAST SEEN TYPE REASON OBJECT MESSAGE 2m12s Normal Scheduled pod/redis-687967dbc5-27vmr Successfully assigned moelove/redis-687967dbc5-27vmr to kind-worker3 2m13s Normal SuccessfulCreate replicaset/redis-687967dbc5 Created pod: redis-687967dbc5-27vmr 2m13s Normal ScalingReplicaSet deployment/redis Scaled up Kreplica set redis-687967dbc5 to 1 2m12s Normal Pulling pod/redis-687967dbc5-27vmr Pulling image "ghcr.io/moelove/redis:alpine" 2m6s Normal Pulled pod/redis-687967dbc5-27vmr Successfully pulled image "ghcr.io/moelove/redis:alpine" in 6.814310968s 2m5s Normal Created pod/redis-687967dbc5-27vmr Created container redis 2m5s Normal Started pod/redis-687967dbc5-27vmr Started container redis
通过以上的操作,我们可以发现 events 实际上是 Kubernetes 集群中的一种资源。当 Kubernetes 集群中资源状态发生变化时,可以产生新的 events。
深入 Events 单个 Event 对象
既然 events 是 Kubernetes 集群中的一种资源,正常情况下它的 metadata.name 中应该包含其名称,用于进行单独操作。所以我们可以使用如下命令输出其 name :
(MoeLove) kubectl -n moelove get events --sort-by='{.metadata.creationTimestamp}' -o jsonpath='{range .items[*]}{.metadata.name}{" "}{end}'&nubbsp;redis-687967dbc5-27vmr.16c4fb7bde8c69d2 redis-687967dbc5.16c4fb7bde6b54c4 redis.16c4fb7bde1bf769 redis-687967dbc5-27vmr.16c4fb7bf8a0ab35 redis-687967dbc5-27vmr.16c4fb7d8ecaeff8 redis-687967dbc5-27vmr.16c4fb7d99709da9 redis-687967dbc5-27vmr.16c4fb7d9be30c06
选择其中的任意一条 event 记录,将其输出为 YAML 格式进行查看:
(MoeLove) kubectl -n moelove get events redis-687967dbc5-27vmr.16c4fb7bde8c69d2 -o yaml action: Binding apiVersion: v1 eventTime: "2021-12-28T19:31:13.702987Z" firstTimestamp: null involvedObject: apiVersion: v1 kind: Pod name: redis-687967dbc5-27vmr namespace: moelove resourceVersion: "330230" uid: 71b97182-5593-47b2-88cc-b3f59618c7aa kind: Event lastTimestamp: null message: Successfully assigned moelove/redis-687967dbc5-27vmr to kind-worker3 metadata: creationTimestamp: "2021-12-28T19:31:13Z" name: redis-687967dbc5-27vmr.16c4fb7bde8c69d2 namespace: moelove resourceVersion: "330235" uid: e5c03126-33b9-4559-9585-5e82adcd96b0 reason: Scheduled reportingComponent: default-scheduler reportingInstance: default-scheduler-kind-control-plane source: {} type: Normal&ernbsp;
可以看到其中包含了很多信息, 这里我们先不展开。我们看另一个例子。
kubectl describe 中的 Events
我们可以分别对 Deployment 对象和 Pod 对象执行 describe 的操作,可以得到如下结果(省略掉了中间输出):
对 Deployment 操作 (MoeLove) kubectl -n moelove describe deploy/redis Name: redis Namespace: moelove ... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 15m deployment-controller Scaled up replica set redis-687967dbc5 to 1 对 Pod 操作 (MoeLove) kubectl -n moelove describe pods redis-687967dbc5-27vmr Name: redis-687967dbc5-27vmr &nenbsp; Namespace: moelove Priority: 0 Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 18m default-scheduler Successfully assigned moelove/redis-687967dbc5-27vmr to kind-worker3 Normal Pulling 18m kubelet Pulling image "ghcr.io/moelove/redis:alpine" Normal Pulled 17m kubelet Successfully pulled image "ghcr.io/moelove/redis:alpine" in 6.81431te0968s Normal Created 17m kubelet Created container redis Normal Started 17m kubelet Started container redis
我们可以发现 对不同的资源对象进行 describe 的时候,能看到的 events 内容都是与自己有直接关联的。在 describe Deployment 的时候,看不到 Pod 相关的 Events 。
这说明, Event 对象中是包含它所描述的资源对象的信息的,它们是有直接联系的。
结合前面我们看到的单个 Event 对象,我们发现 involvedObject 字段中内容就是与该 Event 相关联的资源对象的信息。
更进一步了解 Events
我们来看看如下的示例,创建一个 Deployment ,但是使用一个不存在的镜像:
(MoeLove) kubectl -n moelove create deployment non-exist --image=ghcr.io/moelove/non-exist deployment.apps/non-exist created (MoeLove) kubectl -n moelove get pods NAME READY STATUS RESTARTS AGE non-exist-d9ddbdd84-tnrhd 0/1 ErrImagePull 0 11s redis-687967dbc5-27vmr 1/1 Running 0  s ; 26m
我们可以看到当前的 Pod 处于一个 ErrImagePull 的状态。查看当前 namespace 中的 events (我省略掉了之前 deploy/redis 的记录)
(MoeLove) kubectl -n moelove get events --sort-by='{.metadata.creationTimestamp}' LAST SEEN TYPE REASON OBJECT MESSAGE 35s Normal SuccessfulCreate replicaset/non-exist-d9ddbdd84 Created pod: non-exist-d9ddbdd84-tnrhd 35s Normal ScalingReplicaSet deployment/non-exist Scaled up replica set non-exist-d9ddbdd84 to 1 35s Normal Sch中的eduled pod/non-exist-d9ddbdd84-tnrhd Successfully assigned moelove/non-exist-d9ddbdd84-tnrhd to kind-worker3 17s Warning Failed pod/non-exist-d9ddbdd84-tnrhd Error: ErrImagePull 17s Warning Failed pod/non-exist-d9ddbdd84-tnrhd Failed to pull image "ghcr.io/moelove/non-exist": rpc error: code = Unknown desc = failed to pull and unpack image "ghcr.io/moelove/non-exist:latest": failed to resolve reference "ghcr.io/moelove/non-exist:latest": failed to authorize: failed to fetch anonymous token: unexpected status: 403 Forbidden 18s Normal Pulling pod/non-exist-d9ddbdd84-tnrhd Pulling image "ghcr.io/moelove/non-exist" 4s Warning E Failed pod/non-exist-d9ddbdd84-tnrhd Error: ImagePullBackOff 4s Normal BackOff pod/non-exist-d9ddbdd84-tnrhd Back-off pulling image "ghcr.io/moelove/non-exist"
对这个 Pod 执行 describe 操作:
(MoeLove) kubectl -n moelove describe pods non-exist-d9ddbdd84-tnrhd ... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 4m default-scheduler Successfully assigned moelove/non-exist-d9ddbdd84-tnrhd&nbvesp;to kind-worker3 Normal Pulling 2m22s (x4 over 3m59s) kubelet Pulling image "ghcr.io/moelove/non-exist" Warning Failed 2m21s (x4 over 3m59s) kubelet Failed to pull image "ghcr.io/moelove/non-exist": rpc error: code = Unknown desc = failed to pull and unpack image "ghcr.io/moelove/non-exist:latest": failed to resolve reference "ghcr.io/moelove/non-exist:latest": failed to authorize: failed to fetch anonymous token: unexpected status: 403 Forbidden Warning Failed 2m21s (x4 over 3m59s) kubelet Error: ErrImagePull Warning Failed 2m9s (x6 over 3m58s) kubelet Error: ImagePullBackOff Normal BackOff 115s (x7 over 3m58s) kubelet nt Back-off pulling image "ghcr.io/moelove/non-exist"
我们可以发现,这里的输出和之前正确运行 Pod 的不一样。最主要的区别在于 Age 列。这里我们看到了类似 115s (x7 over 3m58s) 这样的输出。
它的含义表示:该类型的 event 在 3m58s 中已经发生了 7 次,最近的一次发生在 115s 之前
但是当我们去直接 kubectl get events 的时候,我们并没有看到有 7 次重复的 event 。这说明 Kubernetes 会自动将重复的 events 进行合并。
选择最后一条 Events (方法前面内容已经讲了) 并将其内容使用 YAML 格式进行输出:
(MoeLove) kubectl -n moelove get events non-exist-d9ddbdd84-tnrhd.16c4fce570cfba46 -o yaml apiVersion: v1 count: 43 eventTime: null firstTimestamp: "2021-12-28T19:57:06Z" involvedObject: apiVersion: v1 fieldPath: spec.containers{non-exist} kind: Pod name: non-exist-d9ddbdd84-tnrhd namespace: moelove resourceVersion: "333366" uid: 33045163-146e-4282-b559-fec19a189a10 kind: Event lastTimestamp: "2021-12-28T18:07:14Z" message: Back-off pulling image "ghcr.io/moelove/non-exist" metadata: creationTimestamp: "2021-12-28T19:57:06Z" name: non-exist-d9ddbdd84-tnrhd.16c4fce570cfba46 namespace: moelove resourceVersion: "334638" uid: 60708be0-23b9-481b-a290-dd208fed6d47 reason: BackOff reportingComponent: "" reportingInstance: ""彻底 source: component: kubelet host: kind-worker3 type: Normal
这里我们可以看到其字段中包括一个 count 字段,表示同类 event 发生了多少次。以及 firstTimestamp 和 lastTimestamp 分别表示了这个 event 首次出现了最近一次出现的时间。这样也就解释了前面的输出中 events 持续的周期了。
彻底搞懂 Events
以下内容是从 Events 中随便选择的一条,我们可以看到它包含的一些字段信息:
apiVersion: v1 count: 1 eventTime: null firstTimestamp: "2021-12-28T19:31:13Z" involvedObject: apiVersion: apps/v1 kind: ReplicaSet name: redis-687967dbc5 namespace: moelove resourceVersion: "330227" uid: 11e98a9d-9062-4ccb-92cb-f51cc74d4c1d kind: Event lastTimestamp: "2021-12-28T19:31:13Z" message: 'Created pod: redis-687967dbc5-27vmr' metadata: creationTimestamp: "2021-12-28T19:31:13Z" name: redis-687967dbc5.16c4fb7bde6b54c4 namespace: moelove resourceVersion: "330231" uid: 8e37ec1e-b3a1-420c-96d4-3b3b2995c300 reason: SuccessfulCreate reportingComponent: "" reportingInstance: "" source: component: replicaset-controller type: Normal
其中主要字段的含义如下:
count: 表示当前同类的事件发生了多少次 (前面已经介绍) involvedObject: 与此 event 有直接关联的资源对象 (前面已经介绍) , 结构如下: type ObjectReference struct { Kind string Namespace string &n搞懂bsp;Name string UID types.UID APIVersion string ResourceVersion string FieldPath string } source: 直接关联的组件, 结构如下: type EventSource struct { Component string Host string } reason: 简单的总结(或者一个固定的代码),比较适合用于做筛选条件,主要是为了让机器可读,当前有超过 50 种这样的代码; message: 给一个更易让人读懂的详细说明 type: 当前只有 Normal 和 Warning 两种类型, 源码中也分别写了其含义: // staging/src/k8s.io/api/core/v1/types.go const ( // Information only and will not cause any problems EventTypeNormal string = "Normal" // These events are to warn that something might go wrong EventTypeWarning string = "Warning" )
所以,当我们将这些 Events 都作为 tracing 的 span 采集回来后,就可以按照其 source 进行分类,按 involvedObject 进行关联,按时间进行排序了。
总结
在这篇文章中,我主要通过两个示例,一个正确部署的 Deploy,以及一个使用不存在镜像部署的 Deploy,深入的介绍了 Events 对象的实际的作用及其各个字段的含义。
对于 Kubernetes 而言,Events 中包含了很多有用的信息,但是这些信息却并不会对 Kubernetes 造成什么影响,它们也并不是实际的 Kubernetes 的日志。默认情况下 Kubernetes 中的日志在 1 小时后就会被清理掉,以便释放对 etcd 的资源占用。
所以为了能更好的让集群管理员知道发生了什么,在生产环境中,我们通常会把 Kubernetes 集群的 events 也给采集回来。我个人比较推荐的工具是:https://github.com/opsgenie/kubernetes-event-exporter
本文转载自微信公众号「MoeLove
Tags:
相关文章
标签: 无忧传媒 雷彬艺 王暖暖 孕妇泰国坠崖案
热点云梦级5月13日云梦级消息,据红星新闻报道,5月12日凌晨2点多,无忧传媒创始人兼CEO雷彬艺在朋友圈发文,配图为近期热门电影《给阿嬷的情书》的画面,图片显示,“无情无义的人不能交往”。 下午,雷彬艺...
【热点】
阅读更多云安全日报220414:思科无线局域网控制器发现身份验证绕过漏洞,需要尽快升级
热点4月13日,思科发布了安全更新,修复了思科无线局域网控制器中发现的身份验证绕过漏洞。以下是漏洞详情: 漏洞详情 来源:https://tools.cisco.com/security/center/c...
【热点】
阅读更多标签: 天美G1工作室 高敏 VincentGao 腾讯游戏
热点云梦级5月9日云梦级消息,据竞核获悉,5月8日天美内部发布公告宣布了高敏(Vincent Gao)不再担任G1工作室总经理职务。知情人士透露,5月初高敏已经正式离职。 公开资料显示,高敏于2016年底...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.abbreviations.com/谷歌蜘蛛池租用【tg:seogoogle987】evc
- https://www.shacknews.com/editorial-search?q=谷歌蜘蛛池租用【tg:seogoogle987】,0XX
- https://hsmoa.com/search?query=谷歌蜘蛛池租用【tg:seogoogle987】9We
- https://www.remit.co.jp/pt/?action=CHECK&s=谷歌蜘蛛池租用【tg:seogoogle987】,gZ8
- https://www.pmindia.gov.in/en/?s=谷歌蜘蛛池租用【tg:seogoogle987】,7R4
- https://www.resopal.de/catalogsearch/result/?action=SEARCH&q=谷歌蜘蛛池租用【tg:seogoogle987】,ZRc
- https://www.lechzuers.com/de/search?cat=163&q=谷歌蜘蛛池租用【tg:seogoogle987】,KJK
- https://yscc1986.net/search/谷歌蜘蛛池租用【tg:seogoogle987】dN3
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌蜘蛛池租用【tg:seogoogle987】OPI
- https://www.sebson.de/en/search?query=谷歌蜘蛛池租用【tg:seogoogle987】,ua3
- https://www.lyrics.com/lyrics/kkk3谷歌蜘蛛池租用【tg:seogoogle987】,36A
- https://www.tailoy.com.pe/catalogsearch/result/?q=谷歌蜘蛛池租用【tg:seogoogle987】,EhG
- https://www.universal-music.co.jp/search/?q=谷歌蜘蛛池租用【tg:seogoogle987】,T8b
- https://www.kaliastyle.com/catalogsearch/result/?q=GOOGLE谷歌蜘蛛池租用【tg:seogoogle987】,tOP
- https://www.pickett.co.uk/collections/vendors?q=谷歌蜘蛛池租用【tg:seogoogle987】,V7N
- https://www.teamlab.art/es/?submit=谷歌蜘蛛池租用【tg:seogoogle987】,cpJ
- https://www.youtube.com/results?search_query=谷歌蜘蛛池租用【tg:seogoogle987】,rl9
- https://blvdseafood.com/?s=谷歌蜘蛛池租用【tg:seogoogle987】5bY
- https://yokohama-oht.com/?action=CHECK&s=谷歌蜘蛛池租用【tg:seogoogle987】,CKd
- https://www.goodreads.com/quotes/tag/谷歌蜘蛛池租用【tg:seogoogle987】6q6
- https://www.oikos-tecnics.com/?s=谷歌蜘蛛池租用【tg:seogoogle987】,3Hs
- https://www.kubb-tool.bfs.admin.ch/en/search?action=CHECK&q=谷歌蜘蛛池租用【tg:seogoogle987】,CX8
- https://www.roberthalf.com/us/en/jobs/all/谷歌蜘蛛池租用【tg:seogoogle987】Urc
- https://www.roberthalf.com/us/en/jobs/all/谷歌蜘蛛池租用【tg:seogoogle987】ONz
- https://www.vivaraise.fr/catalogsearch/result/?q=谷歌蜘蛛池租用【tg:seogoogle987】,mAU
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌蜘蛛池租用【tg:seogoogle987】,BoQ
- https://www.mi.com/search?keyword=谷歌蜘蛛池租用【tg:seogoogle987】,aZR
- https://www.nctsn.org/search?cat=191&query=谷歌蜘蛛池租用【tg:seogoogle987】,ufj
- https://www.qwant.com/?q=谷歌蜘蛛池租用【tg:seogoogle987】,YR9
- https://www.normani.de/search?q=谷歌蜘蛛池租用【tg:seogoogle987】,6Ya
- https://www.miamiopenmri.com/?action=CHECK&s=谷歌蜘蛛池租用【tg:seogoogle987】,n65
- https://www.teamlab.art/ko/?submit=谷歌蜘蛛池租用【tg:seogoogle987】,fdZ
- https://www.repetto.jp/item/search?action=QUERY&q=谷歌蜘蛛池租用【tg:seogoogle987】,DSm
- https://www.teamlab.art/th/?submit=谷歌蜘蛛池租用【tg:seogoogle987】,4Tr
- https://www.sonepar.ch/fr/search?q=谷歌蜘蛛池租用【tg:seogoogle987】,lxA
- https://wydden.com/?ID=r8w9WMSKWkOcmiaFWaUv0dzMdqZWfKtF&s=谷歌蜘蛛池租用【tg:seogoogle987】,xhV
- https://www.polliwogs.com/search/谷歌蜘蛛池租用【tg:seogoogle987】,eZR
- https://www.totolink.vn/search/谷歌蜘蛛池租用【tg:seogoogle987】,qwE
- https://www.samen-maier.at/search/谷歌蜘蛛池租用【tg:seogoogle987】,PGS
- https://www.woorden.org/spreekwoord.php?woord=谷歌蜘蛛池租用【tg:seogoogle987】,jaq
- https://www.wellbees.com/index.php/catalogsearch/result/index/?q=谷歌蜘蛛池租用【tg:seogoogle987】,3SR
- https://www.le-trend.com/en/search/=谷歌蜘蛛池租用【tg:seogoogle987】,avg
- https://www.nyif.com/catalogsearch/result/?q=谷歌蜘蛛池租用【tg:seogoogle987】,tQh
- https://jihoon471.cafe24.com/product/search.html?banner_action=&keyword=谷歌蜘蛛池租用【tg:seogoogle987】z4A
- https://www.tiktok.com/tag/谷歌蜘蛛池租用【tg:seogoogle987】,lcy
- https://5gaa.org/?token=6e2l7hjt2i26gzgctgwdrv7v0j6nxorq&s=谷歌蜘蛛池租用【tg:seogoogle987】GBw
- https://www.kuaikanmanhua.com/sou/谷歌蜘蛛池租用【tg:seogoogle987】,Z5I
- https://www.nuovabai.it/catalogsearch/result/?action=SEARCH&q=谷歌蜘蛛池租用【tg:seogoogle987】,FHp
- https://www.ons.gov.uk/search?q=谷歌蜘蛛池租用【tg:seogoogle987】,sNL
- https://www.rockngem.com/?action=CHECK&s=谷歌蜘蛛池租用【tg:seogoogle987】,4Eo