Kubernetes

Kubernetes(minikube)でNode・Memoryのリソース使用量のメトリクスを取得するコマンド(kubectl top)

概要

kubectl top コマンドを使用すると、ノードまたはポッドのリソース消費量(CPU/メモリ)を確認できるコマンド

minikubeをインストールしただけでは使えませんが、アドオンのmetrics-serverを有効にするだけで使用できます。

metrics-server を設定する

アドオンが有効でないとコマンドを実行してもエラーになります

tech-0222@MSI:~$ kubectl top pod
error: Metrics API not available
tech-0222@MSI:~$
tech-0222@MSI:~$ kubectl top nodes
error: Metrics API not available
tech-0222@MSI:~$

"minikube addons enable metrics-server"でアドオンを有効にしてみる

tech-0222@MSI:~$ minikube addons enable metrics-server
💡  metrics-server is an addon maintained by Kubernetes. For any concerns contact minikube on GitHub.
You can view the list of minikube maintainers at: https://github.com/kubernetes/minikube/blob/master/OWNERS
    ▪ Using image registry.k8s.io/metrics-server/metrics-server:v0.6.4
🌟  The 'metrics-server' addon is enabled
tech-0222@MSI:~$

アドオンを有効にするとmetrics-server関連のPodが起動してきます
※ここで少し待ちます

tech-0222@MSI:~$ kubectl get po -A
NAMESPACE     NAME                               READY   STATUS    RESTARTS     AGE
kube-system   coredns-5dd5756b68-jbjsr           1/1     Running   2 (6d ago)   39d
kube-system   etcd-minikube                      1/1     Running   2 (6d ago)   39d
kube-system   kube-apiserver-minikube            1/1     Running   2 (6d ago)   39d
kube-system   kube-controller-manager-minikube   1/1     Running   2 (6d ago)   39d
kube-system   kube-proxy-g5lrv                   1/1     Running   2 (6d ago)   39d
kube-system   kube-scheduler-minikube            1/1     Running   2 (6d ago)   39d
kube-system   metrics-server-7c66d45ddc-cjwdk    0/1     Running   0            67s ★これ
kube-system   storage-provisioner                1/1     Running   5 (6d ago)   39d
tech-0222@MSI:~$

minikubeの設定でもアドオン用のProfile(minikube)が作成され、
minikube addons list で metrics-server が enabled なのが確認できますね

tech-0222@MSI:~$ minikube profile list
|--------------|-----------|------------|--------------|------|---------|---------|-------|--------|
|   Profile    | VM Driver |  Runtime   |      IP      | Port | Version | Status  | Nodes | Active |
|--------------|-----------|------------|--------------|------|---------|---------|-------|--------|
| minikube     | docker    | docker     | 192.168.58.2 | 8443 | v1.28.3 | Stopped |     1 |        |
| test         | docker    | containerd | 192.168.67.2 | 8443 | v1.28.3 | Running |     1 | *      |
|--------------|-----------|------------|--------------|------|---------|---------|-------|--------|
tech-0222@MSI:~$

