צריך להשלים את תהליך העברת ההגדרות לכל אפליקציית Cloud Foundry שמעבירים ל-Cloud Run. העברת ההגדרות כוללת את הפעולות הבאות:
- המרת
manifest.yamlשל Cloud Foundry ל-service.yamlשל Cloud Run. - צירוף שירותי גיבוי לאפליקציה לצורך פריסה ב-Cloud Run.
- פריסת האפליקציה בשירות Cloud Run.
המרת manifest.yaml ל-service.yaml
צריך להמיר מניפסט של Cloud Foundry או פקודות של cf CLI ל-YAML של הגדרת שירות מקביל ב-Cloud Run.
ב-Cloud Run, לכל אפליקציה צריך להיות קובץ YAML נפרד משלה. כדי להעביר אפליקציה במניפסט של Cloud Foundry לקובץ YAML של שירות:
אוספים את המאפיינים שמפורטים בטבלה הבאה לגבי האפליקציה. יכול להיות שהגדרות של נכסים שלא שונו ברמת האפליקציה בוטלו על ידי הגדרות גלובליות של פלטפורמת Cloud Foundry. כדי לקבל את הערכים בפועל, אפשר לעיין במסמכי התיעוד שסופקו על ידי מנהלי הפלטפורמה.
מאפיין של האפליקציה cfדגלים ב-CLI v6תיאור nameארגומנט אחד ( NAME)השם הייחודי של האפליקציה ב-Cloud Foundry. command-cפקודה שתופעל ב- /bin/shאו ב-/bin/bashdisk_quota-kכמות הדיסק שתוקצה לאפליקציה.
יחידות המרחק הקבילות הן:
M,MB,Gו-r Bברירת מחדל סבירה: 1G
docker.image--docker-image,-oהתמונה שמכילה את האפליקציה להפעלה. health-check-http-endpointלא רלוונטי נקודת הקצה (endpoint) שמשמשת לקביעת תקינות ה-HTTP אם סוג בדיקת התקינות הוא HTTP. ברירת מחדל:
/health-check-invocation-timeoutלא רלוונטי הזמן בשניות בין בדיקות תקינות נפרדות שמבוססות על יציאה ועל HTTP. ברירת מחדל: 1
health-check-type--health-check-type,-uסוג בדיקת התקינות שרוצים לבצע באפליקציה. הערכים התקפים הם: port, http, none, process.ברירת מחדל:
portinstances-iמספר המופעים של האפליקציה ש-Cloud Foundry יפעיל. ברירת מחדל: 1
memory-mמגבלת הזיכרון לכל מופע של האפליקציה. יחידות המרחק הקבילות הן:
M,MB,GאוGBברירת מחדל סבירה: 1G
timeout-tמספר השניות המותרות בין הפעלת האפליקציה לבין בדיקת התקינות הראשונה. ברירת מחדל סבירה: 60
כדי להגדיר את הפרויקט Google Cloud ואת Cloud Run, צריך לאסוף את הפרטים הבאים:
מאפיין (property) תיאור project_numberמספר הפרויקט ב-Google Cloud שאליו רוצים לפרוס. regionהאזור שבו רוצים לפרוס את האפליקציה. vpc-access-connectorהשם של מחבר ה-VPC שהאדמין של הפלטפורמה רוצה שאפליקציות יתחברו אליו. vpc-access-egressהשם של יציאת ה-VPC שבה מנהל הפלטפורמה רוצה שהאפליקציות יהיו. custom-audiencesקהלים בהתאמה אישית שיכולים לעבור אימות באפליקציה שלכם. serviceAccountNameהזהות שהאפליקציה תפעל בשמה ב-Google Cloud. imageתמונת האפליקציה שיצרתם בשלב הקודם. מאכלסים את התבנית הבאה בקובץ
service.yamlבשורש של הפרויקט
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
# Set this to be the name of your app
name: "APP_NAME"
# Set this to be the project number of the project you're deploying to.
namespace: "PROJECT_NUMBER"
labels:
# Set this to be the region you're deploying in.
cloud.googleapis.com/location: REGION
migrated-from: cloud-foundry
annotations:
run.googleapis.com/ingress: internal-and-cloud-load-balancing
spec:
template:
metadata:
annotations:
# Set to the greater of 1 or the `instances` attribute.
autoscaling.knative.dev/minScale: '1'
# Set to the greater of 1 or the `instances` attribute.
autoscaling.knative.dev/maxScale: '1'
run.googleapis.com/cpu-throttling: CPU_ALLOCATION
run.googleapis.com/startup-cpu-boost: 'true'
# Set to true if you rely on sticky sessions. These will be turned
# on in Cloud Foundry if the server sends a JSESSIONID cookie back
# on responses.
run.googleapis.com/sessionAffinity: 'false'
run.googleapis.com/execution-environment: gen2
# Set the following values to match what your platform administrator recommends.
run.googleapis.com/vpc-access-connector: ADMINISTRATOR_PROVIDED
run.googleapis.com/vpc-access-egress: ADMINISTRATOR_PROVIDED
run.googleapis.com/custom-audiences: ADMINISTRATOR_PROVIDED
spec:
# CF doesn't limit, but CR has a max of 1000.
containerConcurrency: 1000
# Default value for gorouter in PCF.
timeoutSeconds: 900
# Set the following value to match what your platform administrator recommends.
serviceAccountName: ADMINISTRATOR_PROVIDED
containers:
- name: user-container
# Set the following value to either:
# - The image you built for your application in the last section of the guide.
# - The docker.image attribute of your app's configuration if it's a Docker app.
image: IMAGE
# Set `command` based on the following rules:
# - If your app has no `command` attribute: null.
# - If your app has a docker.image attribute: ['/bin/sh', '-c']
# - Otherwise: ['/bin/bash', '-c']
command: null
# Set `args` based on the following rules:
# - If your app has no `command` attribute: null.
# - If your app has a `command` attribute: ['value of command']
args: null
ports:
# Set name based on the following rules:
# - If your app is HTTP/2 or gRPC: "h2c"
# - Else: "http1"
- name: HTTP1_OR_H2C
containerPort: 8080
env:
# For each key/value pair in your space's running environment variable groups,
# which can be retried by running `cf running-environment-variable-group`,
# add the following:
- name: KEY
value: VALUE
# For each key/value pair in your manifest's `env` map, add the following:
- name: KEY
value: VALUE
# Populate MEMORY_LIMIT with the amount of memory supplied to this instance
# in MiB with 'M' as a suffix.
- name: MEMORY_LIMIT
value: '0M'
# Set the following values in the JSON below:
# - `application_name` and `name` to match metadata.name in this file.
# - `application_uris` and `uris` to be the URI you want to assign the app on the
# load balancer.
# - `limits.disk` to be the amount (in MiB) of disk assigned to your app.
# The amount will be in the `disk_quota` attribute of the CF manifest, or a
# default value for your cluster, typically 1GiB.
# - `limits.mem` to be the amount (in MiB) of memory assigned to your app.
# The amount will be in your `memory` attribute of the CF manifest, or a
# default value for your cluster, typically 1GiB.
# - `space_name` to be the value of metadata.space in this file.
- name: VCAP_APPLICATION
value: |-
{
"application_id": "00000000-0000-0000-0000-000000000000",
"application_name": "app-name",
"application_uris": [],
"limits": {
"disk": 1024,
"mem": 256
},
"name": "app-name",
"process_id": "00000000-0000-0000-0000-000000000000",
"process_type": "web",
"space_name": "none",
"uris": []
}
resources:
limits:
# Set memory limit to be the sum of the memory and disk assigned to your app in CF.
#
# Disk amount will be in the `disk_quota` attribute of the CF manifest, or a
# default value for your cluster, typically 1GiB.
#
# Memory will be in your `memory` attribute of the CF manifest, or a
# default value for your cluster, typically 1GiB.
memory: MEMORY_LIMIT
# Set cpu according to the following calculation:
#
# 1. Take the amount of memory in your `memory` attribute of the CF
# manifest, or a default value for your cluster, typically 1GiB.
# 2. Divide that by the total amount of memory on the underlying BOSH VM.
# 3. Multiply that by the total number of CPUs on the BOSH VM.
# 4. Find the nearest valid value based on the rules in:
# https://cloud.google.com/run/docs/configuring/cpu#setting
cpu: CPU_LIMIT
# If `health-check-type` is "process" or "none", delete the startupProbe section.
startupProbe:
# If `health-check-type` is "port" or blank, delete the httpGet section.
httpGet:
# Set to be the value of `health-check-http-endpoint` or / if blank.
path: CHECK_PATH
port: 8080
# If `health-check-type` is "http", delete the tcpSocket section.
tcpSocket:
port: 8080
# Set to the value of `health-check-invocation-timeout` or 1
timeoutSeconds: 1
# Set failure threshold to be the following calculation:
#
# 1. Take the `timeout` from the CF manifest, use 60 if unset.
# 2. Divide by 2.
# 3. Round up to the nearest integer.
failureThreshold: 1
successThreshold: 1
periodSeconds: 2
# If `health-check-type` is "process" or "none", delete the livenessProbe section.
livenessProbe:
# If `health-check-type` is "port" or blank, delete the httpGet section.
httpGet:
# Set to be the value of `health-check-http-endpoint` or / if blank.
path: CHECK_PATH
port: 8080
# If `health-check-type` is "http", delete the tcpSocket section.
tcpSocket:
port: 8080
# Set to the value of `health-check-invocation-timeout` or 1.
timeoutSeconds: 1
failureThreshold: 1
successThreshold: 1
periodSeconds: 30
traffic:
- percent: 100
latestRevision: true
צירוף שירותי גיבוי
צריך ליצור משתנה סביבה VCAP_SERVICES כדי לאפשר הוספה של שירותים וגילוי שלהם על ידי אפליקציית Cloud Foundry, כמו Spring או Steeltoe. צריך לעשות את זה לכל אפליקציה שמעבירים. מידע נוסף זמין במסמכי התיעוד של Cloud Foundry VCAP_SERVICES.
אם האפליקציה כבר פועלת ב-Cloud Foundry ואתם רוצים לצרף אותה לאותם שירותים ב-Cloud Run, אתם יכולים להשתמש במשתנה הסביבה הקיים. אחרת, תצטרכו ליצור VCAP_SERVICES חדש.
כדי להגדיר את משתנה הסביבה VCAP_SERVICES:
במקרה של
VCAP_SERVICESקיים:- מנסים לקבל את משתנה הסביבה
VCAP_SERVICESעל ידי הפעלת הפקודהcf env APP_NAME. - אם זה לא עובד:
- מתחברים לאפליקציה ב-Cloud Foundry:
cf ssh APP_NAME - מריצים את הפקודה
envומקבלים את הפלט שלVCAP_SERVICES. - יוצאים מהסשן של SSH על ידי הפעלת
exit.
- מתחברים לאפליקציה ב-Cloud Foundry:
- שומרים את הערך
VCAP_SERVICESבקובץ חדש בשםvcap.json.
- מנסים לקבל את משתנה הסביבה
אם רוצים להוסיף שירותים או להתחבר לשירותים שונים מאלה שב-Cloud Foundry, צריך ליצור
VCAP_SERVICES:- יוצרים מפת JSON ריקה בכלי לעריכת טקסט
{} - לכל שירות שרוצים להוסיף, מבצעים את הפעולות הבאות:
- אפשר לעיין במסמכי התיעוד של הספרייה שבה האפליקציה משתמשת כדי לנתח את
VCAP_SERVICESולמצוא את הסוג שרוצים להוסיף, כדי להבין איך מתבצעת הגילוי של הקישור. - מוסיפים מפתח למפה עם השם של ספק השירות, אם הוא לא קיים כבר. בדרך כלל זה משהו כמו
mysql,postgresqlאוelasticsearch. מגדירים את הערך כמערך ריק: מוסיפים אובייקט למערך עם המאפיינים הבאים:
מטא-נתונים שבדרך כלל לא משמשים לגילוי שירותים או לקישור שלהם:
-
binding_name, מחרוזת שמייצגת את המשאב שמעניק לאפליקציה שלכם הרשאות בשירות. יכול להיות שזה שם משתמש במסד נתונים, כלל בחומת אש, שם של חשבון שירות או משהו אחר. -
instance_name, מחרוזת שמייצגת את השם של שירות הגיבוי. זה יכול להיות שם מסד הנתונים, ערך אקראי או ערך שמציין שירות גלובלי. -
name, הערךbinding_nameאם הוא קיים, אחרת הערךinstance_name. בדרך כלל הערך הזה לא חשוב. -
label, הערך של המפתח במיפויVCAP_SERVICESשהקישור הזה מקונן בו. -
plan, השם של תוכנית השירות. דוגמאות: user-provided, high-availability.
-
ערכים שמשמשים לעיתים קרובות לגילוי שירותים או לקישור שירותים:
tagsרשימת תגים שעוזרת לספריות למצוא שירותים תואמים. השם הזה כולל בדרך כלל את השם הנפוץ של השירות, למשלmysqlל-MySQL ול-MariaDB,redisל-Redis או ל-Cloud Memorystore, אוpostgresלמסדי נתונים שתואמים ל-Postgres.credentialsאובייקט שמכיל פרטי כניסה שספריית הלקוח משתמשת בהם כדי לבצע את החיבור. רוב ספריות הלקוח מסתמכות על שדהuriשמכיל את ה-URI הרגיל של השירות או את הפורמט של JDBC.
שומרים את התוכן כ-
vcap.json.
- יוצרים מפת JSON ריקה בכלי לעריכת טקסט
צירוף פרטי כניסה למשאב Cloud Run
כדי לצרף פרטי כניסה:
יוצרים סוד כדי להחזיק את התוכן של משתנה הסביבה
VCAP_SERVICESורושמים את הגרסה שמוצגת בפלט של הפקודה:gcloud secrets create APP_NAME-vcap \ --replication-policy="automatic" \ --data-file=vcap.jsonנותנים לחשבון השירות של האפליקציה הרשאה לקרוא את הסוד:
gcloud secrets add-iam-policy-binding APP_NAME-vcap \ --member="serviceaccount:app-service-account" \ --role="roles/secretmanager.secretAccessor"מוסיפים את משתנה הסביבה הבא לאפליקציה
service.yamlבקובץspec.template.spec.containers[0].env array:- name: VCAP_SERVICES valueFrom: secretKeyRef: key: Version output by step 1 name: APP_NAME-vcap
תבניות לשירותי גיבוי נפוצים
בקטעים הבאים מפורט מידע על שירותי גיבוי נפוצים
MySQL
ספריות MySQL בדרך כלל מצפות לתג mysql. בדרך כלל כוללים את המפתחות הבאים ב-credentials:
- תבנית
uri:mysql://username:password@host:port/dbname. במסמכי התיעוד של MySQL אפשר למצוא עזרה ביצירת מחרוזת URI. היציאה היא בדרך כלל3306. -
usernameשם המשתמש לחיבור, שנדרש על ידי חלק מהספריות גם אם הוא כלול ב-uri -
passwordסיסמת החיבור, שנדרשת בספריות מסוימות גם אם היא כלולה ב-uri
Redis
ספריות Redis בדרך כלל מצפות לתג redis. בדרך כלל כוללים את המפתחות הבאים ב-credentials:
uriתבנית:redis://:password@host:port/dbunumber.
מסמכי התיעוד של IANA Redis URI יכולים לעזור ביצירת מחרוזת URI. היציאה היא בדרך כלל 6379.
RabbitMQ
בדרך כלל, ספריות RabbitMQ מצפות לתג rabbitmq ולמפתחות הבאים ב-credentials:
uriתבנית:amqp://username:password@host:port/vhost?query.
במסמכי התיעוד של RabbitMQ יש מידע שיכול לעזור לכם ליצור מחרוזת URI. היציאה היא בדרך כלל 5672.
שירותים שהמשתמשים סיפקו
שירותים שסופקו על ידי המשתמש הם סוג מיוחד של שירות ב-Cloud Foundry שמאפשר להזין פרטי כניסה כלשהם. התווית היא תמיד user-provided. התגים הם הערכים שמועברים אל cf create-user-provided-service באמצעות הדגל -t, והאישורים הם התוכן של הדגל -p.
פריסת האפליקציה
כדי לפרוס את אפליקציית Cloud Foundry שהועברה במלואה לשירות Cloud Run:
אם עדיין לא עשיתם זאת, מגדירים את סביבת Cloud Run.
מריצים את הפקודה
gcloud run services replace service.yamlמחכים כמה רגעים עד שהפריסה תושלם. אם הפעולה בוצעה ללא שגיאות, כתובת ה-URL של השירות תוצג בשורת הפקודה.
נכנסים לשירות שפרסתם על ידי פתיחת כתובת ה-URL של השירות בדפדפן אינטרנט.
כל הכבוד! העברתם עכשיו את אפליקציית Cloud Foundry אל Cloud Run