במדריך הזה מוסבר איך לרשום את כתובת ה-IP הפרטית של מופע של מאגר עובדים ב-Cloud Run באזור מנוהל ב-Cloud DNS. במדריך הזה משתמשים בסקריפט לטעינה בזמן ההפעלה כדי לזהות באופן דינמי את כתובת ה-IP של המכונה דרך שרת המטא-נתונים, ויוצרים אפליקציית Node.js לטיפול בתעבורת כניסה ישירות ברשת ה-VPC.
מטרות
- יוצרים תחום מנוהל ב-Cloud DNS כדי לארח את הרשומות של מאגר העובדים.
- יוצרים אפליקציית Node.js לטיפול בתעבורת נתונים נכנסת (ingress) ברשת VPC.
- פריסת מאגר עובדים עם Direct VPC ingress כדי להקצות כתובת IP פרטית מרשת המשנה ל-worker instance.
- בודקים את האפליקציה כדי לגשת לכתובת ה-IP הפרטית ממופע של מכונה וירטואלית (VM) באותה רשת VPC.
עלויות
במסמך הזה משתמשים ברכיבים הבאים של Cloud de Confiance by S3NS, והשימוש בהם כרוך בתשלום:
לפני שמתחילים
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Cloud de Confiance project.
מפעילים את Cloud DNS API, Cloud Run API, Compute Engine API, Artifact Registry API ו-Cloud Build API, אם אחד מהם עדיין לא מופעל.
תפקידים שנדרשים להפעלת ממשקי API
כדי להפעיל ממשקי API, נדרשת ההרשאה
serviceusage.services.enable. אם יצרתם את הפרויקט, סביר להניח שכבר יש לכם את ההרשאה הזו דרך התפקיד 'בעלים' (roles/owner). אחרת, תוכלו לקבל את ההרשאה הזו דרך התפקיד 'אדמין בממשק Service Usage' (roles/serviceusage.serviceUsageAdmin). איך מקצים תפקידים- מתקינים ומפעילים את ה-CLI של gcloud.
- מעדכנים את הרכיבים:
gcloud components update
- מגדירים את מזהה הפרויקט באמצעות הפקודה הבאה:
מחליפים את PROJECT_ID במזהה הפרויקט. Cloud de Confiance by S3NSgcloud config set project PROJECT_ID
התפקידים הנדרשים
כדי לקבל את ההרשאות שדרושות להשלמת המדריך, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים בפרויקט:
- אדמין של Artifact Registry Repository (
roles/artifactregistry.repoAdmin) - עריכה ב-Cloud Build (
roles/cloudbuild.builds.editor) - אדמין ב-Cloud Run (
roles/run.admin) - יצירת חשבונות שירות (
roles/iam.serviceAccountCreator) - אדמין של Cloud DNS (
roles/dns.admin) - משתמש בחשבון שירות (
roles/iam.serviceAccountUser) - צרכן שימוש בשירות (
roles/serviceusage.serviceUsageConsumer)
להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.
יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.
יצירת חשבון שירות בהתאמה אישית
יוצרים חשבון שירות בהתאמה אישית עם ההרשאות המינימליות שנדרשות לרישום רשומות DNS. כדי להגדיר את חשבון השירות:
כדי ליצור חשבון שירות, מריצים את הפקודה הבאה:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME \ --display-name="DNS Worker Pool Service Account"
מחליפים את SERVICE_ACCOUNT_NAME בשם שרוצים לתת לחשבון השירות המותאם אישית, לדוגמה
dns-worker-sa.מקצים לחשבון השירות את התפקיד Cloud DNS Admin:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.s3ns.iam.gserviceaccount.com" \ --role="roles/dns.admin"
יצירת תחום DNS ב-Cloud DNS
יוצרים תחום מנוהל פרטי ב-Cloud DNS בשם test-wp כדי לרשום את כתובת ה-IP של המופע של מאגר העובדים.
gcloud dns managed-zones create test-wp --description="new DNS zone for worker pools" --dns-name="workerpools.example.com." --visibility="private" --networks=default
יצירת אפליקציה ב-Node.js
יוצרים אפליקציית Node.js שמחלצת את כתובת ה-IP של מופע מאגר העובדים באמצעות שרת המטא-נתונים, ואז רושמת אותה ב-Cloud DNS.
יוצרים תיקייה בשם
workerועוברים אליה:mkdir worker cd workerיוצרים קובץ בשם
setup_dns.shומוסיפים את הסקריפט הבא שמופעל כשמופעלת מכונה במאגר העובדים. הסקריפט הזה משתמש בשם המארח של המופע כדי ליצור את השם של רשומת ה-DNS. אם יש יותר ממופע אחד, כמה מופעים יכולים להשתמש באותו שם מארח, מה שמוביל ליצירה של רשומות DNS חופפות.#!/bin/bash # --- Variables --- # The name of your Cloud DNS managed zone. ZONE_NAME="test-wp" # The base domain suffix for the DNS entry. Use the part *after* the hostname. # For example, for "testinstance.workerpools.example.com.", the suffix is ".workerpools.example.com." DNS_SUFFIX=".workerpools.example.com." # The Time-To-Live (TTL) in seconds. TTL="300" # The record type (A for IPv4). RECORD_TYPE="A" # ----------------- # 1. Dynamically generate DNS_NAME # Get the simple hostname (e.g., "testinstance") and append the defined suffix. # We use 'hostname -s' to get the short hostname. SHORT_HOSTNAME=$(hostname -s) DNS_NAME="${SHORT_HOSTNAME}${DNS_SUFFIX}" # 2. Dynamically assign NEW_IP # Get the IP address from metadata server using predefined key. NEW_IP=$(curl "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip" -H "Metadata-Flavor: Google") # --- Input Validation --- if [ -z "$NEW_IP" ]; then echo "❌ ERROR: Could not obtain a valid IP address from metadata server. Aborting." exit 1 fi echo "Starting DNS record update for ${DNS_NAME} in zone ${ZONE_NAME}..." echo "New IP detected on this instance: ${NEW_IP}" # 3. Get the current existing IP address (if any) # This is required to know what to put in the --rrdatas for the delete command. EXISTING_IP=$(gcloud dns record-sets list \ --zone="${ZONE_NAME}" \ --name="${DNS_NAME}" \ --type="${RECORD_TYPE}" \ --format="value(rrdatas[0])" 2>/dev/null) # --- Conditional Deletion/Skip Check --- if [ -n "$EXISTING_IP" ] && [ "$EXISTING_IP" != "$NEW_IP" ]; then echo "Found existing IP: ${EXISTING_IP}. It is different from the new IP." # Delete the existing record echo "Deleting old record..." gcloud dns record-sets delete "${DNS_NAME}" \ --zone="${ZONE_NAME}" \ --type="${RECORD_TYPE}" \ --quiet if [ $? -ne 0 ]; then echo "❌ ERROR: Failed to delete existing record. Aborting." exit 1 fi elif [ -n "$EXISTING_IP" ] && [ "$EXISTING_IP" == "$NEW_IP" ]; then echo "Existing IP (${EXISTING_IP}) matches the new IP. Skipping update." exit 0 else echo "No existing record found for ${DNS_NAME}. Proceeding with creation." fi # ---------------------------------------- # 4. Add the new record echo "Creating new record with IP: ${NEW_IP}..." gcloud dns record-sets create "${DNS_NAME}" \ --zone="${ZONE_NAME}" \ --type="${RECORD_TYPE}" \ --ttl="${TTL}" \ --rrdatas="${NEW_IP}" # Final status check if [ $? -eq 0 ]; then echo "✅ Successfully created/updated DNS record for ${DNS_NAME} to ${NEW_IP}." else echo "❌ ERROR: Failed to create the new DNS record." exit 1 fiכדי לטפל בתעבורת נתונים נכנסת מכתובת IP פרטית, יוצרים קובץ
server.jsעם הקוד הבא:// server.js // 1. Import the built-in 'http' module console.log("hello from worker pool") const http = require('http'); // Define the port the server will listen on const PORT = 3000; // 2. Create the server instance const server = http.createServer((req, res) => { // Set the response HTTP header with status and type of content res.writeHead(200, {'Content-Type': 'text/plain'}); // Write the response body res.end('Hello World!\n'); }); // 3. Start the server and listen on the specified port server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); });כדי לבצע את הגדרת ה-DNS לפני שמריצים את אפליקציית Node.js, יוצרים סקריפט
start.shעם הקוד הבא:#!/bin/bash set -e # 1. Execute the setup script echo "Running setup_dns.sh..." /app/setup_dns.sh # 2. Run the main application echo "Starting server.js..." exec node /app/server.jsיוצרים את קובץ ה-Dockerfile הבא כדי לארוז את האפליקציה בקובץ אימג':
# Choose a base image. This image includes gcloud, kubectl, etc. FROM google/cloud-sdk:latest # Install Node.js on top of the Cloud SDK image. RUN apt-get update && \ apt-get install -y curl && \ curl -sL https://deb.nodesource.com/setup_lts.x | bash - && \ apt-get install -y nodejs && \ # Clean up to reduce image size apt-get clean && \ rm -rf /var/lib/apt/lists/* # Set the working directory inside the container. WORKDIR /app # Copy the application and scripts into the container. COPY setup_dns.sh . COPY server.js . COPY start.sh . # Make both scripts executable. RUN chmod +x setup_dns.sh start.sh # Define the entrypoint and default command. ENTRYPOINT ["/app/start.sh"] CMD []
פריסת מאגר worker-ים עם Direct VPC ingress
פורסים מאגר עובדים בשם test-cli-dns ומצרפים אותו לרשת VPC.
gcloud run worker-pools deploy test-cli-dns \ --network default \ --subnet default \ --region us-central1 \ --source . \ --service-account SERVICE_ACCOUNT_NAME@PROJECT_ID.s3ns.iam.gserviceaccount.com \ --project PROJECT_ID
מחליפים את PROJECT_ID במזהה הפרויקט Cloud de Confiance by S3NS ואת SERVICE_ACCOUNT_NAME בשם של חשבון השירות שיצרתם.
בדיקת האפליקציה
מאגרי עובדים של Cloud Run עם Direct VPC ingress מיועדים לתעבורה פנימית בלבד. אין להם כתובת IP ציבורית. כדי לוודא שהפונקציונליות של התנועה הנכנסת פועלת, פועלים לפי השלבים הבאים כדי להריץ בקשה באמצעות curl ממכונה וירטואלית של Compute Engine שמתארחת באותה רשת VPC ורשת משנה:
יוצרים מכונה וירטואלית לבדיקה באותה רשת ואותו אזור כמו מאגר העובדים:
gcloud compute instances create wp-test-vm \ --zone=us-central1-a \ --network=default \ --subnet=defaultמתחברים למכונה הווירטואלית לבדיקה באמצעות SSH על ידי הרצת הפקודה הבאה:
gcloud compute ssh wp-test-vm --zone=us-central1-aממסוף המכונה הווירטואלית, מפעילים את השירות בכתובת ה-IP הפרטית של מופע מאגר העובדים שרשום ב-Cloud DNS:
curl http://localhost.workerpools.example.com:3000הפלט אמור להיראות כך:
Hello World!
הצלחת, הגדרתם בהצלחה את מאגר העובדים של Cloud Run עם Direct VPC באמצעות כתובת IP פרטית ו-Cloud DNS.
הסרת המשאבים
כדי להימנע מחיובים נוספים בחשבון Cloud de Confiance by S3NS , מוחקים את כל המשאבים שהצבתם באמצעות המדריך הזה.
מחיקת הפרויקט
אם יצרתם פרויקט חדש בשביל המדריך הזה, מוחקים את הפרויקט. אם השתמשתם בפרויקט קיים ואתם רוצים לשמור אותו בלי השינויים שהוספתם במדריך הזה, תצטרכו למחוק את המשאבים שיצרתם לצורך המדריך.
הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך.
כדי למחוק את הפרויקט:
- במסוף Cloud de Confiance , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.
מחיקת משאבים של מדריך
מוחקים את שירות Cloud Run שפרסתם במדריך הזה. שירותי Cloud Run לא צוברים עלויות עד שהם מקבלים בקשות.
כדי למחוק את שירות Cloud Run, מריצים את הפקודה הבאה:
gcloud run services delete SERVICE-NAME
מחליפים את SERVICE-NAME בשם השירות.
אפשר גם למחוק שירותים של Cloud Run מCloud de Confiance המסוף.
מסירים את הגדרת ברירת המחדל של האזור
gcloudשהוספתם במהלך ההגדרה של המדריך:gcloud config unset run/regionמסירים את הגדרת הפרויקט:
gcloud config unset projectמחיקה של Cloud de Confiance by S3NS משאבים אחרים שנוצרו במדריך הזה: