Kubernetes中查看日志的方法
一、查看集群事件
[root@master01 ~]# kubectl get events LAST SEEN TYPE REASON OBJECT MESSAGE 45s Normal CREATE ingress/example-ingress Ingress default/example-ingress node/master01
二、kubectl describe 来查看节点或者pod的日志
[root@master01 ~]# kubectl describe node master01 Name: master01 Roles: master Labels: beta.kubernetes.io/arch=amd64 beta.kubernetes.io/os=linux kubernetes.io/arch=amd64 kubernetes.io/hostname=master01 kubernetes.io/os=linux node-role.kubernetes.io/master= Annotations: kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock node.alpha.kubernetes.io/ttl: 0 projectcalico.org/IPv4Address: 192.168.43.90/24 projectcalico.org/IPv4IPIPTunnelAddr: 10.224.241.64 volumes.kubernetes.io/controller-managed-attach-detach: true CreationTimestamp: Thu, 25 May 2023 10:10:15 +0800 Taints: node-role.kubernetes.io/master:NoSchedule Unschedulable: false
查看pod
[root@master01 ~]# kubectl describe pod maria-6d46559b5c-tjhfb Name: maria-6d46559b5c-tjhfb Namespace: default Priority: 0 Node: <none> Labels: app=maria pod-template-hash=6d46559b5c Annotations: <none> Status: Pending IP: IPs: <none> Controlled By: ReplicaSet/maria-6d46559b5c
三、kubectl logs查看pod日志
[root@master01 ~]# kubectl logs -f weave-scope-cluster-agent-myui-75859b9bc4-8wgbz time="2023-09-07T03:17:23Z" level=info msg="publishing to: myui-weave-scope.default.svc:80" <probe> INFO: 2023/09/07 03:17:23.710375 Basic authentication disabled <probe> INFO: 2023/09/07 03:17:38.861462 command line args: --mode=probe --probe-only=true --probe.kubernetes.role=cluster myui-weave-scope.default.svc:80 <probe> INFO: 2023/09/07 03:17:38.861515 probe starting, version 1.12.0, ID 69b7e92b5eaf4c03 <probe> ERRO: 2023/09/07 03:17:38.862176 Error getting docker bridge ip: route ip+net: no such network interface
--tail 参数查看最后日志
[root@master01 ~]# kubectl logs --tail 100 weave-scope-cluster-agent-myui-75859b9bc4-8wgbz time="2023-09-07T03:17:23Z" level=info msg="publishing to: myui-weave-scope.default.svc:80" <probe> INFO: 2023/09/07 03:17:23.710375 Basic authentication disabled <probe> INFO: 2023/09/07 03:17:38.861462 command line args: --mode=probe --probe-only=true --probe.kubernetes.role=cluster myui-weave-scope.default.svc:80 <probe> INFO: 2023/09/07 03:17:38.861515 probe starting, version 1.12.0, ID 69b7e92b5eaf4c03 <probe> ERRO: 2023/09/07 03:17:38.862176 Error getting docker bridge ip: route ip+net: no such network interface <probe> INFO: 2023/09/07 03:17:38.876678 kubernetes: targeting api server https://10.96.0.1:443 <probe> ERRO: 2023/09/07 03:17:38.902146 plugins: problem loading: no such file or directory
--since=1h查看最近一小时的日志
[root@master01 ~]# kubectl logs --since=1h weave-scope-cluster-agent-myui-75859b9bc4-8wgbz
四、journalctl查看系统组件的日志
[root@master01 ~]# journalctl -u kubelet -- Logs begin at Thu 2023-09-07 11:14:07 CST, end at Thu 2023-09-07 11:36:57 CST. -- Sep 07 11:14:33 master01 systemd[1]: Started kubelet: The Kubernetes Node Agent. Sep 07 11:14:52 master01 kubelet[748]: Flag --cgroup-driver has been deprecated, This parameter shoul Sep 07 11:14:52 master01 kubelet[748]: Flag --cgroup-driver has been deprecated, This parameter shoul Sep 07 11:14:54 master01 kubelet[748]: Flag --cgroup-driver has been deprecated, This parameter shoul Sep 07 11:14:54 master01 kubelet[748]: Flag --cgroup-driver has been deprecated, This parameter shoul Sep 07 11:14:54 master01 kubelet[748]: I0907 11:14:54.549551 748 server.go:417] Version: v1.18.0 Sep 07 11:14:54 master01 kubelet[748]: I0907 11:14:54.578569 748 plugins.go:100] No cloud provide Sep 07 11:14:54 master01 kubelet[748]: I0907 11:14:54.593792 748 server.go:837] Client rotation i Sep 07 11:14:54 master01 kubelet[748]: I0907 11:14:54.784634 748 certificate_store.go:130] Loadin Sep 07 11:15:35 master01 kubelet[748]: I0907 11:15:35.578321 748 server.go:646] --cgroups-per-qos
查看其他组件
[root@master01 ~]# journalctl -u kube-apiserver [root@master01 ~]# journalctl -u kube-etcd [root@master01 ~]# journalctl -u kube-apiserver