Using Kubernetes kubectl CLI (command-line tool)
What is kubectl?
kubectl is the Kubernetes command-line tool, and it lets you run commands against Kubernetes clusters.
Basic Structure of kubectl Commands
kubectl [command] [TYPE] [NAME] [flags]
- command: command to run against a resource (object), such as create, get, delete, or edit
- TYPE: resource type, such as node, pod, or service
- NAME: resource name
- flags: additional options to configure, such as –help or -o options
Example command:
kubectl get pod webserver -o wide
kubectl Command Autocompletion
bash
To use bash autocompletion for kubectl commands on Linux, run the command below.
source <(kubectl completion bash)
To apply it every time, add it to .bashrc as follows.
echo 'source <(kubectl completion bash)' >>~/.bashrc
kubectl commands
kubectl --help
kubectl command --help
kubectl run <resource name> <options>
kubectl create -f obj.yaml
kubectl apply -f obj.yaml
kubectl get <resource name> <object name>
kubectl edit <resource name> <object name>
kubectl describe <resource name> <object name>
kubectl delete <resource name> <options>
kubectl api-resources
kubectl --help
kubectl logs --help
Execute pod
kubectl exec -it [Pod name] -- [path]
For example:
kubectl exec -it prism-69b8c846c-gc9zj -- /bin/sh
- In some cases, the container may have been created with
/bin/bashinstead of/bin/sh.