Skip to main content

Kubernetes Monitoring & Logs: Monitor Applications with top, Monitor Events Pod specific and Cluster wide, Container STDOUT and STDERR Logs

614 words·
Kubernetes Kubectl Kubernetes Metrics Server
Table of Contents
Kubernetes-Components - This article is part of a series.
Part 8: This Article

Monitoring: Monior Applications with top, Monitor Events Pod specific and Cluster wide, Container STDOUT and STDERR Logs
#

Create Example Multi-Container Pod
#

Alternative run a pod with several containers

# Create a manifest for the pod
vi example-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
    - name: nginx
      image: nginx
    - name: busybox
      image: busybox
      command: ["sleep", "3600"]
# Create the pod
kubectl create -f example-pod.yaml

Monitoring the Resource Usage
#

Pod & Containers Inside Pod
#

# List pod metrics
kubectl top pod example-pod

# Shell output:
NAME          CPU(cores)   MEMORY(bytes)
example-pod   0m           4Mi
# List pod metrics: For containers inside the pod
kubectl top pod example-pod --containers

# Shell output:
POD           NAME      CPU(cores)   MEMORY(bytes)
example-pod   busybox   0m           0Mi
example-pod   nginx     0m           4Mi

Namespace Specific
#

# List pod metrics: Specific namespace
kubectl top pod -n default

# List pod metrics: All namespaces
kubectl top pod -A

Sort by CPU or Memory
#

# List pod metrics: Sort by CPU usage (default namespace)
kubectl top pod --sort-by=cpu

# List pod metrics: Sort by memory usage (default namespace)
kubectl top pod --sort-by=memory

Example: List all Pods, Sorted by CPU Consumption, Output to File
#

# List all pods in the cluster, sorted by highest CPU consumption, save to file
kubectl top pod -A --sort-by=cpu > max-cpu-pods



Kubernetes Events
#

Pod Specific
#

# List pod details
kubectl describe pod example-pod

# Shell output:
...
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  15s   default-scheduler  Successfully assigned default/example-pod to ubuntu3
  Normal  Pulling    15s   kubelet            Pulling image "nginx"
  Normal  Pulled     13s   kubelet            Successfully pulled image "nginx" in 1.022s (1.022s including waiting). Image size: 72955450 bytes.
  Normal  Created    13s   kubelet            Created container nginx
  Normal  Started    13s   kubelet            Started container nginx
  Normal  Pulling    13s   kubelet            Pulling image "busybox"
  Normal  Pulled     13s   kubelet            Successfully pulled image "busybox" in 930ms (930ms including waiting). Image size: 2166802 bytes.
  Normal  Created    12s   kubelet            Created container busybox
  Normal  Started    12s   kubelet            Started container busybox

Namespace & Cluster Wide
#

Watch & Sort
#

# List events: In default namespace
kubectl get events

# Follow events
kubectl get events --watch
# List events: Sort by creation time
kubectl get events --sort-by=.metadata.creationTimestamp

Specific Namespace
#

# List events: Specific namespace
kubectl get events -n namespace-name

# List events: All namespaces
kubectl get events -A



Kubernetes Logs
#

Example Pod
#

The following Busybox container container1 will print the date every second to the standard output stream STDOUT:

# Create a manifest for the pod
vi log-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: log-pod
spec:
  containers:
    - name: container1
      image: busybox:1.28
      args:
        - /bin/sh
        - -c
        - 'while true; do echo "$(date)"; sleep 1; done'
    - name: nginx
      image: nginx:latest
# Create the pod
kubectl create -f log-pod.yaml

Container STDOUT and STDERR Logs
#

List Logs of Pod and Specific Container
#

# List logs of a pod
kubectl logs log-pod

# List logs of a pod: Specific container
kubectl logs log-pod -c container1
# Follow the logs of a pod
kubectl logs -f log-pod

Sort with Time
#

# List logs of a pod: Last 10 seconds
kubectl logs log-pod --since=10s

# Shell output:
Tue Nov 12 11:47:25 UTC 2024
Tue Nov 12 11:47:26 UTC 2024
Tue Nov 12 11:47:27 UTC 2024
Tue Nov 12 11:47:28 UTC 2024
Tue Nov 12 11:47:29 UTC 2024
Tue Nov 12 11:47:30 UTC 2024
Tue Nov 12 11:47:31 UTC 2024
Tue Nov 12 11:47:32 UTC 2024
Tue Nov 12 11:47:33 UTC 2024
Tue Nov 12 11:47:34 UTC 2024

Note: Use s for seconds, m for minutes and h for hours.


More Logs Commands
#

# List more logs commands
kubectl logs --help
Kubernetes-Components - This article is part of a series.
Part 8: This Article