Ir al contenido

Kubectl (K8s)

kubectl is the command-line tool for controlling Kubernetes clusters. Manage resources, deployments, services, and troubleshoot issues.

Installation

Linux

# Download kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/

# Verify installation
kubectl version --client

macOS

# Using Homebrew
brew install kubectl

# Download directly
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/

# For Apple Silicon
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl"

Windows

# Using Chocolatey
choco install kubernetes-cli

# Using Scoop
scoop install kubectl

# Download from official source
# https://dl.k8s.io/release/v<VERSION>/bin/windows/amd64/kubectl.exe

Cluster & Context

CommandDescription
kubectl cluster-infoDisplay cluster endpoint
kubectl config viewShow current kubeconfig
kubectl config current-contextGet current context
kubectl config use-context [context]Switch context
kubectl config get-contextsList all contexts
kubectl config set-context [name] --cluster=[cluster] --user=[user]Create context
kubectl config delete-context [name]Remove context
kubectl versionShow kubectl and server version

Nodes

CommandDescription
kubectl get nodesList all nodes
kubectl get nodes -o wideList nodes with details
kubectl describe node [name]Show node details
kubectl top nodesShow node resource usage
kubectl label node [name] key=valueAdd label to node
kubectl taint node [name] key=value:NoScheduleAdd taint to node
kubectl cordon [node]Mark node as unschedulable
kubectl drain [node]Drain node (remove pods)
kubectl uncordon [node]Mark node as schedulable

Namespaces

CommandDescription
kubectl get namespaceList all namespaces
kubectl get nsShort form for namespace
kubectl create namespace [name]Create namespace
kubectl delete namespace [name]Delete namespace
kubectl config set-context --current --namespace=[name]Set default namespace
kubectl get pods -n [namespace]Get pods in namespace
kubectl get all -n [namespace]Get all resources in namespace

Pods

CommandDescription
kubectl get podsList pods in current namespace
kubectl get pods -AList pods in all namespaces
kubectl get pods -o wideList pods with node/IP info
kubectl describe pod [name]Show pod details
kubectl logs [pod-name]View pod logs
kubectl logs -f [pod-name]Follow pod logs (streaming)
kubectl logs [pod-name] -c [container]View specific container logs
kubectl exec -it [pod-name] /bin/bashInteractive shell in pod
kubectl exec [pod-name] -- [command]Execute command in pod
kubectl port-forward [pod-name] [local]:[remote]Forward port to pod
kubectl delete pod [name]Delete pod
kubectl delete pods --allDelete all pods

Deployments

Creating & Viewing

# Create deployment
kubectl create deployment [name] --image=[image] --replicas=3

# Get all deployments
kubectl get deployments

# Describe deployment
kubectl describe deployment [name]

# Get deployment in YAML
kubectl get deployment [name] -o yaml

# Export deployment to file
kubectl get deployment [name] -o yaml > deployment.yaml

Scaling & Updating

# Scale deployment
kubectl scale deployment [name] --replicas=5

# Set image (update)
kubectl set image deployment/[name] [container]=[image:tag]

# Rollout status
kubectl rollout status deployment/[name]

# Rollout history
kubectl rollout history deployment/[name]

# Rollback to previous version
kubectl rollout undo deployment/[name]

# Rollback to specific revision
kubectl rollout undo deployment/[name] --to-revision=2

# Pause rollout
kubectl rollout pause deployment/[name]

# Resume rollout
kubectl rollout resume deployment/[name]

Deleting

# Delete deployment
kubectl delete deployment [name]

# Delete deployment and pods
kubectl delete deployment [name] --cascade=foreground

# Delete by filename
kubectl delete -f deployment.yaml

Services & Ingress

Services

# Get all services
kubectl get services

# Get service details
kubectl describe service [name]

# Create service
kubectl expose deployment [name] --type=LoadBalancer --port=80 --target-port=8080

# Create NodePort service
kubectl expose deployment [name] --type=NodePort --port=80

# Delete service
kubectl delete service [name]

# Get service endpoints
kubectl get endpoints [name]

