Questão de Container Orchestration #019e3a4f
Which of the following will view the snapshot of previously terminated ruby container logs from Pod web-1?
kubectl logs -p -c ruby web-1
kubectl logs -c ruby web-1
kubectl logs -p ruby web-1
kubectl logs -p -c web-1 ruby
Explicação Oficial / Explicações da Comunidade
The correct command to view the snapshot of previously terminated ruby container logs from Pod web-1 is kubectl logs -p -c ruby web-1. The -p flag (or --previous) tells kubectl to retrieve logs from the previous instance of the container (i.e., the terminated container). The -c ruby flag specifies the container name within the pod, and web-1 is the pod name. This command outputs the logs of the terminated ruby container.
How it Works:
When a container in a pod restarts or is terminated, Kubernetes retains the logs of the previous container instance. The kubectl logs command with the -p flag accesses these retained logs. The -c flag is necessary when the pod has multiple containers; it selects which container's logs to display. Without -c, the command would fail if the pod has more than one container, or it might default to the only container if there is just one, but specifying the container explicitly is best practice.
Why the other options are incorrect:
kubectl logs -c ruby web-1: This command retrieves the current logs of the ruby container, not the previous (terminated) instance. It lacks the-pflag.kubectl logs -p ruby web-1: This command attempts to get previous logs but does not specify a container with-c. If the pod has multiple containers, this will fail because kubectl requires a container name. Even if the pod has only one container, it is ambiguous and not the correct syntax for multi-container pods.kubectl logs -p -c web-1 ruby: This command incorrectly swaps the container name and pod name. The syntax iskubectl logs [flags] [-c container] pod. Here,web-1is treated as the container name andrubyas the pod name, which is reversed. This would result in an error because there is no container namedweb-1in podruby.