Bird
Raised Fist0
GCPcloud~5 mins

Deploying workloads in GCP - Commands & Configuration

Choose your learning style10 modes available
Introduction
Deploying workloads means putting your app or service on Google Cloud so it can run and be used by others. This process helps you move your code from your computer to the cloud where it can work continuously and handle users.
When you want to run a web app on Google Cloud for users to access anytime.
When you need to launch a background job that processes data regularly.
When you want to update your app with new features and make it live.
When you want to test your app in the cloud before sharing it widely.
When you want to scale your app automatically based on user demand.
Config File - app.yaml
app.yaml
runtime: python39
entrypoint: gunicorn -b :$PORT main:app

handlers:
- url: /static
  static_dir: static/

- url: /.*
  script: auto

This file tells Google App Engine how to run your Python app.

  • runtime: Specifies the Python version.
  • entrypoint: Command to start your app server.
  • handlers: Define how URLs are handled, like serving static files.
Commands
This command uploads your app configuration and code to Google Cloud and starts the deployment process.
Terminal
gcloud app deploy app.yaml
Expected OutputExpected
Services to deploy: descriptor: app.yaml Do you want to continue (Y/n)? Y Beginning deployment... Updating service [default]... Waiting for operation to complete... Deployed service [default] to [https://PROJECT_ID.uc.r.appspot.com] You can stream logs from the command line by running: gcloud app logs tail -s default
This command opens your deployed app in the web browser so you can see it running live.
Terminal
gcloud app browse
Expected OutputExpected
Launching default service in default browser...
This command shows the live logs of your app, helping you see what is happening and catch errors.
Terminal
gcloud app logs tail -s default
Expected OutputExpected
2024-06-01 12:00:00 default[20230601t123456] INFO Starting gunicorn 20.1.0 2024-06-01 12:00:05 default[20230601t123456] INFO Listening on port 8080
→
-s - Specifies the service name to show logs for
Key Concept

If you remember nothing else from this pattern, remember: deploying workloads means sending your app and its instructions to Google Cloud so it can run and serve users.

Common Mistakes
Not creating or updating the app.yaml file before deployment
Without this file, Google Cloud does not know how to run your app, so deployment fails or the app does not work correctly.
Always create a valid app.yaml file with the correct runtime and entrypoint before deploying.
Not confirming the deployment prompt by typing Y
The deployment stops if you do not confirm, so your app is not uploaded or updated.
Type Y and press Enter when asked to continue deployment.
Trying to browse the app before deployment finishes
The app URL is not ready until deployment completes, so the browser shows an error.
Wait for the deployment command to finish successfully before opening the app.
Summary
Create an app.yaml file to tell Google Cloud how to run your app.
Use 'gcloud app deploy app.yaml' to upload and start your app in the cloud.
Use 'gcloud app browse' to open your live app and 'gcloud app logs tail -s default' to see its logs.

Practice

(1/5)
1. What does deploying a workload on Google Cloud Run primarily do?
easy
A. Stores your application code without running it
B. Creates a virtual machine that you must manage manually
C. Runs your application in a managed environment that scales automatically
D. Only backs up your application data

Solution

  1. Step 1: Understand Cloud Run's purpose

    Cloud Run runs your app in a managed environment that automatically scales based on traffic.
  2. Step 2: Compare options

    Options B, C, and D describe other services or actions, not workload deployment on Cloud Run.
  3. Final Answer:

    Runs your application in a managed environment that scales automatically -> Option C
  4. Quick Check:

    Cloud Run = managed auto-scaling app hosting [OK]
Hint: Cloud Run runs and scales apps automatically [OK]
Common Mistakes:
  • Confusing Cloud Run with virtual machines
  • Thinking Cloud Run only stores code
  • Assuming manual scaling is needed
2. Which of the following is the correct gcloud command to deploy a container image to Cloud Run in region us-central1 with service name my-service?
easy
A. gcloud run deploy my-service --image=gcr.io/my-project/my-image --region=us-central1
B. gcloud deploy run --service=my-service --image=my-image --region=us-central1
C. gcloud run create --name my-service --image gcr.io/my-project/my-image --zone us-central1
D. gcloud container deploy my-service --image=gcr.io/my-project/my-image --region=us-central1

Solution

  1. Step 1: Recall correct gcloud run deploy syntax

    The correct command uses 'gcloud run deploy SERVICE_NAME --image=IMAGE --region=REGION'.
  2. Step 2: Match options to syntax

    gcloud run deploy my-service --image=gcr.io/my-project/my-image --region=us-central1 matches the correct syntax exactly. Others have wrong commands or flags.
  3. Final Answer:

    gcloud run deploy my-service --image=gcr.io/my-project/my-image --region=us-central1 -> Option A
  4. Quick Check:

    Correct gcloud run deploy syntax = gcloud run deploy my-service --image=gcr.io/my-project/my-image --region=us-central1 [OK]
Hint: Use 'gcloud run deploy SERVICE --image=IMAGE --region=REGION' [OK]
Common Mistakes:
  • Using 'gcloud deploy run' instead of 'gcloud run deploy'
  • Confusing --zone with --region
  • Using 'gcloud container deploy' which is invalid
3. Given this command: gcloud run deploy my-app --image=gcr.io/project/app:v1 --region=us-east1, what happens after deployment?
medium
A. The app is uploaded but does not run until manually started
B. The app runs on Cloud Run and scales automatically with traffic
C. A virtual machine is created but the app is not deployed
D. The app runs only on your local machine

Solution

  1. Step 1: Understand Cloud Run deployment behavior

    Deploying with gcloud run deploy starts the app on Cloud Run immediately with auto-scaling.
  2. Step 2: Eliminate incorrect options

    Options B, C, and D describe behaviors not matching Cloud Run's managed service.
  3. Final Answer:

    The app runs on Cloud Run and scales automatically with traffic -> Option B
  4. Quick Check:

    Cloud Run deploy = app runs and scales automatically [OK]
Hint: Deploy starts app running with auto-scaling on Cloud Run [OK]
Common Mistakes:
  • Thinking deployment only uploads code
  • Assuming manual start is needed
  • Confusing Cloud Run with VM creation
4. You run gcloud run deploy my-service --image=gcr.io/my-project/my-image but get an error about missing region. What is the fix?
medium
A. Remove the --image flag and try again
B. Deploy to Cloud Storage instead
C. Use --zone instead of --region
D. Add the flag --region=REGION with a valid region like us-central1

Solution

  1. Step 1: Identify missing required flag

    The error indicates the region flag is missing, which is required for Cloud Run deployment.
  2. Step 2: Correct the command by adding --region

    Adding --region=us-central1 (or another valid region) fixes the error.
  3. Final Answer:

    Add the flag --region=REGION with a valid region like us-central1 -> Option D
  4. Quick Check:

    Missing region error = add --region flag [OK]
Hint: Always specify --region when deploying to Cloud Run [OK]
Common Mistakes:
  • Using --zone instead of --region
  • Removing --image flag which is required
  • Trying to deploy to wrong service like Cloud Storage
5. You want to deploy a new version of your app to Cloud Run but keep the old version running for testing. How can you achieve this?
hard
A. Deploy the new version with the same service name and region
B. Deploy to a different Google Cloud project
C. Use gcloud run update to replace the old version
D. Deploy the new version with a different service name

Solution

  1. Step 1: Understand Cloud Run revisions

    Cloud Run automatically creates a new revision when deploying to an existing service, keeping previous revisions available for testing via revision-specific invocations or traffic splitting.
  2. Step 2: Deploy new version with the same service name and region

    This leverages Cloud Run's built-in revision management to keep both versions accessible.
  3. Final Answer:

    Deploy the new version with the same service name and region -> Option A
  4. Quick Check:

    Same service = new revision, old available for testing [OK]
Hint: Deploy to same service name to use revisions for multi-version testing [OK]
Common Mistakes:
  • Deploying to a different Google Cloud project unnecessarily
  • Using 'gcloud run update' incorrectly for image changes
  • Deploying with a different service name when revisions suffice
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard