Accessing Kubernetes Pods from the Command Line
To access Kubernetes Pods from the command line, you can use the kubectl
command-line tool, which is the primary interface for interacting with a Kubernetes cluster.
Listing Pods
To list all the Pods in your Kubernetes cluster, you can use the following command:
kubectl get pods
This will display a list of all the Pods in the default namespace. You can also specify a different namespace using the -n
or --namespace
flag.
Describing a Pod
To get more detailed information about a specific Pod, you can use the describe
command:
kubectl describe pod <pod-name>
This will provide information about the Pod, such as its containers, volumes, and events.
Accessing a Pod's Logs
To view the logs of a container within a Pod, you can use the logs
command:
kubectl logs < pod-name > [-c < container-name > ]
If the Pod has multiple containers, you can specify the container name using the -c
flag.
Executing Commands in a Pod
To execute a command inside a running Pod, you can use the exec
command:
kubectl exec -it <pod-name> -- <command>
The -it
flags allow you to interact with the container in an interactive mode.
Forwarding a Local Port to a Pod
To forward a local port to a Pod, you can use the port-forward
command:
kubectl port-forward <pod-name> <local-port>:<pod-port>
This will allow you to access the Pod's service from your local machine.
By using these kubectl
commands, you can effectively access and interact with Kubernetes Pods from the command line, which is essential for managing and troubleshooting your applications running in a Kubernetes cluster.