0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kubernetesクラスタの各ノードの残リソース確認方法

Posted at

ぱっと取得できるかと思いきやだいぶ複雑なことをやる羽目になってしまったので、もっと単純な方法をご存知の方は是非ご教示ください。。

前提条件

metrics-serverが導入済みであること。

結論

こうやる。

CPUの場合

コマンド

paste <(kubectl get nodes -o custom-columns="NODE:.metadata.name,CPU_ALLOC:.status.allocatable.cpu" --no-headers) \
      <(kubectl top nodes --no-headers | awk '{print $2}') | \
awk '{cpu_free=($2 * 1000) - ($3+0); print "Node:", $1, "| Free CPU:", cpu_free "m"}'

出力

Node: controlplane-01 | Free CPU: 3870m
Node: controlplane-02 | Free CPU: 3907m
Node: controlplane-03 | Free CPU: 3902m
Node: node-01 | Free CPU: 7809m
Node: node-02 | Free CPU: 7799m
Node: node-03 | Free CPU: 7899m
Node: node-04 | Free CPU: 7833m

メモリの場合

コマンド

paste <(kubectl get nodes -o custom-columns="NODE:.metadata.name,MEM_ALLOC:.status.allocatable.memory" --no-headers) \
      <(kubectl top nodes --no-headers | awk '{print $4}') | \
awk '{mem_free=($2 / 1024) - ($3+0); print "Node:", $1, "| Free Memory:", mem_free "Mi"}'

出力

Node: controlplane-01 | Free Memory: 3837.85Mi
Node: controlplane-02 | Free Memory: 4321.84Mi
Node: controlplane-03 | Free Memory: 4562.84Mi
Node: node-01 | Free Memory: 10417.8Mi
Node: node-02 | Free Memory: 9374.84Mi
Node: node-03 | Free Memory: 11681.8Mi
Node: node-04 | Free Memory: 10127.8Mi

これができると何が嬉しいのか

なぜこんな複雑なことをやる必要があるのか。

これができると、
CPUやメモリといったリソースの余力がその時点でどれだけ残っているか一目で分かるからである。

K8sでのリソース使用状況確認といったらmetrics-serverだが、metrics-serverではノードやPodごとのCPU/メモリの使用状況は確認することができるものの、使用量と使用率で表示される。
つまり、後どれだけリソースが残っているか一目で確認することはできない。

# kubectl top node
NAME              CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
controlplane-01   110m         2%     3973Mi          50%       
controlplane-02   97m          2%     3588Mi          45%       
controlplane-03   92m          2%     3208Mi          40%       
node-01           183m         2%     5481Mi          34%       
node-02           196m         2%     6514Mi          41%       
node-03           96m          1%     4211Mi          26%       
node-04           160m         2%     5752Mi          36%   

よって例えばCPUの場合、以下の順序で必要な情報を取得、整形、計算の上で表示する必要がある。

ノードの名称と割り当て可能CPUの最大値を取得する

# kubectl get nodes -o custom-columns="NODE:.metadata.name,CPU_ALLOC:.status.allocatable.cpu" --no-headers
controlplane-01   4
controlplane-02   4
controlplane-03   4
node-01           8
node-02           8
node-03           8
node-04           8

現在の使用量を取得する

# kubectl top nodes --no-headers | awk '{print $2}'
107m
106m
105m
150m
147m
97m
246m

出力を連結する

pasteコマンドを使用する。

# paste <(kubectl get nodes -o custom-columns="NODE:.metadata.name,CPU_ALLOC:.status.allocatable.cpu" --no-headers) \
      <(kubectl top nodes --no-headers | awk '{print $2}')
controlplane-01   4        116m
controlplane-02   4        96m
controlplane-03   4        84m
node-01           8        148m
node-02           8        138m
node-03           8        94m
node-04           8        274m

桁と単位を合わせた上で計算、表示する

awkコマンドを使用する。

# paste <(kubectl get nodes -o custom-columns="NODE:.metadata.name,CPU_ALLOC:.status.allocatable.cpu" --no-headers) \
      <(kubectl top nodes --no-headers | awk '{print $2}') | \
awk '{cpu_free=($2 * 1000) - ($3+0); print "Node:", $1, "| Free CPU:", cpu_free "m"}'
Node: controlplane-01 | Free CPU: 3870m
Node: controlplane-02 | Free CPU: 3907m
Node: controlplane-03 | Free CPU: 3902m
Node: node-01 | Free CPU: 7809m
Node: node-02 | Free CPU: 7799m
Node: node-03 | Free CPU: 7899m
Node: node-04 | Free CPU: 7833m

以上で、Kubernetesクラスタの各ノードの残リソースが一目で確認できるようになる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?