k8s 实战 - kubectl logs - 外传
通过执行“kubectl logs -h” ,可以查看kubectl logs的帮助信息。kubectl logs 可以协助排查容器内pod或者是其他资源的问题,它通过打印日志的形式输出。在minikube实战篇中都有关于kubectl logs的使用,它可以输出pod的日志信息,定位问题。以下是logs的帮助信息,我主要翻译一下及简单的使用说明。
·
kubectl logs
使用
通过执行
kubectl logs -h
,可以查看kubectl logs的帮助信息。kubectl logs 可以协助排查容器内pod或者是其他资源的问题,它通过打印日志的形式输出。在minikube实战篇
中都有关于kubectl logs的使用,它可以输出pod的日志信息,定位问题。以下是logs的帮助信息,我主要翻译一下及简单的使用说明。
1、kubectl logs
使用说明
C:\Users\halo\Desktop>kubectl logs -h
# 打印pod中容器或指定资源的日志。如果pod只有一个容器,则容器名称为可选的。
Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is
optional.
# 示例:
Examples:
# 从只有一个容器的pod nginx返回快照日志
# Return snapshot logs from pod nginx with only one container
kubectl logs nginx
# 从多容器的pod nginx返回快照日志
# Return snapshot logs from pod nginx with multi containers
kubectl logs nginx --all-containers=true
# 从label app=nginx定义的pods中的所有容器返回快照日志
# Return snapshot logs from all containers in pods defined by label app=nginx
kubectl logs -l app=nginx --all-containers=true
# 从pod web-1返回先前终止的ruby容器日志的快照
# Return snapshot of previous terminated ruby container logs from pod web-1
kubectl logs -p -c ruby web-1
# 开始在pod web-1中传输ruby容器的日志流
# Begin streaming the logs of the ruby container in pod web-1
kubectl logs -f -c ruby web-1
# 开始从label app=nginx定义的pods中所有容器的日志流
# Begin streaming the logs from all containers in pods defined by label app=nginx
kubectl logs -f -l app=nginx --all-containers=true
# 在pod nginx中只显示最近的20行输出
# Display only the most recent 20 lines of output in pod nginx
kubectl logs --tail=20 nginx
# 显示过去一小时内从pod nginx写入的所有日志
# Show all logs from pod nginx written in the last hour
kubectl logs --since=1h nginx
# 显示包含过期服务证书的kubelet日志
# Show logs from a kubelet with an expired serving certificate
kubectl logs --insecure-skip-tls-verify-backend nginx
# 从一个叫hello的任务的第一个容器返回快照日志
# Return snapshot logs from first container of a job named hello
kubectl logs job/hello
# 返回一个名为nginx部署的容器nginx-1的快照日志
# Return snapshot logs from container nginx-1 of a deployment named nginx
kubectl logs deployment/nginx -c nginx-1
# 选项:
Options:
--all-containers=false:
# 获取pods中所有容器的日志
Get all containers' logs in the pod(s).
-c, --container='':
# 打印此容器的日志
Print the logs of this container
-f, --follow=false:
# 指定是否应该持续输出日志
Specify if the logs should be streamed.
--ignore-errors=false:
# 如果监视/跟踪pod日志,则允许出现任何非致命错误
If watching / following pod logs, allow for any errors that occur to be non-fatal
--insecure-skip-tls-verify-backend=false:
# 跳过验证发出日志请求的kubelet的身份。理论上,攻击者可以提供返回的日志内容无效。如果您的kubelet服务证书已经过期,您可能想使用这个。
Skip verifying the identity of the kubelet that logs are requested from. In theory, an attacker could provide
invalid log content back. You might want to use this if your kubelet serving certificates have expired.
--limit-bytes=0:
# 返回的日志的最大字节数。默认为无限制
Maximum bytes of logs to return. Defaults to no limit.
--max-log-requests=5:
# 指定选择器使用时要跟随的最大并发日志数。默认为5
Specify maximum number of concurrent logs to follow when using by a selector. Defaults to 5.
--pod-running-timeout=20s:
# 等待的时间长度(如5s、2m或3h,高于零),直到至少一个pod运行
The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one pod is running
--prefix=false:
# 在每个日志行前加上日志源(pod名称和容器名称)
Prefix each log line with the log source (pod name and container name)
-p, --previous=false:
# 如果为真,则打印pod中容器的前一个实例的日志(如果它存在)
If true, print the logs for the previous instance of the container in a pod if it exists.
-l, --selector='':
# 选择器(标签查询)过滤,支持'=','=='和'!='。- l key1 = value1, key2 = value2)。匹配的对象必须满足所有指定的标签约束。
Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching
objects must satisfy all of the specified label constraints.
--since=0s:
# 只返回相对时间范围内,(如5s、2m或3h之内)更新的日志。默认为所有日志。只能使用since-time / since中的一个。
Only return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to all logs. Only one of
since-time / since may be used.
--since-time='':
# 只返回特定日期之后的日志(RFC3339)。默认为所有日志。只能使用since-time / since中的一个。
Only return logs after a specific date (RFC3339). Defaults to all logs. Only one of since-time / since may be
used.
--tail=-1:
# 要显示的最近日志文件的行数。默认为-1,没有选择器,如果提供了选择器,则显示所有日志行,否则显示10。
Lines of recent log file to display. Defaults to -1 with no selector, showing all log lines otherwise 10, if a
selector is provided.
--timestamps=false:
# 在日志输出的每一行中包含时间戳
Include timestamps on each line in the log output
Usage:
kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER] [options]
# 对全局命令行选项列表使用“kubectl options”(适用于所有命令)。
Use "kubectl options" for a list of global command-line options (applies to all commands).
2、kubectl logs
示例
下面是介绍一下’kubectl logs’简单的使用说明:
执行参数之前:
C:\Users\halo\Desktop>kubectl logs hello-minikube-5c494f986c-zwqsq --tail=10
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:12 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:12 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:12 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:12 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:13 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:13 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:14 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:14 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:14 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
::ffff:192.168.65.3 - - [02/Sep/2022:08:01:14 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
执行参数之后,(查看日志的时候追加了--timestamps=true --prefix=true
):
C:\Users\halo\Desktop>kubectl logs hello-minikube-5c494f986c-zwqsq --timestamps=true --prefix=true --tail=10
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:12.143434200Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:12 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:12.220710200Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:12 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:12.909611000Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:12 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:12.997047200Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:12 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:13.685898700Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:13 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:13.780309600Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:13 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:14.135455600Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:14 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:14.231313300Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:14 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:14.729768600Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:14 +0000] "GET / HTTP/1.1" 200 877 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:14.823172400Z ::ffff:192.168.65.3 - - [02/Sep/2022:08:01:14 +0000] "GET /favicon.ico HTTP/1.1" 200 811 "http://192.168.10.27:30192/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70"
从上面的执行参数之前和执行参数之后,查看日志变化效果如下,执行参数之后,日志追加内容:
[pod/hello-minikube-5c494f986c-zwqsq/echoserver] 2022-09-02T08:01:12.143434200Z
从内容可知,日志新增了pod信息及时间信息,由使用说明描述,时间信息是--timestamps=true
的执行效果,pod信息是--prefix=true
的执行效果。--tail=10
表示只输出最近10行日志
更多推荐
已为社区贡献9条内容
所有评论(0)