Learn how to deploy a service in Cloud Run for Anthos on Google Cloud and then create and send events to that service from Pub/Sub.
Before you begin
To complete this task, you must have an events broker and know in which namespace it's running. Learn how to configure Events for Cloud Run for Anthos and create an events broker.
If you have an events broker running, you can view the Kubernetes namespace by running:
kubectl get brokers -n NAMESPACE
Deploying a Cloud Run for Anthos event receiver service
Deploy a Cloud Run for Anthos service that receives events from the event broker.
To deploy the Cloud Run for Anthos service:
Clone the repository:
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git cd python-docs-samples/eventarc/pubsub
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git cd java-docs-samples/eventarc/pubsub
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git cd golang-samples/eventarc/pubsub
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd nodejs-docs-samples/eventarc/pubsub
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git cd dotnet-docs-samples/eventarc/pubsub
Build the container and upload it to Cloud Build:
gcloud builds submit \ --tag gcr.io/$(gcloud config get-value project)/CLOUD_RUN_CONTAINER_FILENAME
where CLOUD_RUN_CONTAINER_FILENAME is the filename of your container.
Deploy the container image to Cloud Run for Anthos:
gcloud run deploy CLOUD_RUN_SERVICE_NAME \ --namespace=NAMESPACE \ --image gcr.io/$(gcloud config get-value project)/CLOUD_RUN_CONTAINER_NAME
where NAMESPACE is the same namespace as your events broker.
When you see the service URL, it has been successfully deployed.
Creating a trigger for Pub/Sub
Create a Pub/Sub topic:
gcloud pubsub topics create TOPIC_NAME
Create a trigger with
--parameters topic=
TOPIC_NAME and--target-service=
CLOUD_RUN_SERVICE_NAME:gcloud beta events triggers create TRIGGER_NAME \ --namespace NAMESPACE \ --target-service=CLOUD_RUN_SERVICE_NAME \ --type=google.cloud.pubsub.topic.v1.messagePublished \ --source=CloudPubSubSource \ --parameters topic=TOPIC_NAME
where NAMESPACE is the same namespace as your events broker, TOPIC-NAME is the name of the Pub/Sub topic you created, and CLOUD_RUN_SERVICE_NAME is the name of the event receiver service on Cloud Run for Anthos.
Optional Verify the trigger is operational using the
gcloud
command-line tool:gcloud beta events triggers list \ --target-service 'CLOUD_RUN_SERVICE_NAME' \ --namespace NAMESPACE
where NAMESPACE is the same namespace as your events broker.
The output is similar to the following:
TRIGGER EVENT TYPE TARGET trigger-name google.cloud.pubsub.topic.v1.messagePublished cloud-run-service
Generating an event
Publish a message to the Pub/Sub topic:
gcloud pubsub topics publish TOPIC_NAME \ --message "World"
Verify the published event by looking at the logs of the receiving Cloud Run for Anthos service:
kubectl logs \ --selector serving.knative.dev/service=CLOUD_RUN_SERVICE_NAME \ -c user-container \ -n NAMESPACE \ --tail=100
where NAMESPACE is the same namespace as your events broker.
In the logs you will find an entry similar to the following:
Hello World! ID: 961750303502725
Clean up
Delete the resources created in this tutorial to avoid recurring charges.
Delete the trigger, type:
gcloud beta events triggers delete TRIGGER-NAME \ --namespace NAMESPACE
where NAMESPACE is the same namespace as your events broker.