Kahibaro
Discord Login Register

6.5.3 Linux on GCP

Understanding Linux on Google Cloud Platform

Running Linux on Google Cloud Platform (GCP) largely means working with Linux-based virtual machines and managed services that themselves run on Linux behind the scenes. This chapter focuses on how Linux is used specifically in GCP, and what is different compared to other clouds.

GCP’s Linux Building Blocks

Google Compute Engine (GCE) Linux VMs

The core place you explicitly run Linux on GCP is Google Compute Engine:

Linux on GCE is typically accessed via SSH, often using your local terminal or the browser-based SSH client in the console.

Linux in GCP Managed Services

Many managed services on GCP run on Linux under the hood, even if you do not manage the OS directly:

This chapter focuses on when you do manage Linux directly (mainly Compute Engine), plus some GCP-specific tools and patterns around it.

Creating and Managing Linux VMs on GCE

Choosing a Linux Image

When creating a VM:

Each image comes with GCP agents and basic tools for integration (metadata, logging, etc.) in most official images.

Basic VM Creation with `gcloud`

From your local shell or Cloud Shell:

gcloud compute instances create my-linux-vm \
  --zone=us-central1-a \
  --machine-type=e2-micro \
  --image-family=debian-12 \
  --image-project=debian-cloud \
  --boot-disk-size=20GB

Key Linux-specific considerations:

Connecting via SSH

GCP strongly encourages key-based SSH. Typical methods:

  gcloud compute ssh my-linux-vm --zone=us-central1-a
  ssh USER@EXTERNAL_IP

GCP manages SSH keys via instance and project metadata; when you connect via gcloud compute ssh, it will:

  1. Generate a key pair (if you don’t have one).
  2. Add your public key to the instance/project metadata.
  3. Use it to authenticate.

On the Linux VM, these keys show up in the user’s ~/.ssh/authorized_keys.

Using Startup Scripts (Linux Metadata)

Compute Engine can run custom scripts on Linux during boot using metadata:

  # Simple Debian/Ubuntu example
  #! /bin/bash
  apt-get update -y
  apt-get install -y nginx
  systemctl enable nginx
  systemctl start nginx

Attach via:

gcloud compute instances create web-vm \
  --metadata-from-file startup-script=./startup.sh

This is a lightweight way to bootstrap Linux instances (install packages, configure services) without full-blown configuration management.

GCP-Specific Linux Agents and Tools

Guest Environment and Metadata Access

Linux VMs on GCP often include the Google guest environment, providing:

Example: Query hostname from inside Linux:

curl -H "Metadata-Flavor: Google" \
  http://169.254.169.254/computeMetadata/v1/instance/hostname

Typical metadata uses:

Logging and Monitoring Agents

To send Linux logs and metrics to GCP:

Install example (Debian/Ubuntu):

curl -sSO https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh
sudo bash add-google-cloud-ops-agent-repo.sh --also-install

Then configure log/metric pipelines using config files under /etc/google-cloud-ops-agent/config.yaml.

This effectively connects your Linux system internals to Cloud Logging and Cloud Monitoring.

Networking and Firewalls for Linux VMs

VPC and Internal Addresses

Each Linux VM gets:

From inside Linux, usual networking commands (ip, ss, ping) behave as on any other system, but the network topology is controlled by VPC settings.

Google Cloud Firewalls vs Linux Firewalls

On GCP, there are two layers:

  1. VPC firewall rules:
    • Managed at the project/VPC level.
    • Applied based on tags, network, and IP ranges.
    • Example: allow TCP 22 (SSH) from your IP only.
  2. Linux host firewall (e.g., iptables, nftables, ufw, firewalld):
    • Runs inside the VM.
    • Controls traffic after it reaches the VM.

Common patterns:

External Access and SSH Security

For Linux on GCP, consider:

IAP means your Linux VM may only have an internal IP; SSH flow goes through GCP and IAM-controlled access, not open internet.

Storage on Linux VMs in GCP

Persistent Disks

Linux VMs typically use Persistent Disks (PD) as block devices:

Example: Attaching and mounting an extra disk on Linux:

  1. Create and attach disk (gcloud or console).
  2. On the VM:
   lsblk            # find the new device, e.g., /dev/sdb
   sudo mkfs.ext4 /dev/sdb
   sudo mkdir /data
   sudo mount /dev/sdb /data
  1. For automatic mounting, add to /etc/fstab using the disk’s UUID:
   UUID=xxxx-xxxx  /data  ext4  defaults  0 2

Snapshots and Images from Linux Disks

GCP can snapshot disks backing a Linux VM:

Disk snapshots and images are managed from GCP, but the consistency and application state are controlled from within the Linux OS.

Using Containers and Kubernetes (Linux-Focused) on GCP

While there are separate chapters for containers and Kubernetes, here is how Linux plays into GCP’s container offerings.

Linux Container Hosts on GCP

Use Container-Optimized OS (COS) or Ubuntu as minimal, hardened Linux for:

Typical pattern:

GKE Node OS

GKE node pools use Linux images such as:

You do not usually manage these nodes like ordinary Linux servers, but:

Identity and Access Management for Linux on GCP

IAM vs Linux Users

There are two identity layers:

  1. GCP IAM:
    • Controls who can create/modify/delete VMs, connect with IAP, etc.
    • Users are Google accounts or service accounts.
  2. Linux user accounts:
    • Defined in /etc/passwd, /etc/group.
    • Control what a user can do once logged into the VM (sudo, file ownership, etc.).

Typical workflow:

OS Login Integration

OS Login lets IAM control Linux login access:

From an admin perspective:

On the Linux VM, sshd uses PAM modules to integrate with OS Login so that IAM decisions determine whether a user can log in and what groups/privileges they get.

Automating Linux Workloads on GCP

Using `gcloud` and Cloud Shell

For automation, you often:

Example script snippet:

#!/usr/bin/env bash
set -e
PROJECT_ID=my-gcp-project
ZONE=us-central1-a
NAME=web-$(date +%s)
gcloud config set project "$PROJECT_ID"
gcloud compute instances create "$NAME" \
  --zone="$ZONE" \
  --machine-type=e2-small \
  --image-family=debian-12 \
  --image-project=debian-cloud \
  --metadata-from-file startup-script=./web-startup.sh

Linux scripting skills directly translate to orchestrating GCP resources.

Service Accounts from Linux

For Linux-based applications that need to call GCP APIs:

  TOKEN=$(curl -H "Metadata-Flavor: Google" \
    "http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token" \
    | jq -r .access_token)
curl -H "Authorization: Bearer $TOKEN" \
  "https://www.googleapis.com/storage/v1/b"

Or use Google Cloud client libraries on Linux, which automatically fetch tokens when running on a VM with a service account.

Best Practices for Linux on GCP

By understanding how Linux integrates with Compute Engine, IAM, networking, and storage on GCP, you can design systems that use familiar Linux tools while benefiting from GCP’s automation, scalability, and managed services.

Views: 129

Comments

Please login to add a comment.

Don't have an account? Register now!