kubectl命令应用

Posted 码出未来_远

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kubectl命令应用相关的知识,希望对你有一定的参考价值。

kubectl命令应用

文章目录

kubectl常用操作

创建资源对象:

kubectl create -f xxx.yaml (不建议使用,无法更新,必须先delete)
kubectl apply -f xxx.yaml (创建+更新,可以重复使用)

删除资源对象:

kubectl delete -f xxx.yaml

重启pod:

(无法删除对应的应用,因为存在deployment/rc之类的副本控制器,删除pod也会重新拉起来)

kubectl get pod -n kube-system

查看pod描述:

kubectl describe pod XXX -n kube-system

查看pod 日志: (如果pod有多个容器需要加-c 容器名)

kubectl logs xxx -n kube-system

查看集群信息:

kubectl cluster-info

获取集群的所有命名空间

kubectl get namespaces

获取集群的所有resource:

kubectl get all

查看指定pod:

kubectl get pod $pod名称 

查看pod的虚拟ip:

kubectl get pods  -o wide

查看service:

kubectl get svc
kubectl get service

查看运行pod的环境变量:

kubectl exec podName env

查看指定pod的日志:

kubectl logs -f --tail 500 -n kube-system kube-apiserver-k8s-master

查看所有名称空间的service信息:

kubectl get svc -A

查看指定名称空间的service信息:

kubectl get svc -n kube-system

查看资源对象:

[root@master ~]# kubectl get pods       查看所有 pod 列表
NAME                     READY   STATUS    RESTARTS   AGE
nginx-6799fc88d8-hnjrq   1/1     Running   1          22h


kubectl get pod -n kube    -n 后跟 namespace, 查看指定的命名空间中的pod

#查看Service列表                              svc是service简写,也可以写全拼
[root@master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        23h
nginx        NodePort    10.111.131.14   <none>        80:31775/TCP   23h

#kubectl get pod,svc -o wide                 -o wide 查看详细信息
[root@master ~]# kubectl get pod,svc -o wide 
NAME                         READY   STATUS    RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
pod/nginx-6799fc88d8-hnjrq   1/1     Running   1          23h   10.244.2.3   node2   <none>           <none>

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        23h   <none>
service/nginx        NodePort    10.111.131.14   <none>        80:31775/TCP   23h   app=nginx

语法:kubectl get pod <pod-name> -o yaml

显示Pod更多信息

kubectl get pod <pod-name> -o wide ,列如:
[root@master ~]# kubectl get pod,rc -o wide 
NAME                         READY   STATUS    RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
pod/nginx-6799fc88d8-hnjrq   1/1     Running   1          23h   10.244.2.3   node2   <none>           <none>

以yaml格式显示Pod的详细信息

[root@master ~]# kubectl get pod,svc -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  metadata:
    creationTimestamp: "2021-08-26T05:50:56Z"

以自定义列名显示Pod的信息

[root@master ~]# kubectl get pod,svc -o=custom-columns=NAME:.metadata.name,RSRC:.metadata.resourceVersion
NAME                     RSRC
nginx-6799fc88d8-hnjrq   1926
kubernetes               202
nginx                    1266

基于文件的自定义列名输出

[root@master ~]# kubectl get pods,svc -o=custom-columns-file=template.txt
NAME                     RSRC
nginx-6799fc88d8-hnjrq   1926
kubernetes               202
nginx                    1266

[root@master ~]# kubectl get pods nginx-6799fc88d8-hnjrq  -o=custom-columns-file=template.txt 
NAME                     RSRC
nginx-6799fc88d8-hnjrq   1926

template.txt文件的内容为
NAME                RSRC
metadata.name       metadata.resourceVersion

创建一个deployment类型的容器

[root@master ~]# kubectl create deployment deploy-web --image=xialuo:v0.1 --replicas=3
deployment.apps/deploy-web created

[root@master ~]# kubectl get pods
NAME                         READY   STATUS              RESTARTS   AGE
deploy-web-dbc599567-tpvh4   0/1     ContainerCreating   0          12s
deploy-web-dbc599567-xhp26   0/1     ContainerCreating   0          12s
deploy-web-dbc599567-z4gxf   0/1     ContainerCreating   0          12s
nginx-6799fc88d8-hnjrq       1/1     Running             1          23h

删除
[root@master ~]# kubectl delete deployment deploy-web
deployment.apps "deploy-web" deleted

创建一个deployment类型的容器

[root@master ~]# kubectl create deployment deploy-web --image=xialuo:v0.1 --replicas=3
deployment.apps/deploy-web created

[root@master ~]# kubectl get pods
NAME                         READY   STATUS              RESTARTS   AGE
deploy-web-dbc599567-tpvh4   0/1     ContainerCreating   0          12s
deploy-web-dbc599567-xhp26   0/1     ContainerCreating   0          12s
deploy-web-dbc599567-z4gxf   0/1     ContainerCreating   0          12s
nginx-6799fc88d8-hnjrq       1/1     Running             1          23h

删除
[root@master ~]# kubectl delete deployment deploy-web
deployment.apps "deploy-web" deleted

实时监控拉取镜像的状态

[root@master ~]# kubectl get pods -w
NAME                            READY   STATUS              RESTARTS   AGE
deploy-nginx-6b777fdcd5-8lrd4   0/1     ContainerCreating   0          63s
deploy-nginx-6b777fdcd5-bmk6q   0/1     ContainerCreating   0          63s
deploy-nginx-6b777fdcd5-mmtjk   0/1     ContainerCreating   0          63s
nginx-6799fc88d8-hnjrq          1/1     Running             1          23h
deploy-nginx-6b777fdcd5-bmk6q   0/1     ErrImagePull        0          67

暴露部署

[root@master ~]# kubectl expose deployment deploy-nginx --port=80 --protocol=TCP
service/deploy-nginx exposed
[root@master ~]# kubectl get svc
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
deploy-nginx   ClusterIP   10.96.65.25     <none>        80/TCP         14s
kubernetes     ClusterIP   10.96.0.1       <none>        443/TCP        23h
nginx          NodePort    10.111.131.14   <none>        80:31775/TCP   23h

以上是关于kubectl命令应用的主要内容,如果未能解决你的问题,请参考以下文章

kubectl 常用命令

kubectl 常用命令总结

k8s kubectl常用命令总结

kubectl命令介绍

kubectl命令介绍

kubectl命令介绍