[kubectl-resource-view]: 一款用于查看k8s资源使用情况的插件

1.简介

一款自己编写的k8s 命令行插件,用于查看k8s node和pod资源的 cpu、 memory、 gpu的request 和limit 使用情况。

2.安装

可直接解压下载使用,也可以从github仓库中进行下载最新版本,支持不同架构,喜欢的麻烦给个小星星,有啥额外的需求可以提issue

https://github.com/bryant-rh/kubectl-resource-view

如果遇到下面这种错误,说明你的Metrics Server服务没有安装或者正常运行。

[root@k8s-master1 ~]#  kubectl-resource-view node
Error from server (ServiceUnavailable): the server is currently unable to handle the request (get nodes.metrics.k8s.io)
[kubectl-resource-view]: 一款用于查看k8s资源使用情况的插件

3.Usage

$ kubectl-resource-view -h

Display Resource (cpu/memory/gpu/podcount) Usage and Request and Limit.

 The resource command allows you to see the resource consumption for nodes or pods.

 This command requires Metrics Server to be correctly configured and working on the server.

Usage:
  kubectl-resource-view [flags] [options]
  kubectl-resource-view [command]

Examples:
  node        Display Resource (cpu/memory/gpu/podcount) usage of nodes
  pod         Display Resource (cpu/memory/gpu)          usage of pods

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  node        Display resource (cpu/memory/gpu/podcount) usage of nodes
  pod         Display resource (cpu/memory/gpu) usage of pods

Flags:
      --as string                      Username to impersonate for the operation. User could be a regular user or a service account in a namespace.
      --as-group stringArray           Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
      --as-uid string                  UID to impersonate for the operation.
      --cache-dir string               Default cache directory (default "/root/.kube/cache")
      --certificate-authority string   Path to a cert file for the certificate authority
      --client-certificate string      Path to a client certificate file for TLS
      --client-key string              Path to a client key file for TLS
      --cluster string                 The name of the kubeconfig cluster to use
      --context string                 The name of the kubeconfig context to use
  -h, --help                           help for kubectl-resource-view
      --insecure-skip-tls-verify       If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
      --kubeconfig string              Path to the kubeconfig file to use for CLI requests.
      --match-server-version           Require server version to match client version
  -n, --namespace string               If present, the namespace scope for this CLI request
      --request-timeout string         The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
  -s, --server string                  The address and port of the Kubernetes API server
      --tls-server-name string         Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used
      --token string                   Bearer token for authentication to the API server
      --user string                    The name of the kubeconfig user to use
  -v, --version                        version for kubectl-resource-view

3.1 查看node资源

$ kubectl-resource-view  node -h

Display resource (cpu/memory/gpu/podcount) usage of nodes.

 The resource node command allows you to see the resource consumption of nodes.

Usage:
  kubectl-resource-view node [NAME | -l label]

Aliases:
  node, nodes, no

Examples:
  # Show metrics for all nodes
  kubectl resource-view node

  # Show metrics for a given node
  kubectl resource-view node NODE_NAME

  # Show metrics for the node defined by type name=cpu,memory,gpu,pod
  kubectl resource-view node -t cpu,memory,gpu,pod

Flags:
  -h, --help              help for node
      --no-format         If present, print output without format table
  -l, --selector string   Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
      --sort-by string    If non-empty, sort nodes list using specified field. The field can be either 'cpu' or 'memory'
  -t, --type string       Type information hierarchically (default: All Type)[possible values: cpu,memory,pod,gpu], Multiple can be specified, separated by commas

1、查看所有节点的cpu,内存,gpu使用情况

