Receive events using Pub/Sub messages (gcloud CLI)

This quickstart shows you how to set up a Google Kubernetes Engine (GKE) service as a destination to receive events from a Pub/Sub topic using Eventarc.

In this quickstart, you will:

  1. Create a GKE cluster.
  2. Set up a service account to pull events from Pub/Sub using an event forwarder component that forwards events to the target.
  3. Initialize GKE destinations in Eventarc.
  4. Deploy a GKE service that receives events.
  5. Create an Eventarc trigger that connects a Pub/Sub topic to the GKE service.
  6. Generate and view a Pub/Sub event.

Before you begin

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  6. Install and initialize the Google Cloud CLI. If prompted to configure a default compute region, enter n.
  7. Update gcloud components:
    gcloud components update
  8. Enable the Google Cloud, Cloud Build, Resource Manager, Google Kubernetes Engine API, Container Registry, and Eventarc APIs:
    gcloud services enable cloudapis.googleapis.com
    gcloud services enable cloudbuild.googleapis.com
    gcloud services enable cloudresourcemanager.googleapis.com
    gcloud services enable container.googleapis.com
    gcloud services enable containerregistry.googleapis.com
    gcloud services enable eventarc.googleapis.com
    
  9. Set the configuration variables used in this quickstart:
    gcloud config set project PROJECT_ID
    gcloud config set run/cluster events-cluster
    gcloud config set run/cluster_location us-central1
    gcloud config set run/platform gke
    gcloud config set eventarc/location us-central1
    Replace PROJECT_ID with your Google Cloud project ID.
  10. Optional: You can check the configuration settings using the gcloud_name by typing:
    gcloud config list
    The output should be similar to the following:
    [eventarc]
    location = us-central1
    [run]
    cluster = events-cluster
    cluster_location = us-central1
    platform = gke

Create a GKE cluster

Create a GKE cluster with the HttpLoadBalancing addon. Enable workload identity to access Google Cloud services from applications running within GKE.

PROJECT_ID=$(gcloud config get-value project)

gcloud beta container clusters create events-cluster \
  --addons=HttpLoadBalancing \
  --machine-type=n1-standard-4 \
  --enable-autoscaling --min-nodes=2 --max-nodes=10 \
  --no-issue-client-certificate --num-nodes=2  \
  --logging=SYSTEM,WORKLOAD \
  --monitoring=SYSTEM \
  --scopes=cloud-platform,logging-write,monitoring-write,pubsub \
  --zone us-central1 \
  --release-channel=rapid \
  --workload-pool=$PROJECT_ID.svc.id.goog

Wait for the cluster creation to complete. You can ignore the warnings during the creation process. Once the cluster is created, the output should be similar to the following:

