This page shows you how to set up Events for Cloud Run for Anthos and receive events from Cloud Storage.
In this quickstart, you:
- Create a GKE cluster and enable Cloud Run for Anthos on Google Cloud.
- Initialize Events for Cloud Run for Anthos.
- Create an events broker to route events from the source to receiver.
- Deploy a Cloud Run for Anthos service that receives events.
- Set up Cloud Storage to generate events.
- Generate and view an event.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Install and initialize the Cloud SDK.
- Make sure you are using the
latest version of the
gcloudcommand-line tool to use your GCP project:
gcloud config set project PROJECT-ID
- Install the
gcloudcommand-line tool beta components:
gcloud components install beta
- Enable services used in this quickstart:
gcloud services enable cloudapis.googleapis.com gcloud services enable container.googleapis.com gcloud services enable containerregistry.googleapis.com gcloud services enable cloudbuild.googleapis.com
gcloudcommand-line tool configuration variables:
gcloud config set run/cluster events-cluster gcloud config set run/cluster_location us-central1 gcloud config set run/platform gke
- Optional: You can check the configuration settings using the
gcloudcommand-line tool by typing:
gcloud config list
The output is similar to the following:
[run] cluster = events-cluster cluster_location = us-central1 platform = gke
Creating a GKE cluster with Cloud Run for Anthos enabled
Create a Google Kubernetes Engine cluster for Cloud Run for Anthos with the
gcloud beta container clusters create events-cluster \ --addons=HttpLoadBalancing,HorizontalPodAutoscaling,CloudRun \ --machine-type=n1-standard-4 \ --enable-autoscaling --min-nodes=3 --max-nodes=10 \ --no-issue-client-certificate --num-nodes=3 \ --enable-stackdriver-kubernetes \ --scopes=cloud-platform,logging-write,monitoring-write,pubsub \ --zone us-central1 \ --release-channel=rapid
Wait a couple of minutes for the cluster creation to complete. During the creation process, you might see warnings that you can safely ignore. When cluster creation has been completed, the output is similar to the following:
Creating cluster events-cluster...done. Created [https://container.googleapis.com/v1beta1/ \ projects/my-project/zones/us-central1/clusters/events-cluster].
my-projectis your project ID. You have just created a new Google Kubernetes Engine cluster named
events-clusterin the project
Initializing Events for Cloud Run for Anthos
To initialize Events for Cloud Run for Anthos.
Initialize Events for Cloud Run for Anthos for Cloud Run for Anthos using the
gcloud beta events init --platform gke
When prompted, select
events-clusteras the GKE cluster.
Yto all the prompts asking to create service accounts, bind them to specific roles, generating new keys, and enable necessary services.
This creates pods in two Kubernetes namespaces called
Optional: Check successful pod creation:
kubectl get pods -n cloud-run-events
The output is similar to the following:
NAME READY STATUS RESTARTS AGE controller-9cc679b67-2952n 1/1 Running 0 22s webhook-8576c4cfcb-dhz82 1/1 Running 0 16m
To check pods
kubectl get pods -n knative-eventing
The output is similar to the following:
NAME READY STATUS RESTARTS AGE eventing-controller-77f46f6cf8-kj9ck 1/1 Running 0 17m eventing-webhook-5bc787965f-hcmwg 1/1 Running 0 17m
Creating an event broker
In Events for Cloud Run for Anthos, brokers route events from the source to a destination service. In this quickstart you will create a broker that routes events from Cloud Storage to a Cloud Run for Anthos service.
To create a broker:
kubectl create namespace events
Initialize the namespace with the default secret:
gcloud beta events namespaces init events \ --copy-default-secret
Create the event broker:
gcloud beta events brokers create default \ --namespace events
Optional: After a few minutes, you can check the status of your broker by running:
kubectl get brokers \ -n events
Deploying a Cloud Run for Anthos service to receive events
Deploy a Cloud Run for Anthos service that receives events from the broker and logs it.
To deploy the Cloud Run for Anthos service:
Clone the repository:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git cd python-docs-samples/eventarc/audit-storage
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git cd java-docs-samples/eventarc/audit-storage
git clone https://github.com/GoogleCloudPlatform/golang-samples.git cd golang-samples/eventarc/audit_storage
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/eventarc/audit-storage
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git cd dotnet-docs-samples/eventarc/audit-storage
Build the container and upload it to Cloud Build:
gcloud builds submit \ --tag gcr.io/$(gcloud config get-value project)/events-quickstart-container
Deploy the container image to Cloud Run for Anthos:
gcloud run deploy events-quickstart-receiver \ --namespace=events \ --image gcr.io/$(gcloud config get-value project)/events-quickstart-container
When you see the service URL, it has successfully deployed.
Setting up Cloud Storage
Cloud Storage generates events when there are changes to the storage bucket and send them to the event broker. To set up Cloud Storage:
Create a Cloud Storage bucket using the
gsutil mb \ -p $(gcloud config get-value project) \ -l us-central1 \ gs://"events-quickstart-$(gcloud config get-value project)"
In this example, the Cloud Storage bucket is created in the us-central1 region.
Retrieve the Cloud Storage service account:
curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/storage/v1/projects/$(gcloud config get-value project)/serviceAccount"
Give the Cloud Storage service account publish rights to Pub/Sub:
gcloud projects add-iam-policy-binding $(gcloud config get-value project) \ --member=serviceAccount:CLOUD-STORAGE-SERVICE-ACCOUNT \ --role roles/pubsub.publisher
Creating a Cloud Storage trigger
When you create a trigger, you must specify the Cloud Run for Anthos service that receives events from Cloud Storage.
Create the Cloud Storage trigger:
gcloud beta events triggers create trigger-storage \ --namespace events \ --target-service events-quickstart-receiver \ --type=google.cloud.storage.object.v1.finalized \ --parameters bucket=events-quickstart-$(gcloud config get-value project)
This creates a trigger for your
events-quickstart-bucketCloud Storage bucket.
Optional: You can check the status of the trigger by running:
gcloud beta events triggers list \ --namespace=events
Note that while trigger creation is complete and ready, it can take up to 10 minutes for it to propagate and filter Cloud Storage events.
Generating and viewing an event
To generate an event, create a text file with the filename
random.txtcontaining the text "Hello World", and upload it to Cloud Storage using the
echo "Hello World" > random.txt gsutil cp random.txt gs://events-quickstart-$(gcloud config get-value project)/random.txt
Your upload generates an event and the Cloud Run for Anthos service logs the event's message.
To view the message in Cloud Logging:
Congratulations! You have successfully deployed Events for Cloud Run for Anthos, triggered an event and logged its contents.
To stop billing for all resources used within your test Cloud project, delete the project:
- In the Cloud Console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.