Kubernetes/k8s | kubectl常用命令汇总、stern命令行工具安装


一、Kubernetes/k8s基本概念

不了解k8spodsvc等概念的可以阅读一下k8s的官方文档,目前已有中文版:https://kubernetes.io/zh/docs/home/.


CKA证书:

有兴趣的开发或运维同学可以考一下CKA证书


二、kubectl常用命令汇总

# 进入pod容器
kubectl exec -it pod容器名称 bash


# 查看所有命名空间
kubectl get ns


# 查看所有命名空间中的pod
kubectl get pod --all-namespaces


# 查看所有 pod 列表,  -n 后跟 namespace, 查看指定的命名空间
kubectl get pod
kubectl get pod -n kube  
kubectl get pod -o wide


# 查看 RC 和 service 列表, -o wide 查看详细信息
kubectl get rc,svc
kubectl get pod,svc -o wide  
kubectl get pod <pod-name> -o yaml


# 显示 Node 的详细信息
kubectl describe node 192.168.0.212


# 显示 Pod 的详细信息, 特别是查看 pod 无法创建的时候的日志
kubectl describe pod <pod-name>
eg:
kubectl describe pod redis-master-tqds9


# 根据 yaml 创建资源, apply 可以重复执行,create 不行
kubectl create -f pod.yaml
kubectl apply -f pod.yaml


# 基于 pod.yaml 定义的名称删除 pod 
kubectl delete -f pod.yaml 


# k8s集群上删除pod及service
1、先删除pod
2、再删除对应的deployment
否则只是删除pod是不管用的,还会看到pod,因为deployment.yaml文件中定义了副本数量
kubectl get pod -n jenkins
kubectl delete pod jenkins2-8698b5449c-grbdm -n jenkins

kubectl get deployment -n jenkins
kubectl delete deployment jenkins2 -n jenkins



# 删除所有包含某个 label 的pod 和 service
kubectl delete pod,svc -l name=<label-name>


# 删除所有 Pod
kubectl delete pod --all


# 查看 endpoint 列表
kubectl get endpoints


# 执行 pod 的 date 命令
kubectl exec <pod-name> -- date
kubectl exec <pod-name> -- bash
kubectl exec <pod-name> -- ping 10.24.51.9


# 通过bash获得 pod 中某个容器的TTY,相当于登录容器
kubectl exec -it <pod-name> -c <container-name> -- bash
eg:
kubectl exec -it redis-master-cln81 -- bash


# 查看容器的日志
kubectl logs <pod-name>
kubectl logs -f <pod-name> # 实时查看日志
kubectl log  <pod-name>  -c <container_name> # 若 pod 只有一个容器,可以不加 -c 

kubectl logs -l app=frontend # 返回所有标记为 app=frontend 的 pod 的合并日志。


# 查看指定pod的日志
kubectl logs <pod_name>
kubectl logs -f <pod_name> #类似tail -f的方式查看(tail -f 实时查看日志文件 tail -f 日志文件log)

# 查看指定pod中指定容器的日志
kubectl logs <pod_name> -c <container_name>

kubectl logs pod_name -c container_name -n namespace (一次性查看)
kubectl logs -f <pod_name> -n namespace (tail -f方式实时查看)
例:kubectl logs --tail -f java-fbo-new--api-6d955f8f4-kmqw2  -n java-fbo-new


# 查看注释
kubectl explain pod
kubectl explain pod.apiVersion

# 查看节点 labels
kubectl get node --show-labels

# 重启 pod
kubectl get pod <POD名称> -n <NAMESPACE名称> -o yaml | kubectl replace --force -f -

# 修改网络类型
kubectl patch service istio-ingressgateway -n istio-system -p '{"spec":{"type":"NodePort"}}'

# 伸缩 pod 副本
# 可用于将Deployment及其Pod缩小为零个副本,实际上杀死了所有副本。当您将其缩放回1/1时,将创建一个新的Pod,重新启动您的应用程序。
kubectl scale deploy/nginx-1 --replicas=0
kubectl scale deploy/nginx-1 --replicas=1

# 查看前一个 pod 的日志,logs -p 选项 
kubectl logs --tail 100 -p user-klvchen-v1.0-6f67dcc46b-5b4qb > pre.log

三、stern命令行工具安装

安装命令:

yum -y install wget
wget https://github.com/wercker/stern/releases/download/1.11.0/stern_linux_amd64
sudo install stern_linux_amd64 /usr/local/bin/stern

stern常用命令:

# 实时查看当前 Namespace 中所有 Pod 中所有容器的日志
stern  . 

# 查看命名空间所有容器日志(多个容器日志汇总到一个控制台输出)
stern . -n 命名空间

# 实时查看指定时间范围内容器的日志,下面的例子表示是 15 分钟内
stern auth -t --since 15m 

# 实时查看指定命名空间中容器的日志
stern kubernetes-dashboard --namespace 命名空间 

# 实时查看所有命名空间中符合指定标签容器的日志
stern --all-namespaces -l run=nginx 

四、其他kubectl命令文章

猜你喜欢

转载自blog.csdn.net/qq_25112523/article/details/121402506