kubectl commands


print the IP address of the specified Pod:

$ kubectl get pods my-pod -o jsonpath --template={.status.podIP}

see the logs for a running container:

$ kubectl logs <pod-name>

use the exec command to execute a command in a running container:

$ kubectl exec -it <pod-name> -- bash

If you don’t have bash or some other terminal available within your container, you can always attach to the running process:

$ kubectl attach -it <pod-name>

You can also copy files to and from a container using the cp command:

$ kubectl cp <pod-name>:</path/to/remote/file> </path/to/local/file>

opens up a connection that forwards traffic from the local machine on port 8080 to the remote container on port 80.

$ kubectl port-forward <pod-name> 8080:80

You can also use the port-forward command with services by specifying services/<service-name> instead of <pod-name>, but note that if you do port-forward to a service, the requests will only ever be forwarded to a single Pod in that service. They will not go through the service load balancer.

If you want to view Kubernetes events, you can use the kubectl get events command to see a list of the latest 10 events on all objects in a given namespace:

$ kubectl get events

If you are interested in how your cluster is using resources, you can use the top command to see the list of resources in use by either nodes or Pods. This command:

$ kubectl top nodes
$ kubectl top pods