This sample shows how to configure the Cloud Pub/Sub Pull Subscription. This event source is most useful as a bridge from other Google Cloud services from a Pub/Sub topic, such as Cloud Storage, IoT Core and Cloud Scheduler.
-
Create a Google Cloud project and install the
gcloud
CLI and rungcloud auth login
. This sample will use a mix ofgcloud
andkubectl
commands. The rest of the sample assumes that you've set the$PROJECT_ID
environment variable to your Google Cloud project id, and also set your project ID as default usinggcloud config set project $PROJECT_ID
. -
Enable the
Cloud Pub/Sub API
on your project:gcloud services enable pubsub.googleapis.com
-
Create a Google Cloud Service Account. This sample creates one service account for both registration and receiving messages, but you can also create a separate service account for receiving messages if you want additional privilege separation.
-
Create a new service account named
cloudrunevents-pullsub
with the following command:gcloud iam service-accounts create cloudrunevents-pullsub
-
Give that Service Account the
Pub/Sub Editor
role on your Google Cloud project:gcloud projects add-iam-policy-binding $PROJECT_ID \ --member=serviceAccount:cloudrunevents-pullsub@$PROJECT_ID.iam.gserviceaccount.com \ --role roles/pubsub.editor
-
Download a new JSON private key for that Service Account. Be sure not to check this key into source control!
gcloud iam service-accounts keys create cloudrunevents-pullsub.json \ --iam-account=cloudrunevents-pullsub@$PROJECT_ID.iam.gserviceaccount.com
-
Create a secret on the kubernetes cluster with the downloaded key:
# The secret should not already exist, so just try to create it. kubectl --namespace default create secret generic google-cloud-key --from-file=key.json=cloudrunevents-pullsub.json
google-cloud-key
andkey.json
are pre-configured values inpullsubscription.yaml
.
-
-
Create a Cloud Pub/Sub Topic.
export TOPIC_NAME=testing gcloud pubsub topics create $TOPIC_NAME
-
Update
TOPIC_NAME
in thepullsubscription.yaml
and apply it.If you're in the pullsubscription directory, you can replace
TOPIC_NAME
and apply in one command:sed "s/\TOPIC_NAME/$TOPIC_NAME/g" pullsubscription.yaml | \ kubectl apply --filename -
If you are replacing
TOPIC_NAME
manually, then make sure you apply the resulting YAML:kubectl apply --filename pullsubscription.yaml
-
[Optional] If not using GKE, or want to use a Pub/Sub topic from another project, uncomment and replace the
MY_PROJECT
placeholder inpullsubscription.yaml
and apply it.If you're in the pullsubscription directory, you can replace
MY_PROJECT
andTOPIC_NAME
and then apply in one command:sed "s/\TOPIC_NAME/$TOPIC_NAME/g" pullsubscription.yaml | \ sed "s/\#project: MY_PROJECT/project: $PROJECT_ID/g" | \ kubectl apply --filename -
If you are replacing
MY_PROJECT
manually, then make sure you apply the resulting YAML:kubectl apply --filename pullsubscription.yaml
-
Create a service that the Pub/Sub Subscription will sink into:
kubectl apply --filename event-display.yaml
Publish messages to your Cloud Pub/Sub Topic:
gcloud pubsub topics publish testing --message='{"Hello": "world"}'
We will verify that the published message was sent by looking at the logs of the service that this PullSubscription sinks to.
-
We need to wait for the downstream pods to get started and receive our event, wait 60 seconds.
-
You can check the status of the downstream pods with:
kubectl get pods --selector app=event-display
You should see at least one.
-
-
Inspect the logs of the service:
kubectl logs --selector app=event-display -c user-container
You should see log lines similar to:
☁️ cloudevents.Event
Validation: valid
Context Attributes,
specversion: 0.2
type: google.pubsub.topic.publish
source: //pubsub.googleapis.com/PROJECT_ID/topics/TOPIC_NAME
id: 9f9b0968-a15f-4e74-ac58-e8a1c4fa587d
time: 2019-06-10T17:52:36.73Z
contenttype: application/json
Data,
{
"Hello": "world"
}
For more information about the format of the Data,
second of the message, see
the data
field of
PubsubMessage documentation.
For more information about CloudEvents, see the HTTP transport bindings documentation.
The Pub/Sub PullSubscription implements what Knative Eventing considers to be a
source
. This component can work alone, but it also works well when
Knative Serving and Eventing are installed in
the cluster.
- Delete the Pub/Sub PullSubscription:
If you're in the pullsubscription directory, you can replace TOPIC_NAME
and
delete in one command:
sed "s/\TOPIC_NAME/$TOPIC_NAME/g" pullsubscription.yaml | \
kubectl delete --filename -
If you are replaced TOPIC_NAME
manually, then make sure you delete the
resulting YAML:
kubectl apply --filename pullsubscription.yaml
- Delete the service used as the sink:
kubectl delete --filename event-display.yaml