Skip to main content

Certification track: AI Tooling

Ollama on Google Cloud Run

Ollama on Google Cloud Run

Ollama is an open-source LLM inference server that serves large language models — Llama, Mistral, Gemma, Phi, and others — via a REST API. This module deploys Ollama on Cloud Run v2 (CPU-only, serverless) on top of the App_CloudRun foundation, which provisions and manages the shared Google Cloud infrastructure.

This guide focuses on the cloud services Ollama uses and how to explore and operate them from the Google Cloud Console and the command line. For the mechanics common to every Cloud Run application — service identity, ingress and load balancing, scaling and concurrency, CI/CD, Cloud Armor, IAP, Binary Authorization, VPC Service Controls, backups, and the deployment lifecycle — refer to the App_CloudRun foundation guide rather than repeating them here.


1. Overview

Ollama runs as a containerised inference server on Cloud Run v2. The deployment wires together a focused set of Google Cloud services:

CapabilityGoogle Cloud serviceNotes
ComputeCloud Run v2 (gen2)Ollama service, 4 vCPU / 8 GiB by default (3B models), request-based autoscaling
Model storageCloud Storage + GCS FuseModels bucket mounted at /mnt/gcs; weights persist across container restarts and new revisions
SecretsSecret ManagerNo app-managed secrets — Ollama requires no credentials
IngressCloud Run URL (VPC-internal)Default internal ingress; only services in the same VPC can reach the API

Sensible defaults worth knowing up front:

  • No database, no Redis. Ollama is stateless beyond its GCS-backed model cache. Neither Cloud SQL nor Redis is provisioned.
  • GCS Fuse is the persistence layer. Model weights are stored in a dedicated GCS bucket and mounted into the container at /mnt/gcs via GCS Fuse. Container restarts load models from GCS rather than re-downloading them.
  • ingress_settings = "internal" by default. Ollama is designed as a shared VPC-internal inference endpoint called by other applications (Flowise, N8N, RAGFlow, Django). Setting ingress_settings = "all" exposes the unauthenticated API to the public internet — always pair with IAP in that case.
  • execution_environment = "gen2" is required for GCS Fuse support and cannot be downgraded to gen1.
  • Automatic model pull. When default_model is set and no custom initialization_jobs are provided, a Cloud Run Job named model-pull is created on first deployment and stores the model in the GCS bucket.
  • Three environment variables are always injected: OLLAMA_MODELS, OLLAMA_HOST, and OLLAMA_KEEP_ALIVE. Do not override OLLAMA_MODELS or OLLAMA_HOST in environment_variables.

2. Google Cloud Services & How to Explore Them

All commands assume PROJECT and REGION are set. Service and resource names are reported in the deployment Outputs.

A. Cloud Run — the Ollama service

Ollama runs as a Cloud Run v2 service (gen2) that autoscales by request load between the minimum and maximum instance counts. Each deployment creates an immutable revision; traffic can be split across revisions for safe rollouts.

  • Console: Cloud Run → select the service for revisions, traffic, logs, and metrics.
  • CLI:
    gcloud run services list --project "$PROJECT" --region "$REGION"
    gcloud run services describe <service-name> --project "$PROJECT" --region "$REGION"
    gcloud run revisions list --service <service-name> --project "$PROJECT" --region "$REGION"
    # Send a test inference request from within the VPC:
    curl -s "$OLLAMA_API_URL/api/tags" | jq '.models[].name'

See App_CloudRun for scaling, concurrency, execution environment, and traffic splitting.

B. Cloud Storage — model weight persistence

Ollama model weights are stored in a dedicated GCS bucket (named <resource_prefix>-models) and mounted into the container via GCS Fuse at /mnt/gcs. The environment variable OLLAMA_MODELS is set to /mnt/gcs/ollama/models so Ollama discovers and caches models there.

  • Console: Cloud Storage → Buckets → select the models bucket to browse downloaded model files.
  • CLI:
    # Bucket name is in the Outputs (models_bucket)
    gcloud storage ls gs://<models-bucket>/ollama/models/
    gcloud storage buckets describe gs://<models-bucket> --project "$PROJECT"

The bucket name is reported as the models_bucket output. See App_CloudRun for GCS Fuse, CMEK options, and lifecycle policies.

C. Secret Manager

