工具 & 效率10 分钟阅读
Kubernetes 运维命令 100 条
本文基于 Kubernetes 1.36 的 kubectl 命令体系,整理集群、工作负载、网络、存储、配置、发布和排障常用的 100 条命令。示例命名空间为 prod。
2026年8月19日阅读—点赞—收藏—
dba100kubernetes墨力计划
在知识库中专注阅读,并随时返回相关工具与课程
本文基于 Kubernetes 1.36 的 kubectl 命令体系,整理集群、工作负载、网络、存储、配置、发布和排障常用的 100 条命令。示例命名空间为 prod。
本文基于 Kubernetes 1.36 的 kubectl 命令体系,整理集群、工作负载、网络、存储、配置、发布和排障常用的 100 条命令。示例命名空间为 prod。
删除、强制终止、扩缩容、驱逐、污点和回滚会影响线上服务,执行前应确认上下文、命名空间、选择器和 PodDisruptionBudget。
1kubectl version1kubectl cluster-info1kubectl config current-context1kubectl config get-contexts1kubectl config use-context prod-cluster1kubectl config set-context --current --namespace=prod1kubectl api-resources1kubectl api-versions1kubectl explain deployment.spec1kubectl get --raw='/readyz?verbose'1kubectl get nodes -o wide1kubectl describe node worker-11kubectl top nodes1kubectl cordon worker-11kubectl drain worker-1 --ignore-daemonsets --delete-emptydir-data1kubectl uncordon worker-11kubectl label node worker-1 workload=database1kubectl taint node worker-1 dedicated=database:NoSchedule1kubectl get namespaces1kubectl create namespace prod1kubectl -n prod get pods -o wide1kubectl -n prod get pods -w1kubectl -n prod describe pod api-01kubectl -n prod get pod api-0 -o yaml1kubectl -n prod get pods -l app=api1kubectl -n prod get pods --field-selector=status.phase!=Running1kubectl -n prod top pod --containers1kubectl -n prod exec -it api-0 -c api -- sh1kubectl -n prod exec api-0 -- env1kubectl -n prod delete pod api-01kubectl -n prod logs api-01kubectl -n prod logs -f api-0 -c api1kubectl -n prod logs --tail=200 api-01kubectl -n prod logs --since=1h api-01kubectl -n prod logs --previous api-0 -c api1kubectl -n prod logs -l app=api --all-containers --prefix1kubectl -n prod cp ./app.conf api-0:/tmp/app.conf -c api1kubectl -n prod cp api-0:/tmp/diag.tar.gz ./diag.tar.gz -c api1kubectl -n prod debug -it api-0 --image=busybox:1.36 --target=api1kubectl debug node/worker-1 -it --image=ubuntu1kubectl -n prod get deployments1kubectl -n prod create deployment api --image=nginx:1.291kubectl -n prod rollout status deployment/api1kubectl -n prod rollout history deployment/api1kubectl -n prod set image deployment/api api=registry.example.com/api:v21kubectl -n prod rollout pause deployment/api1kubectl -n prod rollout resume deployment/api1kubectl -n prod rollout undo deployment/api1kubectl -n prod rollout undo deployment/api --to-revision=31kubectl -n prod rollout restart deployment/api1kubectl -n prod scale deployment/api --replicas=61kubectl -n prod autoscale deployment/api --min=2 --max=10 --cpu-percent=701kubectl -n prod get hpa1kubectl -n prod get statefulsets1kubectl -n prod rollout restart statefulset/db1kubectl -n prod get daemonsets1kubectl -n prod create job migrate --image=registry.example.com/app:v2 -- ./migrate1kubectl -n prod create job --from=cronjob/backup backup-manual1kubectl -n prod get cronjobs1kubectl -n prod patch cronjob backup -p '{"spec":{"suspend":true}}'1kubectl -n prod get services1kubectl -n prod expose deployment api --port=80 --target-port=80801kubectl -n prod get endpointslices -l kubernetes.io/service-name=api1kubectl -n prod port-forward service/api 8080:801kubectl -n prod get ingress1kubectl -n prod get networkpolicies1kubectl -n prod run netshoot --rm -it --restart=Never --image=nicolaka/netshoot -- bash1kubectl -n prod run dns-test --rm -it --restart=Never --image=busybox:1.36 -- nslookup api1kubectl -n prod run curl --rm -it --restart=Never --image=curlimages/curl -- curl -fsS http://api/health1kubectl -n kube-system logs -l k8s-app=kube-dns --tail=2001kubectl -n prod create configmap app-config --from-file=app.yaml1kubectl -n prod get configmap app-config -o yaml1kubectl -n prod create secret generic db-auth --from-literal=username=app --from-literal=password='REPLACE_ME'1kubectl -n prod get secret db-auth -o jsonpath='{.data.username}' | base64 -d; echo1kubectl -n prod create secret tls app-tls --cert=tls.crt --key=tls.key1kubectl -n prod get pvc1kubectl get pv1kubectl get storageclass1kubectl -n prod describe pvc data-db-01kubectl -n prod get volumesnapshots1kubectl apply --dry-run=server -f app.yaml1kubectl apply -f app.yaml1kubectl diff -f app.yaml1kubectl delete -f app.yaml1kubectl -n prod get deployment api -o yaml > api.yaml1kubectl -n prod edit deployment api1kubectl -n prod patch deployment api --type=merge -p '{"spec":{"replicas":4}}'1kubectl auth can-i --list -n prod1kubectl auth can-i delete pods -n prod1kubectl auth can-i get secrets --as=system:serviceaccount:prod:api -n prod1kubectl -n prod get events --sort-by=.metadata.creationTimestamp1kubectl -n prod events --types=Warning1kubectl -n prod wait --for=condition=available deployment/api --timeout=120s1kubectl -n prod wait --for=condition=Ready pod -l app=api --timeout=120s1kubectl -n prod get all1kubectl -n prod get pods -o custom-columns='NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName'1kubectl -n prod get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[*].image}{"\n"}{end}'1kubectl -n prod get pod api-0 -o jsonpath='{.metadata.finalizers}'1kubectl -n kube-system get leases1{ kubectl -n prod get pods -o wide; kubectl -n prod get events --sort-by=.lastTimestamp; kubectl -n prod top pods; } > k8s-prod-check.txtKubernetes 排障应从上下文、事件、控制器状态、Pod 条件、日志、网络和存储逐层定位。线上操作务必显式指定命名空间,优先声明式变更,并为回滚、资源配额和中断预算留出余量。