# 查看Pod
kubectl get pod # 当前命名空间
kubectl get pod -n test # 指定命名空间
kubectl get pod -A # 所有命名空间
kubectl get pod -o wide # 详细信息
kubectl get pod --show-labels # 显示标签
# 查看其他资源
kubectl get deployment
kubectl get service
kubectl get namespace
kubectl get node
kubectl get configmap
kubectl get secret
详细查询
# 查看单个资源
kubectl get pod nginx-pod-xxx
kubectl get deployment mysql-deploy -n prod
# 查看完整配置
kubectl get pod nginx-pod -o yaml
kubectl get deployment nginx-deploy -o yaml
kubectl get pod nginx-pod -o json
# 查看描述信息
kubectl describe pod nginx-pod
kubectl describe deployment nginx-deploy
筛选查询
# 按标签筛选
kubectl get pod -l app=nginx
# 按字段筛选
kubectl get pod --field-selector=status.phase=Running
# 排序输出
kubectl get pod --sort-by='.status.startTime'
# 导出运行中资源的配置
kubectl get deployment nginx-deploy -o yaml > deployment.yaml
kubectl get service nginx-svc -o yaml --export > service.yaml
实用组合命令
# 一键清理失败的Pod
kubectl delete pod --field-selector=status.phase=Failed
# 批量重启Deployment
kubectl rollout restart deployment -n default
# 查看所有容器镜像版本
kubectl get pods -o jsonpath='{.items[*].spec.containers[*].image}'
# 列出所有Pod及其运行节点
kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName
# 检查集群状态
kubectl get componentstatuses
kubectl version
kubectl cluster-info
常用命令别名
# 添加到 ~/.bashrc 或 ~/.zshrc
alias k='kubectl'
alias kg='kubectl get'
alias kd='kubectl describe'
alias kc='kubectl create'
alias ka='kubectl apply'
alias kdel='kubectl delete'
alias ke='kubectl edit'
alias kl='kubectl logs'
alias kex='kubectl exec -it'
alias kpf='kubectl port-forward'
alias kt='kubectl top'
alias kns='kubectl config set-context --current --namespace'
Comments NOTHING