How to Create a VM in GCP & How to Monitor it with Cloud Monitoring.

How to Create a VM in GCP & How to Monitor it with Cloud Monitoring.

Let's see How to create a VM in GCP


  • Login to https://cloud.google.com/ with your account.
  • A default dashboard screen will open
  • Go to Navigation menu > Compute Engine > VM instances, then click Create.

VM Instance option.png

OR

  • You can directly search 'VM Instances' in Search bar.

Screenshot from 2021-01-10 18-16-17.png

  • A window will open, click on 'Create'

Screenshot from 2021-01-10 17-21-29.png

  • Fill in the fields as follows, leaving all other fields at the default value: Name lamp-1-vm or (any name you want)

Region us-central1 (Iowa)

Zone us-central1-a

Series N1

Machine type n1-standard-2 (Depend on machine type, cost will change and it will show in Right Hand Side as in fig)

Firewall check Allow HTTP traffic

Screenshot from 2021-01-10 17-22-34.png

Allow HTTP Traffic.

Screenshot from 2021-01-10 17-22-50.png

Then click on Create, VM will be created in a few seconds.

Screenshot from 2021-01-10 17-23-45.png

The next step is to monitor it with 'Cloud Monitoring'


  • Click Navigation menu > Monitoring > Overview Screenshot from 2021-01-10 17-37-43.png

Monitoring comes under the Operation section. I guess it formerly called (Stackdriver) though not 100% sure.

  • When the Monitoring dashboard opens, your workspace is ready.

Screenshot from 2021-01-10 17-40-20.png

Like in the above image, it shows disk, VM-instances, firewall, and other charts

Screenshot from 2021-01-10 17-40-39.png

if we click on 'VM Instances' it shows all VM-Instances available in Cloud in this Project.

Screenshot from 2021-01-10 17-41-00.png

By default, Monitoring can access some instance metrics including CPU utilization, some disk traffic metrics, network traffic, and uptime information.

But what if we want to monitor third-party applications, for example, let's suppose we install apache on VM box.

Click on SSH to connect to VM and run the below command

carbon (1).png

Screenshot from 2021-01-10 17-52-40.png

To verify whether the apache server is UP or not. Click on External IP address shown in VM Instances Screen Screenshot from 2021-01-10 17-23-45.png a new window will open as below.

Screenshot from 2021-01-10 17-54-54.png

To monitor third-party applications in Cloud Monitoring also other metrics of VM (which not fetch by default) we need to install the Monitoring Agent on VM

To configure the Cloud Monitoring agent to monitor supported third-party applications. The configuration is only possible when the Monitoring agent is running on Linux. The Monitoring agent on Windows monitors IIS and MS SQL applications; however, on these systems, the agent cannot be configured to collect metrics from other third-party applications.

Example of third-party appications:- • Apache web server • JVM Monitoring • Memcached • MySQL • Nginx • Redis • StatsD

Install the monitoring agent using the below commands:-

carbon (1).png Stackdriver Monitoring agent is used to monitor the VM which is a collectd-based daemon that collects system and application metrics from virtual machine instances. It then sends these metrics to Monitoring. The Monitoring agent by default collects metrics information on disk, CPU, network, and processes.

Let's also install the Logging-agent

The Cloud Logging agent to stream logs from your VM instances to Cloud Logging.

Cloud Monitoring and Cloud Logging are closely integrated

google-fluentd is the logging agent. carbon (2) (1).png

Create an uptime check


Uptime checks verify that a resource is always accessible. For practice, create an uptime check to verify your VM is up.

mdt35y5ElNzYoCkdrVkodx81TWTuTWAbBYfoj0Ps5r4=.png

Set the following fields: Title: Lamp Uptime Check, then click Next.

Protocol: HTTP

Resource Type: Instance

Applies to: Single, lamp-1-vm

Path: leave at default

Check Frequency: 1 min

HYlXLfWnUXFtXC0A34MfQljuNeFLsUr4XX93uPYfWPY=.png Click Test to verify that your uptime check can connect to the resource.

Create an alerting policy


Use Cloud Monitoring to create one or more alerting policies.

In the left menu, click Alerting, and then click Create Policy.

Screenshot from 2021-01-10 18-03-21.png

Click Add Condition.

Set the following in the panel that opens, leave all other fields at the default value.

Target: Start typing "VM" in the resource type and metric field, and then select:

Resource Type: VM Instance (gce_instance) Metric: Type "network", and then select Network traffic (gce_instance+1). Be sure to choose the Network traffic resource with agent.googleapis.com/interface/traffic:

Configuration

Condition: is above Threshold: 500 For: 1 minute Click ADD.

Click on Next.

iaqRe2fY42Td+8T8lu0UJUPRp377nHLXBvuXi+UGyBk=.png

Click on drop down arrow next to Notification Channels, then click on Manage Notification Channels.

akOFdDyEuKOmhn6BWOrBP7cGEvEy5jTDg28Q71R6Xms=.png

A Notification channels page will open in new tab. Screenshot from 2021-01-10 18-06-47.png

In Create Email Channel dialog box, enter your personal email address in the Email Address field and a Display name.

Click on Save.

Go back to the previous Create alerting policy tab.

Click on Notification Channels again, then click on the Refresh icon to get the display name you mentioned in the previous step.

fXnoFzeQzHQdc905vD6QLzfKuwfk3F0BXZZADgsaUaQ=.png

Now, select your Display name and click OK.

Click Next.

Mention the Alert name as Inbound Traffic Alert.

Add a message in documentation, which will be included in the emailed alert.

Click on Save.

You've created an alert!

View your logs


Select Navigation menu > Logging > Logs Explorer.

Screenshot from 2021-01-10 18-31-55.png

Select the logs you want to see, in this case, you select the logs for the lamp-1-vm instance you created at the start of this lab:

Click on Resource.

oQG0TLaDLkFzNOAC25twWtuq55mOUPJDw05rZ3hMPXM=.png

Select VM Instance > lamp-1-vm in the Resource drop-down menu.

At5somXo_LgoR0YnOtwttk6q1QZUNTJssq2lxhl5luE=.png

Click Add. Leave the other fields with their default values. Click the Stream logs.

47cMAwVFt6zz3jGKFihSNTzHxaWmxpdyeXFi0QnUptw=.png

You see the logs for your VM instance:

Screenshot from 2021-01-10 18-35-44.png

Check out what happens when you start and stop the VM instance.

If we stop the VM instance, same get reflected in logs

Screenshot from 2021-01-10 18-37-39.png If we start the VM instance, we see the same in logs.

Screenshot from 2021-01-10 18-38-08.png

Check the uptime check results and triggered alerts

As we created an email alert, So whenwe stopped the VM, we get the alerts in our email box.

Screenshot from 2021-01-10 18-41-34.png

When the VM down, we see all RED mark in Uptime.

Screenshot from 2021-01-10 18-39-24.png When the VM UP, we see all Green Tick in Uptime.

Screenshot from 2021-01-10 18-42-58.png


Resources

https://googlecourses.qwiklabs.com/ - GSP089