Kubernetes Pod Operations

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Get Information about Pods

get

All pods in a namespace (or the default namespace if -n is not used):

kubectl [-n namespace] get pods|po

Monitor a pod and be notified when the status of the pod is changing:

kubectl get --watch pod <pod-name>

Get more columns in the output:

kubectl get -o wide pod <pod-name>

Along the default columns, we get IP, NODE, NOMINATED NODE, READINESS GATES.

More general options:

kubectl get

describe

kubectl describe pod <pod-name>

Pod Log (Output)

kubectl log <pod-name>

"Follow" logging:

kubectl logs -f <pod-name>

This shell command could be used to log a pod in such a way logging survives a pod restart:

while ! kubectl -n my-namespace logs -f my-pod; do sleep 1; done

If the pod has multiple containers, the target container can be specified with the --container flag:

kubectl logs ... --container <target-container-name> ...

Create Pods

A singleton pod can be created by posting a pod manifest to the API server with:

kubectl apply -f <pod-manifest-file-name>.yaml

Execute Commands inside a Pod

Execute a command in the first container of the pod:

kubectl exec [-it] <pod-name> <command>

The -it flags make the exec session interactive and connects stdin and stdout of the terminal kubectl was executed from to the stdin and stdout of the process executing inside the first container of the pod.

If the pod has multiple containers, the target container can be specified with the --container flag:

kubectl exec ... --container <target-container-name> ...

Removing an Individual Pod

kubectl delete pod <pod-name>

Remove Immediately

kubectl delete pod <pod-name> --grace-period=0 --force