kubectl 备忘单

  1. # 查看集群信息
  2. kubectl cluster-info
  3. # 查看某个node的详细信息
  4. kubectl describe node node1
  5. # 查看pod信息
  6. kubectl get pods -n kube-system
  7. # 查看所有namespace
  8. kubectl get namespace
  9. NAME STATUS AGE
  10. default Active 3d1h
  11. kube-node-lease Active 3d1h
  12. kube-public Active 3d1h
  13. kube-system Active 3d1h
  14. kubernetes-dashboard Active 7m28s
  15. # 重新运行一个pod
  16. kubectl get pod PODNAME -n NAMESPACE -o yaml | kubectl replace --force -f -
  17. # 查看日志
  18. kubectl logs <POD> [docker_name]
  19. # 重置
  20. [root@vm101 ~]# kubeadm reset
  21. [reset] Reading configuration from the cluster...
  22. [reset] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
  23. [reset] WARNING: Changes made to this host by 'kubeadm init' or 'kubeadm join' will be reverted.
  24. [reset] Are you sure you want to proceed? [y/N]: y
  25. [preflight] Running pre-flight checks
  26. [reset] Stopping the kubelet service
  27. [reset] Unmounting mounted directories in "/var/lib/kubelet"
  28. # 查看状态
  29. kubectl get cs
  30. Warning: v1 ComponentStatus is deprecated in v1.19+
  31. NAME STATUS MESSAGE ERROR
  32. controller-manager Healthy ok
  33. scheduler Healthy ok
  34. etcd-0 Healthy {"health":"true","reason":""}

journalctl -l -u kubelet 看kubelet日志

k8s相关镜像可以先在github上建一个项目, 然后给每个镜像定义一个Dockerfile, 内容为FROM ${K8S_IMGS}, 然后可以通过阿里云容器镜像服务将这个镜像构建到阿里云上, 然后pull到本地后重新打tag为k8s镜像原来得名字
使用flannel参考https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/#pod-network , 要加上—pod-network-cidr=10.244.0.0/16

日志级别最高是9,使用-v=9

配置文件读取顺序
--kubeconfig 命令行指定,优先级最高
$KUBECONFIG 环境变量指定,优先级次之
${HOME}/.kube/config 默认配置文件路径
通过kubectl config管理kubectl配置文件

配置文件解读

  1. [root@ty01 ~]# kubectl config view --minify
  2. apiVersion: v1
  3. clusters:
  4. - cluster:
  5. certificate-authority-data: DATA+OMITTED
  6. server: https://172.31.0.50:6443
  7. name: kubernetes
  8. contexts:
  9. - context:
  10. cluster: kubernetes
  11. user: kubernetes-admin
  12. name: kubernetes-admin@kubernetes
  13. current-context: kubernetes-admin@kubernetes
  14. kind: Config
  15. preferences: {}
  16. users:
  17. - name: kubernetes-admin
  18. user:
  19. client-certificate-data: REDACTED
  20. client-key-data: REDACTED

image.png
查看所有支持的对象
kubectl api-resources

查看对象的信息
kubectl get [node|pod|service|namespace|rs]
-o wide 以详细信息查看对象
-o [yaml|json] 以更详细的信息查看对象
-w watch变化
—show-labbels 查看label

展示资源的详细信息和event
kubbectl describe

查看node的详细信息和event
kubectl describe nodes kubernetes-node-emt8.c.myproject.internal

查看pod的详细信息和event
kubectl describe pods/nginx

查看所有的pod详细信息和event
kubectl describe pods

查看所有的pod详细信息和event的同时使用label进行过滤
kubectl describe po -l name=myLabel

kubectl exec
进入pod,用于debug
kubectl客户端 -> 控制面 -> kubelet -> 容器

kubectl logs
查看pod的标准输入和输出

查看一个pod的日志
kubectl logs nginx

查看一个pod中多个容器的日志
kubectl logs nginx —all-containers=true

过滤含有相关label的容器的日志
kubectl logs -l app=nginx —all-containers=true

返回pod ruby中已经停止的容器web-1的日志快照
kubectl logs -p -c ruby web-1

查看web-1 pod中容器 ruby的日志
kubectl logs -f -c ruby web-1
kubectl logs -f -l app=nginx —all-containers=true

查看日志时指定条数
kubectl logs —tail=20 nginx

输出pod nginx中最近一小时内产生的所有日志
kubectl logs —since=1h nginx

指定起始时间
kubectl logs nginx-deployment-7bc6d87686-94vkr —since-time ‘2022-05-15T07:20:50.52+08:00’

查看名称为hello的job的第一个容器日志
kubectl logs job/hello

查看名称为nginx的deployment的中nginx-1的容器日志
kubectl logs deployment/nginx -c nginx-1

官网文档
https://kubernetes.io/docs/reference/kubectl/kubectl/