如何跟踪Kubernetes集群中的所有日志

hiz5n14c  于 2023-04-05  发布在  Kubernetes
关注(0)|答案(7)|浏览(106)

我试了这个命令:

kubectl logs --tail

我得到了这个错误/帮助输出:

Error: flag needs an argument: --tail

Aliases:
logs, log

Examples:
  # Return snapshot logs from pod nginx with only one container
  kubectl logs nginx

  # Return snapshot logs for the pods defined by label app=nginx
  kubectl logs -lapp=nginx

  # Return snapshot of previous terminated ruby container logs from pod web-1
  kubectl logs -p -c ruby web-1

  # Begin streaming the logs of the ruby container in pod web-1
  kubectl logs -f -c ruby web-1

  # Display only the most recent 20 lines of output in pod nginx
  kubectl logs --tail=20 nginx

  # Show all logs from pod nginx written in the last hour
  kubectl logs --since=1h nginx

  # Return snapshot logs from first container of a job named hello
  kubectl logs job/hello

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

我只是想查看所有的日志,这不是一件很常见的事情吗?我如何跟踪一个集群的所有日志?

9w11ddsr

9w11ddsr1#

kail仅适用于Linux和macOS,但Stern也适用于Windows。
它可以基于例如名称的正则表达式匹配来进行pod匹配,然后可以遵循日志。
要跟踪所有pod而不打印default命名空间中的任何先前日志,您可以运行例如:

stern ".*" --tail 0

对于所有的事情,包括在kube-system命名空间中发生的内部事情:

stern ".*" --all-namespaces --tail 0

或者,您可以跟踪所有login-.*容器,并使用

stern "login-.*" --tail 25
x3naxklr

x3naxklr2#

如果您不介意使用第三方工具,kail完全可以实现您所描述的功能。
从所有匹配的pod的所有容器中流出日志。[...]在没有参数的情况下,kail匹配集群中的所有pod。

ma8fv8wu

ma8fv8wu3#

你唯一能做的就是使用标签选择器获取多个pod的日志,如下所示:

kubectl logs -f -l app=nginx -l app=php

要获取整个集群的所有日志,您必须设置集中式日志收集,如Elasticsearch,Fluentd和Kibana。最简单的方法是使用Helm charts安装,如下所述:https://linux-admin.tech/kubernetes/logging/2018/10/24/elk-stack-installation.html

bejyjqdl

bejyjqdl4#

我推荐使用一个名为kubetail的bash脚本。
您可以下载bash script并将其添加到您的项目中,然后运行例如:

$ ./some-tools-directory/kubetail.sh --selector app=user --since 10m

查看标签为app=user的所有Pod。
请注意每个pod的颜色显示:

(*)运行./tools/kubetail.sh -h以查看一些不错的执行选项。

kubetail.sh <search term> [-h] [-c] [-n] [-t] [-l] [-d] [-p] [-s] [-b] [-k] [-v] [-r] [-i] -- tail multiple Kubernetes pod logs at the same time

where:
    -h, --help              Show this help text
    -c, --container         The name of the container to tail in the pod (if multiple containers are defined in the pod).
                            Defaults to all containers in the pod. Can be used multiple times.
    -t, --context           The k8s context. ex. int1-context. Relies on ~/.kube/config for the contexts.
    -l, --selector          Label selector. If used the pod name is ignored.
    -n, --namespace         The Kubernetes namespace where the pods are located (defaults to "default")
    -f, --follow            Specify if the logs should be streamed. (true|false) Defaults to true.
    -d, --dry-run           Print the names of the matched pods and containers, then exit.
    -p, --previous          Return logs for the previous instances of the pods, if available. (true|false) Defaults to false.
    -s, --since             Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to 10s.
    -b, --line-buffered     This flags indicates to use line-buffered. Defaults to false.
    -e, --regex             The type of name matching to use (regex|substring)
    -j, --jq                If your output is json - use this jq-selector to parse it.
                            example: --jq ".logger + \" \" + .message"
    -k, --colored-output    Use colored output (pod|line|false).
                            pod = only color pod name, line = color entire line, false = don't use any colors.
                            Defaults to line.
    -z, --skip-colors       Comma-separated list of colors to not use in output
                            If you have green foreground on black, this will skip dark grey and some greens -z 2,8,10
                            Defaults to: 7,8
        --timestamps        Show timestamps for each log line
        --tail              Lines of recent log file to display. Defaults to -1, showing all log lines.
    -v, --version           Prints the kubetail version
    -r, --cluster           The name of the kubeconfig cluster to use.
    -i, --show-color-index  Show the color index before the pod name prefix that is shown before each log line.
                                                Normally only the pod name is added as a prefix before each line, for example "[app-5b7ff6cbcd-bjv8n]",
                                                but if "show-color-index" is true then color index is added as well: "[1:app-5b7ff6cbcd-bjv8n]".
                            This is useful if you have color blindness or if you want to know which colors to exclude (see "--skip-colors").
                                Defaults to false.

examples:
    kubetail.sh my-pod-v1
    kubetail.sh my-pod-v1 -c my-container
    kubetail.sh my-pod-v1 -t int1-context -c my-container
    kubetail.sh '(service|consumer|thing)' -e regex
    kubetail.sh -l service=my-service
    kubetail.sh --selector service=my-service --since 10m
    kubetail.sh --tail 1
ia2d9nvy

ia2d9nvy5#

我几乎没有见过有人从整个集群中提取所有日志,因为你通常需要日志来手动搜索某些问题或遵循(-f)例程,或收集审计信息,或将所有日志流到日志接收器以备监视(例如prometheus)。
但是,如果需要获取所有日志,则使用--tail选项并不是您想要的(tail仅显示特定日志源的最后行数,并避免将单个日志源的整个日志历史溢出到您的终端)。
对于kubernetes,您可以使用自己选择的语言(bash、Python等)编写一个简单的脚本到kubectl get all --show-all --all-namespaces,并迭代pod以运行kubectl -n <namespace> logs <pod>;但是要注意,在一个Pod中可能有多个容器,每个容器都有单独的日志,并且还记录集群节点本身、部署中的状态更改、更改的额外 meta信息、卷供应和堆等。
这可能就是为什么从整个集群中提取所有日志非常罕见的原因,因此没有简单(快捷)的方法来做到这一点。

z0qdvdin

z0qdvdin6#

# assumes you have pre-set the KUBECONFIG or using the default one ...
do_check_k8s_logs(){
  
  # set the desired namespaces here vvvv
  for namespace in `echo apiv2 default kube-system`; do
    while read -r pod ; do
      while read -r container ; do
        kubectl -n $namespace logs $pod $container | tail -n 2000
      done < <(kubectl -n $namespace get pods -o json | jq -r ".items[]|select(.metadata.name | contains ( \"$pod\"))| .status.containerStatuses[].name") ;
    done < <(kubectl -n $namespace get pods -o json | jq -r '.items[].metadata.name') \
        | tee -a  ~/Desktop/k8s-$namespace-logs.`date "+%Y%m%d_%H%M%S"`.log
  done

}

do_check_k8s_logs
r1zk6ea1

r1zk6ea17#

对于应用程序数据,您可能只想跟踪集群中的所有pod。
但是如果你想要一个集群的 * 控制平面 * 的日志-你可以用途:https://aws.amazon.com/about-aws/whats-new/2019/04/amazon-eks-now-delivers-kubernetes-control-plane-logs-to-amazon-/

相关问题