Part-43: How to Configure Cloud Run Jobs, Cloud Scheduler to trigger Screenshot job in GCP




Cloud Run Jobs



Step-01: Introduction

Demo-1: Create very basic Cloud Run Job

Demo-2: Create a advanced Cloud Run Job demo

  • Uses Cloud IAM
  • Uses Cloud Storage Buckets
  • Create demo using gcloud commands
  • Also Schedule the Job using Triggers



Step-02: Create Cloud Run Job

r1

  • Go to Cloud Run -> Create Job
  • Container Image URL: stacksimplify/google-cloud-run-job-demo1:1.0.0
  • Job Name: google-cloud-run-job-demo1
  • Region: us-central1
  • Number of Tasks: 10
  • REST ALL LEAVE TO DEFAULTS
  • Click on Create

# Docker Images
stacksimplify/google-cloud-run-job-demo1:1.0.0




Step-03: Review and Execute Job

  • Go to Cloud Run -> Jobs -> google-cloud-run-job-demo1
  • Click on Execute
  • Review Tasks and Logs

r2

r3




Step-04: Update Job

  • Click on EDIT to update job

r4

r5




Step-05: Screenshot Job

  • Google Service we use
  • IAM Service Account
  • Cloud Storage Buckets
  • Cloud Run Jobs



Step-06: gcloud: Setup Project and Region

# Project Config
PROJECT_ID=gcp-zero-to-hero-468909
REGION=us-central1
gcloud config set core/project $PROJECT_ID
gcloud config set run/region $REGION
Enter fullscreen mode

Exit fullscreen mode




Step-07: gcloud: Create IAM Service Account and Associate Cloud Storage Admin role

# Create IAM Service Account for a Job to upload screenshots to Cloud Storage 
gcloud iam service-accounts create screenshot-sa --display-name="Screenshot app service account"

# Grant the service account permissions to access Cloud Storage
gcloud projects add-iam-policy-binding $PROJECT_ID \
  --role roles/storage.admin \
  --member serviceAccount:screenshot-sa@$PROJECT_ID.iam.gserviceaccount.com 
Enter fullscreen mode

Exit fullscreen mode




Step-08: Create Cloud Run Jobs

# Command Help
gcloud run jobs --help

# Create Cloud Run Job
gcloud run jobs create screenshot-demo2 \
  --image=stacksimplify/google-cloud-run-job-demo2-screenshot:1.0.0 \
  --args="https://dev.to/latchudevops" \
  --args="https://github.com/kohlidevops" \
  --tasks=2 \
  --task-timeout=5m \
  --set-env-vars=BUCKET_NAME=screenshot-$PROJECT_ID \
  --service-account=screenshot-sa@$PROJECT_ID.iam.gserviceaccount.com

# List Cloud Run Jobs
gcloud run jobs list

# Review Container Arguments on Cloud Run console
1. Go to Cloud Run -> Jobs -> screenshot-demo2 -> EDIT
2. Verify "Container Arguments" section
Enter fullscreen mode

Exit fullscreen mode

r6

r7




Step-09: gcloud: Execute Cloud Run Job and Verify

# Cloud Run Jobs - Execute Job
gcloud run jobs execute JOB-NAME
gcloud run jobs execute screenshot-demo2

# Describe Job Execution
gcloud run jobs executions describe 
gcloud run jobs executions describe screenshot-demo2-w4gtf

# Describe Task from a Job
gcloud run jobs executions tasks describe screenshot-demo2-w4gtf-task0
gcloud run jobs executions tasks describe screenshot-demo2-w4gtf-task1
Enter fullscreen mode

Exit fullscreen mode

r8

If you go and check the buckets > image > screenshot should be available

r9




Step-10: gcloud: Update Cloud Run Job

# Update Job
gcloud run jobs update screenshot-demo2 \
  --args="https://github.com/kohlidevops/DevOpswithHelm/tree/main" \
  --tasks=1

# Execute Job
gcloud run jobs execute screenshot-demo2
Enter fullscreen mode

Exit fullscreen mode




Step-11: Schedule a Job

# Enable Cloud Scheduler
gcloud services enable cloudscheduler.googleapis.com

# ADD SCHEDULE TRIGGER
- Go to Cloud Run -> Jobs -> screenshot-demo2 -> TRIGGERS
- Name: screenshot-demo2-scheduler-trigger
- Region: us-central1
- Frequency: */5 * * * *  (Runs every 5 minutes)
- Timezone: Select accordingly
- Click on CONTINUE
- Service Account: Compute Engine default service account
- CREATE

# WAIT FOR 10MINUTES
- Verify if the jobs executed automatically
Enter fullscreen mode

Exit fullscreen mode

r10

Even you can check with Cloud Scheduler

r11


Cleanup



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *