Skip to content

kubectl basics

On Kestrel, kubectl authenticates via OIDC using the kubelogin plugin, which you install and wire into your kubeconfig yourself — see Set up kubelogin. Once that’s done, if you can run kubectl get ns and see your namespace listed, you’re ready.

Terminal window
kubectl get ns
Terminal window
kubectl get pods
kubectl get deployments
kubectl get services
kubectl get all

Useful flags:

FlagEffect
-o wideShow extra columns (node, IP)
-o yamlFull YAML output
--show-labelsAppend labels column
-wWatch for changes in real time

describe shows events, conditions, and configuration in human-readable form. Use it when a Pod won’t start or a Service isn’t routing.

Terminal window
kubectl describe pod my-app-7b4f6d8c9-x2lkj
kubectl describe service my-app

The Events section at the bottom is where most debugging starts — it tells you about image pull failures, scheduling problems, and readiness probe failures.

Terminal window
kubectl logs my-app-7b4f6d8c9-x2lkj
kubectl logs -f my-app-7b4f6d8c9-x2lkj # follow (tail -f)
kubectl logs my-app-7b4f6d8c9-x2lkj -c sidecar # specific container in a multi-container Pod
kubectl logs --previous my-app-7b4f6d8c9-x2lkj # logs from the last crash
Terminal window
kubectl exec -it my-app-7b4f6d8c9-x2lkj -- /bin/sh

This opens an interactive shell inside the container. Use it for ad-hoc debugging — checking files, testing network connectivity, or running one-off commands.

Terminal window
kubectl exec my-app-7b4f6d8c9-x2lkj -- env # print environment variables
kubectl exec my-app-7b4f6d8c9-x2lkj -- cat /etc/hosts # check DNS entries
Terminal window
kubectl apply -f deployment.yaml
kubectl apply -f k8s/ # apply every YAML file in a directory

apply is idempotent: run it again with the same manifest and nothing changes.

Terminal window
kubectl delete -f deployment.yaml
kubectl delete pod my-app-7b4f6d8c9-x2lkj

Deleting a Pod managed by a Deployment just triggers a replacement. To stop the workload, delete the Deployment.

TaskCommand
List Podskubectl get pods
Pod details + eventskubectl describe pod <name>
Follow logskubectl logs -f <pod>
Shell into a containerkubectl exec -it <pod> -- /bin/sh
Apply a manifestkubectl apply -f <file>
Delete a resourcekubectl delete -f <file>
Scale a Deploymentkubectl scale deployment/<name> --replicas=N
Rollback a Deploymentkubectl rollout undo deployment/<name>
Check rollout statuskubectl rollout status deployment/<name>
Port-forward for local testingkubectl port-forward svc/<name> 8080:80

To test a Service locally without an Ingress:

Terminal window
kubectl port-forward svc/my-app 8080:80

Then open http://localhost:8080 in your browser. The tunnel stays open until you press Ctrl+C.