[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-02。"],[],[],null,["# Migrate from Cloud Run to Google Kubernetes Engine\n\nCloud Run and Kubernetes both use standard container images as\ndeployment artifacts, they both use a declarative API model with resources that\ncan be represented in YAML files with the same standard structure.\n\nIntroduction\n------------\n\nThe [Cloud Run Admin API v1](/run/docs/reference/rest/v1/namespaces.services)\nis designed to maximize portability with Kubernetes,\nfor example, the Cloud Run Admin API resources share the same structure\nconventions and attribute names as Kubernetes resources. See the\n[Cloud Run service YAML reference](/run/docs/reference/yaml/v1#service).\n\nThe Cloud Run Admin API v1 implements the [Knative Serving API specification](https://github.com/knative/specs/blob/main/specs/serving/knative-api-specification-1.0.md),\nbut you do **not** need to migrate to Knative to move your\nCloud Run workloads to a Kubernetes cluster like GKE.\n\nQuickstart\n----------\n\nThis quickstart is an example of a simple migration.\n\n### Simple resource comparison\n\nCompare the following simple Cloud Run service named `my-app` with the\nequivalent Kubernetes [deployment](https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/deployment-v1/).\nNote how the YAML files are almost identical.\n\nHowever, the parts in blue\nare different and need to be changed.\nThe parts in green\nshould be added.\n\n### Migrate a simple Cloud Run service to GKE\n\n1. Download service YAML file into the current directory:\n\n ```bash\n gcloud run services describe my-app --format export \u003e my-app.yaml\n ```\n2. Modify the YAML to match a Kubernetes deployment:\n\n - For the \"`kind`\" attribute: replace the value \"`Service`\" with \"`Deployment`\"\n - For the \"`apiVersion`\" attribute: replace the value \"`serving.knative.dev/v1`\" with \"`apps/v1`\"\n - Replace `metadata.namespace` with the namespace of the GKE cluster you want to deploy to, for example `default`.\n - Add a new label to under `metadata` and `spec.template.metadata`.\n - Set a fixed number of instances (\"replicas\") using `spec.template.spec.replicas` and set a label selector in `spec.template.spec.selector`.\n3. [Install](/kubernetes-engine/docs/how-to/cluster-access-for-kubectl) and\n use the `kubectl` command line tool to deploy the `my-app.yaml` file\n to your GKE cluster:\n\n ```bash\n kubectl apply -f ./my-app.yaml\n ```\n4. Expose the deployment as a service:\n\n ```bash\n kubectl expose deployment my-app --type LoadBalancer --port 80 --target-port 8080\n ```\n\nConsiderations when migrating from Cloud Run to GKE\n---------------------------------------------------\n\n**Cluster**:\n\n- Cloud Run is a fully managed platform, while GKE\n requires more platform management.\n If you have not yet created a GKE cluster, use [GKE Autopilot](/kubernetes-engine/docs/concepts/autopilot-overview).\n\n- The scalability of GKE workloads is restricted by the size of the\n cluster. If you are not using an Autopilot cluster, consider using\n [node auto-provisioning](/kubernetes-engine/docs/how-to/node-auto-provisioning)\n and a [cluster autoscaler](/kubernetes-engine/docs/concepts/cluster-autoscaler)\n to resize your cluster.\n\n- Cloud Run has a [built-in zonal redundancy](/architecture/disaster-recovery#cloud-run),\n so migrate to a [regional cluster](/kubernetes-engine/docs/concepts/regional-clusters)\n and provision enough replicas to ensure your service is resilient to a zonal\n outage in the selected Google Cloud region.\n\n**Pricing**\n\nCloud Run [charges](/run/pricing) for used resources, while\nGKE [charges](/kubernetes-engine/pricing) for provisioned\nresources.\n\n**Security**:\n\n- Contrary to Cloud Run, invoking a GKE service is not\n subject to an [IAM invoker permission](/run/docs/reference/iam/permissions).\n\n- Because GKE does not provide strong isolation between containers,\n consider using [GKE Sandbox](/kubernetes-engine/docs/concepts/sandbox-pods)\n if you need to execute unknown or untrusted code.\n\n**Networking**\n\nCloud Run requires a Serverless VPC Access connector to access other resources in a\nVPC. GKE workloads are directly in a VPC and do not need a\nconnector.\n\nFeatures not supported by Google Kubernetes Engine\n--------------------------------------------------\n\nThe following Cloud Run features are not available on GKE:\n\n- Configuration versioning via [revisions](/run/docs/resource-model#revisions)\n- [Traffic splitting](/run/docs/rollouts-rollbacks-traffic-migration) for gradual rollouts and rollbacks of revisions\n- [Request-based billing](/run/docs/configuring/billing-settings)\n- [CPU boost](/run/docs/configuring/services/cpu#startup-boost)\n- [Session affinity](/run/docs/configuring/session-affinity)\n- [Labels](/run/docs/configuring/services/labels) on GKE workloads are not Google Cloud labels\n- [Tags](/run/docs/configuring/tags) on workloads\n\nMigrate Cloud Run resources\n---------------------------\n\nThe following sections describe migrating resources used in Cloud Run, such as\nCloud Run services, jobs, and secrets.\n\n### Migrate Cloud Run services\n\nYou can migrate a Cloud Run service to the following resources on\nGKE:\n\n1. [Kubernetes deployment](https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/deployment-v1/) to create instances (called \"pods\" in Kubernetes).\n2. [Kubernetes services](https://kubernetes.io/docs/concepts/services-networking/service/) to expose the deployment at a specific endpoint.\n3. [Kubernetes Horizontal Pod Autoscaler](https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/horizontal-pod-autoscaler-v2/): to automatically scale the deployment.\n\nThe attributes of a Kubernetes deployment are a superset of the attributes of a\nCloud Run services.\nAs shown in the [quickstart](#quickstart), after you change the `apiVersion` and\n`kind` attributes to `apps/v1` and `Deployment`, you also need to change the following:\n\n- Replace `namespace` with the GKE cluster namespace to deploy to, for example `default`.\n- `serviceAccountName` should reference a Kubernetes service account, which can optionally act as an IAM service account with Workload Identity Federation for GKE.\n- Add a \u003cvar translate=\"no\"\u003eLABEL\u003c/var\u003e at `metadata.labels` and `spec.template.metadata.labels` that will be used to select the deployment and pods. For example: `app: `\u003cvar translate=\"no\"\u003eNAME\u003c/var\u003e\n- Under `spec.template`:\n - Add a `replicas` attribute to specify a number of \"instances\".\n - Add a `selector.matchLabels` attribute that selects on the label \u003cvar translate=\"no\"\u003eLABEL\u003c/var\u003e.\n- If your Cloud Run service mounts secrets, see [Migrate secrets](#secrets).\n- If the migrated Cloud Run service was accessing resources on a Virtual Private Cloud, you do not need to use a Serverless VPC Access connector.\n\nAfter creating the Kubernetes deployment, create a Kubernetes services to expose\nit: \n\n```\napiVersion: v1\nkind: Service\nmetadata:\n name: NAME\nspec:\n selector:\n LABEL\n ports:\n - protocol: TCP\n port: 80\n targetPort: PORT\n```\n\nReplace:\n\n- \u003cvar translate=\"no\"\u003eNAME\u003c/var\u003e: with the name of your service.\n- \u003cvar translate=\"no\"\u003eLABEL\u003c/var\u003e: with the label defined in your deployment. For example `app: `\u003cvar translate=\"no\"\u003eNAME\u003c/var\u003e.\n- \u003cvar translate=\"no\"\u003ePORT\u003c/var\u003e: with the `containerPort` of the container receiving requests in the Cloud Run service, which defaults to `8080`.\n\nYou can then optionally create a Kubernetes Horizontal Pod Autoscaler in order to\nautomatically scale the number of pods.\nFollow the Kubernetes [Horizontal Pod Autoscaling](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/)\ndocumentation to create an `HorizontalPodAutoscaler`.\nUse the [minimum instances](/run/docs/configuring/min-instances#yaml)\n(`autoscaling.knative.dev/minScale`)\nand [maximum instances](/run/docs/configuring/max-instances#yaml)\n(`autoscaling.knative.dev/maxScale`)\nvalues of your Cloud Run service as values for the `minReplicas` and\n`maxReplicas` attributes `HorizontalPodAutoscaler`.\n\n### Migrate Cloud Run jobs\n\nYou can migrate a [Cloud Run job](/run/docs/resource-model#jobs) to a\n[Kubernetes job](https://kubernetes.io/docs/concepts/workloads/controllers/job/)\non GKE.\n\nContrary to Cloud Run jobs, Kubernetes jobs are executed when they are\ncreated. If you want to execute the job again, you need to create a new job.\n\nThe following samples show the structural difference between a\nCloud Run job and a Kubernetes job:\n\n### Migrate secrets\n\nYou can keep existing secrets in Secret Manager or you can migrate\nthem to Kubernetes secrets.\n\nIf you choose to keep secrets in Secret Manager, you will need to [update how you use them on GKE](/secret-manager/docs/using-other-products#google-kubernetes-engine)\n\nIf you choose to migrate from Secret Manager to Kubernetes secrets,\nconsider these differences between Secret Manager secrets\nand Kubernetes secrets:\n\n- **Allowed characters in names** :\n - [Kubernetes secrets](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names): `[a-z0-9-.]{1,253}`\n - Secret Manager secrets: `[a-zA-Z0-9_-]{1,255}`\n- **Versioning**: Secrets from Secret Manager are versioned, while Kubernetes secrets aren't.\n- **Payload** : Secrets from Secret Manager contain a single `[]byte`, while Kubernetes secrets contain a `map\u003cstring, string\u003e`.\n\nMigration strategy\n------------------\n\nAfter you [create the equivalent resources](#migrate-resources),\nexposing external endpoints behind a global external Application Load Balancer\nenables you to gradually migrate traffic between Cloud Run and\nGoogle Kubernetes Engine (GKE)."]]