Tag: Google Cloud Console

  • Creating an Autoscaled Managed Instance Group Using an Instance Template

    What is an Autoscaled Managed Instance Group?

    An autoscaled MIG is a collection of identical virtual machine (VM) instances that can automatically adjust in size based on demand. This dynamic scaling ensures your applications have the necessary resources during peak traffic while optimizing costs during lulls.

    Prerequisites

    1. Instance Template: Create an instance template that defines the properties of the VMs you want in your MIG (e.g., machine type, boot disk, network configuration).
    2. Health Check: Configure a health check to monitor the health of your instances.
    3. Load Balancer (Optional): If distributing traffic, set up a load balancer.

    Step 1: Create the Managed Instance Group

    • In the GCP Console, go to “Compute Engine” > “Instance groups.”
    • Click “Create instance group.”
    • Choose “Managed instance group” and select your desired zone.
    • Select the “Instance template” you created and specify the initial number of instances.
    • Configure autoscaling:
      • Select “On” to enable autoscaling.
      • Set minimum and maximum instance counts.
      • Define scaling policies based on metrics like CPU utilization or load balancer traffic.
    • Set up autohealing if desired, which will automatically recreate unhealthy instances.
    • Click “Create.”

    Step 2: Verify and Monitor

    • After creation, your MIG will start provisioning instances.
    • Observe the “Instance groups” page to track the group’s status.
    • Monitor the autoscaling logs to see how the group scales up or down.
    • Use the load balancer’s monitoring tools to observe how traffic is distributed (if applicable).

    Example Using the gcloud Tool

    # Create the MIG
    gcloud compute instance-groups managed create my-mig \
        --template=my-template \
        --base-instance-name=my-instance \
        --size=2 \
        --zone=us-central1-a
    
    # Set up autoscaling
    gcloud compute instance-groups managed set-autoscaling my-mig \
        --max-num-replicas=10 \
        --min-num-replicas=2 \
        --target-cpu-utilization=0.75 \
        --zone=us-central1-a

    Important Considerations

    • Choose the right instance template to match your workload requirements.
    • Carefully consider the autoscaling parameters (min/max instances, metrics, and thresholds) to balance cost and performance.
    • Regularly review logs and monitor metrics to optimize your MIG configuration.

    By following these steps, you can effortlessly create and manage autoscaled instance groups, allowing your applications to adapt to changing demands while maintaining efficiency and cost-effectiveness.

  • Launching a Compute Instance Using the Google Cloud Console and Cloud SDK (gcloud)

    Google Cloud Platform (GCP) offers two primary methods for launching Compute Engine virtual machines (VMs): the Google Cloud Console (web interface) and the Cloud SDK (gcloud command-line tool). This guide demonstrates a hybrid approach, leveraging both tools for streamlined and customizable instance deployment.

    Prerequisites

    1. Active GCP Project: Ensure you have an active Google Cloud Platform project.
    2. SSH Key Pair:
      • If needed, generate an SSH key pair on your local machine using ssh-keygen.
      • Add the public key to your project’s metadata:
        • In the Cloud Console, navigate to Compute Engine > Metadata > SSH Keys.
        • Click “Edit,” then “Add Item,” and paste your public key.
    3. Firewall Rule: Configure a firewall rule permitting ingress SSH traffic (port 22) from your authorized IP address(es).

    Step 1: Initial Configuration (Google Cloud Console)

    1. Open the Cloud Console and navigate to Compute Engine > VM instances.

    2. Click Create Instance.

    3. Provide the following details:

      • Name: A descriptive name for your instance.
      • Region/Zone: The desired geographical location for your instance.
      • Machine Type: Select the appropriate vCPU and memory configuration for your workload.
      • Boot Disk:
        • Image: Choose your preferred operating system (e.g., Ubuntu, Debian).
        • Boot disk type: Typically, “Standard Persistent Disk (pd-standard)” is suitable.
        • Size: Specify the desired storage capacity.
      • Firewall: Enable “Allow HTTP traffic” and “Allow HTTPS traffic” if required.
      • Networking: Adjust network settings if you have specific requirements.
      • Advanced Options (Optional):
        • Preemptibility: If cost optimization is a priority, consider preemptible instances.
        • Availability Policy: For high availability, configure a regional policy.
    4. Click “Create” to initiate instance creation.

    Step 2: Advanced Configuration (Cloud SDK)

    1. Authenticate: Ensure you are authenticated with your GCP project:

      gcloud auth login
      gcloud config set project your-project-id 
      
    2. Create Instance: Execute the following gcloud command, replacing placeholders with your specific values:

      gcloud compute instances create instance-name \
          --zone=your-zone \
          --machine-type=machine-type \
          --image=image-name \
          --image-project=image-project \
          --boot-disk-size=disk-sizeGB \
          --boot-disk-type=pd-balanced \
          --metadata-from-file=startup-script=gs://your-bucket/startup.sh \
          --tags=http-server,https-server \
          --maintenance-policy=maintenance-policy \ 
          --preemptible  # (Optional) 
      
    3. Additional Disks (Optional): To attach additional disks, use:

      gcloud compute instances attach-disk instance-name \
         --disk=disk-name \
         --zone=your-zone
      

    Step 3: Connect via SSH:

    gcloud compute ssh instance-name --zone=your-zone