LuckyByte

Easily Scale Your Apps with Cloud Run

Want to build and deploy apps without worrying about the backend? Google Cloud Run is here to help. It's a simple, managed platform that lets you focus on coding while it handles the rest. Let's explore how Cloud Run can make your life easier.

What is Cloud Run?

Benefits You'll Love

  1. Scale Automatically

    Your app grows, Cloud Run grows with it.

  2. Secure by Default

    Enjoy top-notch security with easy access controls.

  3. Easy Monitoring

    Understand your app's performance at a glance.

  4. Pay Only for What You Use

    No surprise bills, just fair pricing.

Real-Life Uses of Cloud Run

Example: "Image Resizer" - A Serverless Image Processing Service

Scenario:

Solution:

Deploy an "Image Resizer" service on Cloud Run.

How it Works:

  1. Trigger:

    Whenever a new product image is uploaded to GreenEarth's Cloud Storage bucket.

  2. Cloud Run Service: "Image Resizer"
    • Containerized Application:
      • Written in Node.js using Sharp (image processing library).
      • Listens for new image uploads in the designated Cloud Storage bucket.
    • Functionality:
      1. Downloads the newly uploaded image.
      2. Resizes the image into three predefined dimensions (thumbnail, medium, large).
      3. Uploads the resized images back to a separate Cloud Storage bucket.
  3. Output:

    Three resized images for each uploaded product image, stored in Cloud Storage.

Cloud Run Configuration:

Benefits for GreenEarth:

Containers and Microservices

Containers

  • Definition:

    Lightweight and standalone executable packages that include everything an application needs to run (code, dependencies, libraries, settings).

  • Key Benefits:

    • Isolation:

      Run multiple containers simultaneously without conflicts.

    • Portability:

      Deploy containers across different environments (dev, staging, prod) without worries.

    • Efficiency:

      Optimize resource utilization (e.g., CPU, memory).

Microservices

  • Definition:

    Software development technique that structures an application as a collection of small, independent services.

  • Key Benefits:

    • Scalability:

      Scale individual services as needed.

    • Flexibility:

      Use different programming languages, databases, or frameworks for each service.

    • Resilience:

      If one service fails, others remain operational.

Containerization Examples

  • Netflix's Content Delivery Network (CDN):

    • Use Case:

      Netflix uses Docker containers to package and deploy its CDN servers globally, ensuring consistent and fast content delivery.

    • Benefits:

      Reduced deployment time from hours to minutes, increased scalability, and improved resource utilization.

  • PayPal's Payment Processing:

    • Use Case:

      PayPal utilizes containers to isolate and deploy multiple payment processing services, ensuring high availability and security.

    • Benefits:

      Improved fault tolerance, reduced latency, and enhanced security through isolation.

  • Spotify's Music Streaming:

    • Use Case:

      Spotify employs containers to deploy and manage its music streaming services, allowing for seamless scaling and updates.

    • Benefits:

      Increased agility, reduced costs, and improved user experience through faster deployment of new features.

Microservices Examples

  • Amazon's E-commerce Platform:

    • Use Case:

      Amazon's platform is built as a collection of microservices, each responsible for a specific function (e.g., product catalog, order processing, payment gateway).

    • Benefits:

      Enhanced scalability, improved fault tolerance, and increased development velocity.

Scaling E-commerce Success with Cloud Run: The Story of "FashionFrenzy"

Background:

FashionFrenzy, a trendy online clothing store, was launched in 2020 by two entrepreneurs, Emma and Ryan. Initially, the website was built on a traditional monolithic architecture, hosted on a single virtual machine. As the brand gained popularity, especially among young adults, the site faced scalability issues, leading to frequent crashes during peak shopping seasons (e.g., Black Friday, Holiday Sales).

Challenges Faced by FashionFrenzy:

Solution:

Emma and Ryan decided to migrate FashionFrenzy to Google Cloud Run to leverage its serverless, scalable, and secure environment.

Implementation Highlights:

Quotes from FashionFrenzy Founders:

Step 1: Create a Simple "Hello World" Flask App

  1. Create a new directory for your project and navigate into it:
    mkdir flask-cloudrun-example
      cd flask-cloudrun-example
  2. Create a file named app.py with the following content:
    from flask import Flask
      app = Flask(__name__)
      
      @app.route("/")
      def hello_world():
          return "Hello, World from Cloud Run!"
      
      if __name__ == "__main__":
          app.run(debug=True, host="0.0.0.0", port=8080)
    app.py Code

Step 2: Dockerize Your Flask Application

  1. Create a Dockerfile in the same directory with the following content:
    FROM python:3.9-slim
      
      WORKDIR /app
      
      COPY . .
      
      # Install any needed packages specified in requirements.txt
      RUN pip install --trusted-host pypi.python.org flask
      
      # Make port 8080 available to the world outside this container
      EXPOSE 8080
      
      # Define environment variable
      ENV NAME World
      
      # Run app.py when the container launches
      CMD ["python", "app.py"]
    Dockerfile All Files in Directory

Step 3: Build Your Docker Image

  1. Build your Docker image by running:
    docker build -t hello-world .
      docker tag hello-world gcr.io/[YOUR_GCP_PROJECT_ID]/hello-world

    Replace [YOUR_GCP_PROJECT_ID] with your project ID.

    Build Docker Image Tag for GCR

Step 4: Push Your Image to Google Container Registry (GCR)

  1. Configure Docker to use gcloud as a credential helper, then push your image:
    gcloud auth configure-docker
      docker push gcr.io/[YOUR_GCP_PROJECT_ID]/hello-world

    Replace [YOUR_GCP_PROJECT_ID] with your project ID.

    Push to GCR

