# kubernetes集群管理常用命令一

[https://www.cnblogs.com/tylerzhou/p/11056440.html](https://www.cnblogs.com/tylerzhou/p/11056440.html)

## [kubernetes集群管理常用命令一](https://www.cnblogs.com/tylerzhou/p/11056440.html)

> [系列目录](https://www.cnblogs.com/tylerzhou/p/10969041.html)

我们把集群管理命令分为两个部分,第一部分介绍一些简单的,但是可能是非常常用的命令以及一些平时可能没有碰到的技巧.第二部分将综合前面介绍的工具通过示例来讲解一些更为复杂的命令.

### 列出集群中所有pod <a href="#lie-chu-ji-qun-zhong-suo-you-pod" id="lie-chu-ji-qun-zhong-suo-you-pod"></a>

使用`kubectl get pods`获取的一般是默认名称空间的信息(可以在配置文件中指定默认全称空间,如果没有指定则默认为default).如果想要获取获取非默认名称空间的pod,则需要指定-n(或者--namespace)参数.但是有些时候我们想要查看整个集群中所有名称空间的pod,则可以通过`kubectl get pods --all-namespaces`

> 有的童鞋可能会好奇,可不可能不选择全部,但是选择多个名称空间下的pod,其实也是可以的.以下示例同时选择了default和kube-system名称空间下的所有pod

```
[centos@k8s-master ~]$ kubectl get po --namespace={default,kube-system}
NAME                                                   READY   STATUS             RESTARTS   AGE
coredns-78d4cf999f-8lpm6                               1/1     Running            4          64d
coredns-78d4cf999f-hd7nn                               1/1     Running            4          64d
etcd-k8s-master                                        1/1     Running            6          64d
jxing-nginx-ingress-controller-7d9d4fc99d-bksl2        1/1     Running            0          2d1h
jxing-nginx-ingress-default-backend-7875d67576-cmwz5   0/1     ImagePullBackOff   0          2d1h
kube-apiserver-k8s-master                              1/1     Running            0          23d
kube-controller-manager-k8s-master                     1/1     Running            7          64d
kube-flannel-ds-amd64-5w2d6                            1/1     Running            1          2d2h
kube-flannel-ds-amd64-69vhm                            1/1     Running            1          31h
kube-flannel-ds-amd64-bh5hw                            1/1     Running            1          2d7h
kube-flannel-ds-amd64-csnqm                            1/1     Running            0          47h
kube-flannel-ds-amd64-fpdx5                            1/1     Running            6          64d
kube-flannel-ds-amd64-j6v7h                            1/1     Running            4          3h50m
kube-flannel-ds-amd64-qsrxw                            1/1     Running            1          46h
kube-proxy-7xcb7                                       1/1     Running            6          64d
kube-proxy-d4jxx                                       1/1     Running            2          2d7h
kube-proxy-ftjsk                                       1/1     Running            2          46h
kube-proxy-jrfxs                                       1/1     Running            5          3h50m
kube-proxy-kw4fw                                       1/1     Running            1          31h
kube-proxy-thzdp                                       1/1     Running            1          2d2h
kube-proxy-wrbmm                                       1/1     Running            0          47h
kube-scheduler-k8s-master                              1/1     Running            9          64d
kubernetes-dashboard-6b8667558-dtn6r                   1/1     Running            0          2d1h
tiller-deploy-6df646875f-tp26r                         1/1     Running            0          2d1h

```

### 只列出资源的名称 <a href="#zhi-lie-chu-zi-yuan-de-ming-cheng" id="zhi-lie-chu-zi-yuan-de-ming-cheng"></a>

在测试环境中,有时候我们可能想要把所有类型的资源全部删除.比如删除所有deployment.我们可以通过`kubectl get deployment`来列出所有的deployment,然后逐个删除.如果是全部删除的话,这样显然是低效的,并且往往因为名称拼写错误导致操作失败.在docker里们,我们想要删除全部运行的容器,可以通过`docker ps -q`来获取所有pod的id,然后通过循环来删除.其实在kubernetes里,可以通过`kubectl get deployment -o=name`仅仅输出名称,然后使用循环逐个删除.

### 列出包含特定标签的pod <a href="#lie-chu-bao-han-te-ding-biao-qian-de-pod" id="lie-chu-bao-han-te-ding-biao-qian-de-pod"></a>

有经验的童鞋都知道,受某个Deployment控制的一组pod的名称往往跟这个Deployment的名称有关系,往往是Deployment的名称加上一串hash.但是如果如果两个deployment名称很类似,我就不就太容易找出某个deployment所控制的pod.最为无助的是service和它选择的pod之间往往从表面上看不出关系的,如何知道一个service所控制.其实我们可以通过service里的labelselector选择的lable来过滤此service所控制的pod.

比如我有一个名为helloworld的service,我们使用`kubectl describe service helloworld`来查看它的信息

```
[centos@k8s-master ~]$ kubectl describe service helloworld
Name:                     helloworld
Namespace:                default
Labels:                   run=helloworld
Annotations:              field.cattle.io/publicEndpoints:
                            [{"addresses":["192.168.9.40"],"port":32001,"protocol":"TCP","serviceName":"default:helloworld","allNodes":true}]
                          kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"run":"helloworld"},"name":"helloworld","namespace":"default"},...
Selector:                 app=helloworld
Type:                     NodePort
IP:                       10.108.25.188
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  32001/TCP
Endpoints:                10.244.2.30:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

```

它的labelSelector的值为`app=helloworld`.我们可以通过这个label来过滤pod

```
[centos@k8s-master ~]$ kubectl get pod -l=app=helloworld
NAME                          READY   STATUS    RESTARTS   AGE
helloworld-6d6bdb66dd-ncwk5   1/1     Running   0          3h4m
```

> 其实,除了service以外,deployment,ReplicationController,ReplicaSet也可以通过类似方式选择,这样选择出的结果更为准确.

### 选择指定节点上的pod <a href="#xuan-ze-zhi-ding-jie-dian-shang-de-pod" id="xuan-ze-zhi-ding-jie-dian-shang-de-pod"></a>

有一定经验的童鞋可能会知道,使用`kubectl get pods'时是不显示节点信息的,通过指定参数`-o wide`就可以展示出来pod所在的节点信息,然后便可以通过grep命令来过滤结果.比如我们想要查找`k8s-node2\`节点上的运行了哪些pod(节点名称可以自定义的,大家的不必是k8s-nodexxx这种形式)

```
[centos@k8s-master easymock]$ kubectl get po --all-namespaces -o wide|grep k8s-node2
cattle-system      cattle-node-agent-djg2h                                1/1     Running            2          2d8h    192.168.122.74   k8s-node2    <none>           <none>
default            consul-0                                               1/1     Running            0          2d2h    10.244.2.27      k8s-node2    <none>           <none>
default            consul-2                                               1/1     Running            0          2d2h    10.244.2.28      k8s-node2    <none>           <none>
default            helloworld-6d6bdb66dd-ncwk5                            1/1     Running            0          3h48m   10.244.2.30      k8s-node2    <none>           <none>
default            stodagent-6f47976ccb-8fzmv                             1/1     Running            0          2d2h    10.244.2.23      k8s-node2    <none>           <none>
default            stodagent-6f47976ccb-vf7kx                             1/1     Running            0          2d2h    10.244.2.22      k8s-node2    <none>           <none>
default            trackingapi-gateway-dep-79bb86bb57-x9xzp               1/1     Running            0          2d2h    10.244.2.24      k8s-node2    <none>           <none>
ingress-nginx      nginx-ingress-controller-c7dcb4d44-rhp22               1/1     Running            0          2d2h    10.244.2.25      k8s-node2    <none>           <none>
kube-system        kube-flannel-ds-amd64-bh5hw                            1/1     Running            1          2d8h    192.168.122.74   k8s-node2    <none>           <none>
kube-system        kube-proxy-d4jxx                                       1/1     Running            2          2d8h    192.168.122.74   k8s-node2    <none>           <none>
kube-system        tiller-deploy-6df646875f-tp26r                         1/1     Running            0          2d2h    10.244.2.26      k8s-node2    <none>           <none>
tekton-pipelines   tekton-pipelines-webhook-79d8658cdc-2tp84              1/1     Running            0          2d2h    10.244.2.19      k8s-node2    <none>           <none>

```

这是通过linux的grep命令来实现的,其实,kubectl也有着非常强大的过滤功能,我们可以对它字段选择过滤器`feild selector`,通过pod描述文件里的`spec.nodeName`来过滤指定节点上的pod.命令示例如下:

```
[centos@k8s-master easymock]$ kubectl get po --all-namespaces -o wide --field-selector=spec.nodeName=k8s-node2
NAMESPACE          NAME                                        READY   STATUS    RESTARTS   AGE     IP               NODE        NOMINATED NODE   READINESS GATES
cattle-system      cattle-node-agent-djg2h                     1/1     Running   2          2d8h    192.168.122.74   k8s-node2   <none>           <none>
default            consul-0                                    1/1     Running   0          2d2h    10.244.2.27      k8s-node2   <none>           <none>
default            consul-2                                    1/1     Running   0          2d2h    10.244.2.28      k8s-node2   <none>           <none>
default            helloworld-6d6bdb66dd-ncwk5                 1/1     Running   0          3h52m   10.244.2.30      k8s-node2   <none>           <none>
default            stodagent-6f47976ccb-8fzmv                  1/1     Running   0          2d2h    10.244.2.23      k8s-node2   <none>           <none>
default            stodagent-6f47976ccb-vf7kx                  1/1     Running   0          2d2h    10.244.2.22      k8s-node2   <none>           <none>
default            trackingapi-gateway-dep-79bb86bb57-x9xzp    1/1     Running   0          2d2h    10.244.2.24      k8s-node2   <none>           <none>
ingress-nginx      nginx-ingress-controller-c7dcb4d44-rhp22    1/1     Running   0          2d2h    10.244.2.25      k8s-node2   <none>           <none>
kube-system        kube-flannel-ds-amd64-bh5hw                 1/1     Running   1          2d8h    192.168.122.74   k8s-node2   <none>           <none>
kube-system        kube-proxy-d4jxx                            1/1     Running   2          2d8h    192.168.122.74   k8s-node2   <none>           <none>
kube-system        tiller-deploy-6df646875f-tp26r              1/1     Running   0          2d2h    10.244.2.26      k8s-node2   <none>           <none>
tekton-pipelines   tekton-pipelines-webhook-79d8658cdc-2tp84   1/1     Running   0          2d2h    10.244.2.19      k8s-node2   <none>           <none>
```

### 列出所有状态不是运行中的Pod <a href="#lie-chu-suo-you-zhuang-tai-bu-shi-yun-hang-zhong-de-pod" id="lie-chu-suo-you-zhuang-tai-bu-shi-yun-hang-zhong-de-pod"></a>

有时候我们需要列出所有未在运行的pod,以便决定下一步要做什么行动.未运行的原因有很多,比如镜像未能正常拉取,pod无法被正常调度等,我们首先要把所有未运行的pod过滤出来.

```
[root@k8s-master helloworld]# kubectl get pod --field-selector=status.phase!=Running
NAME                          READY   STATUS             RESTARTS   AGE
redis-cache-f87d8488c-4d9dl   0/1     ErrImagePull       0          4d4h
redis-cache-f87d8488c-gjxqj   0/1     ImagePullBackOff   0          20h
redis-cache-f87d8488c-v4zjx   0/1     ImagePullBackOff   0          4d4h
```

> 这里不同的是,前面我们使用的是等号来判断相等关系,其实也可以像这里使用(!=)来表示否定.

> 还有一点需要特别注意`phase=Running`的pod并不全是正常运行的pod,要确认一个pod是正常运行的,必须`.status.conditions里的Initialized,Ready,ContainersReady和PodScheduled几种类型的值必须全部是"true",也可以通过展示字段STATUS的值为Running`

### 列出所有节点 <a href="#lie-chu-suo-you-jie-dian" id="lie-chu-suo-you-jie-dian"></a>

我们通常都是和pod,service这些资源打交道,但是有些时候,我们也需要和节点打交道.可以使用`kubectl get nodes`来查看集群中的所有节点信息

> 查看node信息是通过`kubectl get`命令,kubectl get的参数在这里同样适用.

> node也是k8s的资源,我们可以通过kubectl describe+节点名称查看节点内存,cpu核数,以及该节点的压力等信息.