Ollama requires no application-managed credentials — there is no admin password and no database password. Secret Manager is available for any custom secrets you inject via secret_environment_variables.

  • Console: Security → Secret Manager.
  • CLI:
    gcloud secrets list --project "$PROJECT"
    gcloud secrets versions access latest --secret=<secret-name> --project "$PROJECT"

See App_CloudRun for injection and rotation details.

D. Networking & ingress

The service is reachable by default only from within the same VPC (ingress_settings = "internal"). Any Cloud Run service, GKE pod, or Compute Engine VM in the VPC can call the Ollama API at the service URL on port 11434. An external HTTPS load balancer with a custom domain, Cloud CDN, and Cloud Armor can be added for external access.

  • Console: Cloud Run (service URL); Network services → Load balancing.
  • CLI:
    gcloud run services describe <service-name> \
    --region "$REGION" --format='value(status.url)'
    gcloud compute addresses list --project "$PROJECT"

See App_CloudRun.

E. Cloud Logging & Monitoring

Container logs flow to Cloud Logging; Cloud Run metrics flow to Cloud Monitoring, with optional uptime checks and alert policies.

  • Console: Logging → Logs Explorer; Monitoring → Dashboards / Alerting.
  • CLI:
    gcloud run services logs read <service-name> \
    --project "$PROJECT" --region "$REGION" --limit 50

3. Ollama Application Behaviour

  • No first-deploy database setup. Ollama has no database. There is no db-init job and no Cloud SQL instance.

  • Model pull on first deploy. When default_model is set and initialization_jobs is empty, a Cloud Run Job named model-pull runs once. It starts a local Ollama server in the background, pulls the named model, stores it in the GCS bucket, then shuts down. The job mounts the ollama-models GCS volume so the weights persist. The timeout is controlled by model_pull_timeout_seconds (default 3600 seconds); large models (7B+) can take 20–30 minutes on first pull.

    Inspect the job and its executions:

    gcloud run jobs list --project "$PROJECT" --region "$REGION"
    gcloud run jobs executions list --job <model-pull-job-name> \
    --project "$PROJECT" --region "$REGION"
  • Cold-start model loading. On each instance start, Ollama loads model weights from GCS Fuse. This typically takes 30–120 seconds depending on model size. The startup probe uses a 30 s initial delay with 20 failure attempts (roughly 5 minutes) to accommodate this. OLLAMA_KEEP_ALIVE is set to "24h" so loaded models stay resident between requests. Override via environment_variables.

  • Automatically injected variables. OLLAMA_MODELS (/mnt/gcs/ollama/models), OLLAMA_HOST (0.0.0.0:11434), and OLLAMA_KEEP_ALIVE (24h) are injected automatically. Do not override the first two; the third may be overridden.

  • Health endpoint. The Ollama root path (/) responds with "Ollama is running" once the server is ready. Both startup and liveness probes target this path.

  • Additional tuning. Use environment_variables to set OLLAMA_NUM_PARALLEL (default 1, increase for concurrent callers), OLLAMA_ORIGINS (restrict CORS), and other Ollama environment variables.


4. Configuration Variables

Variables are grouped exactly as they appear on the deployment platform. Only settings specific to or notable for Ollama are listed; every other input is inherited from App_CloudRun with its standard behaviour.

Group 1 — Project & Identity

VariableDefaultDescription
project_id(required)Target Google Cloud project.
regionus-central1Region for the service and regional resources.

Group 2 — Deployment Environment

VariableDefaultDescription
tenant_deployment_iddemoShort suffix that makes resource names unique per environment.
support_users[]Emails granted project access and monitoring alerts.
resource_labels{}Labels applied to all resources.

Group 3 — Application Identity

VariableDefaultDescription
application_nameollamaBase name for resources. Do not change after first deploy.
application_display_nameOllama LLM ServerFriendly name shown in the Console.
description(set)Service description.
application_versionlatestOllama image tag; pin to a specific version (e.g., 0.3.12) in production.

Group 4 — Runtime & Scaling

