Building Serverless Applications with Google Cloud Run

Book description

Learn how to build a real-world serverless application in the cloud that's reliable, secure, maintainable, and scalable. If you have experience building web applications on traditional infrastructure, this hands-on guide shows you how to get started with Cloud Run, a container-based serverless product on Google Cloud.

Through the course of this book, you'll learn how to deploy several example applications that highlight different parts of the serverless stack on Google Cloud. Combining practical examples with fundamentals, this book will appeal to developers who are early in their learning journey as well as experienced practitioners.

  • Build a serverless application with Google Cloud Run
  • Learn approaches for building containers with (and without) Docker
  • Explore Google Cloud's managed relational database: Cloud SQL
  • Use HTTP sessions to make every user's experience unique
  • Explore identity and access management (IAM) on Cloud Run
  • Provision Google Cloud resources using Terraform
  • Learn how to handle background task scheduling on Cloud Run
  • Move your service from Cloud Run to Knative Serving with little effort

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Preface
    1. Why I Wrote This Book
    2. Who This Book Is For
    3. Why Use the Go Language?
    4. Navigating This Book
    5. Conventions Used in This Book
    6. Using Code Examples
    7. O’Reilly Online Learning
    8. How to Contact Us
    9. Acknowledgments
  3. 1. Introduction
    1. Serverless Applications
      1. A Simple Developer Experience
      2. Autoscalable Out of the Box
      3. A Different Cost Model
      4. Serverless Is Not Functions as a Service
    2. Google Cloud
    3. Serverless on Google Cloud
    4. Cloud Run
      1. Service
      2. Container Image
      3. Scalability and Self-Healing
      4. HTTPS Serving
      5. Microservices Support
      6. Identity, Authentication, and Access Management
      7. Monitoring and Logging
      8. Transparent Deployments
      9. Pay-Per-Use
    5. Concerns About Serverless
      1. Unpredictable Costs
      2. Hyper-Scalability
      3. When Things Go Really Wrong
      4. Separation of Compute and Storage
      5. Open Source Compatibility
    6. Summary
  4. 2. Understanding Cloud Run
    1. Getting Started with Google Cloud
      1. Costs
      2. Interacting with Google Cloud
      3. Google Cloud Projects
      4. Installing and Authenticating the SDK
      5. Installing Beta Components
    2. Deploying Your First Service
      1. Deploying the Sample Container
      2. Region
      3. Structure of the HTTPS Endpoint
      4. Viewing Your Service in the Web Console
      5. Deploying a New Version
      6. Revision
    3. Understanding Cloud Run
      1. Container Life Cycle
      2. CPU Throttling
      3. Task Scheduling and Throttling
      4. Load Balancer and Autoscaler
      5. Concurrent Request Limit
      6. Autoscaler
      7. Tuning the Concurrency Setting
      8. Cold Starts
      9. Disposable Containers
      10. In-Memory Filesystem
      11. Ready for Requests
      12. Cloud Run Key Points
    4. Choosing a Serverless Compute Product on Google Cloud
      1. Cloud Functions: Glue Code
      2. App Engine: Platform as a Service
      3. Key Differences
      4. What Will the Future Look Like?
    5. Summary
  5. 3. Building Containers
    1. Containers: A Hands-On Exploration
      1. Running an Interactive Shell
      2. Overriding the Default Command
      3. Running a Server
    2. Containers from First Principles
      1. Inside a Container Image
      2. The Linux Kernel
      3. Container Isolation
      4. Starting a Container
      5. Building a Container with Docker
      6. Dockerfile Instructions
      7. Installing Additional Tooling
      8. Smaller Is Better When Deploying to Production
      9. Creating Small Containers with Distroless
    3. Artifact Registry
      1. Building and Tagging the Container Image
      2. Authenticating and Pushing the Container Image
    4. Building a Container Without a Dockerfile
      1. Go Containers with ko
      2. Java Containers with Jib
      3. Cloud Native Buildpacks
    5. Cloud Build
      1. Remote Docker Build
      2. Advanced Builds
      3. Running Arbitrary Programs
      4. Connecting with Version Control
    6. Shutting Down
    7. Summary
  6. 4. Working with a Relational Database
    1. Introducing the Demo Application
      1. Creating the Cloud SQL Instance
      2. Understanding Cloud SQL Proxy
      3. Connecting and Loading the Schema
      4. Securing the Default User
      5. Connecting Cloud Run to Cloud SQL
      6. Disabling the Direct Connection
      7. Deploying the Demo Application
      8. Connection String
      9. Public and Private IP
    2. Limiting Concurrency
      1. Transaction Concurrency
      2. Resource Contention
      3. Scaling Boundaries and Connection Pooling
      4. External Connection Pool
      5. A Real-World Example
    3. Cloud SQL in Production
      1. Monitoring
      2. Automatic Storage Increase
      3. High Availability
      4. Making Your Application Resilient Against Short Downtime
    4. Shutting Down
    5. Summary
  7. 5. Working with HTTP Sessions
    1. How HTTP Sessions Work
    2. Storing Sessions in Memorystore: A Hands-On Exploration
      1. Creating a Memorystore Instance
      2. What Is a VPC Connector?
      3. Creating a VPC Connector
      4. Deploying the Demo App
    3. Alternative Session Stores
    4. Session Affinity
      1. Use Cases
      2. Session Affinity Is Not for Session Data
    5. Shutting Down
    6. Summary
  8. 6. Service Identity and Authentication
    1. Cloud IAM Fundamentals
      1. Roles
      2. Policy Binding
      3. Service Accounts
      4. Creating and Using a New Service Account
    2. Sending Authenticated Requests to Cloud Run
      1. Deploying a Private Service
      2. Using an ID Token to Send Authenticated Requests
      3. When Is an ID Token Valid?
    3. Programmatically Calling Private Cloud Run Services
      1. Google Frontend Server
      2. A Story About Inter-Service Latency
    4. Demo Application
      1. Embedded Read-Only SQL Database
      2. Running Locally
      3. Edit, Compile, Reload
      4. Deploying to Cloud Run
      5. Update the Frontend Configuration
      6. Add Custom Service Accounts
      7. Add IAM Policy Binding
    5. Summary
  9. 7. Task Scheduling
    1. Cloud Tasks
    2. Hands-On Learning: A Demo Application
      1. Building the Container Images
      2. Creating a Cloud Tasks Queue
      3. Creating Service Accounts
      4. Deploying the Worker Service
      5. Deploying the Task App Service
      6. Connecting the Task Queue
      7. Scheduling a Task with the Cloud Tasks Client Library
      8. Automatic ID Token
      9. Connecting the Worker
      10. Test the App
    3. Queue Configuration
      1. Retry Configuration
      2. Rate Limiting
      3. Viewing and Updating Queue Configuration
    4. Considerations
      1. Cloud Tasks Might Deliver Your Request Twice
      2. Local Development
      3. Alternatives
    5. Summary
  10. 8. Infrastructure as Code Using Terraform
    1. What Is Infrastructure as Code?
    2. Why Infrastructure as Code?
    3. Serverless Infrastructure
      1. How It Works
      2. When Not to Use Infrastructure as Code
    4. Terraform
      1. Installing Terraform
      2. Getting Started with a Minimal Example
    5. The Terraform Workflow
      1. Change with Terraform: Adding the Access Policy
      2. Expressing Dependencies with References
    6. Supplemental Resources
    7. Summary
  11. 9. Structured Logging and Tracing
    1. Logging on Cloud Run
      1. Viewing Logs in the Web Console
      2. Viewing Logs in the Terminal
      3. Finding Invisible Logs
      4. Plain-Text Logs Leave You Wanting More
      5. Demo Application
    2. Structured Logging
      1. Client Libraries
      2. Structured Logging in Other Languages
      3. How to Use Log Levels
      4. Capturing Panics
      5. Local Development
    3. Request Context
    4. Trace Context
      1. Forwarding Trace ID
      2. Preparing All Incoming Requests with the Trace ID
      3. Passing Request Context to Outgoing Requests
      4. Viewing Trace Context in Cloud Logging
      5. Additional Resources About Tracing
    5. Log-Based Metrics with Cloud Monitoring
    6. Summary
  12. 10. Cloud Run and Knative Serving
    1. What Is Knative Serving?
      1. Cloud Run Is Not Managed Knative Serving
      2. Knative Serving on Google Cloud
    2. Understanding Kubernetes
      1. API Server
      2. Kubernetes Resources
      3. Database
      4. Controllers
      5. Adding Extensions to Kubernetes
    3. Running Knative Serving Locally
      1. Running a Local Kubernetes Cluster
      2. Installing Minikube and kubectl
      3. Starting Your Local Cluster
      4. Install the Knative Operator
      5. Starting Minikube Tunnel
      6. Installing an HTTP Load Balancer
      7. Configuring DNS
      8. Deploying a Service
      9. Deploying the Same Service to Cloud Run
      10. Alternative API Clients
    4. Shutting Down
    5. Discussion
      1. Serving
      2. Moving from Kubernetes to Cloud Run Is Harder
      3. Service Identity and Authentication
      4. Proprietary Managed Services
    6. Summary
  13. Index

Product information

  • Title: Building Serverless Applications with Google Cloud Run
  • Author(s): Wietse Venema
  • Release date: December 2020
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492057093