התחברות מ-Cloud Run
כדי לחבר אפליקציה בלי שרת (serverless) ל-AlloyDB ל-PostgreSQL, אתם יכולים לפרוס אפליקציית אינטרנט לדוגמה ב-Go בשירות Cloud Run במסוףGoogle Cloud , ולהגדיר אותה כך שתתקשר עם המכונה של AlloyDB דרך רשת פרטית של ענן וירטואלי פרטי (VPC).
קוד אפליקציה לדוגמה
קוד האפליקציה לדוגמה של המדריך למתחילים הזה נמצא בספרייה examples/go במאגר GoogleCloudPlatform/alloydb-auth-proxy ב-GitHub.
האפליקציה כתובה בשפת התכנות Go ומשמשת ככלי לאיסוף הצבעות. הלוגיקה העיקרית נמצאת בקובץ main.go, שמפעיל שרת אינטרנט ומטפל בבקשות של משתמשים.
החיבור למסד הנתונים מוגדר באמצעות משתני סביבה, שאתם מגדירים כשאתם יוצרים את שירות Cloud Run. המשתנים האלה מציינים לאפליקציה איך להתחבר למכונה של AlloyDB, כולל שם מסד הנתונים, המשתמש, הסיסמה וכתובת ה-IP של המכונה. הקוד משתמש במשתנים האלה כדי ליצור חיבור מאובטח למסד הנתונים ב-AlloyDB.
מאגר המקורות של הקוד
המאגר alloydb-auth-proxy מכיל את AlloyDB Auth Proxy, כלי שעוזר להתחבר בצורה מאובטחת למכונות של AlloyDB.
המאגר כולל גם ספריות ודוגמאות קשורות, כמו אפליקציית Go שמשמשת במדריך למתחילים הזה.
לא צריך את כל הפרויקט ב-alloydb-auth-proxy כדי להתחבר ל-AlloyDB מ-Cloud Run. הפקודה cloudshell_open משכפלת את כל המאגר לנוחיותכם, אבל הקבצים היחידים שמשמשים לבניית הקונטיינר למדריך למתחילים הזה נמצאים בספרייה examples/go.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, Cloud Run, AlloyDB, Cloud Resource Manager, Service Networking, Cloud Build, and Container Registry APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud 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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, Cloud Run, AlloyDB, Cloud Resource Manager, Service Networking, Cloud Build, and Container Registry APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.- מוודאים שיש לכם אשכול AlloyDB ומכונה ראשית. מידע נוסף זמין במאמר יצירת אשכול והמכונה הראשית שלו.
התפקידים הנדרשים
כדי לקבל את ההרשאות שדרושות לחיבור מ-Cloud Run, בקשו מהאדמין להקצות לכם את התפקידים הבאים ב-IAM בפרויקט:
- אדמין של רשת Compute (
roles/compute.networkAdmin) - אדמין ב-AlloyDB (
roles/alloydb.admin) - אדמין של Artifact Registry (
roles/artifactregistry.admin) - אדמין ב-Cloud Run (
roles/run.admin) - אדמין באחסון (
roles/storage.admin) - עריכה ב-Cloud Build (
roles/cloudbuild.builds.editor) - אדמין של Container Analysis (
roles/containeranalysis.admin) - יצירת אסימונים בחשבון שירות (
roles/iam.serviceAccountTokenCreator) - כתיבת מדדי מעקב (
roles/monitoring.metricWriter) - משתמש בחשבון שירות (
roles/iam.serviceAccountUser)
להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.
יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.
התחברות למכונה ויצירת מסד נתונים
אם אתם לא נמצאים בדף Overview של האשכול החדש שיצרתם, במסוף Google Cloud נכנסים לדף Clusters.
כדי להציג את הדף Overview של האשכול, לוחצים על שם האשכול
my-cluster.בתפריט הניווט, לוחצים על AlloyDB Studio.
ברשימה Database בקטע Log in to your database, בוחרים באפשרות
postgres.ברשימה הנפתחת User, בוחרים באפשרות
postgres.בשדה Password, מזינים את הסיסמה שיצרתם בקטע יצירת אשכול והמכונה הראשית שלו.
לוחצים על אימות. בחלונית Explorer מוצגת רשימה של האובייקטים במסד הנתונים.
בכרטיסייה Untitled query, יוצרים מסד נתונים:
CREATE DATABASE quickstart_db;לוחצים על Run. מחכים עד שההודעה
Statement executed successfullyתופיע בחלונית Results.
איך מוצאים את מזהה הפרויקט
נכנסים לדף Dashboard במסוף Google Cloud .
מזהה הפרויקט מופיע בכרטיס Project info.
רושמים את מזהה הפרויקט. תצטרכו אותו בשלב הבא של המדריך למתחילים.
אכלוס Artifact Registry בתמונה של האפליקציה לדוגמה
במסוף Google Cloud , פותחים את Cloud Shell.
ב-Cloud Shell Editor, משתמשים בפקודה הבאה כדי לשכפל את המאגר עם קוד האפליקציה לדוגמה מ-GitHub:
cloudshell_open --repo_url \ "https://github.com/GoogleCloudPlatform/alloydb-auth-proxy" \ --dir \ "examples/go" \ --force_new_cloneיוצרים מאגר ב-Artifact Registry לקובצי אימג' של Docker:
gcloud artifacts repositories create cloud-run-source-deploy --location us-central1 --repository-format=docker --project PROJECT_IDמחליפים את
PROJECT_IDבמזהה הפרויקט.בתיבת הדו-שיח Authorize Cloud Shell, לוחצים על Authorize. ההודעה הזו לא תופיע אם כבר ביצעתם את השלב הזה בעבר.
כדי ליצור קונטיינר של Docker ולפרסם אותו ב-Artifact Registry, משתמשים בפקודה הבאה:
gcloud builds submit --tag us-central1-docker.pkg.dev/PROJECT_ID/cloud-run-source-deploy/run-sql --project PROJECT_IDמחליפים את
PROJECT_IDבמזהה הפרויקט.קוד האפליקציה לדוגמה של המדריך למתחילים הזה נמצא בספרייה
examples/goבמאגרGoogleCloudPlatform/alloydb-auth-proxyב-GitHub. הפקודהcloudshell_openבשלב הקודם משכפלת באופן אוטומטי את המאגר הזה ופותחת את הספרייה הנכונה.
איך מוצאים את כתובת ה-IP של המכונה החדשה
נכנסים לדף Clusters במסוף Google Cloud .
בטבלה, מוצאים את השורה שבה שם המשאב הוא
my-primary.שימו לב לערך של Private IP address בשורה הזו. הוא נדרש לשלב הבא במדריך למתחילים.
דוגמה לפורמט של כתובת IP:
172.19.209.2:5432.
יצירת שירות ל-Cloud Run
נכנסים לדף Cloud Run במסוף Google Cloud .
בכרטיסייה Services (שירותים), לוחצים על Deploy Container (פריסת מאגר תגים).
בדף Create service, בוחרים באפשרות Deploy one revision from an existing container image.
בשדה Container image URL, לוחצים על Select.
בחלונית Select container image, מבצעים את הפעולות הבאות:
- לוחצים על הכרטיסייה Artifact Registry.
- מרחיבים את
us-central1-docker.pkg.dev/PROJECT_ID/cloud-run-source-deploy. - מרחיבים את
run-sql. - בוחרים את התמונה האחרונה.
- לוחצים על בחירה.
בשדה Service name, מזינים
quickstart-service.מוודאים שהאפשרות
us-central1 (Iowa)נבחרה כברירת מחדל ברשימה Region.בקטע Authentication, בוחרים באפשרות Allow public access.
מוודאים שהאפשרות CPU is only allocated during request processing מסומנת בקטע CPU allocation and pricing.
מרחיבים את הקטע Container(s), Volumes, Networking, Security.
בכרטיסייה Container(s) (מאגרי תגים), בוחרים בכרטיסייה Variables & secrets (משתנים וסודות) ומבצעים את השלבים הבאים:
בקטע Environment variables, לוחצים על Add variable.
מזינים את הפרטים הבאים בשדות
Nameו-Value:DB_NAME:quickstart_dbDB_USER:postgres-
DB_PASS: הסיסמה שהזנתם כשיצרתם את האשכול. DB_PORT:5432-
INSTANCE_HOST: כתובת ה-IP של המכונה הראשית של האשכול, שמצאתם בקטע איתור כתובת ה-IP של המכונה החדשה. אל תכללו את מספר היציאה – לדוגמה, הגדירו את172.19.209.2ולא את172.19.209.2:5432.
עוברים מהכרטיסייה Container(s) לכרטיסייה Networking ומבצעים את הפעולות הבאות:
מסמנים את התיבה Connect to a VPC for outbound traffic.
בוחרים באפשרות שליחת תנועה ישירות ל-VPC.
ברשימה הנפתחת רשת, בוחרים את הערך
default.ברשימה הנפתחת Subnet, מוודאים שהערך
defaultנבחר.
לוחצים על Create כדי ליצור את שירות Cloud Run.
אחרי שפורסים את שירות Cloud Run, כתובת ה-URL של האפליקציה לדוגמה מופיעה בשדה URL בדף quickstart-service.
האפליקציה משתמשת ב-AlloyDB כמאגר הנתונים שלה.
הצגת האפליקציה לדוגמה
נכנסים לדף Cloud Run במסוף Google Cloud .
בדף quickstart-service, לוחצים על content_copy Copy to clipboard כדי להעתיק את כתובת ה-URL של האפליקציה.
בכרטיסייה חדשה בדפדפן, מדביקים את כתובת ה-URL של האפליקציה שהועתקה ולוחצים על Enter.
הסרת המשאבים
כדי לא לצבור חיובים בחשבון Google Cloud על המשאבים שבהם השתמשתם בדף הזה, מבצעים את הפעולות הבאות.
ניקוי האשכול
נכנסים לדף Clusters במסוף Google Cloud .
ליד האשכול ב-
my-clusterשרוצים למחוק, לוחצים על more_vert Actions ואז על delete Delete.בתיבת הדו-שיח Delete cluster שמופיעה, מקלידים
my-clusterבשדה my-cluster כדי לאשר שרוצים למחוק אותו.לוחצים על Delete.
ניקוי השירות
נכנסים לדף Cloud Run במסוף Google Cloud .
מסמנים את התיבה לצד שם השירות
quickstart-service.לוחצים על delete Delete בחלק העליון של הדף Cloud Run.
בתיבת הדו-שיח Delete quickstart-service שמופיעה, לוחצים על Delete כדי לאשר.