VariableDefaultDescription
deploy_applicationtrueSet false to provision storage and IAM without deploying the service.
cpu_limit4000mCPU per instance. 3B models: "4000m"; 7B models: "8000m". Cloud Run max is "8".
memory_limit8GiMemory per instance. 3B models: "8Gi"; 7B models: "16Gi".
min_instance_count1Minimum instances. Keep ≥ 1 to avoid 60–120 s cold-start model-loading latency.
max_instance_count1Maximum instances. LLM inference is CPU-saturating; multiple instances increase cost.
execution_environmentgen2Must remain "gen2" — required for GCS Fuse volume mounts.
timeout_seconds3600Maximum request duration. Large model inference can be slow; 3600 s is the Cloud Run maximum.
container_protocolhttp1HTTP protocol version.
traffic_split[]Traffic allocation across revisions for canary deployments. All entries must sum to 100.
enable_image_mirroringtrueMirror ollama/ollama to Artifact Registry to avoid Docker Hub rate limits.

Group 5 — Access & Networking

VariableDefaultDescription
ingress_settingsinternalKeep "internal" for VPC-only access. "all" exposes the unauthenticated API to the public internet.
vpc_egress_settingPRIVATE_RANGES_ONLYOutbound traffic routing through VPC.
enable_iapfalseRequire Google sign-in via Identity-Aware Proxy. Required when ingress_settings = "all".
iap_authorized_users / iap_authorized_groups[]Who may access through IAP.
enable_cloud_armorfalseAttach a Cloud Armor WAF fronted by a Global HTTPS Load Balancer.
application_domains[]Custom domain names for the HTTPS load balancer.
enable_cdnfalseEnable Cloud CDN on the HTTPS load balancer.
enable_vpc_scfalseEnforce a VPC-SC perimeter (requires organization_id).
enable_audit_loggingfalseDetailed Cloud Audit Logs.

Group 6 — Environment Variables & Secrets

VariableDefaultDescription
environment_variables{}Extra non-secret settings. OLLAMA_MODELS, OLLAMA_HOST, and OLLAMA_KEEP_ALIVE are injected automatically. Use OLLAMA_NUM_PARALLEL, OLLAMA_ORIGINS, etc. here.
secret_environment_variables{}Map of env var → Secret Manager secret name.

Group 7 — Backup & Maintenance

Not applicable for Ollama — model weights are stored durably in GCS. These variables are present for interface compatibility only. See App_CloudRun.

Group 8 — CI/CD & Binary Authorization

Standard App_CloudRun Cloud Build / Cloud Deploy integration — see App_CloudRun. Key inputs: enable_cicd_trigger, github_repository_url, github_token, enable_cloud_deploy, enable_binary_authorization.

Group 9 — NFS Instance & Custom Initialization

VariableDefaultDescription
initialization_jobs[]Leave empty and set default_model to use the auto-generated model-pull job. Provide a non-empty list to replace it entirely.
cron_jobs[]Recurring Cloud Run Jobs triggered by Cloud Scheduler.

Group 11 — Storage & Filesystem

VariableDefaultDescription
create_cloud_storagetrueProvision GCS buckets in storage_buckets. The models bucket is always created regardless.
storage_buckets[]Additional GCS buckets beyond the models bucket.
enable_nfsfalseNot required for Ollama (uses GCS Fuse).
gcs_volumes[]Additional GCS Fuse volume mounts. The ollama-models bucket at /mnt/gcs is always appended automatically.
manage_storage_kms_iam / enable_artifact_registry_cmekfalseCMEK options.
max_images_to_retain / delete_untagged_images / image_retention_days(set)Artifact Registry cleanup policy.

Group 12 — Database Backend

Not applicable for Ollama. database_type is fixed to "NONE" — no Cloud SQL instance is provisioned. These variables are present for interface compatibility only.

Group 14 — Observability & Health

VariableDefaultDescription
startup_probe{ type="HTTP", path="/", initial_delay_seconds=30, failure_threshold=20 }20-attempt threshold allows ~5 minutes for model loading from GCS.
liveness_probe{ type="HTTP", path="/", initial_delay_seconds=60, failure_threshold=3 }60 s delay avoids false restarts during model load.
uptime_check_config{ enabled=false, path="/" }Cloud Monitoring uptime check.
alert_policies[]Metric alert policies.
max_revisions_to_retain7Not referenced — declared for interface compatibility only; has no effect on this module's deployment.

Group 19 — Ollama Model Configuration

VariableDefaultDescription
default_model""Model to pull on first deployment (e.g., "llama3.2:3b", "mistral", "llama3:8b"). Leave empty to skip the auto-pull job.
model_pull_timeout_seconds3600Timeout for the model-pull Cloud Run Job. Large models (7B+) can take 20–30 minutes. Valid range: 300–7200.

