kubedog:解决K8S在CI/CD中无法持续追踪问题
需求
在Jenkins的CI/CD流水线中,无论是通过Kubernetes CLI
还是Kubernetes Continuous Deploy
插件,在应用yaml后无法检查资源是否部署成功,只能通过kubectl手动检查。
这种现象类似于当通过kubectl apply对资源进行配置后,需以下操作进一步获取资源的运行信息:
- kubectl get -w
- kubectl logs
- kubectl describe
多次输入以上命令我们才能确定pod是否运行成功或定位pod失败原因,操作比较繁琐。
而kubedog则在一定程度上简化了这个过程,可以跟踪指定资源的状态并将状态信息直接输出。
下面我们就来详细了解下kubedog。
kubedog
Kubedog是一个库,用于在 CI/CD 部署pipeline 中监视和跟踪 Kubernetes 资源;同时还支持CLI,但是它提供了访问库函数的最小接口,主要目的是为了检查库功能和调试。Kubedog最终为用户提供有关资源的足够信息,从而无需进行其他调试和查看资源状况的kubectl调用,将与资源有关的所有数据将统一为一个事件流。
kubedog主要使用以下三种方式进行资源跟踪:
- follow
- rollout
- multitrack
分别对应三个命令:
- kubedog follow
- kubedog rollout track
- kubedog multitrack
安装
# 1.安装
curl -L https://dl.bintray.com/flant/kubedog/v0.3.4/kubedog-linux-amd64-v0.3.4 -o /tmp/kubedog
chmod +x /tmp/kubedog
sudo mv /tmp/kubedog /usr/local/bin/kubedog
# 2.添加环境变量
vim /etc/profile
KUBEDOG_KUBE_CONFIG=/root/.kube/config
source /etc/profile
使用
1. follow
follow可以跟踪资源从创建到Ready对外提供服务的整个过程,并将pod日志打印。
# 1.资源配置
# kubectl apply -f helloworld.yaml
deployment.apps/helloworld created
service/helloworld created
ingress.extensions/helloworld created# 2.跟踪资源状态
# kubedog follow -n test deployment helloworld
deploy/helloworld added
deploy/helloworld new rs/helloworld-8d958c978 added
deploy/helloworld rs/helloworld-8d958c978(new) po/helloworld-8d958c978-wrb7m added
deploy/helloworld event: po/helloworld-8d958c978-wrb7m Pulling: Pulling image "harbor.test.cn/helloworld/helloworld:1311c4520122dfa67bb60e0103c9519fcb370e50"
deploy/helloworld event: po/helloworld-8d958c978-wrb7m Pulled: Successfully pulled image "harbor.test.cn/helloworld/helloworld:1311c4520122dfa67bb60e0103c9519fcb370e50"
deploy/helloworld event: po/helloworld-8d958c978-wrb7m Created: Created container helloworld
deploy/helloworld event: po/helloworld-8d958c978-wrb7m Started: Started container helloworld
>> deploy/helloworld rs/helloworld-8d958c978(new) po/helloworld-8d958c978-wrb7m helloworld
LOGBACK: No context given for c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@1674896058. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.2.5.RELEASE)2020-09-29 10:05:13.290 INFO 7 --- [ main] c.d.helloworld.HelloworldApplication : Starting HelloworldApplication v0.0.1-SNAPSHOT on helloworld-8d958c978-wrb7m with PID 7 (/helloworld.jar started by root in /)
....省略....
2020-09-29 10:06:09.380 INFO 7 --- [ main] c.d.helloworld.HelloworldApplication : Started HelloworldApplication in 76.601 seconds (JVM running for 89.287)
2020-09-29 10:07:43.980 INFO 7 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-09-29 10:07:43.981 INFO 7 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-09-29 10:07:43.989 INFO 7 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 msdeploy/helloworld become READY
2. rollout
rollout与follow对比,不会输出pod运行日志,直接打印pod状态。
# 1.资源配置
# kubectl apply -f helloworld.yaml
deployment.apps/helloworld created
service/helloworld created
ingress.extensions/helloworld created
# 2.跟踪资源状态
# kubedog rollout track -n test deployment helloworld
deploy/helloworld added
deploy/helloworld rs/helloworld-8d958c978 added
deploy/helloworld po/helloworld-8d958c978-wrb7m added
deploy/helloworld become READY# 3.检查运行状态
# echo $?
0# 4.如果运行报错
# kubedog rollout track -n test deployment helloworld
deploy/helloworld added
deploy/helloworld rs/helloworld-fc99f6486 added
deploy/helloworld po/helloworld-fc99f6486-x27xl added
deploy/helloworld event: ScalingReplicaSet: Scaled up replica set helloworld-fc99f6486 to 1
deploy/helloworld event: po/helloworld-fc99f6486-x27xl Pulling: Pulling image "harbor.test.cn/helloworld/helloworld:1311c4520122dfa67bb60e0103c9519fcb370e5"
deploy/helloworld event: po/helloworld-fc99f6486-x27xl Failed: Error: ErrImagePull
deploy/helloworld event: po/helloworld-fc99f6486-x27xl Failed: Failed to pull image "harbor.test.cn/helloworld/helloworld:1311c4520122dfa67bb60e0103c9519fcb370e5": rpc error: code = Unknown desc = ror response from daemon: manifest for harbor.test.cn/helloworld/helloworld:1311c4520122dfa67bb60e0103c9519fcb370e5 not found: manifest unknown: manifest unknown
deploy/helloworld event: po/helloworld-fc99f6486-x27xl Failed: Error: ImagePullBackOff
deploy/helloworld event: po/helloworld-fc99f6486-x27xl BackOff: Back-off pulling image "harbor.test.cn/helloworld/helloworld:1311c4520122dfa67bb60e0103c9519fcb370e5"
deploy/helloworld po/helloworld-fc99f6486-x27xl helloworld error: ErrImagePull: rpc error: code = Unknown desc = Error response from daemon: manifest for harbor.test.cn/helloworld/helloworld:1311c4520122dfa67bb60e0103c9519fcb370e5 not found: manifest unknown: manifest unknown# 3.检查运行状态
# echo $?
130
3.multitrack
官方不建议使用follow和rollout模式,后续也不会更新;而推荐使用multitrack则直观、易读的方式来展示状态信息。
要使用multitrack,您需要将JSON结构传递给kubedog的stdin,如下:
cat << EOF | kubedog multitrack
{"Deployments": [{"ResourceName": "helloworld","Namespace": "test"}],"Deployments": [{"ResourceName": "helloworld1","Namespace": "test"}]
}
EOF
或
echo '{"Deployments": [{"ResourceName": "helloworld","Namespace": "test"}],"Deployments": [{"ResourceName": "helloworld1","Namespace": "test"}]}' | kubedog multitrack
具体使用
# 1.资源配置
kubectl apply -f helloworld.yaml
kubectl apply -f helloworld1.yaml
# 2.跟踪状态
echo '{"Deployments": [{"ResourceName": "helloworld","Namespace": "test"}],"Deployments": [{"ResourceName": "helloworld1","Namespace": "test"}]}' | kubedog multitrack
┌ Status progress
│ DEPLOYMENT REPLICAS AVAILABLE UP-TO-DATE
│ helloworld 1/1 0 1
│ │ POD READY RESTARTS STATUS ---
│ └── 8d958c978-2rswq 0/1 0 Pending Waiting for: available 0->1
└ Status progress
┌ Status progress
│ DEPLOYMENT REPLICAS AVAILABLE UP-TO-DATE
│ helloworld 1/1 0 1
│ │ POD READY RESTARTS STATUS ---
│ └── 8d958c978-2rswq 0/1 0 Pending Waiting for: available 0->1
│ └── error: FailedScheduling: 0/3 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: },
│ that the pod didn't tolerate, 2 Insufficient cpu.
└ Status progress
应用场景
在了解了kubedog如何使用后,那我们到底可以使用kubedog做些什么呢?
在Jenkins的CI/CD流水线中,无论是通过Kubernetes CLI
还是Kubernetes Continuous Deploy
插件,在应用yaml后无法检查资源是否部署成功,只能通过kubectl手动检查,因此我们可以使用kubedog在pipeline完成这最后一步验证。
借助Kubernetes CLI
插件可以直接执行kubedog命令,而无需先通过ssh登录节点再执行,具体如下:
# 在流水线中插入以下命令
withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s-jenkins-slave', namespace: 'test', serverUrl: 'https://192.168.3.217:6443') {sh """kubectl apply -f k8s-test.yaml && kubedog rollout track -n test deployment helloworld"""
}# 控制台日志打印
....部分信息省略....
+ kubectl apply -f k8s-test.yamldeployment.apps/helloworld created
service/helloworld unchanged
ingress.extensions/helloworld configured
+ kubedog rollout track -n test deployment helloworld
# deploy/helloworld added
# deploy/helloworld rs/helloworld-54b4dd8c4f added
# deploy/helloworld po/helloworld-54b4dd8c4f-gcck8 added
# deploy/helloworld event: po/helloworld-54b4dd8c4f-gcck8 Pulled: Container image "harbor.cityre.cn/helloworld/helloworld:1eaf39a86f11ce728829d3700d14362ccdd96867" already present on machine
# deploy/helloworld event: po/helloworld-54b4dd8c4f-gcck8 Created: Created container helloworld
# deploy/helloworld event: po/helloworld-54b4dd8c4f-gcck8 Started: Started container helloworld>> po/helloworld-54b4dd8c4f-gcck8 helloworld
LOGBACK: No context given for c.q.l.core.rolling.SizeAndTimeBasedRollingPolicy@1911006827. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.2.5.RELEASE)2020-09-30 08:52:43.854 INFO 6 --- [ main] c.d.helloworld.HelloworldApplication : Starting HelloworldApplication v0.0.1-SNAPSHOT on helloworld-54b4dd8c4f-gcck8 with PID 6 (/helloworld.jar started by root in /)
2020-09-30 08:52:43.858 INFO 6 --- [ main] c.d.helloworld.HelloworldApplication : The following profiles are active: test2020-09-30 08:52:45.308 INFO 6 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-09-30 08:52:45.324 INFO 6 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-09-30 08:52:45.325 INFO 6 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-09-30 08:52:45.395 INFO 6 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-09-30 08:52:45.395 INFO 6 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1433 ms
2020-09-30 08:52:45.624 INFO 6 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-30 08:52:45.831 INFO 6 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-09-30 08:52:45.836 INFO 6 --- [ main] c.d.helloworld.HelloworldApplication : Started HelloworldApplication in 3.459 seconds (JVM running for 3.998)2020-09-30 08:53:45.967 INFO 6 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-09-30 08:53:45.968 INFO 6 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-09-30 08:53:45.975 INFO 6 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 7 ms
# deploy/helloworld become READY
[Pipeline] }
[kubernetes-cli] kubectl configuration cleaned up
注意:
- 由于需要在CLI模式下执行kubedog命令,因此我们将原来使用的
Kubernetes Continuous Deploy
插件切换为Kubernetes CLI
; Kubernetes CLI
插件必须先执行kubectl apply ,再执行kubudog,直接执行kubedog会报错;- 提前将宿主机kubedog命令挂载到jenkins-slave容器;
kubedog rollout
在服务器单独执行并不会输出日志,而在jenkins中输出,意外惊喜!
总结
通过kubedog解决了K8S 在CI/CD中无法持续追踪的问题,直接通过jenkins的控制台输出就能实时了解pod的启动状态及报错信息;但是在jenkins 中不建议使用kubedog multitrack
跟踪状态,因为控制台输出的应用日志信息和状态信息交叉错乱,因此我们使用的是kubedog rollout
。
kubedog:解决K8S在CI/CD中无法持续追踪问题相关推荐
- GitLab CI/CD中的常用预设变量
导言 在GitLab CI/CD中有很多官方预设的变量,这些变量极大地扩展了流水线的功能,比如有一个预设变量为 CI,在GitLab CI/CD的流水线中它的值始终为true,用于表明当前的运行环境是 ...
- Gitlab CI/CD 中预设变量 .gitlab-ci.yml 的预定义变量
文章有更新, 更加详细和更常用的预设变量可以参考以下两篇新的文章 GitLab CI/CD关键词(五):变量 variables GitLab CI/CD中的常用预设变量 GitLab CI / CD ...
- jenkins+maven+gitlab+harbor+k8s的 CI/CD
安装k8s jenkins maven gitlab harbor 过程略过~ 记得在jenkins机器上yum个git就好,因为需要git去连我们的代码库, 做这套的时候踩过的坑,提醒大家 只写 ...
- 基于K8s的CI/CD自动化持续集成部署框架
文章目录 什么是CI/CD? 1 环境准备 2 组件部署 2.1 jenkins快速部署 2.2 gitlab快速部署 2.3 harbor快速部署 3 CI/CD交付 什么是CI/CD? CI/CD ...
- 测试开发工程师成长日记010 - Jenkins中的CI/CD/CT(持续集成构建/持续交付/持续测试)
CI(Continuous integration,持续集成) CI(Continuous integration,中文意思是持续集成)是一种软件开发时间.持续集成强调开发人员提交了新代码之后,立刻进 ...
- 极狐GitLab的CI/CD中 声明和使用变量的三种方式
更加有关变量的解析可以阅读该文章 极狐GitLab CI/CD关键词(五):变量 variables 使用预设变量 获取当前任务的变量 test_variable:stage: testscript: ...
- Gitlab CI/CD中的yml遇到的错误分析含解决方法(全)
目录 前言 1. COPY failed: stat /xxxx: no such file or directory 2. invalid from flag value builder: pull ...
- Gitlab CI/CD 中使用 ssh-key,登录远程主机部署,执行远程主机的特定脚本
前言 在开发流水线,完成部署作业时,有时需要在流水线构建环境中下载其他项目的代码,并修改推送. 也可能将构建物传输到一个远程主机上,或者登录到远程主机执行一段部署脚本.都是要使用以下这段作业来完成. ...
- 漫画:如何用 K8s 实现 CI/CD 发布流程?
注:本内容来自网络,未能确认原作者,侵权删
最新文章
- Android下载文件
- .Net 内存对象分析
- java 打印abcd_用JAVA编程统计字符串ABCD123!@#$%ab中大写字母、小写字母、数字、其它字符的个数并打印出来...
- 使用MQTT与函数计算做热力图的实践
- 计算机人文研究生专业,西安科技大学,艺术、人文、材料、计算机、通信等专业调剂信息...
- 利用python语言实现分类算法_使用python实现kNN分类算法
- Hicharts实现世界地图思路及踩过的坑(含中文GeoJSON数据集及详细代码)
- SoftICE Window Resizing Commands
- Anomaly Detection with Partially Observed Anomalies译文
- AngularJs 生命周期$onChanges钩子函数总结
- Chapter2.4:数学模型考研参考题
- nodejs-CentOS64下载安装配置
- 10大最受欢迎的国外业务流程管理(BPM)软件
- LESS与SASS的伯与仲
- shell脚本之遍历字符串数组
- 我,32岁,小米公司程序员,揭 开北京“码农”的真实收入
- mysql基础操作(详细版)--增删改查
- 惠斯登电桥传感器电路设计技巧
- 短视频程序源码高仿马蜂窝旅游头像泡泡动画
- 【完美解决】Word已经关闭但是任务管理器中仍有进程且占用CPU
热门文章
- 社招两年,终于冲进了决赛圈,现在犹豫是去阿里P6还是字节2-1,希望大家给点经验
- 爬取51job职位信息--进行专业市场需求可视化分析(python、tableau、DBeaver)
- 11 个对开发有帮助的 JS 技巧,进收藏夹当小词典吧!
- 【计算机图形学】poisson Image Editing泊松图像融合算法
- amos基础6-中介分析
- head first python豆瓣_Head First Python 学习心得(1-6章)
- R plot图片背景设置为透明_matplotlib设置颜色、标记、线条,让你的图像更加丰富...
- 为什么IT行业比传统行业工资高这么多?而且还很缺人
- 动手学习pyTorch之【线性模型】—— 具体代码实现
- 常见的工作流引擎(osworkflow、jbpm、activiti、flowable、camunda)比较