使用kubectl获取pod日志小技巧

目录

1. 前言

2. kubectl logs

3. 附官方命令解释


1. 前言

如何查看k8s中pod的console控制台日志?即类似于docker logs查看容器日志一样;可以使用 kubectl 命令,查看K8S中 Pod的日志。 在这里,将通过kubectl获取 Pod 的日志,包括当前运行、同一deployment下所有副本的日志。

2. kubectl logs

2.1 创建示例

创建nginx deployment,副本为2

 $ kubectl create deployment my-dep --image=nginx --replicas=2
 $ kubectl get pod 
NAME                      READY   STATUS    RESTARTS   AGE
my-dep-5b7868d854-8d5kf   1/1     Running   0          28m
my-dep-5b7868d854-q6lj7   1/1     Running   0          21m

2.2 获取单个pod日志

语法: kubectl logs <pod>

# 示例
$ kubectl logs my-dep-5b7868d854-8d5kf 
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/07/26 01:18:56 [notice] 1#1: using the "epoll" event method

2.3 获取同一deployment下多个副本pod的日志

语法: kubectl logs -l <key>=<value>

# 示例
# 获取pod labels 
$ kubectl get pod --show-labels
NAME                      READY   STATUS    RESTARTS   AGE   LABELS
my-dep-5b7868d854-8d5kf   1/1     Running   0          33m   app=my-dep,pod-template-hash=5b7868d854
my-dep-5b7868d854-q6lj7   1/1     Running   0          26m   app=my-dep,pod-template-hash=5b7868d854

# 获取多个Pod日志
$ kubectl logs -l app=my-dep
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/07/26 01:18:56 [notice] 1#1: using the "epoll" event method
2022/07/26 01:18:56 [notice] 1#1: nginx/1.23.1
2022/07/26 01:18:56 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/07/26 01:18:56 [notice] 1#1: OS: Linux 4.15.0-122-generic
2022/07/26 01:18:56 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/07/26 01:18:56 [notice] 1#1: start worker processes
2022/07/26 01:18:56 [notice] 1#1: start worker process 31
2022/07/26 01:18:56 [notice] 1#1: start worker process 32
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/07/26 01:25:33 [notice] 1#1: using the "epoll" event method
2022/07/26 01:25:33 [notice] 1#1: nginx/1.23.1
2022/07/26 01:25:33 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/07/26 01:25:33 [notice] 1#1: OS: Linux 4.15.0-122-generic
2022/07/26 01:25:33 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/07/26 01:25:33 [notice] 1#1: start worker processes
2022/07/26 01:25:33 [notice] 1#1: start worker process 30
2022/07/26 01:25:33 [notice] 1#1: start worker process 31

2.4 获取pod最近xx行日志

语法:kubectl logs --tail=xx <pod>

# 获取pod最近5行日志
$ kubectl logs --tail=5 my-dep-5b7868d854-8d5kf 
2022/07/26 01:18:56 [notice] 1#1: OS: Linux 4.15.0-122-generic
2022/07/26 01:18:56 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/07/26 01:18:56 [notice] 1#1: start worker processes
2022/07/26 01:18:56 [notice] 1#1: start worker process 31
2022/07/26 01:18:56 [notice] 1#1: start worker process 32

2.5 获取最近一段时间的日志

语法:kubectl logs --since=1h/m <pod>

# 获取最近1分钟/小时的日志
$ kubectl logs my-dep-5b7868d854-8d5kf --since=1m
$ kubectl logs my-dep-5b7868d854-8d5kf --since=1h

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/07/26 01:18:56 [notice] 1#1: using the "epoll" event method
2022/07/26 01:18:56 [notice] 1#1: nginx/1.23.1
2022/07/26 01:18:56 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/07/26 01:18:56 [notice] 1#1: OS: Linux 4.15.0-122-generic
2022/07/26 01:18:56 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/07/26 01:18:56 [notice] 1#1: start worker processes
2022/07/26 01:18:56 [notice] 1#1: start worker process 31
2022/07/26 01:18:56 [notice] 1#1: start worker process 32

2.6 kubectl logs --previous

获取上一个崩溃但还存在的pod的日志,说实在的,不是很懂,官方的解释是:--previous=false: If true, print the logs for the previous instance of the container in a pod if it exists. 意思是:上一个崩溃过,但还存在的实例的日志。暂无场景重现。略过

2.7 获取pod中指定或所有容器的日志

查看指定容器日志 语法:kubectl logs <pod> -c <contianer>

# 获取pod中容器名
$ kubectl get pods my-dep-5b7868d854-8d5kf -o jsonpath={.spec.containers[*].name}
nginx
# 获取指定容器日志
 kubectl logs my-dep-5b7868d854-8d5kf -c nginx

查看同一个Pod中所有容器日志 语法:kubectl logs <pod> --all-containers

$ kubectl logs my-dep-5b7868d854-8d5kf --all-containers 

2.8 持续获取Pod日志

语法:kubectl logs -f <pod>

$ kubectl logs -f my-dep-5b7868d854-8d5kf  

3. 附官方命令解释

kubectl logs --help

 -c, --container="": 容器名。
  -f, --follow[=false]: 指定是否持续输出日志。
      --interactive[=true]: 如果为true,当需要时提示用户进行输入。默认为true。
      --limit-bytes=0: 输出日志的最大字节数。默认无限制。
  -p, --previous[=false]: 如果为true,输出pod中曾经运行过,但目前已终止的容器的日志。
      --since=0: 仅返回相对时间范围,如5s、2m或3h,之内的日志。默认返回所有日志。只能同时使用since和since-time中的一种。
      --since-time="": 仅返回指定时间(RFC3339格式)之后的日志。默认返回所有日志。只能同时使用since和since-time中的一种。
      --tail=-1: 要显示的最新的日志条数。默认为-1,显示所有的日志。
      --timestamps[=false]: 在日志中包含时间戳。
      

觉得有用的点个关注和收藏吧~ 

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值