5. Outputs

Returned on a successful deployment — the quickest way to locate and explore the running resources.

OutputDescription
service_nameCloud Run service name.
ollama_api_urlOllama REST API base URL — append /api/generate, /api/chat, etc. Constructed as <service_url>/api.
service_locationRegion the service runs in.
stage_servicesStage-specific service URLs (Cloud Deploy).
models_bucketGCS bucket name where Ollama model weights are persisted.
storage_bucketsAll provisioned Cloud Storage buckets.
network_name / network_exists / regionsVPC network, presence, regions.
container_image / container_registryDeployed image and Artifact Registry repo.
monitoring_enabled / monitoring_notification_channels / uptime_check_namesMonitoring status, channels, uptime checks.
initialization_jobsNames of the setup jobs (including model-pull when triggered).
deployment_id / tenant_id / resource_prefixNaming identifiers.
project_id / project_numberProject identifiers.
cicd_enabled / github_repository_url / github_repository_owner / github_repository_name / cicd_configurationCI/CD status and details.
artifact_registry_repository / cloudbuild_trigger_name / cloudbuild_trigger_idRegistry and build trigger.
vpc_sc_enabled / vpc_sc_perimeter_name / vpc_sc_dry_run_modeVPC-SC status.
audit_logging_enabled / artifact_registry_cmek_enabledAudit logging and CMEK status.

6. Configuration Pitfalls & Sensible Defaults

Risk: Critical (data loss / outage / security) — High (service degraded) — Medium (cost or partial degradation) — Low (minor).

SettingSensible valueRiskConsequence if wrong
ingress_settingsinternalCritical"all" exposes the unauthenticated Ollama API publicly — any internet caller can query or load models.
enable_iaptrue if ingress_settings = "all"CriticalWithout IAP, the API is unauthenticated and publicly reachable. Ollama has no built-in auth.
memory_limit8Gi (3B) / 16Gi (7B)CriticalInsufficient memory causes OOM-kill mid-inference; container restarts in a loop. Allocate at least 2× the model's quantised weight size.
execution_environmentgen2 (default)HighGen1 does not support GCS Fuse mounts. Downgrading silently prevents model persistence.
cpu_limit4000m (3B) / 8000m (7B)HighToo few CPUs makes token generation extremely slow (minutes per token on 7B models).
min_instance_count1High0 enables scale-to-zero but causes 60–120 s cold starts while the model reloads from GCS.
model_pull_timeout_seconds3600HighA short timeout causes the model-pull Job to fail before the download completes for models over 2 GB.
startup_probe.failure_threshold20 (default)HighToo low a threshold causes Cloud Run to kill and restart the container before Ollama is ready after GCS Fuse mount.
timeout_seconds3600HighLLM inference for large prompts can exceed 5 minutes. Requests to long-running generations are terminated with a 504 if too short.
max_instance_count13HighEach instance independently loads the model. Multiple instances are safe but significantly increase cost and GCS read traffic.
default_modelset to desired modelMediumLeaving empty is safe for initial deploy but the API returns an error on all inference requests until a model is pulled manually.
environment_variables.OLLAMA_NUM_PARALLEL24 for shared useMediumDefault 1 serialises all requests. Increase for shared VPC endpoints with concurrent callers.
environment_variables.OLLAMA_KEEP_ALIVE24h (injected automatically)MediumOllama's own default (5m) evicts models from memory, causing 30–60 s reload delays. The module injects 24h automatically.
environment_variables.OLLAMA_ORIGINSrestrict for browser useMediumOllama's default CORS policy accepts any origin. Restrict to specific UI origins when exposed through a load balancer.
gcs_volumes mount optionsinclude implicit-dirsMediumWithout implicit-dirs, GCS Fuse directory listings fail and Ollama cannot discover cached models.
enable_image_mirroringtrueMediumDisabling pulls from Docker Hub, which is rate-limited. Keep true in production.

For the foundation behaviour referenced throughout — service identity, scaling and concurrency, ingress and load balancing, CI/CD, Cloud Armor, IAP, Binary Authorization, VPC-SC, backups, and image mirroring — see App_CloudRun. Ollama-specific shared application configuration is described in Ollama_Common.