Reputation: 909
I need to install Monitoring Agent on all my compute engine VM's and I was wondering if it's possible to do it automatically using some type startup script or something like that. Is that possible?
According to Google, these are the steps to install it on a single VM:
Upvotes: 1
Views: 572
Reputation: 11277
Startup scripts are not a good solution here because that script will run every time the instance boots. Even if the Stackdriver monitoring agent install is clever and does not reinstall the agent if it is already installed (maybe it does, I don't know), using a startup script here does not seem to be appropriate. If you want to install it at each boot (maybe to keep it updated), it could be a solution, but in your case (install once on all your existing VMs) there is probably better solutions.
If you have root access on your VMs, and use Google Cloud SDK, you could simply iterate on your VMs and run remote commands via SSH for each one, like the following example assuming that all your VMs are in the same project $MY_PROJECT
(be careful to test this on a small subset of VMs before running it on all your VMs in production environment, I have not tested it). The syntax is valid for Google Cloud SDK v243.0.0.
#!/usr/bin/env sh
REMOTE_COMMAND="curl -sSO https://dl.google.com/cloudagents/install-monitoring-agent.sh && \
sudo bash install-monitoring-agent.sh && \
sudo service stackdriver-agent restart"
for instance_name in $(gcloud --project "$MY_PROJECT" compute instances list --format="value(name)")
do
zone=$(gcloud --project "$MY_PROJECT" compute instances list \
--filter="name=($instance_name)" \
--format="value(zone)")
gcloud --project "$MY_PROJECT" compute ssh "$instance_name" \
--zone="$zone" \
--command "$REMOTE_COMMAND"
done
This is similar to connecting to each VM, run the command on this VM, and disconnect. The advantage here is that this could be run from one host, as long as you can connect to your VMs through SSH.
If you are familiar with Ansible or some other provisioning tools, there is probably a better solution than the previous shell script. But if this is one-shot and you want a quick and light solution, then go ahead for the shell script (be careful to test it before though).
For your future Compute Engine VMs, and if all your VMs have similar configuration, I recommend you to take a look at instance templates or images. You could create a specific instance template or image with the Stackdriver monitoring agent pre-installed, and create all your future VMs from these instance template/image.
Upvotes: 3