Kubernetes — The course

Guide 02 / 3

Kubernetes commands · kubectl

Use kubectl to observe, deploy, troubleshoot and administer a training cluster.

Expected outcome :

Choose a command, check its scope and diagnose a deployment without needless destructive action.

Reading
0%

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.

kubectl version --client

kubectl cluster-info

Displays the addresses of the main services of the reachable cluster.

kubectl cluster-info

kubectl config get-contexts

Lists the kubeconfig's contexts: a context combines a cluster, an identity and a default namespace.

kubectl config get-contexts

kubectl config current-context

Displays the active context to avoid acting on the wrong environment.

kubectl config current-context

kubectl config use-context

Changes the context used by default in the local kubectl configuration.

kubectl config use-context lab
Remember :

Be careful not to switch to production by accident.

kubectl get namespaces

Lists the cluster's accessible namespaces.

kubectl get ns

kubectl config set-context --current --namespace

Sets the default namespace of the current context, without changing cluster.

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.

kubectl get pods

kubectl get -A / -n

-A targets all namespaces; -n picks a specific one for namespaced resources.

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.

kubectl get pods -o wide

kubectl get -o yaml

Displays the complete YAML representation returned by the API, including fields managed by the cluster.

kubectl get deployment web -o yaml
Remember :

Do not reuse uid and resourceVersion as they are in a manifest.

kubectl describe

Produces a readable description with state, configuration, errors and events.

kubectl describe pod web-abc123

kubectl get events

Shows the cluster's or namespace's events: placement, startup or mount failures.

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.

kubectl api-resources

kubectl explain

Explains the fields expected by an API object using the cluster's known schema.

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.

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.

kubectl diff -f ./manifests/
Remember :

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.

kubectl apply --dry-run=server -f deployment.yaml
Remember :

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.

kubectl create namespace dev

kubectl create deployment

Creates a Deployment from the command line, useful for a quick demonstration.

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.

kubectl edit deployment web
Remember :

For a reproducible configuration, then carry the change over into Git.

kubectl delete

Deletes targeted resources, in the selected namespace or context.

kubectl delete -f deployment.yaml
Remember :

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.

kubectl logs pod/web-abc -c nginx

kubectl logs -f / --previous

-f follows the logs; --previous consults the previous instance of a restarted container.

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.

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.

kubectl port-forward svc/web 8080:80
Remember :

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.

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.

kubectl top pods
kubectl top nodes

kubectl debug

Allows more advanced debugging, for example with an ephemeral container or a copy of the Pod.

kubectl debug -it pod/web-abc --image=busybox:1.36 --target=nginx
Remember :

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.

kubectl scale deployment/web --replicas=3

kubectl set image

Updates a container's image reference in a Deployment or compatible object.

kubectl set image deployment/web nginx=nginx:stable
Remember :

Prefer an immutable image reference or a versioned tag.

kubectl rollout status

Waits for or checks the deployment's progress, with a possible timeout.

kubectl rollout status deployment/web --timeout=120s

kubectl rollout history

Displays the recorded revisions of a Deployment, if the history is available.

kubectl rollout history deployment/web

kubectl rollout undo

Requests a return to an earlier revision of a Deployment.

kubectl rollout undo deployment/web
Remember :

A rollback does not restore business data.

kubectl rollout restart

Triggers a new deployment cycle by updating the Pod template.

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.

kubectl get nodes -o wide

kubectl cordon

Prevents scheduling of new ordinary Pods on a node, without moving existing Pods.

kubectl cordon worker-1

kubectl drain

Evicts the relevant Pods from a node for maintenance, respecting certain protections.

kubectl drain worker-1 --ignore-daemonsets
Remember :

Run only on an authorised environment; possible application impacts.

kubectl uncordon

Re-allows scheduling of Pods on a node that was previously blocked.

kubectl uncordon worker-1

kubectl auth can-i

Checks whether the active identity is allowed to perform a specific action in a scope.

kubectl auth can-i get secrets -n dev

kubectl get pvc,pv,storageclass

Inspects the claims, available volumes and storage classes.

kubectl get pvc -n dev
kubectl get pv,storageclass

kubectl get svc,ingress

Inspects the namespace's Services and HTTP ingress rules.

kubectl get svc,ingress -n dev

kubectl get configmap,secret

Lists the namespace's configuration objects; secret values are not displayed by default.

kubectl get configmaps,secrets -n dev
Remember :

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.

kubectl --context lab -n dev get pods

-l / --selector

Filters objects by their labels; lets you target a functional subset.

kubectl get pods -l app=web

-o jsonpath

Extracts specific fields from the API response, useful for scripts and automation.

kubectl get pod web-abc -o jsonpath="{.status.phase}"

--watch / -w

Continuously observes the changes to a list of resources.

kubectl get pods -w

kubectl config view --minify

Displays the information related to the active context, to check the configuration in use.

kubectl config view --minify
Remember :

Warning: some configuration outputs may contain sensitive information.