0
0
Kubernetesdevops~5 mins

Container logging architecture in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run applications inside containers, you need a way to collect and view their logs. Container logging architecture helps gather logs from many containers so you can see what is happening inside each one and troubleshoot problems easily.
When you want to see error messages from your app running inside a Kubernetes pod.
When you need to collect logs from multiple containers running on different nodes in a cluster.
When you want to store logs outside the container for long-term analysis.
When you want to monitor your app's behavior in real time.
When you want to debug issues without accessing the container directly.
Commands
This command fetches and shows the logs from the container inside the pod named 'my-app-pod'. It helps you see what the app is printing to its standard output.
Terminal
kubectl logs my-app-pod
Expected OutputExpected
2024-06-01T12:00:00Z Starting application 2024-06-01T12:00:05Z Application ready to accept requests
-f - Follow the log output in real time
-c - Specify which container's logs to show if the pod has multiple containers
This command streams the logs from 'my-app-pod' live, so you can watch new log entries as they happen.
Terminal
kubectl logs -f my-app-pod
Expected OutputExpected
2024-06-01T12:00:00Z Starting application 2024-06-01T12:00:05Z Application ready to accept requests 2024-06-01T12:01:00Z Received new user request
-f - Follow the log output in real time
This command lists all pod names across all namespaces, helping you find the pods you want to check logs for.
Terminal
kubectl get pods --all-namespaces -o jsonpath='{.items[*].metadata.name}'
Expected OutputExpected
my-app-pod other-app-pod database-pod
--all-namespaces - Show pods from all namespaces
-o jsonpath - Format output to show only pod names
Key Concept

If you remember nothing else from container logging, remember: logs are collected from container standard output and can be viewed using kubectl logs commands.

Common Mistakes
Trying to access logs of a pod that does not exist or is misspelled.
The command will fail with an error because Kubernetes cannot find the pod.
Use 'kubectl get pods' to check the exact pod name before running the logs command.
Not specifying the container name when the pod has multiple containers.
kubectl logs will fail or show logs from the wrong container.
Use the '-c container-name' flag to specify which container's logs to view.
Expecting logs to be stored permanently inside the container.
Container logs are ephemeral and lost if the container restarts or is deleted.
Set up a centralized logging system like Fluentd or Elasticsearch to store logs outside containers.
Summary
Use 'kubectl logs pod-name' to view logs from a container inside a pod.
Use the '-f' flag to follow logs live as they are generated.
Always verify pod names and specify container names if needed to get the correct logs.