Date created: October 26, 2018
Last updated: March 10, 2019

Note: I have written a newer version of the this article that goes much deeper into setting up Google Stackdriver on Compute Engine.

Google Compute – Stackdriver Logging – Installation, Setup & Debugging

Google Stackdriver is a very good product for monitoring and logging your compute instances on Google Cloud, AWS, Azure, Alibaba, etc.

To make sure that Stackdriver is installed on each instance, I create instance templates that contain a script in the custom metadata section to automate Stackdriver installation and setup.

An important item to remember, startup scripts are executed every time an instance starts and not just on instance creation.

In my startup script for Debian 9 Stretch, I install Google Stackdriver logging and monitoring agents.

#!/bin/bash
apt-get update

# Install Stackdriver logging
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh

# Install Stackdriver monitoring
curl -sSO https://dl.google.com/cloudagents/install-monitoring-agent.sh
sudo bash install-monitoring-agent.sh

If you are manually creating a Compute instance, copy this script into the Automation -> Startup script section when creating the instance.

To determine the currently installed versions:

Stackdriver Monitoring

dpkg -l stackdriver-agent

Output:

stackdriver-agent   5.5.2-382.stre amd64          Stackdriver system metrics collection daemon

 

Stackdriver Logging

dpkg -l google-fluentd

Output:

google-fluentd      1.6.0-1        amd64          Google Fluentd: A data collector for Google

You can add a startup-script for a running instance from the CLI. Note: this command will replace the existing startup script.

Copy the following to a local file. In this example startup.script. Modify to fit your requirements:

#!/bin/bash
apt-get update

# Install Stackdriver logging
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
bash install-logging-agent.sh

# Install Stackdriver monitoring
curl -sSO https://dl.google.com/cloudagents/install-monitoring-agent.sh
bash install-monitoring-agent.sh

 

Execute the following command from your desktop:

gcloud compute instances add-metadata INSTANCE_NAME --metadata-from-file startup-script=startup.script

 

You can also store your scripts in Google Storage:

gcloud compute instances add-metadata INSTANCE_NAME --metadata startup-script-url=gs://bucket/file

 

The startup script will be executed the next time the instance reboots.

gcloud compute instances reset INSTANCE_NAME --zone=ZONE

 

Installing the Logging Agent

Installing the Monitoring Agent