תחילת העבודה עם Java ב-Compute Engine

במדריך הזה מוסבר איך להתחיל להשתמש ב-Compute Engine. כדי לתרגל את מה שלומדים במדריך הזה, אפשר לפרוס אפליקציית אינטרנט Hello World Java ב-Compute Engine. לקבלת עזרה בתחילת העבודה עם App Engine, אפשר לעיין במאמר בנושא הסביבה הרגילה של App Engine.

מטרות

  • שימוש ב-Cloud Shell להורדה ולפריסה של אפליקציית Hello World לדוגמה.
  • פריסת אפליקציית Hello World לדוגמה במכונה וירטואלית אחת ב-Compute Engine.

עלויות

במסמך הזה משתמשים ברכיבים הבאים של Cloud de Confiance by S3NS, והשימוש בהם כרוך בתשלום:

לפני שמתחילים

  1. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Cloud de Confiance project.

  3. Enable the Compute Engine API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  4. במסוף Cloud de Confiance , פותחים את האפליקציה ב-Cloud Shell.

    כניסה ל-Cloud Shell

    ‫Cloud Shell מספק גישה למשאבי הענן שלכם דרך שורת הפקודה ישירות מהדפדפן.

  5. אם אתם מסכימים לשכפל את המאגר, לוחצים על אישור כדי להוריד את קוד הדוגמה ולעבור לספריית האפליקציה.

  6. ב-Cloud Shell, מגדירים את ה-CLI של gcloud לשימוש בפרויקט החדש Cloud de Confiance :
    # Configure gcloud for your project
    gcloud config set project YOUR_PROJECT_ID

הפעלת האפליקציה ב-Cloud Shell

  1. ב-Cloud Shell, מפעילים שרת אינטרנט מקומי:

    mvn -Plocal clean jetty:run-exploded -DprojectID=YOUR-PROJECT-ID
    
  2. ב-Cloud Shell, לוחצים על תצוגה מקדימה באינטרנט ובוחרים באפשרות תצוגה מקדימה ביציאה 8080. ייפתח חלון חדש עם האפליקציה הפועלת.

    בדפדפן האינטרנט, יופיע הטקסט Hello World, שמוצג מהמחשב המקומי.

  3. כדי להמשיך, לוחצים על Control+C ב-Cloud Shell כדי לעצור את שרת האינטרנט המקומי.

פריסה למופע יחיד

בקטע הזה מוסבר איך להריץ מופע יחיד של האפליקציה ב-Compute Engine.

פריסה של מופע יחיד.

מ-Cloud Shell, אפשר לבצע פריסה למכונה וירטואלית (VM) של Compute Engine שבה האפליקציה פועלת.

שימוש בסקריפט לטעינה בזמן ההפעלה כדי לאתחל מכונה

צריך דרך להורות למופע להוריד ולהפעיל את הקוד. למופע יכול להיות סקריפט לטעינה בזמן ההפעלה שפועל בכל פעם שהמופע מופעל או מופעל מחדש.

סקריפט לטעינה בזמן ההפעלה מופעל כשמכונה מופעלת בפעם הראשונה.

set -e
set -v

# Talk to the metadata server to get the project id
PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")

echo "Project ID: ${PROJECTID}"

# Install dependencies from apt
apt-get install -yq openjdk-11-jdk git maven

mvn --version

# Jetty Setup
mkdir -p /opt/jetty/temp
mkdir -p /var/log/jetty

# Get Jetty
curl -L https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distribution/9.4.13.v20181111/jetty-distribution-9.4.13.v20181111.tar.gz -o jetty9.tgz
tar xf jetty9.tgz  --strip-components=1 -C /opt/jetty

# Add a Jetty User
useradd --user-group --shell /bin/false --home-dir /opt/jetty/temp jetty

cd /opt/jetty
# Add running as "jetty"
java -jar /opt/jetty/start.jar --add-to-startd=setuid
cd /

# Clone the source repository.
git clone https://github.com/GoogleCloudPlatform/getting-started-java
cd getting-started-java/gce

# Build the .war file and rename.
# very important - by renaming the war to root.war, it will run as the root servlet.
mvn clean package -q
mv target/getting-started-gce-1.0-SNAPSHOT.war /opt/jetty/webapps/root.war

# Make sure "jetty" owns everything.
chown --recursive jetty /opt/jetty

# Configure the default paths for the Jetty service
cp /opt/jetty/bin/jetty.sh /etc/init.d/jetty
echo "JETTY_HOME=/opt/jetty" > /etc/default/jetty
{
  echo "JETTY_BASE=/opt/jetty"
  echo "TMPDIR=/opt/jetty/temp"
  echo "JAVA_OPTIONS=-Djetty.http.port=80"
  echo "JETTY_LOGS=/var/log/jetty"
} >> /etc/default/jetty