Creating cluster events-cluster...done.
Created [https://container.googleapis.com/v1beta1/projects/my-project/zones/us-central1/clusters/events-cluster].

Where my-project is your Google Cloud project ID.

This creates a GKE cluster named events-cluster in my-project.

Set up a Google service account

Set up a user-provided service account and grant it specific roles so that the event forwarder component can pull events from Pub/Sub.

  1. Create a service account called TRIGGER_GSA that is used to create triggers:

    TRIGGER_GSA=eventarc-gke-triggers
    gcloud iam service-accounts create $TRIGGER_GSA
  2. Grant the pubsub.subscriber and monitoring.metricWriter roles to the service account:

    gcloud projects add-iam-policy-binding $PROJECT_ID \
      --member "serviceAccount:$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com" \
      --role "roles/pubsub.subscriber"
    
    gcloud projects add-iam-policy-binding $PROJECT_ID \
      --member "serviceAccount:$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com" \
      --role "roles/monitoring.metricWriter"

Enable GKE destinations

For each trigger that targets a GKE service, Eventarc creates an event forwarder component that pulls events from Pub/Sub and forwards it to the target. To create the component and manage resources in the GKE cluster, grant permissions to an Eventarc service account:

  1. Enable GKE destinations for Eventarc:

    gcloud eventarc gke-destinations init
    
  2. At the prompt to bind the required roles, enter y.

    The following roles are bound to the service account:

    • compute.viewer
    • container.developer
    • iam.serviceAccountAdmin

Create a GKE service destination

Using a prebuilt image, gcr.io/cloudrun/hello, deploy a GKE service that will receive and log events:

  1. Create a Kubernetes deployment:

    SERVICE_NAME=hello-gke
    kubectl create deployment $SERVICE_NAME \
    --image=gcr.io/cloudrun/hello
    
  2. Expose it as a Kubernetes service:

    kubectl expose deployment $SERVICE_NAME \
    --type LoadBalancer --port 80 --target-port 8080
    

Create a Pub/Sub trigger

When a message is published to the Pub/Sub topic, the Eventarc trigger sends messages to the hello-gke GKE service.

  1. Create a GKE trigger to listen for Pub/Sub messages:

    New Pub/Sub topic

    gcloud eventarc triggers create gke-trigger-pubsub \
      --destination-gke-cluster=events-cluster \
      --destination-gke-location=us-central1 \
      --destination-gke-namespace=default \
      --destination-gke-service=hello-gke \
      --destination-gke-path=/ \
      --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
      --service-account=$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com
    

    This creates a new Pub/Sub topic and a trigger for it called gke-trigger-pubsub.

    Existing Pub/Sub topic

    gcloud eventarc triggers create gke-trigger-pubsub \
      --destination-gke-cluster=events-cluster \
      --destination-gke-location=us-central1 \
      --destination-gke-namespace=default \
      --destination-gke-service=hello-gke \
      --destination-gke-path=/ \
      --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" \
      --service-account=$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com \
      --transport-topic=projects/PROJECT_ID/topics/TOPIC_ID
    

    Replace the following:

    • PROJECT_ID is your Google Cloud project ID.
    • TOPIC_ID is the ID of the existing Pub/Sub topic. This creates a trigger called gke-trigger-pubsub for the existing Pub/Sub topic.
  2. Confirm the trigger was successfully created:

    gcloud eventarc triggers list

    The output should be similar to the following:

    NAME: gke-trigger-pubsub
    TYPE: google.cloud.pubsub.topic.v1.messagePublished
    DESTINATION: GKE: hello-gke
    ACTIVE: Yes
    

Generate and viewing an event

You can generate an event to trigger the GKE service by publishing a message to the Pub/Sub topic. You can then view the message in the pod logs.

  1. Find and set the Pub/Sub topic, as an environment variable:

    TOPIC=$(gcloud eventarc triggers describe gke-trigger-pubsub --format='value(transport.pubsub.topic)')
    
  2. Send a message to the Pub/Sub topic to generate an event:

    gcloud pubsub topics publish $TOPIC --message="Hello World"
    

    The GKE service logs the event's message.

  3. To view the event message:

    1. Find the pod ID:
      kubectl get pods
      
      The output should be similar to the following:
      NAME                                         READY   STATUS             RESTARTS   AGE
      hello-gke-645964f578-2mjjt                   1/1     Running            0          35s
      Where NAME is the name of the pod. Copy the NAME to use in the next step.
    2. Check the logs of the pod:
      kubectl logs NAME
      Replace NAME with the name of the pod you copied.
    3. Look for a log entry similar to:
      2022/02/24 22:23:49 Hello from Cloud Run! The container started successfully and is listening for HTTP requests on $PORT
      {"severity":"INFO","eventType":"google.cloud.pubsub.topic.v1.messagePublished","message":"Received event of type google.cloud.pubsub.topic.v1.messagePublished. Event data: Hello World"[...]}
      

Clean up

While Cloud Run does not charge when the service is not in use, you might still be charged for storing the container image in Container Registry, Eventarc resources, Pub/Sub messages, and for the GKE cluster.

You can delete your image, delete the Pub/Sub topic, delete the Pub/Sub subscription, and delete the GKE cluster.

To delete the Eventarc trigger:

gcloud eventarc triggers delete gke-trigger-pubsub

Alternatively, you can delete your Google Cloud project to avoid incurring charges. Deleting your Cloud project stops billing for all the resources used within that project.

gcloud projects delete PROJECT_ID_OR_NUMBER

Replace PROJECT_ID_OR_NUMBER with the project ID or number.

What's next

Receive events using Cloud Audit Logs (Google Cloud CLI)