‘kubectl logs’ command is used to print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.
Example 1: kubectl logs nginx
Return snapshot logs from pod nginx with only one container
Example 2: kubectl logs nginx --all-containers=true
Return snapshot logs from pod 'nginx' with multi containers
Example 3: kubectl logs -f -c ruby web-1
Begin streaming the logs of the ruby container in pod web-1
Let’s create a Pod definition file, that prints something to stdout. So we can see what is happening via ‘kubectl logs’ command.
executeCommandPod.yml
apiVersion: v1
kind: Pod
metadata:
name: execute-command-pod
labels:
app: hello-world
author: krishna
serviceType: terminal-app
spec:
containers:
- name: execute-command-container
image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo 'Hello World'; sleep 5; done"]
As you see the definition file, I am just printing the message 'Hello World' to console for every 5 seconds.
Let’s create a Pod using above definition file.
$kubectl create -f executeCommandPod.yml pod/execute-command-pod created
Wait until Pod is in Running state.
$kubectl get pods
NAME READY STATUS RESTARTS AGE
execute-command-pod 1/1 Running 0 9s
Use ‘kubectl logs’ command to see the output of the command that we gave in the definition file.
$kubectl logs execute-command-pod Hello World Hello World
Wait for 5 seconds and re-execute the logs command, you will see another ‘Hello World’ message.
$kubectl logs execute-command-pod Hello World Hello World Hello World
You can delete the pod by executing the below command.
kubectl delete pods execute-command-pod
$kubectl delete pods execute-command-pod
pod "execute-command-pod" deleted
No comments:
Post a Comment