kubernetes cluster management command (b)

Series catalog

On the one we introduce some basic commands, this section we introduce some of the more complex commands.

Sort pod

Use kubectl get pod acquisition pod default resource name is sort of, sometimes we might want to sort in other sequences. For example, we want to be sorted according to the nodes. So you can see all running on different nodes with which pod.

This requirement can use the linux command is very simple to achieve using kubectl get podplus -owideparameters can be displayed node pod is located. We then use linux sort order can be the result. The following example is the result sorted according to specific fields by node sorted

[centos@k8s-master ~]$ kubectl get pod -owide|sort -k 7
sagent-b4dd8b5b9-5m2jc                     1/1     Running   0          15h     10.244.5.17   k8s-node1   <none>           <none>
consul-0                                   1/1     Running   0          2d17h   10.244.2.27   k8s-node2   <none>           <none>
consul-2                                   1/1     Running   0          2d17h   10.244.2.28   k8s-node2   <none>           <none>
stodagent-6f47976ccb-8fzmv                 1/1     Running   0          2d17h   10.244.2.23   k8s-node2   <none>           <none>
stodagent-6f47976ccb-vf7kx                 1/1     Running   0          2d17h   10.244.2.22   k8s-node2   <none>           <none>
trackingapi-gateway-dep-79bb86bb57-x9xzp   1/1     Running   0          2d17h   10.244.2.24   k8s-node2   <none>           <none>
sagent-b4dd8b5b9-6mmst                     1/1     Running   0          15h     10.244.6.6    k8s-node3   <none>           <none>
sagent-b4dd8b5b9-zq649                     1/1     Running   0          15h     10.244.7.7    k8s-node4   <none>           <none>
stodagent-6f47976ccb-j7m8b                 1/1     Running   0          2d17h   10.244.3.6    k8s-node5   <none>           <none>
consul-1                                   1/1     Running   0          2d17h   10.244.3.13   k8s-node5   <none>           <none>
easymock-dep-84767b6f75-l84r4              1/1     Running   0          15h     10.244.8.8    k8s-node6   <none>           <none>
NAME                                       READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES

We can see that the name of the node has to be ordered here inelegant title is ranked in the final surface, but harmless.

In fact kubectl get command has a --sort-by argument. We can specify which fields to sort by resource through it, but not limited to the field exhibited. The following are --sort-bythe results of the parameters sorted by node name.

[centos@k8s-master ~]$ kubectl get pod --sort-by='{.spec.nodeName}' -owide
NAME                                       READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
sagent-b4dd8b5b9-5m2jc                     1/1     Running   0          15h     10.244.5.17   k8s-node1   <none>           <none>
consul-0                                   1/1     Running   0          2d17h   10.244.2.27   k8s-node2   <none>           <none>
stodagent-6f47976ccb-8fzmv                 1/1     Running   0          2d17h   10.244.2.23   k8s-node2   <none>           <none>
consul-2                                   1/1     Running   0          2d17h   10.244.2.28   k8s-node2   <none>           <none>
stodagent-6f47976ccb-vf7kx                 1/1     Running   0          2d17h   10.244.2.22   k8s-node2   <none>           <none>
trackingapi-gateway-dep-79bb86bb57-x9xzp   1/1     Running   0          2d17h   10.244.2.24   k8s-node2   <none>           <none>
sagent-b4dd8b5b9-6mmst                     1/1     Running   0          15h     10.244.6.6    k8s-node3   <none>           <none>
sagent-b4dd8b5b9-zq649                     1/1     Running   0          15h     10.244.7.7    k8s-node4   <none>           <none>
consul-1                                   1/1     Running   0          2d17h   10.244.3.13   k8s-node5   <none>           <none>
stodagent-6f47976ccb-j7m8b                 1/1     Running   0          2d17h   10.244.3.6    k8s-node5   <none>           <none>
easymock-dep-84767b6f75-l84r4              1/1     Running   0          15h     10.244.8.8    k8s-node6   <none>           <none>

In addition to the show out of the field, we can also be sorted by not showing up in the field. For example, we can follow the startTimesort to see which pod long run.

