Share
Embed
Serverless landscape in Google Cloud
An overview of Cloud Functions, Knative, Cloud Run, Eventarc and Workflows on Google Cloud
Mete Atamel
July 21, 2021
Transcript
49 slides
-
Mete Atamel Developer Advocate at Google @meteatamel atamel.dev speakerdeck.com/meteatamel Serverless
landscape in Google Cloud Cloud Functions, Knative/Cloud Run, Eventarc, Workflows -
Operational Model Programming Model No Infra Management Managed Security Pay
only for usage Service-based Event-driven Stateless Serverless -
Proprietary + Confidential Virtual Machine OS Application Runtime Application Virtual
Hardware Dedicated Server OS Application Runtime Application Virtual Hardware Containers OS Application Runtime Application Virtual Hardware Functions Functions Functions Application Platform OS Application Runtime Application Virtual Hardware Functions Serverless OS Application Runtime Application Virtual Hardware Functions Infrastructure Container Platform Function Compute Engine Kubernetes Engine App Engine Cloud Functions On-Premises Serverless Zone Physical Hardware Cloud Run -
Serverless functions Serverless web applications Serverless HTTP workloads Source code-based
event driven functions Source code-based web apps and API backends HTTP containers fully managed HTTP containers on GKE cluster Cloud Functions App Engine Cloud Run Serverless compute options -
Proprietary + Confidential Functions that trigger in response to events
Microservices Cloud "glue" Certain versions of Node.js, Python, Go, Java, .NET, PHP, Ruby Cloud Functions -
Cloud Functions as cloud glue Access 20+ Google services from
GCF Cloud Storage Cloud Pub/Sub HTTPS Firebase Cloud Scheduler Cloud Tasks -
Certain versions of Node.js, Python, Go, Java, .NET, PHP, Ruby
• Node.js 10, 12, 14 • Python 3.7, 3.8, 3.9 • Go 1.11, 1.13 • Java 11 • .NET Core 3.1 • PHP 7.4 • Ruby 2.6, 2.7 -
What to do when Cloud Functions runtime is not enough?
Create your own runtime! -
Containers Any language Any library Ecosystem around containers .js .rb
.go .py .sh … 0 1 0 1 0 0 1 1 1 -
Cloud Run Fully managed, deploy your workloads and don’t see
the cluster. Cloud Run on Anthos Deploy into Anthos, run serverless side-by-side with your existing workloads. Knative Everywhere Use the same APIs and tooling anywhere you run Kubernetes with Knative. Serverless containers with Knative and Cloud Run -
Knative Stack Serving Eventing Kubernetes Platform Products Components Cloud Run
Cloud Run on Anthos Gateway Kourier Istio -
Confidential & Proprietary Knative Serving What is it? Rapid deployment
of serverless containers Automatic (0-n) scaling Configuration and revision management Traffic splitting between revisions -
Confidential & Proprietary Knative Serving Knative Service High level abstraction
for the application Configuration Current/desired state of an application Code & configuration separated (a la 12-factor) Revision Point in time snapshots for your code and configuration Route Maps traffic to revisions -
Container to production in seconds Natively Serverless One experience, where
you want it Cloud Run (Managed Knative Serving on Google Cloud) -
Confidential & Proprietary Knative Eventing What is it? For loosely
coupled, event-driven services A number of different delivery methods Scales from just few events to live streams Uses standard CloudEvents Delivery Methods Event source(s) Event type(s) Event consumer(s) -
Confidential & Proprietary Knative Eventing Namespace subscribe Trigger Service (Callable)
Broker Trigger Service (Callable) subscribe filter= filter= ✓ ✓ ✓ Source Events Source Events ingress ingress publish -
Confidential & Proprietary CloudEvents - cloudevents.io { "specversion": "1.0", "type":
"com.github.pull.create", "source": "https://github.com/cloudevents/spec/pull/123", "id": "A234-1234-1234", "time": "2019-04-08T17:31:00Z", "datacontenttype": "application/json", "data": "{ GitHub Payload... }" } FTP GitHub GCS Broker FTP Receive Adapter GitHub Receive Adapter GCS Receive Adapter CloudEvent -
Confidential & Proprietary Event Sources Name Description Apache Camel Allows
to use Apache Camel components for pushing events into Knative Apache Kafka Brings Apache Kafka messages into Knative AWS SQS Brings AWS Simple Queue Service messages into Knative Cron Job Uses an in-memory timer to produce events on the specified Cron schedule. GCP PubSub Brings GCP PubSub messages into Knative GitHub Brings GitHub organization/repository events into Knative GitLab Brings GitLab repository events into Knative. Google Cloud Scheduler Google Cloud Scheduler events in Knative when jobs are triggered Google Cloud Storage Brings Google Cloud Storage bucket/object events into Knative Kubernetes Brings Kubernetes cluster/infrastructure events into Knative https://github.com/knative/docs/tree/master/docs/eventing/sources -
Confidential & Proprietary Knative GCP Project - github.com/google/knative-gcp Easy configuration
and consumption of Google Cloud Platform events in Knative Ready to use event sources: 1. CloudPubSubSource 2. CloudStorageSource 3. CloudSchedulerSource 4. CloudAuditLogsSource 5. CloudBuildSource -
A new way of getting events to managed Cloud Run
services Private alpha since early 2020 as Events for Cloud Run Public GA as Eventarc in J Public GA as Eventarc in Jan 2021 Inspired by Knative Eventing an 202 Eventarc -
Create a trigger for new object creation in Cloud Storage
→ Cloud Run gcloud eventarc triggers create trigger-auditlog \ --destination-run-service=${SERVICE_NAME} \ --destination-run-region=${REGION} --event-filters="type=google.cloud.audit.log.v1.written" \ --event-filters="serviceName=storage.googleapis.com" \ --event-filters="methodName=storage.objects.create" \ --service-account=${PROJECT_NO}[email protected] gcloud - AuditLog -
Create a trigger for Pub/Sub messages → Cloud Run gcloud
eventarc triggers create trigger-pubsub \ --destination-run-service=${SERVICE_NAME} \ --destination-run-region=${REGION} --event-filters="type=google.cloud.pubsub.topic.v1.messagePublished" --transport-topic=projects/${PROJECT_ID}/topics/${TOPIC_ID} gcloud - Pub/Sub -
POST / HTTP/1.1 Content-Type: application/json; charset=utf-8 Content-Length: 33 ce-specversion: 1.0
ce-type: google.cloud.pubsub.topic.publish ce-time: 2020-09-05T03:56:24Z ce-id: 1234-1234-1234 ce-source: mycontext/subcontext custom-attr: 42 { "message": "Hello Cloud Next!" } CloudEvents cloudevents.io "Data" "Context" -
Cloud Run Service CloudEvent SDK HTTP request → CloudEvent in
your language cloudevents.io Google Events Library Type library for CloudEvent#data Event parsing libraries HTTP POST to Cloud Run URL HTTP body is a CloudEvent with event data ("binary" CloudEvent V1) (optional) (optional) github.com/googleapis/google-cloudevents -
const { HTTP } = require("cloudevents"); const {toLogEntryData} = require('@google/events/cloud/audit/v1/LogEntryData')
app.post('/', async (req, res) => { // Read CloudEvent using CloudEvents SDK const cloudEvent = HTTP.toEvent({ headers: req.headers, body: req.body }); // Read AuditLog using Google.Events library for Node.js const logEntryData = toLogEntryData(cloudEvent.data); // Extract bucket and objectName const tokens = logEntryData.protoPayload.resourceName.split('/'); const bucket = tokens[3]; const objectName = tokens[5]; -
using CloudNative.CloudEvents; using Google.Events; using Google.Events.Protobuf.Cloud.PubSub.V1; public async Task<CloudEvent> Read(HttpContext
context) { // Read CloudEvent using CloudEvents SDK var cloudEvent = await context.Request.ReadCloudEventAsync(); // Read Pub/Sub message using Google.Events library for .NET var messagePublishedData = CloudEventConverters.ConvertCloudEventData<MessagePublishedData>(cloudEvent); // Extract the Pub/Sub message var pubSubMessage = messagePublishedData.Message; -
Choreography (event-driven) Event-driven services Frontend App Engine Order request Payment
Processor Cloud Run Authorize & charge CC Shipper Cloud Functions Prepare & ship items Notifier Cloud Run Notify user Message Broker -
Choreography: Pros and Cons Pros ➕ Services are loosely coupled
➕ Services can be changed / scaled independently ➕ No single point of failure ➕ Events are useful to extend the system Cons ➖ Difficult to monitor the whole system ➖ Errors / retries / timeouts are hard ➖ The business flow is not captured explicitly ➖ Who ensures the whole transaction is successful? -
Orchestration Orchestrated services Frontend App Engine Order request Payment Processor
Cloud Run Authorize & charge CC Shipper Cloud Functions Prepare & ship items Notifier Cloud Run Notify user Orchestrator -
Orchestration: Pros and Cons Pros ➕ Business flow captured centrally
and source controlled ➕ Each step can be monitored ➕ Errors / retries / timeouts are centralized ➕ Use simple REST, no need for events ➕ Services are still independent Cons ➖ A new orchestrator service to worry about ➖ Orchestrator could be a single point of failure ➖ Reliance on REST means more tight-coupling -
It depends... Choreography Services are not closely related Services can
exist in different bounded contexts Orchestration Can you describe the business logic in a flow chart? Are services closely related? Do you want to stay in REST? -
Serverless Compute External API’s Google API’s etc... Workflows - orchestrate
& integrate SaaS API’s Private API’s Other Clouds -
- processPayment: call: http.post args: url: https://payment-processor.run.app/... body: input: ${paymentDetails}
result: processResult - shipItems: call: http.post args: url: https://.../cloudfunctions.net/ship body: input: ${processResult.body} result: shipResult - notifyUser: call: http.post ... Payment Processor Cloud Run Authorize & charge CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items YAML or JSON syntax -
Payment Processor Cloud Run Authorize & charge CC Notifier Cloud
Run Notify user Shipper Cloud Functions Prepare & ship items Payment Processor Cloud Run Authorize & charge CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items WAIT Payment Processor Cloud Run Authorize & charge CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items shipmentDetails userDetails Step Sequencing Serverless Pause Variable passing JSON Parsing Steps -
Errors and retries Payment Processor Cloud Run Authorize & charge
CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items MAX: 5 times BACKOFF Payment Processor Cloud Run Authorize & charge CC Notifier Cloud Run Notify user Shipper Cloud Functions Prepare & ship items Pager Cloud Run Escalate to support SUCCESS ERROR Configurable retries Configurable exception handling -
Conditionals and 3rd party calls Notifier Cloud Run Notify user
Shipper Cloud Functions Prepare & ship items Pager Cloud Run Escalate to support SUCCESS ERROR Out of Stock? No Request from the supplier Yes Read inventory Inventory DB Update inventory Inventory DB Supplier API -
Other useful features Subworkflows to encapsulate common reusable flows Connectors
ßeta to connect to other Google Cloud services & APIs -
Deploy, execute, manage workflows # Deploy a workflow gcloud workflows
deploy my-workflow --source=workflow.yaml # Execute a workflow gcloud workflows execute my-workflow # See the result gcloud workflows executions describe <your-execution-id> --workflow my-workflow