Ops Agent

Architecture

Ops Agent creates a virtual machine and installs the Ops Agent for integrating with Monitoring and Logging services. It uses:

  • Compute Engine
  • Monitoring
  • Logging

Example sets up a simple single virtual machine and wires it up to report to Logging.


Get Started

Click on the following link to a copy of the source code in Cloud Shell. Once there, a single command will spin up a working copy of the application in your project..

Open in Cloud Shell

View source code on GitHub


Ops Agent components

The Ops Agent architecture makes use of several products. The following lists the components, along with more information on the components, including links to related videos, product documentation, and interactive walkthroughs.
Video Docs Walkthroughs
Compute Engine Compute Engine is Google Cloud's Virtual technology. With it you can spin up many different configurations of VM to fit the shape of whatever computing needs you have.
Cloud Monitoring Cloud Monitoring exposes visibility into the performance, availability, and health of your applications and infrastructure
Cloud Logging Cloud Logging provides fully managed, real-time log management with storage, search, analysis and alerting at exabyte scale.

Scripts

The install script uses an executable written in go and Terraform CLI tools to take an empty project and install the application in it. The output should be a working application and a url for the load balancing IP address.

./main.tf

Enable Services

Google Cloud Services are disabled in a project by default. In order to use any of the solutions here, we have to activate the following:

  • Compute Engine — virtual machines and networking
variable "gcp_service_list" {
    description = "The list of apis necessary for the project"
    type        = list(string)
    default = [
        "compute.googleapis.com",
    ]
}

resource "google_project_service" "all" {
  for_each                   = toset(var.gcp_service_list)
  project                    = var.project_number
  service                    = each.key
  disable_dependent_services = false
  disable_on_destroy         = false
}

Create a virtual machine

Creates a VM.

resource "google_compute_instance" "default" {
  project      = var.project_id
  name         = "${var.basename}-instance"
  machine_type = "f1-micro"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "centos-cloud/centos-8-v20210817"
    }
  }
  labels = {
    env        = "prod"
    app        = "myproduct"
    created_by = "terraform"
  }

  network_interface {
    network = "default"

    access_config {
      // Include this section to give the VM an external ip address
    }
  }
  service_account {
    // Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
    // This non production example uses the default compute service account.
    email  = local.sacompute
    scopes = ["cloud-platform"]
  }
}

Install Ops Agent

The Ops Agent is the primary agent for collecting telemetry from your Compute Engine instances. This will show you how to install it, using terraform.

module "agent_policy" {
    source  = "terraform-google-modules/cloud-operations/google//modules/agent-policy"
    version = "~> 0.1.0"
    
    project_id = var.project_id
    policy_id  = "ops-agents-example-policy"
    agent_rules = [
        {
        type               = "ops-agent"
        version            = "current-major"
        package_state      = "installed"
        enable_autoupgrade = true
        },
    ]
    group_labels = [
        {
        env        = "prod"
        app        = "myproduct"
        created_by = "terraform"
        }
    ]
    
    os_types = [
        {
        short_name = "centos"
        version    = "8"
        },
    ]
}

Conclusion

Once run, you should now have a VM that's been properly configured to log events to Google Cloud Logging. Additionally you should have all of the code to modify or extend this solution to fit your environment.