Introduction
In this lab, you will learn how to use the Kubernetes logs
command to view logs from pods running in a Kubernetes cluster. You will start with simple examples and gradually progress to more complex scenarios.
In this lab, you will learn how to use the Kubernetes logs
command to view logs from pods running in a Kubernetes cluster. You will start with simple examples and gradually progress to more complex scenarios.
In this step, you will learn how to view the logs of a container running in a pod.
Start by creating a deployment with one replica and an Nginx container:
kubectl create deployment nginx --image=nginx --replicas=1
Wait for the pod to become ready:
kubectl wait --for=condition=Ready pod -l app=nginx
Use the kubectl logs
command to view the logs of the Nginx container:
kubectl logs <pod_name>
Replace <pod_name>
with the name of the pod created in step 1, and you can get the <pod_name>
with the kubectl get pod -l app=nginx
command.
In this step, you will learn how to view the logs of a specific container running in a pod.
Create a pod with two containers: Nginx and BusyBox:
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: nginx-busybox
spec:
containers:
- name: nginx
image: nginx
- name: busybox
image: busybox
command:
- sleep
- "3600"
EOF
Wait for the pod to become ready:
kubectl wait --for=condition=Ready pod nginx-busybox
Use the kubectl logs
command to view the logs of the BusyBox container:
kubectl logs nginx-busybox -c busybox
In this step, you will learn how to follow logs in real-time as they are generated.
Use the kubectl logs
command with the -f
option to follow logs in real-time:
kubectl logs -f nginx-busybox
Open a new terminal and create a shell in the Nginx container:
kubectl exec -it nginx-busybox -c nginx -- /bin/sh
Generate some logs by running a command inside the container:
curl 127.0.0.1
Switch back to the first terminal where you are following the logs and observe that the new log entry is displayed.
In this step, you will learn how to view logs from multiple containers running in a pod.
Create a pod with two containers: Nginx and Fluentd:
cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: nginx-fluentd
spec:
containers:
- name: nginx
image: nginx
- name: fluentd
image: fluentd
EOF
Wait for the pod to become ready:
kubectl wait --for=condition=Ready pod nginx-fluentd
Use the kubectl logs
command to view logs from both containers:
kubectl logs nginx-fluentd -c nginx
kubectl logs nginx-fluentd -c fluentd
Congratulations, you have successfully completed the Kubernetes logs command lab! In this lab, you learned how to view logs from containers running in a pod using the kubectl logs
command. You also learned how to view logs from specific containers and multiple containers, follow logs in real-time, and generate logs from within a container. These skills are essential for troubleshooting issues in a Kubernetes cluster.