askvity

How do I create a Cloud Run service?

Published in CloudRun 4 mins read

Creating a Cloud Run service involves deploying a container image to Google Cloud's serverless platform. You essentially provide a container, and Cloud Run automatically handles scaling, networking, and infrastructure management. Here's a breakdown of the process:

Steps to Create a Cloud Run Service:

  1. Prepare Your Container Image: This is the foundation of your service. You need a container image that contains your application and all its dependencies. You can build your own using Docker, or use a pre-built image. The container image must be stored in a container registry such as Google Container Registry (GCR) or Artifact Registry.

  2. Choose a Deployment Method: You can deploy to Cloud Run using:

    • Google Cloud Console: A web-based interface.
    • gcloud command-line tool: Offers more automation and control.
    • Terraform or other Infrastructure-as-Code tools: Enables declarative infrastructure management.
  3. Deploying using the Google Cloud Console:

    a. Go to the Cloud Run dashboard.

    b. Click Create Service.

    c. Configure Service Settings:

    • Service Name: Choose a unique name for your service.
    • Region: Select the Google Cloud region where you want to deploy. Choose a region close to your users for lower latency.
    • Container Image URL: Provide the URL of your container image (e.g., gcr.io/your-project/your-image:latest). If using Artifact Registry, the URL will follow a different format.
    • Ingress Control: Determine who can access your service (e.g., allow all traffic or restrict access).
    • Authentication: Choose whether to require authentication.
    • Scaling: Configure scaling settings such as minimum and maximum instances. The default settings are usually sufficient to begin.
    • CPU allocation: Determines the CPU allocation per instance.

    d. Advanced Settings (Optional):

    *   **Connections:** Configure networking settings, such as VPC Connector for private network access.
    *   **Security:** Set up Cloud IAM policies.
    *   **Variables & Secrets:** Define environment variables and secrets that your application needs.
    *   **Health Checks:** Configure health checks to ensure your service is running properly.
    *   **Concurrency:**  Adjust how many requests a single container instance can handle concurrently.

    e. Click Create. Cloud Run will then deploy your container image as a service.

  4. Deploying using the gcloud command-line tool:

    a. Set up Google Cloud CLI: Ensure you have the gcloud CLI installed and configured with your Google Cloud project.

    b. Deploy Command: Use the gcloud run deploy command:

    gcloud run deploy [SERVICE_NAME] \
        --image [IMAGE_URL] \
        --region [REGION] \
        --platform managed \
        --allow-unauthenticated  # Use this flag to allow public access

    Replace:

    • [SERVICE_NAME] with the name of your service.
    • [IMAGE_URL] with the URL of your container image.
    • [REGION] with the Google Cloud region.

    c. Customization: You can add more flags to customize the deployment. For example:

    gcloud run deploy my-service \
        --image gcr.io/my-project/my-image:latest \
        --region us-central1 \
        --platform managed \
        --memory 512Mi \
        --concurrency 80 \
        --timeout 300s

Important Considerations:

  • Container Image: The container image must be built for a Linux environment.
  • Port: Your application within the container should listen on port 8080 by default. You can change this port in the Cloud Run configuration.
  • IAM Permissions: Make sure the service account used by Cloud Run has the necessary permissions to access other Google Cloud resources, if applicable.
  • Scaling: Cloud Run automatically scales your service based on incoming traffic. You can configure minimum and maximum instance counts to control costs and performance.
  • Pricing: Cloud Run charges are based on resource usage (CPU, memory, network) and the number of requests.

Example: Deploying a Prebuilt Container

To quickly get started, you can deploy a prebuilt container from Google's sample repository.

  1. Open the Cloud Run Dashboard.
  2. Click Create Service.
  3. Enter a Service name.
  4. Select a Region.
  5. Under "Container image URL", enter us-docker.pkg.dev/cloudrun/container/hello.
  6. Click Create.

Cloud Run makes it simple to deploy and run containerized applications in a serverless environment. By following these steps, you can quickly launch your own services and benefit from its scalability and ease of use.

Related Articles