8. kubectl 命令大全#

        flowchart LR
    subgraph "kubectl Workflow"
        A[Configure Context] --> B[Get / Describe]
        B --> C[Create / Apply]
        C --> D[Debug / Logs]
        D --> E[Scale / Update]
        E --> F[Delete / Cleanup]
    end
    

配置管理#

# View current config
kubectl config view

# List contexts
kubectl config get-contexts

# Switch context
kubectl config use-context my-cluster

# Set default namespace
kubectl config set-context --current --namespace=dev

# Merge kubeconfig files
export KUBECONFIG=~/.kube/config:~/.kube/config-prod
kubectl config view --merge --flatten > ~/.kube/config-merged

增删改查操作#

查询资源(Get)#

# Basic get
kubectl get pods
kubectl get pods -o wide          # more columns
kubectl get pods -o yaml          # full YAML
kubectl get pods -o json          # full JSON
kubectl get pods -A               # all namespaces
kubectl get pods --show-labels    # show labels

# JSONPath output
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.capacity.cpu}{"\n"}{end}'

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

# Sort
kubectl get pods --sort-by=.metadata.creationTimestamp

# Watch
kubectl get pods -w

查看详情(Describe)#

kubectl describe pod myapp-xyz
kubectl describe node worker-1
kubectl describe svc myapp

创建与应用(Create / Apply)#

# Imperative create
kubectl create deployment web --image=nginx:1.25 --replicas=3
kubectl create service clusterip web --tcp=80:80
kubectl create configmap myconfig --from-literal=key1=value1
kubectl create secret generic mysecret --from-literal=password=s3cret

# Declarative apply (recommended)
kubectl apply -f deployment.yaml
kubectl apply -f ./manifests/        # apply all files in directory
kubectl apply -f https://example.com/manifest.yaml

# Dry run + generate YAML
kubectl create deployment web --image=nginx --dry-run=client -o yaml > deployment.yaml

编辑与补丁(Edit / Patch)#

# Edit in-place (opens $EDITOR)
kubectl edit deployment web

# Strategic merge patch
kubectl patch deployment web -p '{"spec":{"replicas":5}}'

# JSON patch
kubectl patch deployment web --type='json' \
  -p='[{"op":"replace","path":"/spec/replicas","value":5}]'

删除(Delete)#

kubectl delete pod myapp-xyz
kubectl delete -f deployment.yaml
kubectl delete deployment web
kubectl delete pods --all -n dev
kubectl delete pods -l app=myapp

# Force delete (stuck pods)
kubectl delete pod myapp-xyz --grace-period=0 --force

调试排错#

# View logs
kubectl logs myapp-xyz
kubectl logs myapp-xyz -c sidecar    # specific container
kubectl logs myapp-xyz --previous     # previous instance (crash)
kubectl logs -f myapp-xyz             # follow
kubectl logs -l app=myapp --all-containers  # by label

# Execute command
kubectl exec -it myapp-xyz -- /bin/sh
kubectl exec myapp-xyz -- env
kubectl exec myapp-xyz -c sidecar -- cat /etc/config

# Port forward
kubectl port-forward pod/myapp-xyz 8080:80
kubectl port-forward svc/myapp 8080:80

# Copy files
kubectl cp myapp-xyz:/var/log/app.log ./app.log
kubectl cp ./config.yaml myapp-xyz:/etc/config/

# Resource usage
kubectl top nodes
kubectl top pods
kubectl top pods --sort-by=memory

# Debug with ephemeral container
kubectl debug -it myapp-xyz --image=nicolaka/netshoot --target=myapp

# Debug node
kubectl debug node/worker-1 -it --image=ubuntu

资源管理#

# Labels
kubectl label pod myapp-xyz env=production
kubectl label pod myapp-xyz env-                    # remove
kubectl label pods --all env=staging -n dev

# Annotations
kubectl annotate pod myapp-xyz description="Main app"

# Taints and tolerations
kubectl taint nodes worker-1 dedicated=gpu:NoSchedule
kubectl taint nodes worker-1 dedicated-               # remove

# Cordon / Drain
kubectl cordon worker-1                               # mark unschedulable
kubectl drain worker-1 --ignore-daemonsets --delete-emptydir-data
kubectl uncordon worker-1                              # mark schedulable

速查表#

任务

命令

列出所有资源类型

kubectl api-resources

查看资源字段说明

kubectl explain pod.spec.containers

查看事件

kubectl get events --sort-by=.lastTimestamp

查看发布状态

kubectl rollout status deployment/web

查看发布历史

kubectl rollout history deployment/web

回滚

kubectl rollout undo deployment/web

扩缩容

kubectl scale deployment web --replicas=5

自动扩缩容

kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=80

运行一次性 Pod

kubectl run debug --rm -it --image=busybox -- /bin/sh

代理到 API Server

kubectl proxy --port=8001

效率提升技巧#

命令别名(~/.bashrc 或 ~/.zshrc)#

alias k='kubectl'
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kgd='kubectl get deployments'
alias kga='kubectl get all'
alias kaf='kubectl apply -f'
alias kdf='kubectl delete -f'
alias kdp='kubectl describe pod'
alias kl='kubectl logs -f'
alias ke='kubectl exec -it'
alias kns='kubectl config set-context --current --namespace'

kubectl 插件管理(krew)#

# Install krew
(
  set -x; cd "$(mktemp -d)" &&
  OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
  ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/aarch64/arm64/')" &&
  KREW="krew-${OS}_${ARCH}" &&
  curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
  tar zxvf "${KREW}.tar.gz" &&
  ./"${KREW}" install krew
)

# Useful plugins
kubectl krew install ctx       # switch contexts
kubectl krew install ns        # switch namespaces
kubectl krew install neat      # clean up YAML output
kubectl krew install tree      # show resource hierarchy
kubectl krew install images    # show container images
kubectl krew install stern     # multi-pod log tailing