mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
hanzoskills.com/llms.txt now opens with the install line, the API reference index and the company index, so an agent that lands on any one of the three can find the rest. robots.txt claimed 537 documents while llms.txt counts 920. The number is gone from robots.txt; llms.txt derives it. public/ is regenerated from scripts/site.py, which it had fallen behind: the copy-prompt menu and agent bar were in the generator and not in the committed pages. tests/site.mjs passes against the result. Co-authored-by: Hanzo Dev <[email protected]>
565 lines
29 KiB
HTML
565 lines
29 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>cloud-gcp-compute</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="alternate" type="text/markdown" href="/cloud-gcp-compute.md">
|
|
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/cloud-gcp-compute">
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/cloud-gcp-compute.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>cloud-gcp-compute</h1><p class="lede">Google Cloud compute services including Compute Engine, Cloud Run, and GKE</p><h1>GCP Compute Services</h1>
|
|
<p><strong>Scope</strong>: Compute Engine VMs, Cloud Run serverless containers, and GKE basics <strong>Lines</strong>: ~350 <strong>Last Updated</strong>: 2025-10-25 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<hr>
|
|
<h2>When to Use This Skill</h2>
|
|
<p>Activate this skill when:</p>
|
|
<ul><li>Deploying virtual machines on Google Cloud Platform</li><li>Running containerized applications with Cloud Run</li><li>Setting up Kubernetes clusters with GKE</li><li>Optimizing compute costs with preemptible VMs or committed use discounts</li><li>Configuring autoscaling for instance groups</li><li>Choosing between serverless and VM-based workloads</li><li>Migrating workloads from on-premises to GCP compute</li><li>Managing SSH access and OS Login for instances</li></ul>
|
|
<h2>Core Concepts</h2>
|
|
<h3>Concept 1: Compute Engine Instance Types</h3>
|
|
<p><strong>Machine families</strong>:</p>
|
|
<ul><li><strong>General-purpose</strong> (E2, N2, N2D, N1): Balanced CPU/memory for most workloads</li><li><strong>Compute-optimized</strong> (C2, C2D): High CPU performance for compute-intensive tasks</li><li><strong>Memory-optimized</strong> (M2, M1): High memory for in-memory databases and analytics</li><li><strong>Accelerator-optimized</strong> (A2): GPU workloads for ML and HPC</li></ul>
|
|
<pre><code class="lang-bash"># Create a general-purpose VM with sustained use discount benefits
|
|
gcloud compute instances create web-server \
|
|
--machine-type=n2-standard-4 \
|
|
--zone=us-central1-a \
|
|
--image-family=debian-11 \
|
|
--image-project=debian-cloud \
|
|
--boot-disk-size=20GB \
|
|
--boot-disk-type=pd-balanced
|
|
|
|
# Create a preemptible VM for cost savings (up to 80% cheaper)
|
|
gcloud compute instances create batch-processor \
|
|
--machine-type=e2-standard-8 \
|
|
--zone=us-central1-a \
|
|
--preemptible \
|
|
--image-family=ubuntu-2004-lts \
|
|
--image-project=ubuntu-os-cloud</code></pre>
|
|
<h3>Concept 2: Cloud Run Serverless Containers</h3>
|
|
<p><strong>Key features</strong>:</p>
|
|
<ul><li>Fully managed serverless platform for stateless containers</li><li>Automatic scaling from 0 to N instances based on traffic</li><li>Pay only for actual request processing time (100ms granularity)</li><li>Built-in traffic splitting for gradual rollouts</li></ul>
|
|
<pre><code class="lang-python"># Deploy a container to Cloud Run using Python client
|
|
from google.cloud import run_v2
|
|
|
|
def deploy_cloud_run_service(project_id, service_name, image_url, region="us-central1"):
|
|
client = run_v2.ServicesClient()
|
|
|
|
service = run_v2.Service(
|
|
name=f"projects/{project_id}/locations/{region}/services/{service_name}",
|
|
template=run_v2.RevisionTemplate(
|
|
containers=[run_v2.Container(
|
|
image=image_url,
|
|
resources=run_v2.ResourceRequirements(
|
|
limits={"cpu": "1", "memory": "512Mi"}
|
|
),
|
|
ports=[run_v2.ContainerPort(container_port=8080)]
|
|
)],
|
|
scaling=run_v2.RevisionScaling(
|
|
min_instance_count=0,
|
|
max_instance_count=100
|
|
)
|
|
)
|
|
)
|
|
|
|
request = run_v2.CreateServiceRequest(
|
|
parent=f"projects/{project_id}/locations/{region}",
|
|
service=service,
|
|
service_id=service_name
|
|
)
|
|
|
|
operation = client.create_service(request=request)
|
|
return operation.result()</code></pre>
|
|
<h3>Concept 3: GKE Cluster Modes</h3>
|
|
<p><strong>Autopilot vs Standard</strong>:</p>
|
|
<ul><li><strong>Autopilot</strong>: Fully managed, GKE manages nodes, optimizes resources, enforces best practices</li><li><strong>Standard</strong>: User manages node pools, custom machine types, full control over configuration</li></ul>
|
|
<pre><code class="lang-bash"># Create an Autopilot cluster (recommended for most use cases)
|
|
gcloud container clusters create-auto prod-cluster \
|
|
--region=us-central1 \
|
|
--release-channel=regular
|
|
|
|
# Create a Standard cluster with custom node pool
|
|
gcloud container clusters create custom-cluster \
|
|
--zone=us-central1-a \
|
|
--num-nodes=3 \
|
|
--machine-type=n2-standard-4 \
|
|
--enable-autoscaling \
|
|
--min-nodes=1 \
|
|
--max-nodes=10</code></pre>
|
|
<hr>
|
|
<h2>Patterns</h2>
|
|
<h3>Pattern 1: Instance Templates and Managed Instance Groups</h3>
|
|
<p><strong>When to use</strong>:</p>
|
|
<ul><li>Need to create multiple identical VMs</li><li>Require autoscaling based on load</li><li>Want to distribute instances across zones for high availability</li></ul>
|
|
<pre><code class="lang-bash"># ❌ Bad: Creating VMs one at a time manually
|
|
gcloud compute instances create vm-1 --machine-type=n2-standard-2 --zone=us-central1-a
|
|
gcloud compute instances create vm-2 --machine-type=n2-standard-2 --zone=us-central1-a
|
|
# ... error-prone and not scalable
|
|
|
|
# ✅ Good: Use instance template and managed instance group
|
|
# Create instance template
|
|
gcloud compute instance-templates create web-template \
|
|
--machine-type=n2-standard-2 \
|
|
--image-family=debian-11 \
|
|
--image-project=debian-cloud \
|
|
--metadata=startup-script='#!/bin/bash
|
|
apt-get update
|
|
apt-get install -y nginx
|
|
systemctl start nginx'
|
|
|
|
# Create managed instance group with autoscaling
|
|
gcloud compute instance-groups managed create web-mig \
|
|
--base-instance-name=web \
|
|
--template=web-template \
|
|
--size=3 \
|
|
--zone=us-central1-a
|
|
|
|
gcloud compute instance-groups managed set-autoscaling web-mig \
|
|
--zone=us-central1-a \
|
|
--max-num-replicas=10 \
|
|
--min-num-replicas=2 \
|
|
--target-cpu-utilization=0.6</code></pre>
|
|
<p><strong>Benefits</strong>:</p>
|
|
<ul><li>Consistent VM configuration across all instances</li><li>Automatic healing replaces unhealthy instances</li><li>Seamless autoscaling based on metrics</li></ul>
|
|
<h3>Pattern 2: Cloud Run Traffic Splitting</h3>
|
|
<p><strong>Use case</strong>: Gradual rollout of new application versions with instant rollback capability</p>
|
|
<pre><code class="lang-bash"># Deploy revision 1
|
|
gcloud run deploy api-service \
|
|
--image=gcr.io/project/api:v1 \
|
|
--region=us-central1 \
|
|
--tag=v1
|
|
|
|
# Deploy revision 2
|
|
gcloud run deploy api-service \
|
|
--image=gcr.io/project/api:v2 \
|
|
--region=us-central1 \
|
|
--tag=v2 \
|
|
--no-traffic # Don't send traffic to new revision yet
|
|
|
|
# Split traffic: 90% to v1, 10% to v2 (canary deployment)
|
|
gcloud run services update-traffic api-service \
|
|
--region=us-central1 \
|
|
--to-revisions=v1=90,v2=10
|
|
|
|
# After validation, shift all traffic to v2
|
|
gcloud run services update-traffic api-service \
|
|
--region=us-central1 \
|
|
--to-latest</code></pre>
|
|
<h3>Pattern 3: Preemptible VM with Shutdown Script</h3>
|
|
<p><strong>Use case</strong>: Run batch jobs on low-cost preemptible VMs with graceful shutdown</p>
|
|
<pre><code class="lang-python">from google.cloud import compute_v1
|
|
|
|
def create_preemptible_instance(project_id, zone, instance_name):
|
|
client = compute_v1.InstancesClient()
|
|
|
|
# Shutdown script to save state before preemption
|
|
shutdown_script = """#!/bin/bash
|
|
echo "Instance preempted, saving state..."
|
|
gsutil cp /tmp/job_state.json gs://my-bucket/checkpoints/
|
|
"""
|
|
|
|
instance = compute_v1.Instance(
|
|
name=instance_name,
|
|
machine_type=f"zones/{zone}/machineTypes/n2-standard-4",
|
|
scheduling=compute_v1.Scheduling(
|
|
preemptible=True,
|
|
automatic_restart=False,
|
|
on_host_maintenance="TERMINATE"
|
|
),
|
|
disks=[
|
|
compute_v1.AttachedDisk(
|
|
auto_delete=True,
|
|
boot=True,
|
|
initialize_params=compute_v1.AttachedDiskInitializeParams(
|
|
source_image="projects/debian-cloud/global/images/family/debian-11"
|
|
)
|
|
)
|
|
],
|
|
metadata=compute_v1.Metadata(
|
|
items=[
|
|
compute_v1.Items(key="shutdown-script", value=shutdown_script)
|
|
]
|
|
),
|
|
network_interfaces=[
|
|
compute_v1.NetworkInterface(
|
|
network="global/networks/default",
|
|
access_configs=[
|
|
compute_v1.AccessConfig(name="External NAT", type_="ONE_TO_ONE_NAT")
|
|
]
|
|
)
|
|
]
|
|
)
|
|
|
|
operation = client.insert(project=project_id, zone=zone, instance_resource=instance)
|
|
return operation</code></pre>
|
|
<h3>Pattern 4: Regional Managed Instance Groups</h3>
|
|
<p><strong>Use case</strong>: High availability across multiple zones in a region</p>
|
|
<pre><code class="lang-bash"># Create regional MIG for automatic zone distribution
|
|
gcloud compute instance-groups managed create web-regional-mig \
|
|
--base-instance-name=web \
|
|
--template=web-template \
|
|
--size=6 \
|
|
--region=us-central1 \
|
|
--target-distribution-shape=EVEN
|
|
|
|
# GCP automatically distributes instances:
|
|
# 2 in us-central1-a, 2 in us-central1-b, 2 in us-central1-c</code></pre>
|
|
<h3>Pattern 5: Cloud Run with Secrets and Environment Variables</h3>
|
|
<p><strong>Use case</strong>: Securely inject configuration and secrets into Cloud Run services</p>
|
|
<pre><code class="lang-bash"># Create secret in Secret Manager
|
|
echo -n "my-database-password" | gcloud secrets create db-password \
|
|
--data-file=- \
|
|
--replication-policy=automatic
|
|
|
|
# Deploy Cloud Run with secret and environment variable
|
|
gcloud run deploy api-service \
|
|
--image=gcr.io/project/api:latest \
|
|
--region=us-central1 \
|
|
--set-env-vars=DATABASE_URL=postgresql://host/db \
|
|
--set-secrets=DB_PASSWORD=db-password:latest</code></pre>
|
|
<h3>Pattern 6: OS Login for Centralized SSH Access</h3>
|
|
<p><strong>Use case</strong>: Manage SSH access using IAM instead of managing individual SSH keys</p>
|
|
<pre><code class="lang-bash"># Enable OS Login on a project
|
|
gcloud compute project-info add-metadata \
|
|
--metadata enable-oslogin=TRUE
|
|
|
|
# Grant user SSH access via IAM
|
|
gcloud projects add-iam-policy-binding PROJECT_ID \
|
|
--member=user:[email protected] \
|
|
--role=roles/compute.osLogin
|
|
|
|
# SSH using OS Login (no SSH keys needed)
|
|
gcloud compute ssh instance-name --zone=us-central1-a
|
|
|
|
# For sudo access, grant osAdminLogin role
|
|
gcloud projects add-iam-policy-binding PROJECT_ID \
|
|
--member=user:[email protected] \
|
|
--role=roles/compute.osAdminLogin</code></pre>
|
|
<h3>Pattern 7: Committed Use Discounts</h3>
|
|
<p><strong>Use case</strong>: Save up to 57% for predictable workloads with 1 or 3 year commitments</p>
|
|
<pre><code class="lang-bash"># Purchase committed use contract for compute resources
|
|
gcloud compute commitments create web-commitment \
|
|
--region=us-central1 \
|
|
--plan=12-month \
|
|
--resources=vcpu=100,memory=400GB
|
|
|
|
# View active commitments
|
|
gcloud compute commitments list
|
|
|
|
# Instances automatically benefit from commitment pricing
|
|
# when they match committed resource types</code></pre>
|
|
<h3>Pattern 8: GKE Workload Identity</h3>
|
|
<p><strong>Use case</strong>: Allow Kubernetes pods to authenticate as GCP service accounts</p>
|
|
<pre><code class="lang-bash"># Create GKE cluster with Workload Identity enabled
|
|
gcloud container clusters create prod-cluster \
|
|
--workload-pool=PROJECT_ID.svc.id.goog \
|
|
--zone=us-central1-a
|
|
|
|
# Create Kubernetes service account
|
|
kubectl create serviceaccount app-sa
|
|
|
|
# Create GCP service account
|
|
gcloud iam service-accounts create app-gsa
|
|
|
|
# Bind Kubernetes SA to GCP SA
|
|
gcloud iam service-accounts add-iam-policy-binding app-gsa@PROJECT_ID.iam.gserviceaccount.com \
|
|
--role=roles/iam.workloadIdentityUser \
|
|
--member="serviceAccount:PROJECT_ID.svc.id.goog[default/app-sa]"
|
|
|
|
# Annotate Kubernetes service account
|
|
kubectl annotate serviceaccount app-sa \
|
|
iam.gke.io/gcp-service-account=app-gsa@PROJECT_ID.iam.gserviceaccount.com</code></pre>
|
|
<hr>
|
|
<h2>Quick Reference</h2>
|
|
<h3>Compute Service Comparison</h3>
|
|
<pre><code>Service | Use Case | Scaling | Cost Model
|
|
----------------|----------------------------|------------------|------------------
|
|
Compute Engine | VMs, custom OS, full ctrl | Manual/Auto MIG | Per second
|
|
Cloud Run | Stateless containers | Auto 0 to N | Per 100ms request
|
|
GKE Autopilot | Kubernetes, managed nodes | Auto pod scaling | Per pod resources
|
|
GKE Standard | Kubernetes, custom config | Manual/Auto | Per node hour</code></pre>
|
|
<h3>Key gcloud Commands</h3>
|
|
<pre><code class="lang-bash"># Compute Engine
|
|
gcloud compute instances create NAME --machine-type=TYPE --zone=ZONE
|
|
gcloud compute instances list
|
|
gcloud compute instances stop/start/delete NAME --zone=ZONE
|
|
gcloud compute ssh INSTANCE --zone=ZONE
|
|
|
|
# Cloud Run
|
|
gcloud run deploy SERVICE --image=IMAGE --region=REGION
|
|
gcloud run services list
|
|
gcloud run services delete SERVICE --region=REGION
|
|
|
|
# GKE
|
|
gcloud container clusters create CLUSTER --zone=ZONE
|
|
gcloud container clusters get-credentials CLUSTER --zone=ZONE
|
|
gcloud container clusters delete CLUSTER --zone=ZONE</code></pre>
|
|
<h3>Key Guidelines</h3>
|
|
<pre><code>✅ DO: Use preemptible VMs for fault-tolerant batch workloads (up to 80% savings)
|
|
✅ DO: Enable OS Login for centralized SSH access management
|
|
✅ DO: Use regional MIGs for high availability across zones
|
|
✅ DO: Tag instances with network tags for firewall rules
|
|
✅ DO: Use startup scripts for automated instance configuration
|
|
✅ DO: Monitor sustained use discounts and consider committed use for stable workloads
|
|
|
|
❌ DON'T: Use preemptible VMs for stateful services without checkpointing
|
|
❌ DON'T: Create instances without considering machine type right-sizing
|
|
❌ DON'T: Leave unused instances running (set up alerts)
|
|
❌ DON'T: Use Cloud Run for long-running stateful processes
|
|
❌ DON'T: Ignore zone selection (affects latency and cost)</code></pre>
|
|
<hr>
|
|
<h2>Anti-Patterns</h2>
|
|
<h3>Critical Violations</h3>
|
|
<pre><code class="lang-bash"># ❌ NEVER: Run stateful databases on preemptible VMs without proper HA setup
|
|
gcloud compute instances create db-primary \
|
|
--preemptible # Database will be terminated within 24 hours!
|
|
|
|
# ✅ CORRECT: Use standard instances with regional persistent disks
|
|
gcloud compute instances create db-primary \
|
|
--machine-type=n2-standard-8 \
|
|
--zone=us-central1-a \
|
|
--create-disk=size=500GB,type=pd-ssd,replica-zones=us-central1-b</code></pre>
|
|
<p>❌ <strong>Preemptible databases</strong>: Preemptible VMs are terminated within 24 hours or on-demand. Running stateful services without checkpointing causes data loss. ✅ <strong>Correct approach</strong>: Use standard instances with regional persistent disks for automatic replication.</p>
|
|
<h3>Common Mistakes</h3>
|
|
<pre><code class="lang-bash"># ❌ Don't: Use oversized machine types "just in case"
|
|
gcloud compute instances create web-server \
|
|
--machine-type=n2-standard-32 # 32 vCPUs for a low-traffic web app!
|
|
|
|
# ✅ Correct: Right-size and use autoscaling
|
|
gcloud compute instances create web-server \
|
|
--machine-type=e2-medium # Start small, monitor, adjust</code></pre>
|
|
<p>❌ <strong>Over-provisioning</strong>: Using oversized machine types wastes money and doesn't improve performance for most workloads. ✅ <strong>Better</strong>: Start with smaller machine types, monitor metrics, and scale up if needed.</p>
|
|
<pre><code class="lang-python"># ❌ Don't: Deploy Cloud Run without concurrency limits
|
|
# Default concurrency of 80 may overwhelm downstream services
|
|
|
|
# ✅ Correct: Set appropriate concurrency based on backend capacity
|
|
from google.cloud import run_v2
|
|
|
|
service = run_v2.Service(
|
|
template=run_v2.RevisionTemplate(
|
|
containers=[run_v2.Container(
|
|
image="gcr.io/project/api:latest"
|
|
)],
|
|
max_instance_request_concurrency=10 # Limit concurrent requests per instance
|
|
)
|
|
)</code></pre>
|
|
<p>❌ <strong>Unbounded concurrency</strong>: Default Cloud Run concurrency can overwhelm databases or external APIs. ✅ <strong>Better</strong>: Set max concurrency based on backend capacity (e.g., database connection pool size).</p>
|
|
<pre><code class="lang-bash"># ❌ Don't: Ignore sustained use discounts when evaluating costs
|
|
# Manually calculating costs without considering automatic discounts
|
|
|
|
# ✅ Correct: Use Pricing Calculator and understand automatic discounts
|
|
# Sustained use: 20-30% automatic discount for running >25% of month
|
|
# Committed use: 57% discount for 3-year commitment
|
|
# Preemptible: 80% discount for interruptible workloads</code></pre>
|
|
<p>❌ <strong>Ignoring discounts</strong>: Not accounting for sustained use discounts leads to inaccurate cost projections. ✅ <strong>Better</strong>: Use GCP Pricing Calculator and factor in automatic discounts for long-running workloads.</p>
|
|
<hr>
|
|
<h2>Related Skills</h2>
|
|
<ul><li><code>gcp-storage.md</code> - Persistent disks and Cloud Storage for VM data persistence</li><li><code>gcp-networking.md</code> - VPC configuration and load balancing for compute instances</li><li><code>gcp-iam-security.md</code> - Service accounts and IAM roles for compute resources</li><li><code>gcp-serverless.md</code> - Cloud Functions and App Engine as compute alternatives</li><li><code>gcp-databases.md</code> - Managed databases that integrate with compute services</li></ul>
|
|
<hr>
|
|
<p><strong>Last Updated</strong>: 2025-10-25 <strong>Format Version</strong>: 1.0 (Atomic)</p>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/cloud-gcp-compute.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |