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.
Start the Minikube Cluster
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/projectStart Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start- This command sets up a single-node Kubernetes cluster on your local machine.
- Minikube may take a few minutes to start depending on your system's performance.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.
Explore the kubectl logs Command
The 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
Viewing Container Logs
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=1Wait for the pod to become ready:
kubectl wait --for=condition=Ready pod -l app=nginxUse the
kubectl logscommand 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.
Viewing Logs from a Specific Container in a Pod
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" EOFWait for the pod to become ready:
kubectl wait --for=condition=Ready pod nginx-busyboxUse the
kubectl logscommand to view the logs of the BusyBox container:kubectl logs nginx-busybox -c busybox
Following Logs in Real-Time
In this step, you will learn how to follow logs in real-time as they are generated.
Use the
kubectl logscommand with the-foption to follow logs in real-time:kubectl logs -f nginx-busyboxOpen a new terminal and create a shell in the Nginx container:
kubectl exec -it nginx-busybox -c nginx -- /bin/shGenerate some logs by running a command inside the container:
curl 127.0.0.1Switch back to the first terminal where you are following the logs and observe that the new log entry is displayed.
Viewing Logs from Multiple Containers
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 EOFWait for the pod to become ready:
kubectl wait --for=condition=Ready pod nginx-fluentdUse the
kubectl logscommand to view logs from both containers:kubectl logs nginx-fluentd -c nginx kubectl logs nginx-fluentd -c fluentd
Summary
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.


