Kubernetes kubectl 命令

kubectl 命令用来操作 Kubernetes 集群中的资源对象,包括对资源的创建、删除、查看、修改、配置、运行等

命令语法:kubectl [command] [TYPE] [NAME] [flags]

# command:子命令,用于操作 Kubernetes 集群资源对象的命令,如 create, delete, describe, get, apply 等# TYPE:资源对象的类型,如 pod,service,rc,node 等,有些可以简写,如 service 简写为 svc,nodes 简写为 ns# NAME:资源对象的名称,不指定则返回所有,如 kubectl get pod 会返回所有 pod, 如果写成 kubectl get pod nginx 就只返回 nginx 这个 pod# flags:kubectl 子命令的可选参数,例如 -n 指定 namespace,-s 指定 apiserver 的 URL

常见用法:

[root@localhost ~]$ kubectl get pods # 查看所有的Pod资源[root@localhost ~]$ kubectl get pod <pod_name> # 查看指定的Pod资源[root@localhost ~]$ kubectl get pod <pod_name> -o wide # 查看指定的Pod资源,并指定输出格式,其他输出格式
[root@localhost ~]$ kubectl create -f <yaml_filename> # 根据yaml文件创建资源[root@localhost ~]$ kubectl create -f <directory> # 也可以指定一个目录,这样可以一次性根据该目录下所有yaml或json文件创建资源
[root@localhost ~]$ kubectl describe pod <pod_name> # 查看指定Pod资源的描述信息(写法一)[root@localhost ~]$ kubectl describe pod/<pod_name> # 查看指定Pod资源的描述信息(写法二)
[root@localhost ~]$ kubectl delete pods # 删除所有的Pod资源[root@localhost ~]$ kubectl delete pod <pod_name> # 删除指定的Pod资源[root@localhost ~]$ kubectl delete pod -l name=<label_name> # 删除所有带有指定标签名的Pod资源[root@localhost ~]$ kubectl delete -f <yaml_filename> # 根据yaml文件删除资源
[root@localhost ~]$ kubectl exec <pod_name> date # exec用于对指定的资源对象执行指定的命令,这里表示对指定的Pod资源执行date命令[root@localhost ~]$ kubectl exec -it <pod_name> bash # 执行 bash 命令,相当于进入Pod,注意要加上 -it 参数
[root@localhost ~]$ kubectl logs <pod_name> # 查看指定资源(Pod)的日志[root@localhost ~]$ kubectl logs <pod_name> -c <container_name> # 查看指定资源(Pod下指定的container)的日志[root@localhost ~]$ kubectl logs -f <pod_name> -c <container_name> # 动态查看指定资源的日志,类似于 tail -f 

 

 

 

 

 

 

 

    

相关文章