GitHub EN PT

K8s by Example: kubectl Basics

kubectl is the command-line tool for Kubernetes. These are the commands you’ll use daily. Most follow the pattern kubectl [verb] [resource] [name].

terminal

List resources. Works with any resource type: pods, services, deployments, nodes, configmaps, secrets, etc.

$ kubectl get pods
$ kubectl get svc
$ kubectl get deploy
$ kubectl get nodes
$ kubectl get all          # pods, services, deployments, replicasets
terminal

Common flags: -o wide shows more columns, -o yaml dumps the full spec, -w watches for changes, -A shows all namespaces.

$ kubectl get pods -o wide           # show node, IP
$ kubectl get pods -o yaml           # full YAML output
$ kubectl get pods -w                # watch for changes
$ kubectl get pods -A                # all namespaces
$ kubectl get pods -l app=nginx      # filter by label
terminal

Show detailed info about a resource. Includes events, which are essential for debugging why something isn’t working.

$ kubectl describe pod my-pod
Name:         my-pod
Namespace:    default
Node:         kind-worker/172.18.0.3
Status:       Running
IP:           10.244.1.5
...
Events:
  Type    Reason     Age   Message
  Normal  Scheduled  10s   Successfully assigned
  Normal  Pulled     8s    Container image pulled
  Normal  Started    7s    Started container
terminal

apply creates or updates resources from a file. delete removes them. These are the commands you’ll use to deploy.

$ kubectl apply -f deployment.yaml   # create or update
$ kubectl apply -f ./manifests/      # apply all files in directory
$ kubectl delete -f deployment.yaml  # delete resources in file
$ kubectl delete pod my-pod          # delete by name
$ kubectl delete pods --all          # delete all pods in namespace
terminal

View container logs. Add -f to stream logs in real-time (like tail -f). Use —previous to see logs from a crashed container.

$ kubectl logs my-pod                # show logs
$ kubectl logs my-pod -f             # stream logs
$ kubectl logs my-pod --previous     # logs from crashed container
$ kubectl logs my-pod -c nginx       # specific container in pod
$ kubectl logs -l app=nginx          # logs from all pods with label
terminal

Run commands inside a container. Use -it for an interactive shell. Essential for debugging.

$ kubectl exec my-pod -- ls /app           # run command
$ kubectl exec my-pod -it -- sh            # interactive shell
$ kubectl exec my-pod -it -- /bin/bash     # bash if available
$ kubectl exec my-pod -c nginx -it -- sh   # specific container
terminal

Forward a local port to a pod or service. Useful for testing without exposing services externally.

$ kubectl port-forward pod/my-pod 8080:80      # forward to pod
$ kubectl port-forward svc/my-svc 8080:80      # forward to service
$ kubectl port-forward deploy/my-app 8080:80   # forward to deployment
terminal

Quick way to run a pod without writing YAML. Good for debugging. Add —rm -it for a temporary pod that deletes on exit.

$ kubectl run nginx --image=nginx                    # create pod
$ kubectl run debug --image=alpine --rm -it -- sh    # temp debug pod
$ kubectl run curl --image=curlimages/curl --rm -it -- curl my-svc
terminal

Manage cluster contexts. A context combines a cluster, user, and namespace. Switch between clusters with use-context.

$ kubectl config get-contexts           # list contexts
$ kubectl config current-context        # show current
$ kubectl config use-context my-cluster # switch context
$ kubectl config set-context --current --namespace=dev  # set default ns
terminal

When something isn’t working: check pod status, read events, check logs.

# 1. Check pod status
$ kubectl get pods

# 2. If not Running, check events
$ kubectl describe pod my-pod | grep -A 20 Events

# 3. If Running but not working, check logs
$ kubectl logs my-pod

# 4. If logs aren't helpful, shell in
$ kubectl exec my-pod -it -- sh

Index | Use arrow keys to navigate