3.2.6 Interacting with the Deployment

View in the book. Buy the book.

You can exec into the container to run commands:

$ kubectl exec -it deploy/timeserver -- sh
# echo "Testing exec"
Testing exec

Enter exit to close.

You can run the command directly too without bringing up a shell:

$ kubectl exec -it deploy/timeserver -- echo "Testing exec"
Testing exec

It can be useful to copy files out of the Pod:

kubectl cp $POD_NAME:example.txt example.txt

Or to the Pod:

kubectl cp example.txt $POD_NAME:.

For example:

$ echo "blar" > example.txt
$ kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
timeserver-f94cc5dd9-jntt4   1/1     Running   0          12m
timeserver-f94cc5dd9-swl4h   1/1     Running   0          12m
timeserver-f94cc5dd9-xwbnx   1/1     Running   0          12m

$ POD_NAME=timeserver-f94cc5dd9-jntt4
$ kubectl cp example.txt $POD_NAME:.
$ rm example.txt 
$ kubectl cp $POD_NAME:example.txt example.txt
$ cat example.txt 
blar
$