# Reload daemon to pick up new service
systemctl daemon-reload

# Install logging monitor. The monitor will automatically pickup logs sent to syslog.
curl -sSO https://dl.google.com/cloudagents/add-logging-agent-repo.sh
sudo bash add-logging-agent-repo.sh --also-install

service google-fluentd restart &

service jetty start
service jetty check

echo "Startup Complete"

סקריפט לטעינה בזמן ההפעלה מבצע את המשימות הבאות:

  • הסקריפט מתקין את Java 11 ומגדיר אותה כגרסת ברירת המחדל.

  • התקנה והגדרה של Jetty.

  • מעתיקים את קובץ ה-WAR של Java מהקטגוריה של Cloud Storage אל webapps של Jetty ומשנים את השם שלו ל-root.war. כך הוא הופך לסרוולט השורש, ולכן לא צריך לציין את השם שלו בכתובת ה-URL.

  • הסקריפט מתקין את סוכן Cloud Logging ומגדיר אותו למעקב אחרי יומני האפליקציה. המשמעות היא שהרישום ביומן שהגדרתם בשלבים הקודמים של המדריך הזה מועלה בדיוק כמו שהיה קורה אם הייתם משתמשים בסביבה הגמישה של App Engine.

יצירה והגדרה של מכונה של Compute Engine

  1. יצירת מכונה של Compute Engine:

    gcloud compute instances create my-app-instance 
    --image-family=debian-10
    --image-project=debian-cloud
    --machine-type=g1-small
    --scopes userinfo-email,cloud-platform
    --metadata-from-file startup-script=gce/startup-script.sh
    --zone YOUR_ZONE
    --tags http-server
    מחליפים את YOUR_ZONE באזור פיתוח, לדוגמה us-central1-a. מידע נוסף על אזורים ותחומים זמין במאמר מיקום גיאוגרפי ואזורים.

    הפעולה הזו יוצרת מופע חדש, מאפשרת לו לגשת לשירותים של Cloud de Confiance Google ומריצה את הסקריפט לטעינה בזמן ההפעלה. שם המכונה הוא my-app-instance.

  2. בודקים את התקדמות יצירת המופע:

    gcloud compute instances get-serial-port-output my-app-instance --zone YOUR_ZONE
    

    בסיום סקריפט לטעינה בזמן ההפעלה, תוצג ההודעה הבאה:

    startup-script: INFO Finished running startup scripts.
    
  3. יוצרים כלל חומת אש שמאפשר תעבורת נתונים למופע:

    gcloud compute firewall-rules create default-allow-http-80 \
        --allow tcp:80 \
        --source-ranges 0.0.0.0/0 \
        --target-tags http-server \
        --description "Allow port 80 access to http-server"
    

  4. מקבלים את כתובת ה-IP החיצונית של המופע:

    gcloud compute instances list
    
  5. כדי לראות את האפליקציה פועלת, מזינים את כתובת ה-URL הזו בדפדפן:

    http://YOUR_INSTANCE_IP
    

    מחליפים את הערך YOUR_INSTANCE_IP בכתובת ה-IP החיצונית של המכונה.

ניהול של אירוע ומעקב אחריו

אתם יכולים להשתמש במסוף Cloud de Confiance כדי לעקוב אחרי המופע ולנהל אותו.

  1. נכנסים לדף VM instances במסוף Cloud de Confiance .

    כניסה לדף VM instances

  2. ברשימת המכונות הווירטואליות, לוחצים על SSH בשורה של המכונה שרוצים להתחבר אליה.
  3. כדי לראות את כל היומנים שנוצרו על ידי משאבי Compute Engine, עוברים לדף Logs Explorer.

    כניסה לדף Logs Explorer

    Cloud Logging מוגדר באופן אוטומטי לאיסוף יומנים משירותים נפוצים שונים, כולל syslog.

הסרת המשאבים

כדי להימנע מחיובים בחשבון Google Cloud בגלל השימוש במשאבים שנעשה במסגרת המדריך הזה, אפשר למחוק את הפרויקט שמכיל את המשאבים, או להשאיר את הפרויקט ולמחוק את המשאבים בנפרד.

מחיקת הפרויקט

  1. במסוף Cloud de Confiance , נכנסים לדף Manage resources.

    כניסה לדף Manage resources

  2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
  3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

מחיקת המשאבים הבודדים

gcloud compute instances delete my-app-instance --zone=YOUR_ZONE --delete-disks=all
gcloud compute firewall-rules delete default-allow-http-80

המאמרים הבאים