apiVersion:sql.cnrm.cloud.google.com/v1beta1kind:SQLInstancemetadata:labels:label-one:"sql-server-example"name:sqlserver-1spec:region:us-central1databaseVersion:MYSQL_5_7settings:tier:db-g1-small---apiVersion:sql.cnrm.cloud.google.com/v1beta1kind:SQLUsermetadata:name:sqluser-samplespec:instanceRef:name:sqlserver-1host:"%"password:valueFrom:secretKeyRef:name:secret-1# Name of the Secret object to extract a value fromkey:password# Key that identifies the value to be extracted
[[["易于理解","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-08-11。"],[[["\u003cp\u003eSecrets can be used in Config Connector to store sensitive data like passwords and access keys within a GKE cluster, and they can be referenced when creating resources.\u003c/p\u003e\n"],["\u003cp\u003eUpdating a Secret triggers Config Connector to reconcile and update resources that reference it, such as changing a user's password in a Cloud SQL database.\u003c/p\u003e\n"],["\u003cp\u003eDeleting a Secret does not remove the resource's reference to it; for instance, an SQLUser's password remains unchanged even if the referenced Secret is deleted.\u003c/p\u003e\n"],["\u003cp\u003eIf a resource references a non-existent Secret, Config Connector creates a \u003ccode\u003eDependencyNotFound\u003c/code\u003e or \u003ccode\u003eDependencyInvalid\u003c/code\u003e Event to indicate the issue.\u003c/p\u003e\n"],["\u003cp\u003eSecrets are namespace-specific in Kubernetes, meaning if you use the same secret across multiple namespaces, you must create the secret in each namespace separately.\u003c/p\u003e\n"]]],[],null,["# Using Secrets to store sensitive data\n=====================================\n\n*** ** * ** ***\n\nYou can use a\n[Secret](https://kubernetes.io/docs/concepts/configuration/secret/)\nto store sensitive configuration information, such as passwords and access keys,\nin your GKE cluster. This page describes how Config Connector\nuses Secret values when configuring your Google Cloud resources.\n\nOverview\n--------\n\nThis section presents an overview on using Secrets with Config Connector.\n\n### Using a Secret when creating a resource\n\nYou can use the data inside a Secret when you create a resource. In\n[Passing a Secret to a resource](#passing_a_secret_to_a_resource), you create\na Secret that contains a password, then reference the Secret for a user on a\nCloud SQL database.\n\n### Updating Secrets\n\nWhen you apply an update to a Secret, Config Connector updates the resource when\nit next reconciles your desired state. For example, if you update the Secret\nthat is referenced as the password for an `SQLUser`, Config Connector will then\nupdate the user's password.\n\n### Deleting Secrets\n\nIf you delete a Secret that is in use by a resource, Config Connector will\nnot remove the resource's reference to the Secret. For example, if you create\nan SQLUser with a password that references a Secret and then delete the Secret,\nthe password will remain on the SQLUser.\n\n### Troubleshooting\n\nIf you create a resource that references a Secret that does not exist,\nConfig Connector will create a `DependencyNotFound` or `DependencyInvalid`\n[Event](/config-connector/docs/how-to/viewing-events).\n\n### Secrets and Kubernetes namespaces\n\nBecause Kubernetes does not allow access to secrets across Namespaces, if you\nare using the same secret value for resources from different Namespaces, you\nmust apply secrets in every Namespace.\n\nPassing a Secret to a resource\n------------------------------\n\nIn this section, you will create a Cloud SQL server and set the default\nuser's password with a Secret.\n\n### Before you begin\n\nTo complete these steps, you need the following:\n\n- Config Connector installed on a Kubernetes cluster\n- The Cloud SQL Admin API enabled in your project. To confirm that the API\n is enabled, run the following `gcloud` command:\n\n gcloud services list --filter=sqladmin.googleapis.com\n\n If the command prints `Listed 0 items.`, you need to enable the API. You can\n enable the API with Config Connector and the Resource Manager API, or a `gcloud`\n command.\n\n### Service Usage API\n\n1. Config Connector uses the Service Usage API to enable service APIs. To perform these steps, you need to enable the [Service Usage](https://console.cloud.google.com/apis/library/serviceusage.googleapis.com) API. You can enable this API by using the Google Cloud CLI: \n\n ```\n gcloud services enable serviceusage.googleapis.com\n ```\n | **Note:** Enabling the API might take several minutes.\n2. Copy the following YAML into a file named `enable-sql.yaml`\n\n apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1\n kind: Service\n metadata:\n name: sqladmin.googleapis.com\n\n3. Apply `enable-sql.yaml` to your cluster with `kubectl`:\n\n kubectl apply -f enable-sql.yaml --namespace \u003cvar translate=\"no\"\u003eCC_NAMESPACE\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003eCC_NAMESPACE\u003c/var\u003e with the namespace Config Connector\n manages resources from.\n\n### gcloud\n\nTo enable the SQL Admin API with the Google Cloud CLI, run the following command: \n\n gcloud services enable sqladmin.googleapis.com\n\n### Creating a Cloud SQL Server\n\n1. Copy the YAML below into a file named `sql-server-example.yaml`. This\n creates a Cloud SQL server named `sqlserver-1` and a user named\n `sqluser-sample`. The password for `sqluser-sample` is set by a\n [resource reference](/config-connector/docs/how-to/creating-resource-references)\n to a secret named `secret-1`.\n\n apiVersion: sql.cnrm.cloud.google.com/v1beta1\n kind: SQLInstance\n metadata:\n labels:\n label-one: \"sql-server-example\"\n name: sqlserver-1\n spec:\n region: us-central1\n databaseVersion: MYSQL_5_7\n settings:\n tier: db-g1-small\n ---\n apiVersion: sql.cnrm.cloud.google.com/v1beta1\n kind: SQLUser\n metadata:\n name: sqluser-sample\n spec:\n instanceRef:\n name: sqlserver-1\n host: \"%\"\n password:\n valueFrom:\n secretKeyRef:\n name: secret-1 # Name of the Secret object to extract a value from\n key: password # Key that identifies the value to be extracted\n\n2. Copy the YAML below into a file named `example-secret.yaml`. Edit the\n value for `password` before saving. This declares `secret-1`, referenced\n in the prior section.\n\n apiVersion: v1\n kind: Secret\n metadata:\n name: secret-1\n stringData:\n password: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003ePASSWORD\u003c/span\u003e\u003c/var\u003e\n\n3. Apply the configurations to your cluster.\n\n kubectl apply -n \u003cvar translate=\"no\"\u003eCC_NAMESPACE\u003c/var\u003e -f example-secret.yaml\n kubectl apply -n \u003cvar translate=\"no\"\u003eCC_NAMESPACE\u003c/var\u003e -f sql-server-example.yaml\n\n Replace \u003cvar translate=\"no\"\u003eCC_NAMESPACE\u003c/var\u003e with the namespace Config Connector\n manages resources from.\n\n Config Connector creates the Cloud SQL instance and a user with the\n password you specified. This might take several minutes.\n4. Wait several minutes, and confirm the instance is available:\n\n \u003cbr /\u003e\n\n ```\n gcloud sql instances list --filter=\"name=sqlserver-1\"\n ```\n\n \u003cbr /\u003e\n\n The output should include a server with a `STATUS` of `RUNNABLE`.\n5. Confirm that you can connect to the SQL instance:\n\n ```\n gcloud sql connect sqlserver-1 --user=sqluser-sample\n ```\n\n You are prompted for your password.\n\n### Cleaning up\n\nIf you do not wish to keep the instance, delete the Cloud SQL instance, user,\nand secret with `kubectl`. \n\n kubectl delete -n \u003cvar translate=\"no\"\u003eCC_NAMESPACE\u003c/var\u003e -f sql-server-example.yaml\n kubectl delete -n \u003cvar translate=\"no\"\u003eCC_NAMESPACE\u003c/var\u003e -f example-secret.yaml\n\nReplace \u003cvar translate=\"no\"\u003eCC_NAMESPACE\u003c/var\u003e with the namespace Config Connector\nmanages resources from.\n\nWhat's next\n-----------\n\n- Learn how to specify\n [Resource references](/config-connector/docs/how-to/creating-resource-references)\n between Config Connector resources.\n\n- Take a deep dive into\n [Kubernetes Secrets](https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets)."]]