Slide 1

Slide 1 text

Mete Atamel Developer Advocate at Google @meteatamel atamel.dev speakerdeck.com/meteatamel Serverless landscape in Google Cloud Cloud Functions, Knative/Cloud Run, Eventarc, Workflows

Slide 2

Slide 2 text

Operational Model Programming Model No Infra Management Managed Security Pay only for usage Service-based Event-driven Stateless Serverless

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Cloud Functions as cloud glue Access 20+ Google services from GCF Cloud Storage Cloud Pub/Sub HTTPS Firebase Cloud Scheduler Cloud Tasks

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

What to do when Cloud Functions runtime is not enough? Create your own runtime!

Slide 9

Slide 9 text

Containers Any language Any library Ecosystem around containers .js .rb .go .py .sh … 0 1 0 1 0 0 1 1 1

Slide 10

Slide 10 text

Containers Flexibility Serverless Velocity

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Knative Stack Serving Eventing Kubernetes Platform Products Components Cloud Run Cloud Run on Anthos Gateway Kourier Istio

Slide 13

Slide 13 text

Confidential & Proprietary Knative Serving What is it? Rapid deployment of serverless containers Automatic (0-n) scaling Configuration and revision management Traffic splitting between revisions

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Container to production in seconds Natively Serverless One experience, where you want it Cloud Run (Managed Knative Serving on Google Cloud)

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

Confidential & Proprietary Knative Eventing Namespace subscribe Trigger Service (Callable) Broker Trigger Service (Callable) subscribe filter= filter= ✓ ✓ ✓ Source Events Source Events ingress ingress publish

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

BigQuery Processing Pipeline - Knative github.com/meteatamel/knative-tutorial/blob/master/docs/image-processing-pipeline.md

Slide 22

Slide 22 text

What about Knative Eventing on Google Cloud?

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Eventarc - Vision

Slide 25

Slide 25 text

Eventarc - Current State

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Cloud Console - Trigger section of Cloud Run

Slide 29

Slide 29 text

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"

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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];

Slide 32

Slide 32 text

using CloudNative.CloudEvents; using Google.Events; using Google.Events.Protobuf.Cloud.PubSub.V1; public async Task 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(cloudEvent); // Extract the Pub/Sub message var pubSubMessage = messagePublishedData.Message;

Slide 33

Slide 33 text

BigQuery Processing Pipeline - Cloud Run + Eventarc github.com/GoogleCloudPlatform/eventarc-samples/tree/main/processing-pipelines/bigquery

Slide 34

Slide 34 text

Choreography vs Orchestration in microservices

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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?

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Which one is better?

Slide 40

Slide 40 text

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?

Slide 41

Slide 41 text

Serverless Compute External API’s Google API’s etc... Workflows - orchestrate & integrate SaaS API’s Private API’s Other Clouds

Slide 42

Slide 42 text

- 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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

Other useful features Subworkflows to encapsulate common reusable flows Connectors ßeta to connect to other Google Cloud services & APIs

Slide 47

Slide 47 text

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 --workflow my-workflow

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

@meteatamel speakerdeck.com/meteatamel github.com/meteatamel/knative-tutorial github.com/meteatamel/cloudrun-tutorial github.com/GoogleCloudPlatform/eventarc-samples github.com/GoogleCloudPlatform/workflows-demos Thank you!