Port Forwarding

# Forward to pod
kubectl port-forward pod/[name] 8080:8080

# Forward to service
kubectl port-forward svc/[name] 8080:8080

# Forward from specific address
kubectl port-forward --address 0.0.0.0 svc/[name] 8080:8080

ConfigMaps & Secrets

ConfigMaps

# Create ConfigMap from file
kubectl create configmap [name] --from-file=[file]

# Create ConfigMap from literal
kubectl create configmap [name] --from-literal=key=value

# Get ConfigMap
kubectl get configmap [name]

# View ConfigMap data
kubectl get configmap [name] -o yaml

# Edit ConfigMap
kubectl edit configmap [name]

# Delete ConfigMap
kubectl delete configmap [name]

Secrets

# Create secret from file
kubectl create secret generic [name] --from-file=[file]

# Create Docker secret
kubectl create secret docker-registry [name] --docker-server=[server] --docker-username=[user] --docker-password=[pass]

# Get secrets
kubectl get secrets

# Describe secret
kubectl describe secret [name]

# Base64 decode secret value
kubectl get secret [name] -o jsonpath='{.data.[key]}' | base64 -d

# Delete secret
kubectl delete secret [name]

Resources Management

Applying & Creating

# Apply resource from file (create or update)
kubectl apply -f [file.yaml]

# Apply all YAML files in directory
kubectl apply -f [directory]/

# Create from stdin
cat deployment.yaml | kubectl apply -f -

# Diff before applying
kubectl diff -f [file.yaml]

Getting Resources

# Get all resources
kubectl get all

# Get specific resource type
kubectl get [resource-type]

# Get with labels
kubectl get [resource] -l key=value

# Get with column sorting
kubectl get pods --sort-by=.metadata.name

# Export as YAML
kubectl get [resource] [name] -o yaml

# Export as JSON
kubectl get [resource] [name] -o json

# Custom output columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase

Editing & Patching

# Edit resource in editor
kubectl edit [resource] [name]

# Patch resource
kubectl patch [resource] [name] -p '{"spec":{"replicas":3}}'

# Set resource fields
kubectl set [field] [resource/name] [value]

# Delete resource
kubectl delete [resource] [name]

# Delete with grace period
kubectl delete pod [name] --grace-period=10

Troubleshooting

Debugging

# View pod events
kubectl describe pod [name]

# View cluster events
kubectl get events --sort-by='.lastTimestamp'

# View pod logs
kubectl logs [name]

# Follow pod logs
kubectl logs -f [name]

# View previous logs (crash)
kubectl logs [name] --previous

# Debugging pod
kubectl debug pod/[name]

# Copy files from pod
kubectl cp [namespace]/[pod-name]:[path] [local-path]

# Copy files to pod
kubectl cp [local-file] [namespace]/[pod-name]:[path]

Resource Usage

# View node resource usage
kubectl top nodes

# View pod resource usage
kubectl top pods

# View pod resource with sorting
kubectl top pods --sort-by=memory

# View specific namespace usage
kubectl top pods -n [namespace]

Useful Aliases

# Add to .bashrc or .zshrc
alias k='kubectl'
alias kg='kubectl get'
alias kgp='kubectl get pods'
alias kgd='kubectl get deployments'
alias kgs='kubectl get services'
alias kgn='kubectl get nodes'
alias kd='kubectl describe'
alias kdp='kubectl describe pod'
alias kl='kubectl logs'
alias klf='kubectl logs -f'
alias kex='kubectl exec -it'
alias ka='kubectl apply -f'
alias kdel='kubectl delete'
alias kctx='kubectl config current-context'

Best Practices

  • Use namespaces to organize resources
  • Always set resource requests and limits
  • Use health checks (liveness, readiness probes)
  • Tag images with versions, not latest
  • Use RBAC for access control
  • Implement network policies
  • Monitor cluster health and resource usage
  • Keep kubeconfig files secure
  • Use kubectl apply (declarative) over kubectl create (imperative)
  • Regularly back up persistent data

Resources


Last updated: 2026-03-30