引言
本文主要介绍kubeadm、kubectl中常用的命令。
1.kubeadm
| 分类 | 子类 | 命令 | 备注 |
|---|---|---|---|
| init | —— | kubeadm init \ |
—apiserver-advertise-address=10.0.16.16 \
—image-repository registry.aliyuncs.com/google_containers \
—kubernetes-version v1.22.2 \
—pod-network-cidr=10.244.0.0/16 \
—ignore-preflight-errors=all | 初始化集群 |
| join | —— | kubeadm join 10.0.16.16:6443 —token yqr84c.vc3mwgp5dxdgq70o —discovery-token-ca-cert-hash sha256:a45fdebacb2276a9a14aa4b5a4ac61247f8057d498f52994ec5cd31977a1bacf | 【node】join到k8s集群中 |
| token | —— | kubeadm token create —print-join-command
| 生成新的token【方便新node的join】 |
| reset | —— | kubeadm reset | 重置集群 |
| config | —— | kubeadm config images list | 获取kubeadm的镜像列表 |
2.kubectl
| 分类 | 子类 | 命令 | 备注 |
|---|---|---|---|
| nodes | get | kubectl get nodes | 查看节点状态 |
| delete | kubectl delete nodes 节点name | 删除节点 | |
| pod | get | kubectl get pod | 【简略】 查看pods状态 |
| kubectl get pod -A 等效 kubectl get pod —all-namespaces |
查看集群下的所有命名空间下的资源 | ||
| kubectl get pod -n kube-system | 【指定“命名空间= kube-system”】 查看pods状态 | ||
| kubectl get pod -A -o wide | 获取所有pod信息【ip、pod被调度到哪个节点】 | ||
| kubectl get pod nginx-pod -n default -o json 输出保存文件 kubectl get pod nginx-pod -n default -o json > a.json |
【输出json信息】将指定的pod输出为json | ||
| kubectl get pod nginx-pod -n default** -o yaml |
输出保存文件
kubectl get pod nginx-pod -n default -o yaml > a.yaml | 【输出yaml信息】将指定的pod输出为yaml |
| | describe | kubectl describe pod pod的名字 -n namespace | 【指定Pod】详情信息 |
| | delete | kubectl delete pod pod的名字 -n namespace —force —grace-period=0
等效
kubectl delete-f xxx.yaml | 【指定Pod】强制删除 |
| | logs | kubectl logs pod的名字 | 查看日志 |
| | exec | kubectl exec -it nginx-pod — bash
| 【指定Pod】进入容器 |
| | | | |
| 操作资源(yaml) | create | kubectl create -f xxx.yaml | 创建资源对象 |
| | apply | kubectl apply-f xxx.yaml | 更新资源对象 |
| | delete | kubectl delete-f xxx.yaml** | 删除资源对象 |
|
| | | |
| 其他 | scale | | |
| | port-forward | | |
| | rollout history | | |
| | rollout undo | | |
# 部署应用kubectl apply -f app.yaml# 查看 podkubectl get pod -o wide# 查看 pod 详情kubectl describe pod pod-name# 查看 logkubectl logs pod-name# 进入 Pod 容器终端, -c container-name 可以指定进入哪个容器。kubectl exec -it pod-name -- bash# 伸缩扩展副本kubectl scale deployment test-k8s --replicas=5# 把集群内端口映射到节点kubectl port-forward pod-name 8090:8080# 查看历史kubectl rollout history deployment test-k8s# 回到上个版本kubectl rollout undo deployment test-k8s# 回到指定版本kubectl rollout undo deployment test-k8s --to-revision=2# 删除部署kubectl delete deployment test-k8s