[centos@k8s-master ~]$ kubectl get pod --sort-by='{.status.startTime}'
NAME                                       READY   STATUS             RESTARTS   AGE
stodagent-6f47976ccb-vf7kx                 1/1     Running            0          2d18h
stodagent-6f47976ccb-j7m8b                 1/1     Running            0          2d18h
stodagent-6f47976ccb-8fzmv                 1/1     Running            0          2d18h
trackingapi-gateway-dep-79bb86bb57-x9xzp   1/1     Running            0          2d18h
consul-0                                   1/1     Running            0          2d18h
consul-1                                   1/1     Running            0          2d17h
consul-2                                   1/1     Running            0          2d17h
sagent-b4dd8b5b9-5m2jc                     1/1     Running            0          16h
sagent-b4dd8b5b9-zq649                     1/1     Running            0          16h
sagent-b4dd8b5b9-6mmst                     1/1     Running            0          16h
easymock-dep-84767b6f75-l84r4              1/1     Running            0          15h
redis-cache-f87d8488c-v4zjx                0/1     ErrImagePull       0          57s
redis-cache-f87d8488c-4d9dl                0/1     ImagePullBackOff   0          57s
redis-cache-f87d8488c-kxc89                0/1     ImagePullBackOff   0          57s

After reading the contents of the output later, we may confused, in the end is not sorted by the above start time? How to directly see is sorted by start-up time? In fact really is actually sorted by start time, people show this example, I just started soon redis three instances can not run, they are ranked in the bottom.

Can we startTime field is also output out of it, so that you can directly see the output time, it is not sorted by this time will clear up. The answer is yes.

Custom display fields

The previous one we have a problem, we have a demand of the pod startTime displayed. In fact, this demand can be achieved, kubectl get input format parameters of -o(或--output)receiving a custom-columnscustomize the format of the column. We can do it by custom output format.

The format is:
-o=custom-columns=列名:字段值,列名:字段值

Which field values ​​are selected by the resource definition file.

The following examples show us the name and pod startTime

[centos@k8s-master ~]$  kubectl get pod --sort-by=.status.startTime  -o=custom-columns=name:.metadata.name,startTime:.status.startTime
name                                       startTime
stodagent-6f47976ccb-vf7kx                 2019-05-14T07:19:05Z
stodagent-6f47976ccb-j7m8b                 2019-05-14T07:19:05Z
stodagent-6f47976ccb-8fzmv                 2019-05-14T07:19:05Z
trackingapi-gateway-dep-79bb86bb57-x9xzp   2019-05-14T07:19:06Z
consul-0                                   2019-05-14T07:19:28Z
consul-1                                   2019-05-14T07:25:06Z
consul-2                                   2019-05-14T07:25:16Z
sagent-b4dd8b5b9-5m2jc                     2019-05-16T09:20:17Z
sagent-b4dd8b5b9-zq649                     2019-05-16T17:09:37Z
sagent-b4dd8b5b9-6mmst                     2019-05-16T17:19:59Z
easymock-dep-84767b6f75-l84r4              2019-05-16T18:00:07Z
redis-cache-f87d8488c-v4zjx                2019-05-17T01:21:54Z
redis-cache-f87d8488c-4d9dl                2019-05-17T01:21:54Z
redis-cache-f87d8488c-kxc89                2019-05-17T09:11:11Z

So that you can understand at a glance to see that they are chronologically ordered. But we can see, the command becomes a bit complicated, and if you want the same as those as the default output fields are displayed, is not a need to define more and more custom output fields, can be appended to the default output field custom field it. the answer is no, we can not do it. there are suggestions to increase user --extra-columnsparameters additional output fields in the output on the basis of the default, but as of now the pr has not merged. there may also be released to wait several months.

Sort kubernetes resources in the event in time

Sometimes in order to troubleshoot the error, we may need to see kubernetes resources in the event, we can easily see, in chronological order

kubectl get events --sort-by=.metadata.creationTimestamp

Were used go-template, jsonpath, yq, jq to obtain a pod in the mirror

We use the following four examples show how to obtain an image pod called the consul-0's (you can use any of the pod to the exercises.)

In short, all roads lead to Rome, as long as the mastery of one of the methods we can complete the task of our daily work, we do not have all the tools to master, be selected according to the actual situation and their own knowledge.

Get use to-template

The following command

[centos@k8s-master ~]$ kubectl get pod consul-0 -ogo-template='{{range .spec.containers}}{{.image}}{{end}}'
consul:latest

The range function is used to end through the collection for objects traversing .end

Use jsonpath

kubectl get pod consul-0 -ojsonpath='{range .spec.containers[*]}{.image}{end}'
consul:latest

The above and here go-template is very similar, except that the array of objects behind to keep the brackets [] is an array to indicate, in parentheses tape asterisk (*) indicates an index of all.