[root@k8s-master1 ~]#  kubectl-resource-view node
+-------------+---------+--------------+------------+---------------+------------+---------+----------------+------------+-----------------+------------+----------------+-------------------+----------------+-------------------+--------------+--------+
|    NODE     | CPU USE |   CPU REQ    | CPU REQ(%) |    CPU LIM    | CPU LIM(%) | MEM USE |    MEM REQ     | MEM REQ(%) |     MEM LIM     | MEM LIM(%) | NVIDIA/GPU REQ | NVIDIA/GPU REQ(%) | NVIDIA/GPU LIM | NVIDIA/GPU LIM(%) | PODCOUNT (%) |        |
+-------------+---------+--------------+------------+---------------+------------+---------+----------------+------------+-----------------+------------+----------------+-------------------+----------------+-------------------+--------------+--------+
| k8s-master1 | 133m    | 762m/4000m   | 19.05%     | 2000m/4000m   | 50%        | 3884Mi  | 300Mi/7665Mi   | 3.91%      | 600Mi/7665Mi    | 7.83%      | 0/0            | 0%                | 0/0            | 0%                | 10/110       | 9.09%  |
| k8s-worker2 | 682m    | 3277m/40000m | 8.19%      | 17210m/40000m | 43.03%     | 51516Mi | 4404Mi/64324Mi | 6.85%      | 32886Mi/64324Mi | 51.13%     | 0/0            | 0%                | 0/0            | 0%                | 105/110      | 95.45% |
| k8s-worker3 | 126m    | 1162m/26000m | 4.47%      | 2200m/26000m  | 8.46%      | 2876Mi  | 980Mi/29893Mi  | 3.28%      | 1100Mi/29893Mi  | 3.68%      | 0/0            | 0%                | 0/0            | 0%                | 19/110       | 17.27% |
+-------------+---------+--------------+------------+---------------+------------+---------+----------------+------------+-----------------+------------+----------------+-------------------+----------------+-------------------+--------------+--------+

2、只查看cpu,或者内存资源,或者GPU资源

-t, --type string   [ cpu,memory,pod,gpu], Multiple  can be specified
[root@k8s-master1 ~]#  kubectl-resource-view node -t cpu,gpu
+-------------+---------+--------------+------------+---------------+------------+----------------+-------------------+----------------+-------------------+
|    NODE     | CPU USE |   CPU REQ    | CPU REQ(%) |    CPU LIM    | CPU LIM(%) | NVIDIA/GPU REQ | NVIDIA/GPU REQ(%) | NVIDIA/GPU LIM | NVIDIA/GPU LIM(%) |
+-------------+---------+--------------+------------+---------------+------------+----------------+-------------------+----------------+-------------------+
| k8s-master1 | 157m    | 762m/4000m   | 19.05%     | 2000m/4000m   | 50%        | 0/0            | 0%                | 0/0            | 0%                |
| k8s-worker2 | 727m    | 3277m/40000m | 8.19%      | 17210m/40000m | 43.03%     | 0/0            | 0%                | 0/0            | 0%                |
| k8s-worker3 | 108m    | 1162m/26000m | 4.47%      | 2200m/26000m  | 8.46%      | 0/0            | 0%                | 0/0            | 0%                |
+-------------+---------+--------------+------------+---------------+------------+----------------+-------------------+----------------+-------------------+

3、根据cpu或内存使用进行排序

--sort-by [cpu/memory]

[root@k8s-master1 ~]#  kubectl-resource-view node -t cpu --sort-by cpu
+-------------+---------+--------------+------------+---------------+------------+
|    NODE     | CPU USE |   CPU REQ    | CPU REQ(%) |    CPU LIM    | CPU LIM(%) |
+-------------+---------+--------------+------------+---------------+------------+
| k8s-worker2 | 754m    | 3277m/40000m | 8.19%      | 17210m/40000m | 43.03%     |
| k8s-master1 | 155m    | 762m/4000m   | 19.05%     | 2000m/4000m   | 50%        |
| k8s-worker3 | 118m    | 1162m/26000m | 4.47%      | 2200m/26000m  | 8.46%      |
+-------------+---------+--------------+------------+---------------+------------+

4、支持指定label 或者node name来查筛选

-l key1=value1,key2=value2

[root@k8s-master1 ~]#  kubectl-resource-view node -t cpu -l kubernetes.io/hostname=k8s-worker2
+-------------+---------+--------------+------------+---------------+------------+
|    NODE     | CPU USE |   CPU REQ    | CPU REQ(%) |    CPU LIM    | CPU LIM(%) |
+-------------+---------+--------------+------------+---------------+------------+
| k8s-worker2 | 673m    | 3277m/40000m | 8.19%      | 17210m/40000m | 43.03%     |
+-------------+---------+--------------+------------+---------------+------------+