Step 5: Deploy to Cloud Run (Console)

  1. First, open Cloud Run console and click "Deploy Container":
    Select Service

  2. Select the container from Google Container Registry:
    Select Container

  3. Give a name to your service and click "Create":
    Select Name

  4. After deployment, click on the link to view your "Hello, World from Cloud Run!" message:
    Deploy to Cloud Run Console
    Cloud Run Log

Step 6: Verify Your Deployment

  1. After deployment, the gcloud run deploy command will output a URL.
  2. Visit this URL in your web browser to see your "Hello, World from Cloud Run!" message:
    Output
  3. Alternatively, you can find the URL in the Cloud Console under the Cloud Run section for your service.

Congratulations! You've successfully deployed a Flask "Hello World" application as a Docker container to Google Cloud Run.

Environment Variables: Setup and Usage in Cloud Run

What are Environment Variables?

Environment Variables (Env Vars) are key-value pairs that store sensitive information or configurable settings for your application, separate from your code. This approach enhances security and makes it easier to manage different environments (e.g., dev, staging, prod).

Setting Up Environment Variables in Cloud Run

Using the Google Cloud Console

  1. Navigate to Cloud Run: Open the Google Cloud Console, select your project, and click on Cloud Run.
  2. Select Your Service: Choose the service for which you want to set environment variables.
  3. Click on "Variables": In the service's left sidebar, click on Variables (under Settings).
  4. Add Variable:
    • Click Add Variable.
    • Enter the Key (name) and Value for your environment variable.
    • Optionally, select Is secret to encrypt the value.
    • Click Save.
Add Variable in Console

Using the gcloud Command-Line Interface (CLI)

  1. Install/Update gcloud CLI: Ensure you have the latest gcloud CLI installed.
  2. Set Environment Variable:
    gcloud run services update  --set-env-vars =

    Replace , , and with your actual service name, environment variable key, and value, respectively.

Using Environment Variables in Your Application

  • Access Env Vars in Code: Use standard environment variable access methods for your programming language. For example, in Node.js: process.env.MY_VARIABLE.
const myVariable = process.env.MY_VARIABLE;

Example Use Case:

Store a database connection string as an encrypted environment variable (IS_SECRET enabled) to keep it secure across different environments.

Scaling and Traffic Splitting

Autoscaling Features:

Cloud Run's Autoscaling automatically adjusts the number of container instances based on incoming traffic, ensuring efficient resource utilization and optimal responsiveness.

  • Scaling Metrics:
    • Concurrency (default): Scales based on the number of concurrent requests.
    • CPU Utilization: Scales based on the average CPU usage across instances.
  • Scaling Settings:
    • Minimum Instances: The lowest number of instances to maintain.
    • Maximum Instances: The highest number of instances to scale up to.
    • Scaling Settings:
      • Minimum Instances: The lowest number of instances to maintain.
      • Maximum Instances: The highest number of instances to scale up to.
      • Target Value (for CPU Utilization or Concurrency): The threshold that triggers scaling.

Configuring Autoscaling in Cloud Run

Using the Google Cloud Console

  1. Navigate to Cloud Run, select your service, and click on Settings.
  2. Scroll to "Scaling": Adjust the scaling settings as desired (e.g., change the Minimum Instances, Maximum Instances, or Target CPU Utilization).
  3. Click Save.
Autoscaling in Console

Using the gcloud CLI

gcloud run services update  --platform managed --region  \
          --min-instances  --max-instances  \
          --cpu-threshold  --cpu-target-utilization-percentage 

Replace placeholders with your actual values.

Handling Multiple Versions with Traffic Splitting

What is Traffic Splitting?

Traffic Splitting allows you to divert a percentage of incoming traffic to different versions of your service, enabling A/B testing, canary releases, or rolling out updates with minimal risk.

Configuring Traffic Splitting in Cloud Run

Using the Google Cloud Console

  1. Navigate to Cloud Run, select your service, and click on Traffic Splitting.
  2. Add a New Split:
    • Select the Service Version to split traffic to.
    • Set the Percentage of traffic to divert.
    • Click Save.
Traffic Splitting in Console

Using the gcloud CLI

gcloud run services update-traffic  --platform managed --region  \
          --to-revisions =

Replace , , , and with your actual values (e.g., my-service, us-central1, my-revision, and 20 for 20%).

  • Scaling Settings:
  • Configuring Autoscaling in Cloud Run

    Using the Google Cloud Console

    1. Navigate to Cloud Run, select your service, and click on Settings.
    2. Scroll to "Scaling": Adjust the scaling settings as desired (e.g., change the Minimum Instances, Maximum Instances, or Target CPU Utilization).
    3. Click Save.
    Autoscaling in Console

    Using the gcloud CLI

    gcloud run services update  --platform managed --region  \
        --min-instances  --max-instances  \
        --cpu-threshold  --cpu-target-utilization-percentage 

    Replace placeholders with your actual values.

    Handling Multiple Versions with Traffic Splitting

    What is Traffic Splitting?

    Traffic Splitting allows you to divert a percentage of incoming traffic to different versions of your service, enabling A/B testing, canary releases, or rolling out updates with minimal risk.

    Configuring Traffic Splitting in Cloud Run

    Using the Google Cloud Console

    1. Navigate to Cloud Run, select your service, and click on Traffic Splitting.
    2. Add a New Split:
      • Select the Service Version to split traffic to.
      • Set the Percentage of traffic to divert.
      • Click Save.
    Traffic Splitting in Console

    Using the gcloud CLI

    gcloud run services update-traffic  --platform managed --region  \
        --to-revisions =

    Replace , , , and with your actual values (e.g., my-service, us-central1, my-revision, and 20 for 20%).