tech-0222@MSI:~$ minikube addons list
|-----------------------------|----------|--------------|--------------------------------|
|         ADDON NAME          | PROFILE  |    STATUS    |           MAINTAINER           |
|-----------------------------|----------|--------------|--------------------------------|
| ambassador                  | minikube | disabled     | 3rd party (Ambassador)         |
| auto-pause                  | minikube | disabled     | minikube                       |
| cloud-spanner               | minikube | disabled     | Google                         |
| csi-hostpath-driver         | minikube | disabled     | Kubernetes                     |
| dashboard                   | minikube | disabled     | Kubernetes                     |
| default-storageclass        | minikube | enabled ✅   | Kubernetes                     |
| efk                         | minikube | disabled     | 3rd party (Elastic)            |
| freshpod                    | minikube | disabled     | Google                         |
| gcp-auth                    | minikube | disabled     | Google                         |
| gvisor                      | minikube | disabled     | minikube                       |
| headlamp                    | minikube | disabled     | 3rd party (kinvolk.io)         |
| helm-tiller                 | minikube | disabled     | 3rd party (Helm)               |
| inaccel                     | minikube | disabled     | 3rd party (InAccel             |
|                             |          |              | [info@inaccel.com])            |
| ingress                     | minikube | disabled     | Kubernetes                     |
| ingress-dns                 | minikube | disabled     | minikube                       |
| inspektor-gadget            | minikube | disabled     | 3rd party                      |
|                             |          |              | (inspektor-gadget.io)          |
| istio                       | minikube | disabled     | 3rd party (Istio)              |
| istio-provisioner           | minikube | disabled     | 3rd party (Istio)              |
| kong                        | minikube | disabled     | 3rd party (Kong HQ)            |
| kubeflow                    | minikube | disabled     | 3rd party                      |
| kubevirt                    | minikube | disabled     | 3rd party (KubeVirt)           |
| logviewer                   | minikube | disabled     | 3rd party (unknown)            |
| metallb                     | minikube | disabled     | 3rd party (MetalLB)            |
| metrics-server              | minikube | enabled ✅   | Kubernetes                     |
| nvidia-device-plugin        | minikube | disabled     | 3rd party (NVIDIA)             |
| nvidia-driver-installer     | minikube | disabled     | 3rd party (Nvidia)             |
| nvidia-gpu-device-plugin    | minikube | disabled     | 3rd party (Nvidia)             |
| olm                         | minikube | disabled     | 3rd party (Operator Framework) |
| pod-security-policy         | minikube | disabled     | 3rd party (unknown)            |
| portainer                   | minikube | disabled     | 3rd party (Portainer.io)       |
| registry                    | minikube | disabled     | minikube                       |
| registry-aliases            | minikube | disabled     | 3rd party (unknown)            |
| registry-creds              | minikube | disabled     | 3rd party (UPMC Enterprises)   |
| storage-provisioner         | minikube | enabled ✅   | minikube                       |
| storage-provisioner-gluster | minikube | disabled     | 3rd party (Gluster)            |
| storage-provisioner-rancher | minikube | disabled     | 3rd party (Rancher)            |
| volumesnapshots             | minikube | disabled     | Kubernetes                     |
|-----------------------------|----------|--------------|--------------------------------|
💡  To see addons list for other profiles use: `minikube addons -p name list`
tech-0222@MSI:~$

リソース消費量(CPU/メモリ)を確認してみる

Node、Podのリソース消費量(CPU/メモリ)がtopコマンドで確認できるようになってますね

tech-0222@MSI:~$ kubectl top nodes
NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
minikube   302m         1%     827Mi           5%
tech-0222@MSI:~$

tech-0222@MSI:~$ kubectl top po -A
NAMESPACE     NAME                               CPU(cores)   MEMORY(bytes)
kube-system   coredns-5dd5756b68-44v8z           2m           54Mi
kube-system   etcd-minikube                      10m          370Mi
kube-system   kube-apiserver-minikube            24m          316Mi
kube-system   kube-controller-manager-minikube   15m          128Mi
kube-system   kube-proxy-ktwhc                   1m           60Mi
kube-system   kube-scheduler-minikube            2m           64Mi
kube-system   metrics-server-7c66d45ddc-vp7xz    2m           51Mi
kube-system   storage-provisioner                3m           9Mi
tech-0222@MSI:~$

Podのコンテナ単位も表示したい場合は以下で確認できます

tech-0222@MSI:~$ kubectl top po -A --containers=true
NAMESPACE     POD                                NAME                      CPU(cores)   MEMORY(bytes)
kube-system   coredns-5dd5756b68-44v8z           coredns                   1m           58Mi
kube-system   etcd-minikube                      etcd                      10m          377Mi
kube-system   kube-apiserver-minikube            kube-apiserver            22m          331Mi
kube-system   kube-controller-manager-minikube   kube-controller-manager   8m           121Mi
kube-system   kube-proxy-ktwhc                   kube-proxy                1m           60Mi
kube-system   kube-scheduler-minikube            kube-scheduler            2m           65Mi
kube-system   metrics-server-7c66d45ddc-vp7xz    metrics-server            2m           59Mi
kube-system   storage-provisioner                storage-provisioner       2m           15Mi
tech-0222@MSI:~$

