【问题标题】:kubectl: how to display pod logs without specyfing the pod name explicitly?kubectl:如何在不明确指定 pod 名称的情况下显示 pod 日志?
【发布时间】:2019-12-01 08:12:41
【问题描述】:

我的 pod 有一个动态生成的 ID 附加到它们的名称后,例如 my-app-name-7b587cd75b-dscsr,这在每次部署时都不同(下一次可能是 my-app-name-xcgv83bfsd-4kjsf)。

这使得使用某些命令非常麻烦,因为每次我需要查看日志时,我都必须先列出所有 pod,然后将更改后的名称复制粘贴到 logs 命令:kubectl -n [namespace] logs my-app-name-7b587cd75b-dscsr

有没有一种方法可以让我跳过使用 pod 名称或名称的一部分,而像在 port-forward 命令中那样执行 kubectl -n [namespace] logs my-pod-name-~kubectl -n [namespace] logs service/my-pod-name 之类的操作?

我尝试在logs 命令中注入grep 以获取pod 名称并在单个命令中运行logs,但是Windows 上的Cmder 似乎不支持@987654333 @:kubectl -n [namespace] logs $(kubectl -n my-app-name get pod | grep my-app-name | sed 's/ .*//')

【问题讨论】:

  • 我上次看这个时发现 kubetail 很方便,但它是 bash 的东西,看起来你是 Windows。有几个在 Windows/WSL 上运行的 bash shell。 YMMV。
  • 您好,您可以使用kubectl logs -f deploy/DEPLOYMENT_NAME 来获取 pod 的日志
  • 您也可以使用Stern - 您只需从 pod 名称中指定一些字符,它就会为您提供所有 pod 的日志

标签: bash kubernetes kubectl cmder


【解决方案1】:

您可以使用 Deployment/$DEPLOYMENT_NAME 而不是使用 POD/$POD_NAME 来获取 pod 的日志

kubectl logs deployment/$DEPLOY_NAME


  # Return snapshot logs from container nginx-1 of a deployment named nginx
  kubectl logs deployment/nginx -c nginx-1

kubectl logs --help 将提供更多信息

【讨论】:

  • 不起作用。 λ kubectl logs deployment/stg-01-api 返回Error from server (NotFound): deployments.extensions "stg-01-api" not found
  • 但它适用于命名空间:kubectl -n [namespace] logs deployment/stg-01-api
  • 部署是命名空间资源,需要指定具体命名空间。
【解决方案2】:

为部署添加标签并使用标签选择器从匹配的 pod 中查找日志。

参考以下说明

master $ kubectl run webapp --image=nginx --port=80 --labels="app=web"
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/webapp created
master $

master $ kubectl get deploy
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
webapp   1/1     1            1           2m27s
master $

master $ kubectl get po -owide
NAME                      READY   STATUS    RESTARTS   AGE   IP          NODE     NOMINATED NODE   READINESS GATES
webapp-647c6cd6f4-pxr4g   1/1     Running   0          20s   10.44.0.1   node01   <none>           <none>
master $

master $ curl 10.44.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
master $


master $ kubectl logs -l app=web
10.32.0.1 - - [23/Jul/2019:10:07:39 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.47.0" "-"

【讨论】:

  • curl 与日志记录有什么关系?
  • 如果我无法控制应用的部署方式怎么办?
【解决方案3】:

以下命令将获取名称中包含指定子字符串(例如etcd)的第一个 pod 的日志。它也与命名空间无关。

pod=etcd && kubectl logs `kubectl get pods --all-namespaces | grep $pod | head -1 | awk '{print $2}'` -n `kubectl get pods --all-namespaces | grep $pod | head -1 | awk '{print $1}'`

首先检索包含子字符串的所有 pod,提取完整的 pod 名称和命名空间,然后调用 kubectl logs 并提供完整详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 2021-12-29
    • 1970-01-01
    • 2018-07-05
    • 2020-09-12
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多