如指定节点名称,来查看指定节点的对应资源使用情况

[root@k8s-master1 ~]#  kubectl-resource-view node k8s-worker3
+-------------+---------+--------------+------------+--------------+------------+---------+---------------+------------+----------------+------------+----------------+-------------------+----------------+-------------------+--------------+--------+
|    NODE     | CPU USE |   CPU REQ    | CPU REQ(%) |   CPU LIM    | CPU LIM(%) | MEM USE |    MEM REQ    | MEM REQ(%) |    MEM LIM     | MEM LIM(%) | NVIDIA/GPU REQ | NVIDIA/GPU REQ(%) | NVIDIA/GPU LIM | NVIDIA/GPU LIM(%) | PODCOUNT (%) |        |
+-------------+---------+--------------+------------+--------------+------------+---------+---------------+------------+----------------+------------+----------------+-------------------+----------------+-------------------+--------------+--------+
| k8s-worker3 | 97m     | 1162m/26000m | 4.47%      | 2200m/26000m | 8.46%      | 2879Mi  | 980Mi/29893Mi | 3.28%      | 1100Mi/29893Mi | 3.68%      | 0/0            | 0%                | 0/0            | 0%                | 19/110       | 17.27% |
+-------------+---------+--------------+------------+--------------+------------+---------+---------------+------------+----------------+------------+----------------+-------------------+----------------+-------------------+--------------+--------+
  1. 支持类似kubectl 格式显示

--no-format

[root@k8s-master1 ~]#  kubectl-resource-view node -t cpu --no-format
NODE            CPU USE CPU REQ         CPU REQ(%)      CPU LIM         CPU LIM(%)
k8s-master1     219m    762m/4000m      19.05%          2000m/4000m     50%
k8s-worker2     702m    3277m/40000m    8.19%           17210m/40000m   43.03%
k8s-worker3     116m    1162m/26000m    4.47%           2200m/26000m    8.46%

3.2 查看pod 资源

$  kubectl-resource-view  pod -h


Display resource (cpu/memory/gpu) usage of pods.

 The 'resource-view pod' command allows you to see the resource consumption of pods.

 Due to the metrics pipeline delay, they may be unavailable for a few minutes since pod creation.

Usage:
  kubectl-resource-view pod [NAME | -l label]

Aliases:
  pod, pods, po

Examples:
  # Show metrics for all pods in the default namespace
  kubectl resource-view pod

  # Show metrics for all pods in the given namespace
  kubectl resource-view pod --namespace=NAMESPACE

  # Show metrics for a given pod
  kubectl resource-view pod POD_NAME

  # Show metrics for the pods defined by label name=myLabel
  kubectl resource-view pod -l name=myLabel

  # Show metrics for the pods defined by type name=cpu,memory,gpu
  kubectl resource-view pod -t cpu,memory,gpu

Flags:
  -A, --all-namespaces          If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.
      --field-selector string   Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.
  -h, --help                    help for pod
      --no-format               If present, print output without format table
  -l, --selector string         Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)
      --sort-by string          If non-empty, sort pods list using specified field. The field can be either 'cpu' or 'memory'.
  -t, --type string             Type information hierarchically (default: All Type)[possible values: cpu,memory,gpu],Multiple can be specified, separated by commas

1、指定namespace 来查看对应pod 资源使用情况

