最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用kubectl獲取pod日志小技巧分享

 更新時(shí)間:2026年02月25日 08:56:08   作者:lv2  
文章介紹了如何使用kubectl命令查看K8S中Pod的日志,包括獲取單個(gè)Pod日志、同一deployment下多個(gè)副本Pod的日志、最近xx行日志、最近一段時(shí)間的日志、上一個(gè)崩潰但還存在的Pod的日志、指定或所有容器的日志以及持續(xù)獲取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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • MinIO使用基礎(chǔ)教程(最新整理)

    MinIO使用基礎(chǔ)教程(最新整理)

    文章介紹了MinIO云存儲(chǔ)服務(wù)的快速安裝和使用,并通過SpringBoot實(shí)現(xiàn)文件上傳和查詢的功能,感興趣的朋友跟隨小編一起看看吧
    2025-03-03
  • 云原生Kubernetes初始化容器Init使用教程

    云原生Kubernetes初始化容器Init使用教程

    這篇文章主要為大家介紹了云原生Kubernetes初始化容器Init使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步早日升職加薪
    2022-03-03
  • k8s 中的 service 如何找到綁定的 Pod 及實(shí)現(xiàn) Pod 負(fù)載均衡的方法

    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?Affinity親和力詳解(調(diào)度策略)

    K8s?Affinity親和力詳解(調(diào)度策略)

    文章介紹了Kubernetes中親和力和反親和力的調(diào)度策略,包括節(jié)點(diǎn)和Pod的硬性、軟性規(guī)則,用于實(shí)現(xiàn)Pod與節(jié)點(diǎn)的標(biāo)簽匹配、區(qū)域均衡負(fù)載及避免單節(jié)點(diǎn)過載等場(chǎng)景,強(qiáng)調(diào)標(biāo)簽匹配和副本數(shù)限制對(duì)調(diào)度結(jié)果的影響
    2025-08-08
  • kubernetes k8s常用問題排查方法

    kubernetes k8s常用問題排查方法

    新手學(xué)習(xí)K8s最大的難度感覺是在起步動(dòng)手實(shí)踐的時(shí)候,Pod沒有正常啟動(dòng)起來,或者運(yùn)行了一段時(shí)間Pod自己崩潰了。是什么問題導(dǎo)致了它沒運(yùn)行起來,或是什么因素導(dǎo)致了它的崩潰,本文來學(xué)習(xí)總結(jié)幾個(gè)使用 K8s時(shí)常見的錯(cuò)誤現(xiàn)象以及排查這些現(xiàn)象背后問題的方法
    2022-06-06
  • k8s容器的內(nèi)存設(shè)置的踩坑記錄

    k8s容器的內(nèi)存設(shè)置的踩坑記錄

    這篇文章主要介紹了k8s容器的內(nèi)存設(shè)置的踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-07-07
  • 理解k8s控制器DaemonSet創(chuàng)建及使用場(chǎng)景

    理解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ò)策略是怎么樣的

    這篇文章主要為大家介紹了k8s?NetworkPolicy?網(wǎng)絡(luò)策略是怎么樣的深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • K8s中的臨時(shí)容器Ephemeral?Containers使用

    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)境配置

    這篇文章主要介紹了CentOS?8.2?k8s?基礎(chǔ)環(huán)境配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10

最新評(píng)論

天水市| 泾川县| 澳门| 成武县| 崇州市| 会泽县| 兰溪市| 巩义市| 浮山县| 同德县| 勃利县| 保亭| 乌苏市| 成武县| 稷山县| 紫金县| 东台市| 潢川县| 拉孜县| 大洼县| 灵宝市| 钦州市| 广东省| 乐昌市| 昌乐县| 微山县| 鄄城县| 朝阳县| 博客| 迁西县| 和静县| 天峻县| 定日县| 西平县| 石台县| 桦甸市| 黎城县| 米泉市| 鄢陵县| 桃园市| 天长市|