使用kubectl獲取pod日志小技巧分享
1. 前言
如何查看k8s中pod的console控制臺(tái)日志?即類似于docker logs查看容器日志一樣;可以使用 kubectl 命令,查看K8S中 Pod的日志。
在這里,將通過kubectl獲取 Pod 的日志,包括當(dāng)前運(yùn)行、同一deployment下所有副本的日志。
2. kubectl logs
2.1 創(chuàng)建示例
創(chuàng)建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 獲取單個(gè)pod日志
語(yǔ)法: 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下多個(gè)副本pod的日志
語(yǔ)法: 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 # 獲取多個(gè)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行日志
語(yǔ)法: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 獲取最近一段時(shí)間的日志
語(yǔ)法:kubectl logs --since=1h/m <pod>
# 獲取最近1分鐘/小時(shí)的日志 $ 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
獲取上一個(gè)崩潰但還存在的pod的日志,說實(shí)在的,不是很懂,官方的解釋是:--previous=false: If true, print the logs for the previous instance of the container in a pod if it exists.
意思是:上一個(gè)崩潰過,但還存在的實(shí)例的日志。暫無(wú)場(chǎng)景重現(xiàn)。略過
2.7 獲取pod中指定或所有容器的日志
查看指定容器日志 語(yǔ)法: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
查看同一個(gè)Pod中所有容器日志 語(yǔ)法:kubectl logs <pod> --all-containers
$ kubectl logs my-dep-5b7868d854-8d5kf --all-containers
2.8 持續(xù)獲取Pod日志
語(yǔ)法:kubectl logs -f <pod>
$ kubectl logs -f my-dep-5b7868d854-8d5kf
3. 附官方命令解釋
kubectl logs --help
-c, --container="": 容器名。
-f, --follow[=false]: 指定是否持續(xù)輸出日志。
--interactive[=true]: 如果為true,當(dāng)需要時(shí)提示用戶進(jìn)行輸入。默認(rèn)為true。
--limit-bytes=0: 輸出日志的最大字節(jié)數(shù)。默認(rèn)無(wú)限制。
-p, --previous[=false]: 如果為true,輸出pod中曾經(jīng)運(yùn)行過,但目前已終止的容器的日志。
--since=0: 僅返回相對(duì)時(shí)間范圍,如5s、2m或3h,之內(nèi)的日志。默認(rèn)返回所有日志。只能同時(shí)使用since和since-time中的一種。
--since-time="": 僅返回指定時(shí)間(RFC3339格式)之后的日志。默認(rèn)返回所有日志。只能同時(shí)使用since和since-time中的一種。
--tail=-1: 要顯示的最新的日志條數(shù)。默認(rèn)為-1,顯示所有的日志。
--timestamps[=false]: 在日志中包含時(shí)間戳。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Kubernetes kubectl中Pod創(chuàng)建流程源碼解析
- kubectl port-forward的踩坑過程及解決
- k8s kubectl啟動(dòng)成功,但執(zhí)行命令時(shí)報(bào)錯(cuò)解決:The connection to the server localhost:8080 was refused - did you specify
- 解決k8s kubectl啟動(dòng)失敗Unit kubelet.service entered failed state.問題
- K8s使用kubectl訪問api-server失敗,node“XXX“?not?found的解決方案
相關(guān)文章
k8s 中的 service 如何找到綁定的 Pod 及實(shí)現(xiàn) 
service 是一組具有相同 label pod 集合的抽象,集群內(nèi)外的各個(gè)服務(wù)可以通過 service 進(jìn)行互相通信,這篇文章主要介紹了k8s 中的 service 如何找到綁定的 Pod 以及如何實(shí)現(xiàn) Pod 負(fù)載均衡,需要的朋友可以參考下2022-10-10
理解k8s控制器DaemonSet創(chuàng)建及使用場(chǎng)景
這篇文章主要為大家介紹了k8s控制器DaemonSet創(chuàng)建及使用場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
詳解k8s?NetworkPolicy?網(wǎng)絡(luò)策略是怎么樣的
這篇文章主要為大家介紹了k8s?NetworkPolicy?網(wǎng)絡(luò)策略是怎么樣的深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
K8s中的臨時(shí)容器Ephemeral?Containers使用
這篇文章主要介紹了K8s中的臨時(shí)容器Ephemeral?Containers使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
CentOS?8.2?k8s?基礎(chǔ)環(huán)境配置
這篇文章主要介紹了CentOS?8.2?k8s?基礎(chǔ)環(huán)境配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10

