はじめに
こんにちは、StackdriverあらためGoogle Cloud Operations担当者です。今回は担当分野の新しい機能について紹介します。本記事はGoogle Cloud LoggingやGoogle Cloud Monitoringというものがなにかをすでに理解されている方向けに書いています。
TL;DR
Agent Policyを使うことで、Google Cloud LoggingとGoogle Cloud MonitoringのエージェントをGCEインスタンス作成時に自動でインストール&起動させられるようになる。
Google Cloud Logging + Google Cloud Monitoring on Google Compute Engine
Google Cloud LoggingやGoogle Cloud MonitoringはGCPが提供するログとメトリクスに関するマネージドサービスです。Google Cloud Platform上の各種マネージド・サービスやインスタンスのシステムログ、監査ログ、システムメトリクスなどはバックエンドで自動で取り込まれ、これら2つのサービスで確認できます。またGoogle App EngineやGoogle Kubernetes Engineを始めとするランタイムではアプリケーションログやアプリケーションメトリクスなども自動で送られるように設定されています。
しかしGoogle Compute Engineのインスタンスではその性質ゆえに、アプリケーションログやアプリケーションメトリクスの取得のためにはGoogle Cloud LoggingやGoogle Cloud Monitoringの各種エージェントをインスタンス作成後に追加でインストール&起動する必要がありました。
今回の新機能はその手間をAgent Policyという機能を使って無くそう、というものです。
Agent Policyの管理
本記事の内容は上の公式ドキュメントにあるものをそのままなぞっているだけなのですが、現時点でドキュメントが少しわかりにくい部分もあるので補足しながら追っていきます。
本機能は本記事執筆時点でAlpha版なので gcloud components
ではalphaをインストールしておいてください。
$ gcloud components install alpha
権限周りの設定
まず下準備として権限周り(IAMなど)の設定をするのですが、いくつもの権限を設定しなければいけないので、それを一気に行ってくれる便利シェルスクリプトが用意されていますのでこれをダウンロードして適宜オプションを指定して実行します。
ドキュメントにも書いてありますが、このシェルスクリプトを使って行うことは
- Cloud Logging、Cloud Monitoring、OS Configの各APIの有効化
- GCEのデフォルトサービスアカウントへの必要なロールの追加
- OS Configメタデータの有効化
- 指定したosconfigのIAMロールを指定したユーザーまたはサービスアカウントに付与
次の例は agent-install-test
というプロジェクトで admin@example.com
というユーザーに roles/osconfig.guestPolicyAdmin
の権限を付与する場合の例
$ bash set-permissions.sh --project=agents-install-test --iam-user=admin@example.com --iam-permission-role=guestPolicyAdmin
Agent Policyの作成
上の準備ができたら Agent Policyを作成します。次の例は ops-agent-debian
というAgent Policyを作成する例です。
ルールとしてDebian 10のイメージでGCEインスタンスを作成する場合に、Cloud LoggingとCloud Monitoringのエージェントをインストールするよう指定します。
$ gcloud alpha compute instances ops-agents policies create ops-agents-debian \ --agent-rules="type=logging,version=current-major,package-state=installed,enable-autoupgrade=true;type=metrics,version=current-major,package-state=installed,enable-autoupgrade=true" \ --os-types=short-name=debian,version=10
これで Debian 10 のイメージでインスタンスを新規作成すると自動でCloud LoggingとCloud Monitoringのエージェントがインストールされ起動されるようになります。ほかにもGCEインスタンスに特定のタグが付いた場合の条件を設定するには --group-labels
を指定すれば良いです。
テスト1: Debian 10のインスタンスを作成する
早速GCEインスタンスを作ってみます。まず条件に一致するDebian 10のインスタンスです。
$ gcloud compute instances create test0 \ --image-project debian-cloud \ --image-family=debian-10 \ --zone=us-central1-a \ --preemptible \ --boot-disk-auto-delete Created [https://www.googleapis.com/compute/v1/projects/agents-install-test/zones/us-central1-a/instances/test0]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS test0 us-central1-a n1-standard-1 true XX.XX.XX.XX XX.XX.XX.XX RUNNING $ gcloud compute ssh test0 --zone=us-central1-a Writing 3 keys to /home/ymotongpoo/.ssh/google_compute_known_hosts Enter passphrase for key '/home/ymotongpoo/.ssh/google_compute_engine': Linux test0 4.19.0-10-cloud-amd64 #1 SMP Debian 4.19.132-1 (2020-07-24) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. ymotongpoo@test0:~$ sudo service google-fluentd status ● google-fluentd.service - LSB: data collector for Treasure Data Loaded: loaded (/etc/init.d/google-fluentd; generated) Active: active (running) since Tue 2020-08-11 08:50:36 UTC; 1min 17s ago Docs: man:systemd-sysv-generator(8) Tasks: 110 (limit: 4373) Memory: 66.9M CGroup: /system.slice/google-fluentd.service └─2128 /opt/google-fluentd/embedded/bin/ruby /usr/sbin/google-fluentd --log /var/log/google-fluentd/goo Aug 11 08:50:36 test0 systemd[1]: Starting LSB: data collector for Treasure Data... Aug 11 08:50:36 test0 google-fluentd[2106]: Starting google-fluentd 1.7.1: google-fluentd. Aug 11 08:50:36 test0 systemd[1]: Started LSB: data collector for Treasure Data. ymotongpoo@test0:~$ sudo service stackdriver-agent status ● stackdriver-agent.service - LSB: start and stop Stackdriver Agent Loaded: loaded (/etc/init.d/stackdriver-agent; generated) Active: active (running) since Tue 2020-08-11 08:50:42 UTC; 1min 32s ago Docs: man:systemd-sysv-generator(8) Tasks: 13 (limit: 4373) Memory: 6.2M CGroup: /system.slice/stackdriver-agent.service └─2470 /opt/stackdriver/collectd/sbin/stackdriver-collectd -C /etc/stackdriver/collectd.conf -P /var/ru Aug 11 08:50:42 test0 collectd[2469]: plugin_load: plugin "write_gcm" successfully loaded. Aug 11 08:50:42 test0 collectd[2469]: plugin_load: plugin "match_regex" successfully loaded. Aug 11 08:50:42 test0 collectd[2469]: plugin_load: plugin "match_throttle_metadata_keys" successfully loaded. Aug 11 08:50:42 test0 collectd[2469]: plugin_load: plugin "stackdriver_agent" successfully loaded. Aug 11 08:50:42 test0 collectd[2469]: plugin_load: plugin "exec" successfully loaded. Aug 11 08:50:42 test0 collectd[2469]: plugin_load: plugin "aggregation" successfully loaded. Aug 11 08:50:42 test0 stackdriver-agent[2449]: . Aug 11 08:50:42 test0 systemd[1]: Started LSB: start and stop Stackdriver Agent. Aug 11 08:50:42 test0 collectd[2470]: Initialization complete, entering read-loop. Aug 11 08:50:42 test0 collectd[2470]: tcpconns plugin: Reading from netlink succeeded. Will use the netlink method
Cloud LoggingとCloud Monitoringのエージェントがインストールされて動いてますね!
テスト2: CentOS 8のインスタンスを作成する
今度は条件に合致しない、CentOS 8のインスタンスを作成してみます。
$ gcloud compute instances create test1 \ --image-project=centos-cloud \ --image-family=centos-8 \ --zone=us-central1-a \ --preemptible \ --boot-disk-auto-delete Created [https://www.googleapis.com/compute/v1/projects/agents-install-test/zones/us-central1-a/instances/test0]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS test1 us-central1-a n1-standard-1 true XX.XX.XX.XX XX.XX.XX.XX RUNNING $ gcloud compute ssh test1 --zone=us-central1-a Writing 3 keys to /home/ymotongpoo/.ssh/google_compute_known_hosts Enter passphrase for key '/home/ymotongpoo/.ssh/google_compute_engine': [ymotongpoo@test0 ~]$ sudo service google-fluentd status Redirecting to /bin/systemctl status google-fluentd.service Unit google-fluentd.service could not be found. [ymotongpoo@test0 ~]$ sudo service stackdriver-agent status Redirecting to /bin/systemctl status stackdriver-agent.service Unit stackdriver-agent.service could not be found.
こちらは各エージェントがインストールされていないことが分かります。
おわりに
本機能はまだAlphaですので、まだまだ変更の可能性が十分あります。たとえば、現状作成した ops-agents
のポリシー一覧やその詳細は gcloud
コマンドでしか確認できず、UIはありません。
また本機能がサポートされているのは Cloud Logging Agent と Cloud Monitoring Agent がサポートされているLinuxディストリビューションのイメージが使用された場合のみです。もし本機能を利用されていてなにか不具合や要望がありましたら ops-agent-policy-feedback@google.com まで直接連絡するか、もしくは @ymotongpoo まで連絡してください。