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.
Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/project
Start Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status
kubelet
and apiserver
listed as Running
.minikube start
.If you encounter issues starting Minikube. Use minikube delete
to reset the environment if needed.
kubectl logs
CommandThe kubectl logs
command is used to print logs for a container in a pod or a specified resource. It supports snapshot logs, streaming, filtering, and various other options for efficient log management.
Run the following command to view the available options for kubectl logs
:
kubectl logs -h
You will see the following output:
Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
Examples:
## Return snapshot logs from pod nginx with only one container
kubectl logs nginx
## Return snapshot logs from pod nginx with multi containers
kubectl logs nginx --all-containers=true
## Return snapshot logs from all containers in pods defined by label app=nginx
kubectl logs -l app=nginx --all-containers=true
## Return snapshot of previous terminated ruby container logs from pod web-1
kubectl logs -p -c ruby web-1
## Begin streaming the logs of the ruby container in pod web-1
kubectl logs -f -c ruby web-1
## Begin streaming the logs from all containers in pods defined by label app=nginx
kubectl logs -f -l app=nginx --all-containers=true
## Display only the most recent 20 lines of output in pod nginx
kubectl logs --tail=20 nginx
## Show all logs from pod nginx written in the last hour
kubectl logs --since=1h nginx
## Show logs from a kubelet with an expired serving certificate
kubectl logs --insecure-skip-tls-verify-backend nginx
## Return snapshot logs from first container of a job named hello
kubectl logs job/hello
## Return snapshot logs from container nginx-1 of a deployment named nginx
kubectl logs deployment/nginx -c nginx-1
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.