|NO.Z.00017|——————————|^^ 标准 ^^|——|KuberNetes&标准.V16|——|常用操作.V02|操作参数|

一、常用操作
### --- 常用操作参数
~~~     # 查看所有 pod 列表,  -n 后跟 namespace, 查看指定的命名空间

[root@k8s-master01 ~]#  kubectl get pod
[root@k8s-master01 ~]#  kubectl get pod -n kube-system      // 查看指定命令空间的Pod  
[root@k8s-master01 ~]#  kubectl get pod -o wide             // 查看更详细的信息,比如Pod所在的节点
[root@k8s-master01 ~]#  kubectl get pod --show-labels       // 获取pod并查看pod的标签
~~~     # 查看 RC 和 service 列表, -o wide 查看详细信息

[root@k8s-master01 ~]#  kubectl get rc,svc
[root@k8s-master01 ~]#  kubectl get pod,svc -o wide  
[root@k8s-master01 ~]#  kubectl get pod <pod-name> -o yaml
~~~     # 显示 Node 的详细信息
[root@k8s-master01 ~]#  kubectl describe node 192.168.0.212 #可以跟Node IP或者主机名

~~~     # 显示 Pod 的详细信息, 特别是查看 pod 无法创建的时候的日志
[root@k8s-master01 ~]#  kubectl describe pod <pod-name>
~~~     # eg:
[root@k8s-master01 ~]#  kubectl describe pod redis-master-tqds9
~~~     # 根据 yaml 创建资源, apply 可以重复执行,create 不行

[root@k8s-master01 ~]#  kubectl create -f pod.yaml
[root@k8s-master01 ~]#  kubectl apply -f pod.yaml
~~~     # 基于 pod.yaml 定义的名称删除指定资源
[root@k8s-master01 ~]#  kubectl delete -f pod.yaml 
 
~~~     # 删除所有包含某个 label 的pod 和 service
[root@k8s-master01 ~]#  kubectl delete pod,svc -l name=<label-name>
~~~     # 删除默认命名空间下的所有 Pod
[root@k8s-master01 ~]#  kubectl delete pod --all
 
~~~     # 执行 pod 命令
[root@k8s-master01 ~]#  kubectl exec <pod-name> -- date
[root@k8s-master01 ~]#  kubectl exec <pod-name> -- bash
[root@k8s-master01 ~]#  kubectl exec <pod-name> -- ping 10.24.51.9
~~~     # 通过bash获得 pod 中某个容器的TTY,相当于登录容器
[root@k8s-master01 ~]#  kubectl exec -it <pod-name> -c <container-name> -- bash
 
~~~     # eg:
[root@k8s-master01 ~]#  kubectl exec -it redis-master-cln81 -- bash
~~~     # 查看容器的日志

[root@k8s-master01 ~]#  kubectl logs <pod-name>
[root@k8s-master01 ~]#  kubectl logs -f <pod-name>                      // 实时查看日志
[root@k8s-master01 ~]#  kubectl log  <pod-name>  -c <container_name>    // 若 pod 只有一个容器,可以不加 -c 
[root@k8s-master01 ~]#  kubectl logs -l app=frontend                    // 返回所有标记为 app=frontend 的 pod 的合并日志。
~~~     # 查看节点 labels
[root@k8s-master01 ~]#  kubectl get node --show-labels
 
~~~     # 重启 pod
[root@k8s-master01 ~]#  kubectl get pod <POD名称> -n <NAMESPACE名称> -o yaml | kubectl replace --force -f -
二、命令补全
### --- 命令补全
~~~     # 在 bash 中设置当前 shell 的自动补全,要先安装 bash-completion 包。

[root@k8s-master01 ~]#  source <(kubectl completion bash)                           
[root@k8s-master01 ~]#  echo "source <(kubectl completion bash)" >> ~/.bashrc 

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-03-29 13:09  yanqi_vip  阅读(24)  评论(0)    收藏  举报

导航