-n --namespace =NAMESPACE
[root@k8s-master1 ~]# kubectl-resource-view pod -n yunshangshou-test
+-------------------+---------------------------------+----------+------------+---------+---------+---------+------------+---------+---------+----------------+----------------+
|     NAMESPACE     |            POD NAME             | CPU USE  | CPU USE(%) | CPU REQ | CPU LIM | MEM USE | MEM USE(%) | MEM REQ | MEM LIM | NVIDIA/GPU REQ | NVIDIA/GPU LIM |
+-------------------+---------------------------------+----------+------------+---------+---------+---------+------------+---------+---------+----------------+----------------+
| yunshangshou-test | adminapi-844cc89b95-l54jm       | 1m       | 0%         | 0m      | 0m      | 505Mi   | 0%         | 0Mi     | 0Mi     |              0 |              0 |
| yunshangshou-test | cardapi-7859b5f875-54gzh        | 2m       | 0%         | 0m      | 0m      | 1238Mi  | 0%         | 0Mi     | 0Mi     |              0 |              0 |
| yunshangshou-test | cardnotify-65686dd57c-lz255     | 2m       | 0%         | 0m      | 0m      | 1058Mi  | 0%         | 0Mi     | 0Mi     |              0 |              0 |
| yunshangshou-test | cashapi-6494cb5786-g76fm        | 1m       | 0%         | 0m      | 0m      | 199Mi   | 0%         | 0Mi     | 0Mi     |              0 |              0 |
| yunshangshou-test | cashnotify-6fbb5658f-7lrpm      | 1m       | 0%         | 0m      | 0m      | 199Mi   | 0%         | 0Mi     | 0Mi     |              0 |              0 |
| yunshangshou-test | memberapi-6d8c447c6d-frggr      | 1m       | 0%         | 0m      | 0m      | 2666Mi  | 0%         | 0Mi     | 0Mi     |              0 |              0 |
| yunshangshou-test | messageconsumer-d8699b67b-4xbwd | 2m       | 0%         | 0m      | 0m      | 275Mi   | 0%         | 0Mi     | 0Mi     |              0 |              0 |
| yunshangshou-test | openapi-55c4cb9646-r59k5        | 1m       | 0%         | 0m      | 0m      | 247Mi   | 0%         | 0Mi     | 0Mi     |              0 |              0 |
+-------------------+---------------------------------+----------+------------+---------+---------+---------+------------+---------+---------+----------------+----------------+

2、指定podname 查看指定pod 资源使用情况

[root@k8s-master1 ~]# kubectl-resource-view pod adminapi-844cc89b95-l54jm  -n yunshangshou-test
+-------------------+---------------------------+----------+------------+---------+---------+---------+------------+---------+---------+----------------+----------------+
|     NAMESPACE     |         POD NAME          | CPU USE  | CPU USE(%) | CPU REQ | CPU LIM | MEM USE | MEM USE(%) | MEM REQ | MEM LIM | NVIDIA/GPU REQ | NVIDIA/GPU LIM |
+-------------------+---------------------------+----------+------------+---------+---------+---------+------------+---------+---------+----------------+----------------+
| yunshangshou-test | adminapi-844cc89b95-l54jm | 1m       | 0%         | 0m      | 0m      | 505Mi   | 0%         | 0Mi     | 0Mi     |              0 |              0 |
+-------------------+---------------------------+----------+------------+---------+---------+---------+------------+---------+---------+----------------+----------------+

3、只查看cpu,或者内存资源,或者GPU资源

-t, --type string   [ cpu,memory,pod,gpu], Multiple  can be specified
[root@k8s-master1 ~]# kubectl-resource-view pod adminapi-844cc89b95-l54jm  -n yunshangshou-test -t cpu,memory
+-------------------+---------------------------+---------+------------+---------+---------+---------+------------+---------+---------+
|     NAMESPACE     |         POD NAME          | CPU USE | CPU USE(%) | CPU REQ | CPU LIM | MEM USE | MEM USE(%) | MEM REQ | MEM LIM |
+-------------------+---------------------------+---------+------------+---------+---------+---------+------------+---------+---------+
| yunshangshou-test | adminapi-844cc89b95-l54jm | 1m      | 0%         | 0m      | 0m      | 505Mi   | 0%         | 0Mi     | 0Mi     |

4、根据cpu或内存使用进行排序

--sort-by [cpu/memory]

5、支持指定label 筛选

-l key1=value1,key2=value2
声明: 本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
Kubernetes

[kubectl-resource-view]: 一款用于查看k8s资源使用情况的插件

2024-12-3 9:04:15

Kubernetes

Kubernetes Secrets 与跨命名空间访问指南

2024-12-4 16:01:32

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索