jsonpath and go-template except that, in addition to use an asterisk jsonpath *number index than all the elements of the cluster, may also be of a numeric index (Like other programming languages, 0 represents the first element) index or range (e.g., [0: 1], [: 1])

Above commands are simple enough. Use jsonpath there is a more concise way, that is recursive search, so you can look in the detailed structure of the document does not know the specific property name but know when. Examples are as follows

[centos@k8s-master ~]$ kubectl get pod consul-0 -ojsonpath='{..image}'
consul:latest consul:latest

Compared with the above, the results here Rideau out a record. In fact, this is because the statusfield is containerStatusesa field where there are image attributes. We can further narrow down your search using the following command

[centos@k8s-master ~]$ kubectl get pod consul-0 -ojsonpath='{.spec..image}'
consul:latest

Use tools yq

[centos@k8s-master ~]$ kubectl get po consul-0 -oyaml|yq r - spec.containers[*].image
- consul:latest

Use tools jq

Examples are as follows:

[centos@k8s-master ~]$ kubectl get po consul-0 -ojson|jq .spec.containers[].image
"consul:latest"

yq can also use a recursive query, not only is not particularly elegant, and limit is very large, only suitable for simple viewing

[centos@k8s-master ~]$ kubectl get po consul-0 -ojson|jq '..|.image?'
null
null
null
null
null
"consul:latest"
null
null
null
null
null
null
null
null
null
null
null
"consul:latest"
null
null
null

Null output above a lot of influence watch, we can use them to filter out the pipe grep

[centos@k8s-master ~]$ kubectl get po consul-0 -ojson|jq '..|.image?'|grep -v null
"consul:latest"
"consul:latest"

Object Output

The above example we only output the value of a particular field, but if used in a programmable environment, we may need more than just a normal value, there may be similar to the more complex objects common to json object. Here we jq describes how to use the tool outputs the result as an object type

The following example we name the name and image of a mirror image of the object as a json output

[centos@k8s-master ~]$ kubectl get po consul-0 -ojson|jq '{name:.spec.containers[0].name,image:.spec.containers[0].image}'
{
  "name": "consul",
  "image": "consul:latest"
}

These are only the output of an object, if we want to export it is an array of objects, where some processing technology, consider the following example:

[centos@k8s-master ~]$ kubectl get po consul-0 -ojson|jq .status.conditions
[
  {
    "lastProbeTime": null,
    "lastTransitionTime": "2019-05-14T07:19:28Z",
    "status": "True",
    "type": "Initialized"
  },
  {
    "lastProbeTime": null,
    "lastTransitionTime": "2019-05-14T07:25:06Z",
    "status": "True",
    "type": "Ready"
  },
  {
    "lastProbeTime": null,
    "lastTransitionTime": "2019-05-14T07:25:06Z",
    "status": "True",
    "type": "ContainersReady"
  },
  {
    "lastProbeTime": null,
    "lastTransitionTime": "2019-05-14T07:19:28Z",
    "status": "True",
    "type": "PodScheduled"
  }
]

We need to re-assemble the following results, save to contain only the lastTransitionTimecluster type

The following is a common method of operation errors:

[centos@k8s-master ~]$ kubectl get po consul-0 -ojson|jq '.status.conditions[]|{lastTransitionTime:.lastTransitionTime}'
{
  "lastTransitionTime": "2019-05-14T07:19:28Z"
}
{
  "lastTransitionTime": "2019-05-14T07:25:06Z"
}
{
  "lastTransitionTime": "2019-05-14T07:25:06Z"
}
{
  "lastTransitionTime": "2019-05-14T07:19:28Z"
}

We can see that we actually get to a number of objects, but they are not an array, you want to get an array of objects, the correct operation is as follows:

[centos@k8s-master ~]$ kubectl get po consul-0 -ojson|jq '[.status.conditions[]|{lastTransitionTime:.lastTransitionTime}]'
[
  {
    "lastTransitionTime": "2019-05-14T07:19:28Z"
  },
  {
    "lastTransitionTime": "2019-05-14T07:25:06Z"
  },
  {
    "lastTransitionTime": "2019-05-14T07:25:06Z"
  },
  {
    "lastTransitionTime": "2019-05-14T07:19:28Z"
  }
]

We take a closer look at the difference in where

Guess you like

Origin www.cnblogs.com/tylerzhou/p/11058476.html