LoginSignup
6
7

More than 3 years have passed since last update.

cAdvisorのメトリックをPrometheusに格納してGrafanaで確認

Posted at

cAdvisorとは

cAdvisor(Container Advisor)は、稼働中であるコンテナのリソースとパフォーマンス状況を教えてくれるツールです。

Kubernetesの運用コストが高く、導入しづらい場合にとても役立ちそうですね。

Kubernetesでは、cAdvisorはKubeletバイナリに統合されています。
つまり、cAdvisorは、kubeletの一部として動作して、CPU、メモリ、ファイルシステム、およびネットワーク使用状況の統計情報を収集してくれます。

cAdvisorインストール

以下のコマンドを打つのみです。

# docker run --publish=8080:8080 --detach=true --name=cadvisor google/cadvisor:latest
Unable to find image 'google/cadvisor:latest' locally
latest: Pulling from google/cadvisor
ff3a5c916c92: Pull complete
44a45bb65cdf: Pull complete
0bbe1a2fe2a6: Pull complete
Digest: sha256:815386ebbe9a3490f38785ab11bda34ec8dacf4634af77b8912832d4f85dca04
Status: Downloaded newer image for google/cadvisor:latest
2bce8b923942a90d8114649d9067abd3daf476b0cec6e6bf4eb6512ade014ca7

確認して見ましょう。

# docker ps
CONTAINER ID        IMAGE                                             COMMAND                  CREATED             STATUS                PORTS                                                            NAMES
2bce8b923942        google/cadvisor:latest                            "/usr/bin/cadvisor -…"   11 seconds ago      Up 6 seconds          0.0.0.0:8080->8080/tcp    

cAdvisorにログイン

http://[ip]:8080/containers/ に接続します。
このGUIだけでも十分かもというくらい、ちゃんとできてます。

01.PNG

以下はCPUについてです。

02.PNG

他にも、MemoryやNetworkの使用率等を確認できます。

03.PNG

04.PNG

Prometheusと連携

設定ファイルの編集

job_name: cadvisorの箇所からを追加してください。
[cAdvisor IP]は、cAdvisorが載っているサーバのIPとなります。
portフォワーディングを変更している場合は、portの方も変更をします。

prometheus.yml
# vi /etc/prometheus/prometheus.yml
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

  - job_name: cadvisor
    scrape_interval: 5s
    static_configs:
    - targets: ['<cAdvisor IP>:8080']

では、prometheusを再起動して見に行きましょう。

# systemctl restart prometheus

# systemctl status prometheus

Prometheus GUI

cadvisor_version_info
でexecuteを選択し、cAdvisorのVersion情報が出力されることを確認

06.PNG

これだけでは、いまいちピンとこないので、以下でも確認します。
container_memory_usage_bytes

07.PNG

なんか、それっぽくなってきましたね。

Grafana

ここで、最後にGrafanaでdashboardを作成してみましょう。

左の+ボタンからDashboardを選択します。

image.png

そうしたらAdd Queryを選択します。

先ほど使用したcontainer_memory_usage_bytesを使ってみます。

image.png

titleを付けてあげましょう。

image.png

これを繰り返して、様々なグラフを載せましょう。

他にも以下のようなメトリックが使えます。

  • container_memory_usage_bytes
  • container_memory_usage_bytes{name="container name"}
  • container_memory_working_set_bytes
  • container_memory_rss
  • container_memory_cache
  • rate(container_cpu_usage_seconds_total{name="container name"}[1m])
  • rate(container_network_transmit_bytes_total[1m])
  • rate(container_network_receive_bytes_total[1m])

まだまだたくさんあるので、自分が欲しいものをとってこれるメトリックを探してみてください。

6
7
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
6
7