#1PODのコンテナは以下

tech-0222@MSI:~$ kubectl top po etcd-minikube --containers -n kube-system
POD             NAME   CPU(cores)   MEMORY(bytes)
etcd-minikube   etcd   10m          377Mi
tech-0222@MSI:~$

オプションで使用量が多いい順番に並び替えもできますね

tech-0222@MSI:~$ kubectl top po -A --sort-by=cpu
NAMESPACE     NAME                               CPU(cores)   MEMORY(bytes)
kube-system   kube-apiserver-minikube            21m          331Mi
kube-system   etcd-minikube                      10m          374Mi
kube-system   kube-controller-manager-minikube   8m           121Mi
kube-system   coredns-5dd5756b68-44v8z           2m           57Mi
kube-system   kube-scheduler-minikube            2m           65Mi
kube-system   metrics-server-7c66d45ddc-vp7xz    2m           58Mi
kube-system   storage-provisioner                2m           15Mi
kube-system   kube-proxy-ktwhc                   1m           60Mi

tech-0222@MSI:~$ kubectl top po -A --sort-by=memory
NAMESPACE     NAME                               CPU(cores)   MEMORY(bytes)
kube-system   etcd-minikube                      10m          375Mi
kube-system   kube-apiserver-minikube            22m          331Mi
kube-system   kube-controller-manager-minikube   8m           121Mi
kube-system   kube-scheduler-minikube            2m           65Mi
kube-system   kube-proxy-ktwhc                   1m           60Mi
kube-system   metrics-server-7c66d45ddc-vp7xz    2m           58Mi
kube-system   coredns-5dd5756b68-44v8z           1m           57Mi
kube-system   storage-provisioner                2m           15Mi
tech-0222@MSI:~$

オプションで合計も出せるみたい

tech-0222@MSI:~$ kubectl top po -A --sum
NAMESPACE     NAME                               CPU(cores)   MEMORY(bytes)
kube-system   coredns-5dd5756b68-44v8z           2m           57Mi
kube-system   etcd-minikube                      10m          377Mi
kube-system   kube-apiserver-minikube            21m          331Mi
kube-system   kube-controller-manager-minikube   8m           121Mi
kube-system   kube-proxy-ktwhc                   1m           60Mi
kube-system   kube-scheduler-minikube            2m           65Mi
kube-system   metrics-server-7c66d45ddc-vp7xz    2m           59Mi
kube-system   storage-provisioner                1m           15Mi
                                                 ________     ________
                                                 43m          1088Mi
tech-0222@MSI:~$

参考

アドオンの有効化
https://kubernetes.io/ja/docs/tutorials/hello-minikube/#%E3%82%A2%E3%83%89%E3%82%AA%E3%83%B3%E3%81%AE%E6%9C%89%E5%8A%B9%E5%8C%96

人気記事

1

Kubernetes kubectl rollout コマンドでdeploymentをロールバックしてみる 概要 kubectl rollout コマンドでデプロイ履歴を確認してロールバックするやり方 ...

2

  terraformのバージョン管理どうすればいいのか? terraformの複数バージョンを簡単に動かしたい terraformのインストール方法が分からない と、疑問を抱えている人の疑 ...

3

本記事では どんな環境にterraformをインストールできるの? terraformのインストールどうしたらいいの? terraformのどのバージョンをインストールすればいいの? と、困っている人 ...

4

Kubernetes kubectl version コマンドでバージョンを調べてみる 概要 kubectl version は、Kubernetes の Client、Serverのバージョンを確認 ...

-Kubernetes