kubectl
kubectl is a command line tool to work with Kubernetes clusters.
List resources. Specify one or more resource types separated by comma.
- pod
- deploy
- cronjob
- job
- svc
- node
kubectl get <resource-type>
kubectl get <resource-type> -o wide
kubectl get deploy,pod,node
Get logs
kubectl get pods
kubectl logs <pod-name>
kubectl logs deploy/<deploy-name>
Replace deployment
kubectl replace -f <deploy.yml> --force
Apply changes to deployment (resizing, etc.)
kubectl apply -f <deploy.yml>
Remove
kubectl delete <resource-type> <resource-name>
kubectl delete deploy/<deploy-name>
Copy a file from pod to local
kubectl cp <pod-name>:<path> <local-path>
kubectl cp nginx:/var/logs/error.log /tmp/
Get a shell into a pod
kubectl exec -it <pod-name> -- /bin/bash
kubectl exec -it deploy/<deploy-name> -- /bin/bash
Copy a file from pod to local in the event that you have multiple containers. You’ll have to specify the container.
kubectl cp mypod-5df57c74c7-fnr8t:dev-resources/mysystem/responses \
/tmp/ -c my-container
Warnings
Use of create vs apply
If you use kubectl create
to do the deployment, then you can’t use kubectl apply
later on to make a change. Use kubectl apply
from the beginning and you
will be able to do it again later on to resize.
Troubleshooting
Pod is spending forever on ContainerCreating
You’ve waited a while and the pod still says ContainerCreating
.
NAME READY STATUS RESTARTS AGE
pod/webapp-6fb944b47f-lcbxt 0/1 ContainerCreating 0 5m
It could be that you forgot to add the configmap and any secrets. Just delete the deployment, add your configmap and secrets and retry the deployment.
Glossary
- resource
- One of Cronjob, Deployment, Job, Node, Pod, ReplicaSet, Service
- pod
- Smallest unit of deployment. Pods normally have one container, can have more than one in cases of tightly coupled code.
- container
- A process (normally an application) that runs in a pod.