Kubernetes 集群管理

精选 Kubernetes 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。 本页面收录了常用的 kubectl 命令及参数标志。

#查看和查找资源

#节点 (Nodes)

kubectl get no # 显示所有节点信息
kubectl get no -o wide # 显示更详细的节点信息
kubectl describe no # 显示节点的详细描述
kubectl get no -o yaml # 以 YAML 格式显示节点详情
kubectl get node --selector=[label_name] # 按指定标签过滤节点
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type="ExternalIP")].address}'
# 输出 jsonpath 表达式定义的字段信息
kubectl top node [node_name] # 显示节点的 CPU/内存/存储 使用情况

资源名称: nodes,缩写: no

#Pod 容器组

kubectl get po # 显示所有 Pod 信息
kubectl get po -o wide
kubectl describe po
kubectl get po --show-labels # 查看 Pod 的标签
kubectl get po -l app=nginx
kubectl get po -o yaml
kubectl get pod [pod_name] -o yaml --export
kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml
# 以 YAML 格式导出 Pod 信息到文件
kubectl get pods --field-selector status.phase=Running
# 使用字段选择器过滤 Pod 信息

资源名称: pods,缩写: po

#命名空间 (Namespaces)

kubectl get ns
kubectl get ns -o yaml
kubectl describe ns

资源名称: namespaces,缩写: ns

#部署 (Deployments)

kubectl get deploy
kubectl describe deploy
kubectl get deploy -o wide
kubectl get deploy -o yaml

资源名称: deployments,缩写: deploy

#服务 (Services)

kubectl get svc
kubectl describe svc
kubectl get svc -o wide
kubectl get svc -o yaml
kubectl get svc --show-labels

资源名称: services,缩写: svc

#守护进程集 (Daemon Sets)

kubectl get ds
kubectl describe ds --all-namespaces
kubectl describe ds [daemonset_name] -n [namespace_name]
kubectl get ds [ds_name] -n [ns_name] -o yaml

资源名称: daemonsets,缩写: ds

#事件 (Events)

kubectl get events
kubectl get events -n kube-system
kubectl get events -w

资源名称: events,缩写: ev

#日志 (Logs)

kubectl logs [pod_name]
kubectl logs --since=1h [pod_name]
kubectl logs --tail=20 [pod_name]
kubectl logs -f -c [container_name] [pod_name]
kubectl logs [pod_name] > pod.log

#服务账户 (Service Accounts)

kubectl get sa
kubectl get sa -o yaml
kubectl get serviceaccounts default -o yaml >./sa.yaml
kubectl replace serviceaccount default -f ./sa.yaml

资源名称: serviceaccounts,缩写: sa

#副本集 (Replica Sets)

kubectl get rs
kubectl describe rs
kubectl get rs -o wide
kubectl get rs -o yaml

资源名称: replicasets,缩写: rs

#角色 (Roles)

kubectl get roles --all-namespaces
kubectl get roles --all-namespaces -o yaml

#密钥 (Secrets)

kubectl get secrets
kubectl get secrets --all-namespaces
kubectl get secrets -o yaml

#配置映射 (Config Maps)

资源名称: configmaps,缩写: cm

kubectl get cm
kubectl get cm --all-namespaces
kubectl get cm --all-namespaces -o yaml

#入口网关 (Ingresses)

资源名称: ingresses,缩写: ing

kubectl get ing
kubectl get ing --all-namespaces

#持久卷 (Persistent Volumes)

资源名称: persistentvolumes,缩写: pv

kubectl get pv
kubectl describe pv

#持久卷声明 (Persistent Volume Claims)

资源名称: persistentvolumeclaims,缩写: pvc

kubectl get pvc
kubectl describe pvc

#存储类 (Storage Class)

资源名称: storageclasses,缩写: sc

kubectl get sc
kubectl get sc -o yaml

#查看多种资源

kubectl get svc, po
kubectl get deploy, no
kubectl get all
kubectl get all --all-namespaces

#更新资源

#污点 (Taint)

kubectl taint [node_name] [taint_name]

#标签 (Label)

kubectl label [node_name] disktype=ssd
kubectl label [pod_name] env=prod

#维护 / 可调度

kubectl cordon [node_name] # 将节点标记为不可调度(维护模式)
kubectl uncordon [node_name] # 将节点标记为可调度

#驱逐 (Drain)

kubectl drain [node_name] # 安全驱逐节点上的所有 Pod

#节点 / Pod

kubectl delete node [node_name]
kubectl delete pod [pod_name]
kubectl edit node [node_name]
kubectl edit pod [pod_name]

#无状态应用 / 命名空间

kubectl edit deploy [deploy_name]
kubectl delete deploy [deploy_name]
kubectl expose deploy [deploy_name] --port=80 --type=NodePort
kubectl scale deploy [deploy_name] --replicas=5
kubectl delete ns
kubectl edit ns [ns_name]

#服务 (Service)

kubectl edit svc [svc_name]
kubectl delete svc [svc_name]

#守护进程集 (Daemon Set)

kubectl edit ds [ds_name] -n kube-system
kubectl delete ds [ds_name]

#服务账户 (Service Account)

kubectl edit sa [sa_name]
kubectl delete sa [sa_name]

#注解 (Annotations)

kubectl annotate po [pod_name] [annotation]
kubectl annotateno [node_name]

#创建资源

#创建 Pod

kubectl create -f [name_of_file]
kubectl apply -f [name_of_file]
kubectl run [pod_name] --image=nginx --restart=Never
kubectl run [pod_name] --generator=run-pod/v1 --image=nginx
kubectl run [pod_name] --image=nginx --restart=Never

#创建服务 (Service)

kubectl create svc nodeport [svc_name] --tcp=8080:80

#创建无状态应用 (Deployment)

kubectl create -f [name_of_file]
kubectl apply -f [name_of_file]
kubectl create deploy [deploy_name] --image=nginx

#交互式操作

kubectl run [pod_name] --image=busybox --rm -it --restart=Never --sh

#输出 YAML

kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yaml
kubectl get po [pod_name] -o yaml --export > pod.yaml

#帮助信息

kubectl -h
kubectl create -h
kubectl run -h
kubectl explain deploy.spec

#其他常用操作

#API 接口

kubectl get --raw /apis/metrics.k8s.io/

#集群信息

kubectl config
kubectl cluster-info
kubectl get componentstatus

#🔗 参考资源