kubectl命令速查

工作中常用的kubectl 常用命令速查表
更新于: 2023-09-20 16:54:36

安装 kubectl

brew install kubectl

前置配置

ali-prod 为敏感信息,所以,不便公开;  一般可以向运维同学“索取”。

这里可以用 kcl 辅助切换环境

# production
export KUBECONFIG=~/.kube/ali-prod
# beta
export KUBECONFIG=~/.kube/kube_config

针对不同环境 suffix

# prod
--kubeconfig ~/.kube/ali-prod

# beta
--kubeconfig ~/.kube/kube_config

# 环境变量调用方式
KUBECONFIG=~/.kube/ali-prod
KUBECONFIG=~/.kube/kube_config

# 取得当前的KUBECONFIG
echo $KUBECONFIG

常用命令列表

功能命令
滚动重启
# homework-server-prod-api 换成具体的服务名
kubectrl rollout restart deployment homework-server-prod-api -n study-center
查看 active pods
# 简单的信息
kubectl get pods -n study-center | grep homework
# 详细的信息如 ip/节点
kubectl get pods -n study-center -o wide | grep homework
ssh 到具体的 pod
kubectl -n study-center exec -it homework-server-prod-api-8f566795f-rnp9g bash
cp pods 里的 log
kubectl cp study-center/homework-server-prod-api-8f566795f-npk9z:/app/logs/acc.log ./.tmp/acc.log
查看实时 logs
# 打印所有
kubectl logs -f homework-server-prod-api-8f566795f-ms9vw -n study-center -c api
# 打印实时
kubectl logs -f --tail 100 homework-server-prod-api-865dd745db-l6zqt -n study-center -c api >> .tmp/homework-server.acc.log
扩容(min/max)
# 仅指定 min
kubectl patch hpa homework-server-prod-api  -p '{"spec":{"minReplicas":2}}' -n study-center
# 仅操作 max
kubectl patch hpa homework-server-prod-api  -p '{"spec":{"maxReplicas":10}}' -n study-center
# 同时指定 min/max
kubectl patch hpa homework-server-prod-api  -p '{"spec":{"minReplicas":2, "maxReplicas":10}}' -n study-center
重启(删除)
# 有批量,不建议使用
kubectl delete pod homework-server-prod-api-8f566795f-8nhls -n study-center
删除
# 删除,不重启
kubectl delete deploment homework-server-prod-api-8f566795f-8nhls -n study-center
查看ns 下的 pods 内存
kubectl top pod -n study-center | grep homework
查看某个 pods 细节
kubectl describe pod homework-server-prod-api-86d8f449c6-9jspx -n study-center

常用的 .ytl.yml 配置

vars:
  suffix: -n study-center
tasks:
  env:
    - echo $KUBECONFIG
    - echo ${{ vars.suffix }}
  reboot:
    - kubectl delete pod {0} -n study-center
  desc:
    - kubectl describe pod {0} -n study-center
  exec:
    - kubectl -n study-center exec -it {0} bash
  list:
    - kubectl get pods -n study-center -o wide | grep homework
  list_mem:
    - kubectl top pod -n study-center | grep homework
  logs:
    - kubectl logs -f --tail 100 {0} -n study-center -c api >> .tmp/homework-server.acc.log
  log_cp:
    - kubectl cp study-center/{0}:/app/logs/homework-server.access.log ./.tmp/homework-server.access.log

滚动条重启

滚动重启截图

查看Pod 细节

查看节点细节