Skip to the content.

K8s Commands

Types of Commands

Imperative Commands

Command Description Example
kubectl get <resource> List resources like pods, services, etc. kubectl get pods
kubectl describe <resource> Show detailed information about a resource. kubectl describe pod my-pod
kubectl create <resource> Create a resource from a file or stdin. kubectl create -f my-deployment.yaml
kubectl delete <resource> Delete resources by filenames, stdin, or resource names. kubectl delete pod my-pod
kubectl apply <resource> Apply changes to resources from a file or stdin. kubectl apply -f my-deployment.yaml
kubectl exec Execute a command in a container. kubectl exec my-pod -- ls /
kubectl logs Print the logs for a container in a pod. kubectl logs my-pod
kubectl scale Scale the replicas of a deployment or replicaset. kubectl scale --replicas=5 deployment/my-app
kubectl port-forward Forward one or more local ports to a pod. kubectl port-forward my-pod 8080:8080
kubectl rollout Manage the rollout of a resource. kubectl rollout undo deployment/my-deployment
kubectl label Update or add labels to a resource. kubectl label pods my-pod status=unhealthy
kubectl annotate Add or update annotations to a resource. kubectl annotate pods my-pod description="test"
kubectl config Modify kubeconfig files. kubectl config set-context my-context
kubectl cluster-info Display cluster info. kubectl cluster-info
kubectl top Display resource usage (CPU/Memory) for nodes or pods. kubectl top nodes

Declarative Commands

Command Description
kubectl apply -f <filename.yaml> Apply the configuration in <filename.yaml> to the resource.
kubectl apply -f <directory/> Apply the configurations in all YAML or JSON files within a directory.
kubectl apply -k <kustomization> Apply a kustomization directory.
kubectl delete -f <filename.yaml> Delete the resources defined in <filename.yaml>.
kubectl diff -f <filename.yaml> Diff the local resource with the running configuration.
kubectl replace -f <filename.yaml> Replace the resource with the configuration in <filename.yaml>.

Comparison

Aspect Imperative Commands Declarative Commands
Definition Commands that manage resources by direct action. Commands that manage resources based on manifest files.
Usage Utilized for one-off tasks, quick edits, or experimentation. Used for maintaining the desired state as defined in files.
Command Examples kubectl create, kubectl run, kubectl expose kubectl apply -f <filename.yaml>
File Requirement No YAML or JSON file required. Requires YAML or JSON file describing the desired state.
Flexibility Limited in complex scenarios. More flexible and can handle complex configurations.
State Tracking No automatic tracking of the current state. Can track the current state and make updates as needed.
Recommended For Ad-hoc actions and prototyping. Production environments, version control, and collaboration.