Kubernetes 核心命令
查看集群信息
kubectl cluster-info
kubectl version
kubectl api-resources
kubectl api-versions
查看当前上下文
kubectl config get-contexts
kubectl config current-context
切换上下文
kubectl config use-context <context>
查看所有命名空间
kubectl get ns
创建 / 删除
kubectl create ns dev
kubectl delete ns dev
选择 Namespace
kubectl -n dev get pods
查看 Pod
kubectl get pods
kubectl get pods -A
kubectl get pods -owide
kubectl get pods -A --field-selector=status.phase!=Running
kubectl get pods -l app=nginx
查看 Pod 详细信息
kubectl describe pod <pod>
kubectl logs <pod>
kubectl logs -f <pod> -c <container>
进入 Pod 容器
kubectl exec -it <pod> -- sh
kubectl exec -it <pod> -c <container> -- bash
在指定 Namespace
kubectl -n dev exec -it <pod> -- sh
从 Pod 拷贝文件
kubectl cp <ns>/<pod>:<path> <local>
kubectl cp ./local-file dev/my-pod:/tmp/file
查看 Deployment
kubectl get deploy
kubectl describe deploy <name>
更新镜像
kubectl set image deployment/nginx nginx=nginx:1.25
如果有多个 container:
kubectl set image deploy/myapp c1=img1:v1 c2=img2:v2
发布 / 回滚
kubectl rollout restart deploy/nginx
kubectl rollout status deploy/nginx
kubectl rollout history deploy/nginx
kubectl rollout undo deploy/nginx --to-revision=3
查看 Service
kubectl get svc
kubectl describe svc nginx
临时访问(端口转发)
kubectl port-forward svc/my-svc 8080:80
# 关闭:Ctrl+C
ConfigMap
kubectl create configmap conf --from-file=./config.yaml
kubectl get cm
kubectl describe cm conf
kubectl delete cm conf
Secret(Base64 自动编码)
kubectl create secret generic db-pass --from-literal=password=123456
kubectl get secret
kubectl describe secret db-pass
查看节点
kubectl get nodes -owide
查看带特定 label 的节点
kubectl get nodes -l node-role.kubernetes.io/mysql=''
给节点打标签
kubectl label node node01 node-role.kubernetes.io/mysql=
清空节点(排空)
kubectl drain node01 --ignore-daemonsets --delete-emptydir-data
取消排空
kubectl uncordon node01
查看事件
kubectl get events --sort-by=.metadata.creationTimestamp
查看某个 Pod 的事件
kubectl describe pod <pod>
跟踪日志
kubectl logs -f <pod>
kubectl logs -f <pod> -c <container>
kubectl delete pod <pod> --force --grace-period=0
从 YAML 部署
kubectl apply -f app.yaml
kubectl delete -f app.yaml
检查 YAML
kubectl apply --dry-run=client -f app.yaml
查看所有 workload(Pod/Deploy/StatefulSet/DaemonSet/Job)
kubectl get all
查看 Detailed Workload
kubectl get deploy,statefulset,daemonset,job,cronjob
Kubernetes ≥ 1.25:
kubectl debug pod-name -it --image=busybox
kubectl get crd
kubectl describe crd <name>
kubectl auth can-i get pods
kubectl auth can-i delete node --as admin
kubectl auth can-i create pods -n dev
kubectl get pods -A
kubectl logs -f pod
kubectl describe pod pod
kubectl exec -it pod -- sh
kubectl apply -f xxx.yaml
kubectl delete -f xxx.yaml
kubectl get deploy
kubectl set image deploy/myapp app=myimg:v2
kubectl rollout restart deploy/myapp
kubectl rollout undo deploy/myapp
kubectl get svc
kubectl port-forward svc/myapp 8080:80
kubectl get nodes -owide
kubectl drain node01 --ignore-daemonsets
kubectl uncordon node01
kubectl label node node01 key=value
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl cp ns/pod:/file .
kubectl top pod
kubectl top node