๐Ÿ”ง DevOps

kubectl

kubectl

The Kubernetes CLI tool. Runs cluster management commands.

Detailed Explanation

kubectl (pronounced "kube-CTL" or "kube-cuttle") is the official command-line tool for managing Kubernetes clusters. It communicates with the API server and can create, view, modify, and delete all Kubernetes resources, including Pods, Services, and Deployments.

The command structure is kubectl [verb] [resource] [name] [options]. The main verbs are get (view), describe (detailed information), apply (apply), delete (delete), logs (logs), and exec (execute a command). Declarative management is possible with YAML files, and --dry-run lets you validate before execution.

Cluster connection information is stored in the kubeconfig file (~/.kube/config). Multiple clusters are managed with contexts, and resources are isolated with namespaces. The commands that can be executed are restricted according to RBAC permissions.

In practice, kubectl get pods --watch is often used to check status in real time, kubectl logs -f to stream logs, and kubectl exec -it to access a container. Productivity is improved with the alias k=kubectl setting and bash completion.

Code Example

# ๋ฆฌ์†Œ์Šค ์กฐํšŒ
kubectl get pods                     # Pod ๋ชฉ๋ก
kubectl get pods -o wide             # ์ถ”๊ฐ€ ์ •๋ณด (IP, Node)
kubectl get pods -w                  # ์‹ค์‹œ๊ฐ„ ๋ณ€ํ™” ๊ฐ์‹œ
kubectl get all -n production        # ํŠน์ • ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์ „์ฒด ๋ฆฌ์†Œ์Šค

# ์ƒ์„ธ ์ •๋ณด ๋ฐ ๋กœ๊ทธ
kubectl describe pod my-pod          # Pod ์ƒ์„ธ ์ •๋ณด
kubectl logs my-pod -f               # ๋กœ๊ทธ ์ŠคํŠธ๋ฆฌ๋ฐ
kubectl logs my-pod -c nginx         # ํŠน์ • ์ปจํ…Œ์ด๋„ˆ ๋กœ๊ทธ
kubectl logs -l app=web --tail=100   # ๋ ˆ์ด๋ธ”๋กœ ์—ฌ๋Ÿฌ Pod ๋กœ๊ทธ

# ๋ฆฌ์†Œ์Šค ์ƒ์„ฑ/์ ์šฉ
kubectl apply -f deployment.yaml     # ์„ ์–ธ์  ์ ์šฉ
kubectl apply -f ./k8s/              # ๋””๋ ‰ํ† ๋ฆฌ ์ „์ฒด ์ ์šฉ
kubectl create deployment nginx --image=nginx  # ๋ช…๋ นํ˜• ์ƒ์„ฑ
kubectl apply -f - <

How It's Said in Practice

Senior: "The Pod in the production cluster is in CrashLoopBackOff, so check the events with kubectl describe."

Junior: "It shows OOMKilled. I think we need to raise the memory limit from 512Mi to 1Gi."

Interviewer: "What is the difference between kubectl apply and kubectl create?"

Candidate: "create only creates a resource when it does not exist, while apply creates it if it does not exist and updates it if it does. In a GitOps environment, apply is used for declarative management."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "A script that runs kubectl delete directly in the production environment is risky."

Developer: "I'll change it to be managed through ArgoCD, and limit kubectl to read-only use."

Cautions

  • Always verify with --dry-run before delete/scale commands on a production cluster
  • Do not run commands without checking the context - risk of operating on the wrong cluster
  • Editing YAML files and then running apply is recommended over kubectl edit - change history can be tracked
  • Use the kubectx/kubens tools to safely manage switching between clusters/namespaces

Learn More