kubedog:解决K8S中资源跟踪问题

文章目录
[隐藏]

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

1. rollout track

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

例如使用 kubectl run 命令创建一个新的 Deployment 资源,并使用 kubedog 跟进创建进程:

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

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

$ kubectl edit deployment nginx
deployment.extensions/nginx edited
$ kubedog rollout track deployment nginx
# deploy/nginx added
# deploy/nginx rs/nginx-dbddb74b8 added
# deploy/nginx po/nginx-dbddb74b8-x4nkm added
# deploy/nginx event: po/nginx-dbddb74b8-x4nkm Pulled: Successfully pulled image "nginx"
# deploy/nginx event: po/nginx-dbddb74b8-x4nkm Created: Created container
# deploy/nginx event: po/nginx-dbddb74b8-x4nkm Started: Started container
# deploy/nginx event: ScalingReplicaSet: Scaled down replica set nginx-6cc78cbf64 to 0
# deploy/nginx become READY
$ echo $?
0

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

2. follow

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

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

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

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

原文出处:coolops -> https://www.coolops.cn/posts/kubernetes-kubedog/

打赏

如果此文对你有所帮助,请随意打赏鼓励作者^_^