Kubectl Basics & Getting Help
A comprehensive developer cheatsheet covering Kubernetes command-line interface (kubectl) fundamentals, featuring essential CLI syntax, global flags, version inspection, resource documentation exploration with explain, quick-reference commands, and operational best practices for cluster management.
Table of Contents
- kubectl — The Kubernetes CLI
- kubectl version — Show Version Info
- kubectl explain — Get Resource Documentation
- kubectl options — List Global Options
- Quick Reference — Most Used Commands
- Pro Tips
kubectl — The Kubernetes CLI
Basic Syntax
$ kubectl [command] [type] [name] [flags]Example:
$ kubectl get pods -n productionCommon Commands
| Command | Purpose |
|---|---|
get | List resources |
describe | Show resource details |
create | Create a resource |
apply | Apply a configuration |
delete | Delete a resource |
logs | View pod logs |
exec | Execute commands in a pod |
port-forward | Forward local port to pod |
Common Global Flags
| Flag | Purpose | Example |
|---|---|---|
-n, --namespace | Target a specific namespace | kubectl get pods -n production |
-A, --all-namespaces | Target all namespaces | kubectl get pods -A |
--context | Switch to a different context | kubectl get pods --context=prod |
--kubeconfig | Use a specific kubeconfig file | kubectl get pods --kubeconfig=/path/to/config |
-v, --v | Verbosity level (0-10) | kubectl get pods -v=8 |
--dry-run | Preview without executing | kubectl run nginx --image=nginx --dry-run=client -o yaml |
-o, --output | Output format | kubectl get pods -o wide |
--server | API server address | kubectl get pods --server=https://my-cluster:6443 |
kubectl version — Show Version Info
Shows client and server Kubernetes versions.
Basic Usage
$ kubectl version
Client Version: v1.28.0
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.27.3Common Flags
| Flag | Purpose |
|---|---|
--client | Show only client version |
--server | Show only server version |
-o json | Output as JSON |
--short | Show short version |
Examples
Show only client version:
$ kubectl version --client
Client Version: v1.28.0Show only server version:
$ kubectl version --server
Server Version: v1.27.3Show short version:
$ kubectl version --short
Client Version: v1.28.0
Server Version: v1.27.3Show as JSON (for scripting):
$ kubectl version -o json
{
"clientVersion": {
"major": "1",
"minor": "28",
"gitVersion": "v1.28.0"
},
"serverVersion": {
"major": "1",
"minor": "27",
"gitVersion": "v1.27.3"
}
}Why Check Version?
- Ensure compatibility between client and server
- Verify cluster upgrade success
- Debug API version mismatches
kubectl explain — Get Resource Documentation
Shows documentation for a resource or field.
Basic Usage
$ kubectl explain pod
KIND: Pod
VERSION: v1
DESCRIPTION:
Pod is a collection of containers that can run on a host. This resource is
created by clients and scheduled onto hosts.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values.
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client
submits requests to.
metadata <Object>
Standard object's metadata.
spec <Object>
Specification of the desired behavior of the pod.Explore Nested Fields
$ kubectl explain pod.spec
$ kubectl explain pod.spec.containers
$ kubectl explain pod.spec.containers.imageCommon Uses
Explain a specific resource:
$ kubectl explain deployment
$ kubectl explain service
$ kubectl explain ingressExplain with recursion (show all fields):
$ kubectl explain pod --recursivekubectl options — List Global Options
Lists all global command-line options.
Basic Usage
$ kubectl options
--add-dir-header=false
--alsologtostderr=false
--as=''
--as-group=[]
--cache-dir=''
--certificate-authority=''
--client-certificate=''
--client-key=''
--cloud-provider-gce-lb-src-cidrs=130.211.0.0/22,35.191.0.0/16,...
--cluster=''
--context=''
--insecure-skip-tls-verify=false
--kubeconfig=''
--log-backtrace-at=:0
--log-dir=''
--log-file=''
--log-file-max-size=1800
--log-flush-frequency=5s
--logtostderr=true
--match-server-version=false
--namespace=''
--password=''
--profile=''
--profile-output=''
--request-timeout='0'
--server=''
--tls-server-name=''
--token=''
--user=''
--username=''
--v=0
--vmodule=Quick Reference — Most Used Commands
| Task | Command |
|---|---|
| Show version | kubectl version |
| Show client version | kubectl version --client |
| Show server version | kubectl version --server |
| Explain a resource | kubectl explain pod |
| Explain nested field | kubectl explain pod.spec.containers |
| List global options | kubectl options |
| Get help | kubectl --help |
| Get help for a command | kubectl get --help |
Pro Tips
-
Use
--helpto explore commands:bashkubectl --help kubectl get --help -
Enable autocompletion:
bashsource <(kubectl completion bash) source <(kubectl completion zsh) -
Create aliases for speed:
bashalias k='kubectl' alias kg='kubectl get' alias kd='kubectl describe' alias kex='kubectl exec -it' alias klogs='kubectl logs' -
Check client-server compatibility:
bashkubectl version --short -
Use explain to build YAML manifests:
bashkubectl explain pod.spec.containers > manifest-template.txt -
Debug with verbosity:
bashkubectl get pods -v=8 # Level 8 shows detailed API requests