kubectl 备忘单
# 查看集群信息
kubectl cluster-info
# 查看某个node的详细信息
kubectl describe node node1
# 查看pod信息
kubectl get pods -n kube-system
# 查看所有namespace
kubectl get namespace
NAME STATUS AGE
default Active 3d1h
kube-node-lease Active 3d1h
kube-public Active 3d1h
kube-system Active 3d1h
kubernetes-dashboard Active 7m28s
# 重新运行一个pod
kubectl get pod PODNAME -n NAMESPACE -o yaml | kubectl replace --force -f -
# 查看日志
kubectl logs <POD> [docker_name]
# 重置
[root@vm101 ~]# kubeadm reset
[reset] Reading configuration from the cluster...
[reset] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[reset] WARNING: Changes made to this host by 'kubeadm init' or 'kubeadm join' will be reverted.
[reset] Are you sure you want to proceed? [y/N]: y
[preflight] Running pre-flight checks
[reset] Stopping the kubelet service
[reset] Unmounting mounted directories in "/var/lib/kubelet"
# 查看状态
kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
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配置文件
配置文件解读
[root@ty01 ~]# kubectl config view --minify
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://172.31.0.50:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
查看所有支持的对象
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