- Google-fluentd
- Create a service account that has logging roles enabled.
- This can be done in the IAM section of the GCP console.
- As you create the service account, do not forget to download the service account keys. As these are the ones that grant access to any cloud resources your machines or application wishes to make use of. If you see a page such as this:
-
Make sure you are accessing the correct project and if you already are, double check if you have admin right to that project.
-
Make sure to explicitly tell the instance which service account to use as explained here, else it will use the default service account. But If you do not wish to create a new service account(bad practice), add the logging permissions onto the default service account. This is done from the IAM & admin section of the GCP console. Still, make sure you have admin right to the given project else you will not see this screen:
-
- For this project we used Terraform to build our VPC, just like documentation states here, add the
service accountargument to the instance template resource in the terraform script, then add the email of the service account that has the logging roles as demonstrated above then add the logging scopes too as explained here. This is enough to give logging permissions to all our instances that will be created.
- For this project we used Terraform to build our VPC, just like documentation states here, add the
-
- Install the logging agents as follows:
-
whether you are using packer to build your images or just SSH-ing into the VM instances, run the following commands in the VM instance terminal. For packer add it to a bash script and add that script as a
shell provisionerin the packer script. -
- If you are not using an image builder like packer, SSH into the VM instance and
cdinto/etc/google-fluentd/config.dfolder. Create a*.conffile. This file will hold the application logging configurations. The configurations look like below but enclosed between the opening and closingsourcetags;
- If you are not using an image builder like packer, SSH into the VM instance and
@type tail
format none
path /path/to/application/logs/development.log
pos_file /var/lib/google-fluentd/pos/vof.pos
read_from_head true
tag vof_development_logs
-
- For a reference on how to write this configuration file, take a look at this
-
- Save the file and cd into
/var/lib/google-fluentd/posfolder and add a*.posfile, this is the file fluentd uses to track the last read position of the log files. It can hold many log file "last read" positions, so one file can hold all your logs position configurations.
- Save the file and cd into
-
- The contents of this file look like
/var/home/logs/google.log 000000000 00000000where/var/home/logs/google.logis the path to the log file and the zeros are the current read position. This value will change every time fluentd reads the logs.
- The contents of this file look like
-
- With that set, restart the google-fluentd service by running the following command in your terminal
sudo service google-fluentd restart.
- With that set, restart the google-fluentd service by running the following command in your terminal
-
If you are using an image builder like packer, i suggest you write a bash script and include all the above steps from 2 like this:
-
and include that script in your packer
.jsonfile under theshell provisioners. This script will then run when an instance created from this packer built image is started. -
When the instance is up and running, head over to the google console logging section, select the GCE VM instance you have created and the configured application and system logs will be there.
Happy Logging






