Guide 02 / 3
Kubernetes commands · kubectl
Use kubectl to observe, deploy, troubleshoot and administer a training cluster.
Choose a command, check its scope and diagnose a deployment without needless destructive action.
01 · Context and connection
Before each operation, check which cluster and which namespace the commands will run against.
kubectl version --client
Displays the installed client's version, without needing working access to the cluster.
Example
kubectl version --client
kubectl cluster-info
Displays the addresses of the main services of the reachable cluster.
Example
kubectl cluster-info
kubectl config get-contexts
Lists the kubeconfig's contexts: a context combines a cluster, an identity and a default namespace.
Example
kubectl config get-contexts
kubectl config current-context
Displays the active context to avoid acting on the wrong environment.
Example
kubectl config current-context
kubectl config use-context
Changes the context used by default in the local kubectl configuration.
Example
kubectl config use-context lab
Be careful not to switch to production by accident.
kubectl get namespaces
Lists the cluster's accessible namespaces.
Example
kubectl get ns
kubectl config set-context --current --namespace
Sets the default namespace of the current context, without changing cluster.
Example
kubectl config set-context --current --namespace=dev
02 · Observing resources
The diagnostic approach starts with an overview, then the state and events of a specific resource.
kubectl get
Lists resources of a given type in the current namespace.
Example
kubectl get pods
kubectl get -A / -n
-A targets all namespaces; -n picks a specific one for namespaced resources.
Example
kubectl get pods -A
kubectl get pods -n dev
kubectl get -o wide
Adds columns such as the Pod's IP and the node it is placed on, depending on the resource type.
Example
kubectl get pods -o wide
kubectl get -o yaml
Displays the complete YAML representation returned by the API, including fields managed by the cluster.
Example
kubectl get deployment web -o yaml
Do not reuse uid and resourceVersion as they are in a manifest.
kubectl describe
Produces a readable description with state, configuration, errors and events.
Example
kubectl describe pod web-abc123
kubectl get events
Shows the cluster's or namespace's events: placement, startup or mount failures.
Example
kubectl get events -n dev --sort-by=.metadata.creationTimestamp
kubectl api-resources
Lists the resource types recognised by the API and their possible short names.
Example
kubectl api-resources
kubectl explain
Explains the fields expected by an API object using the cluster's known schema.
Example
kubectl explain deployment.spec.template.spec.containers
03 · Declaring, creating and modifying
For durable applications, keep the YAML manifests in Git and apply their changes in a controlled way.
kubectl apply -f
Creates or updates the resources declared in a file or directory.
Example
kubectl apply -f deployment.yaml
kubectl apply -f ./manifests/
kubectl diff -f
Displays the expected differences between the cluster and the manifests before they are applied.
Example
kubectl diff -f ./manifests/
Depending on the context, may need extra rights on the server side.
kubectl apply --dry-run=server
Requests server-side validation without persisting the object; useful before applying.
Example
kubectl apply --dry-run=server -f deployment.yaml
Validation does not guarantee the availability of images or storage.
kubectl create namespace
Quickly creates a namespace; useful in labs or to initialise an environment.
Example
kubectl create namespace dev
kubectl create deployment
Creates a Deployment from the command line, useful for a quick demonstration.
Example
kubectl create deployment web --image=nginx:stable
kubectl edit
Opens the local editor to modify an existing object then submit the changes to the API.
Example
kubectl edit deployment web
For a reproducible configuration, then carry the change over into Git.
kubectl delete
Deletes targeted resources, in the selected namespace or context.
Example
kubectl delete -f deployment.yaml
Check the volumes and dependent objects before deleting.
04 · Diagnosing a Pod
For an application that does not start: state → describe → events → logs → network and configuration check.
kubectl logs
Displays a Pod's logs, optionally specifying the container concerned.
Example
kubectl logs pod/web-abc -c nginx
kubectl logs -f / --previous
-f follows the logs; --previous consults the previous instance of a restarted container.
Example
kubectl logs -f deployment/web
kubectl logs pod/web-abc --previous
kubectl exec
Runs a command in a running container, if the image contains that command.
Example
kubectl exec -it pod/web-abc -- sh
kubectl port-forward
Redirects a local port to a Pod or Service through the kubectl connection, without publishing an external Service.
Example
kubectl port-forward svc/web 8080:80
Temporary diagnostic access, not a production publication.
kubectl cp
Copies files between the local machine and a container; depends notably on tar in the container.
Example
kubectl cp dev/web-abc:/tmp/test.log ./test.log
kubectl top
Displays CPU/memory usage when the metrics API, for example metrics-server, is available.
Example
kubectl top pods
kubectl top nodes
kubectl debug
Allows more advanced debugging, for example with an ephemeral container or a copy of the Pod.
Example
kubectl debug -it pod/web-abc --image=busybox:1.36 --target=nginx
Requires the right permissions and support in the cluster.
05 · Deploying, scaling and rolling back
Updating a Deployment manages the transition to the new version of its Pods.
kubectl scale
Changes the number of replicas of a resource that supports this operation.
Example
kubectl scale deployment/web --replicas=3
kubectl set image
Updates a container's image reference in a Deployment or compatible object.
Example
kubectl set image deployment/web nginx=nginx:stable
Prefer an immutable image reference or a versioned tag.
kubectl rollout status
Waits for or checks the deployment's progress, with a possible timeout.
Example
kubectl rollout status deployment/web --timeout=120s
kubectl rollout history
Displays the recorded revisions of a Deployment, if the history is available.
Example
kubectl rollout history deployment/web
kubectl rollout undo
Requests a return to an earlier revision of a Deployment.
Example
kubectl rollout undo deployment/web
A rollback does not restore business data.
kubectl rollout restart
Triggers a new deployment cycle by updating the Pod template.
Example
kubectl rollout restart deployment/web
06 · Day-to-day administration and permissions
Operations on nodes and authorisations must be reserved for authorised people.
kubectl get nodes
Displays the cluster's nodes and their availability.
Example
kubectl get nodes -o wide
kubectl cordon
Prevents scheduling of new ordinary Pods on a node, without moving existing Pods.
Example
kubectl cordon worker-1
kubectl drain
Evicts the relevant Pods from a node for maintenance, respecting certain protections.
Example
kubectl drain worker-1 --ignore-daemonsets
Run only on an authorised environment; possible application impacts.
kubectl uncordon
Re-allows scheduling of Pods on a node that was previously blocked.
Example
kubectl uncordon worker-1
kubectl auth can-i
Checks whether the active identity is allowed to perform a specific action in a scope.
Example
kubectl auth can-i get secrets -n dev
kubectl get pvc,pv,storageclass
Inspects the claims, available volumes and storage classes.
Example
kubectl get pvc -n dev
kubectl get pv,storageclass
kubectl get svc,ingress
Inspects the namespace's Services and HTTP ingress rules.
Example
kubectl get svc,ingress -n dev
kubectl get configmap,secret
Lists the namespace's configuration objects; secret values are not displayed by default.
Example
kubectl get configmaps,secrets -n dev
Never log or publish the real content of Secrets.
07 · Useful options and good practice
kubectl's options avoid scope errors and make automation easier.
--context / --namespace
Explicitly target the cluster and namespace without changing the persistent local configuration.
Example
kubectl --context lab -n dev get pods
-l / --selector
Filters objects by their labels; lets you target a functional subset.
Example
kubectl get pods -l app=web
-o jsonpath
Extracts specific fields from the API response, useful for scripts and automation.
Example
kubectl get pod web-abc -o jsonpath="{.status.phase}"
--watch / -w
Continuously observes the changes to a list of resources.
Example
kubectl get pods -w
kubectl config view --minify
Displays the information related to the active context, to check the configuration in use.
Example
kubectl config view --minify
Warning: some configuration outputs may contain sensitive information.