Kubedog 是一个开源的 Golang 项目,使用 watch 方式对 Kubernetes 资源进行跟踪,能够方便的用于日常运维和 CI/CD 过程之中,项目中除了一个 CLI 小工具之外,还提供了一组 SDK,用户可以将其中的 Watch 功能集成到自己的系统之中。安装过程非常简单,在项目网页直接下载即可。

源码地址:https://github.com/werf/kubedog.git

kubedog主要使用以下三种方式进行资源跟踪:

  • follow
  • rollout
  • multitrack

分别对应三个命令:

  • kubedog follow
  • kubedog rollout track
  • kubedog multitrack

rollout track

在 Kubernetes 上运行应用时,通常的做法是使用 kubectl apply 提交 YAML 之后,使用 kubectl get -w 或者 watch kubectl get 之类的命令等待 Pod 启动。如果启动成功,则进行测试等后续动作;如果启动失败,就需要用 kubectl logskubectl describe 等命令来查看失败原因。kubedog 能在一定程度上简化这一过程。
例如使用 kubectl run 命令创建一个新的 Deployment 资源,并使用 kubedog 跟进创建进程:

  1. $ kubectl run nginx --image=nginx22
  2. ...
  3. deployment.apps/nginx created
  4. $ kubedog rollout track deployment nginx
  5. # deploy/nginx added
  6. # deploy/nginx rs/nginx-6cc78cbf64 added
  7. # deploy/nginx po/nginx-6cc78cbf64-8pnjz added
  8. # deploy/nginx po/nginx-6cc78cbf64-8pnjz nginx error: ImagePullBackOff: Back-off pulling image "nginx22"
  9. deploy/nginx po/nginx-6cc78cbf64-8pnjz nginx failed: ImagePullBackOff: Back-off pulling image "nginx22"
  10. $ echo $?
  11. 130

很方便的看出,运行失败的状态及其原因,并且可以使用返回码来进行判断,方便在 Pipeline 中的运行。接下来可以使用 kubectl edit 命令编辑 Deployment,修改正确的镜像名称。然后再次进行验证:

  1. $ kubectl edit deployment nginx
  2. deployment.extensions/nginx edited
  3. $ kubedog rollout track deployment nginx
  4. # deploy/nginx added
  5. # deploy/nginx rs/nginx-dbddb74b8 added
  6. # deploy/nginx po/nginx-dbddb74b8-x4nkm added
  7. # deploy/nginx event: po/nginx-dbddb74b8-x4nkm Pulled: Successfully pulled image "nginx"
  8. # deploy/nginx event: po/nginx-dbddb74b8-x4nkm Created: Created container
  9. # deploy/nginx event: po/nginx-dbddb74b8-x4nkm Started: Started container
  10. # deploy/nginx event: ScalingReplicaSet: Scaled down replica set nginx-6cc78cbf64 to 0
  11. # deploy/nginx become READY
  12. $ echo $?
  13. 0

修改完成,重新运行 kubedog,会看到成功运行的情况,并且返回值也变成了 0。

follow

follow 命令的功能和 kubetail 的功能有少量重叠,可以用 Deployment/Job/Daemonset 等为单位,查看其中所有 Pod 的日志,例如前面用的 Nginx,如果有访问的话,就会看到如下结果:

  1. $ kubedog follow deployment nginx
  2. # deploy/nginx appears to be ready
  3. # deploy/nginx rs/nginx-6cc78cbf64 added
  4. # deploy/nginx new rs/nginx-dbddb74b8 added
  5. # deploy/nginx rs/nginx-dbddb74b8(new) po/nginx-dbddb74b8-x4nkm added
  6. # deploy/nginx rs/nginx-6cc54845d9 added
  7. # deploy/nginx event: ScalingReplicaSet: Scaled up replica set nginx-6cc54845d9 to 1
  8. # deploy/nginx rs/nginx-6cc54845d9(new) po/nginx-6cc54845d9-nhlvs added
  9. # deploy/nginx event: po/nginx-6cc54845d9-nhlvs Pulling: pulling image "nginx:alpine"
  10. # deploy/nginx event: po/nginx-6cc54845d9-nhlvs Pulled: Successfully pulled image "nginx:alpine"
  11. # deploy/nginx event: po/nginx-6cc54845d9-nhlvs Created: Created container
  12. # deploy/nginx event: po/nginx-6cc54845d9-nhlvs Started: Started container
  13. # deploy/nginx event: ScalingReplicaSet: Scaled down replica set nginx-dbddb74b8 to 0
  14. # deploy/nginx become READY
  15. # deploy/nginx event: po/nginx-dbddb74b8-x4nkm Killing: Killing container with id docker://nginx:Need to kill Pod
  16. >> deploy/nginx rs/nginx-dbddb74b8 po/nginx-dbddb74b8-x4nkm nginx
  17. >> deploy/nginx rs/nginx-6cc54845d9(new) po/nginx-6cc54845d9-nhlvs nginx
  18. 127.0.0.1 - - [02/Jun/2019:11:35:08 +0000] "GET / HTTP/1.1" 200 612 "-" "Wget" "-"
  19. 127.0.0.1 - - [02/Jun/2019:11:35:11 +0000] "GET / HTTP/1.1" 200 612 "-" "Wget" "-"

multitrack

官方不建议使用follow和rollout模式,后续也不会更新;而推荐使用multitrack则直观、易读的方式来展示状态信息。
要使用multitrack,您需要将JSON结构传递给kubedog的stdin,如下:

  1. cat << EOF | kubedog multitrack
  2. {
  3. "Deployments": [
  4. {
  5. "ResourceName": "helloworld",
  6. "Namespace": "test"
  7. }
  8. ],
  9. "Deployments": [
  10. {
  11. "ResourceName": "helloworld1",
  12. "Namespace": "test"
  13. }
  14. ]
  15. }
  16. EOF
  17. echo '{"Deployments": [{"ResourceName": "helloworld","Namespace": "test"}],"Deployments": [{"ResourceName": "helloworld1","Namespace": "test"}]}' | kubedog multitrack

我们在做CI/CD的时候,无论是通过Kubernetes CLI还是Kubernetes Continuous Deploy插件,在应用yaml后无法检查资源是否部署成功,只能通过kubectl手动检查,因此我们可以使用kubedog在pipeline完成这最后一步验证。