使用密鑰儲存機密資料
您可以使用密鑰,在 GKE 叢集中儲存機密設定資訊,例如密碼和存取權金鑰。本頁面說明 Config Connector 在設定 Google Cloud 資源時,如何使用 Secret 值。
總覽
本節將概略說明如何在 Config Connector 中使用 Secret。
在建立資源時使用密鑰
您可以在建立資源時使用 Secret 中的資料。在「將密鑰傳遞至資源」一文中,您會建立含有密碼的密鑰,然後在 Cloud SQL 資料庫中參照使用者的密鑰。
更新密鑰
當您對機密金鑰套用更新時,Config Connector 會在下次協調所需狀態時更新資源。舉例來說,如果您更新用於 SQLUser
密碼的 Secret,Config Connector 就會更新使用者的密碼。
刪除密鑰
如果您刪除資源正在使用的密鑰,Config Connector 不會移除資源對密鑰的參照。舉例來說,如果您使用參照 Secret 的密碼建立 SQLUser,然後刪除 Secret,密碼仍會保留在 SQLUser 上。
疑難排解
如果您建立的資源參照的密鑰不存在,Config Connector 會建立 DependencyNotFound
或 DependencyInvalid
Event。
Secret 和 Kubernetes 命名空間
由於 Kubernetes 不允許跨命名空間存取 Secret,因此如果您要為不同命名空間的資源使用相同的 Secret 值,就必須在每個命名空間中套用 Secret。
將密鑰傳遞至資源
在本節中,您將建立 Cloud SQL 伺服器,並使用密鑰設定預設使用者密碼。
事前準備
完成這些步驟時,你需要準備以下項目:
- 在 Kubernetes 叢集上安裝 Config Connector
專案中已啟用 Cloud SQL Admin API。如要確認 API 是否已啟用,請執行下列
gcloud
指令:gcloud services list --filter=sqladmin.googleapis.com
如果指令顯示
Listed 0 items.
,您就需要啟用 API。您可以使用 Config Connector 和 Resource Manager API,或gcloud
指令啟用 API。
Service Usage API
- Config Connector 會使用 Service Usage API 啟用服務 API。如要執行這些步驟,您必須啟用 Service Usage API。您可以使用 Google Cloud CLI 啟用此 API:
gcloud services enable serviceusage.googleapis.com
將下列 YAML 複製到名為
enable-sql.yaml
的檔案中apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1 kind: Service metadata: name: sqladmin.googleapis.com
使用
kubectl
將enable-sql.yaml
套用至叢集:kubectl apply -f enable-sql.yaml --namespace CC_NAMESPACE
將
CC_NAMESPACE
替換為 Config Connector 管理資源的命名空間。
gcloud
如要透過 Google Cloud CLI 啟用 SQL Admin API,請執行下列指令:
gcloud services enable sqladmin.googleapis.com
建立 Cloud SQL Server
將以下 YAML 複製到名為
sql-server-example.yaml
的檔案中。這會建立名為sqlserver-1
的 Cloud SQL 伺服器,以及名為sqluser-sample
的使用者。sqluser-sample
的密碼是由資源參照設定為名為secret-1
的密鑰。apiVersion: sql.cnrm.cloud.google.com/v1beta1 kind: SQLInstance metadata: labels: label-one: "sql-server-example" name: sqlserver-1 spec: region: us-central1 databaseVersion: MYSQL_5_7 settings: tier: db-g1-small --- apiVersion: sql.cnrm.cloud.google.com/v1beta1 kind: SQLUser metadata: name: sqluser-sample spec: instanceRef: name: sqlserver-1 host: "%" password: valueFrom: secretKeyRef: name: secret-1 # Name of the Secret object to extract a value from key: password # Key that identifies the value to be extracted
將以下 YAML 複製到名為
example-secret.yaml
的檔案中。請先編輯password
的值,再儲存。這會宣告先前章節中參照的secret-1
。apiVersion: v1 kind: Secret metadata: name: secret-1 stringData: password: PASSWORD
將設定套用至叢集。
kubectl apply -n CC_NAMESPACE -f example-secret.yaml
kubectl apply -n CC_NAMESPACE -f sql-server-example.yaml
將
CC_NAMESPACE
替換為 Config Connector 管理資源的命名空間。Config Connector 會建立 Cloud SQL 執行個體,並使用您指定的密碼建立使用者。這可能需要幾分鐘的時間。
請稍候幾分鐘,然後確認執行個體是否可用:
gcloud sql instances list --filter="name=sqlserver-1"
輸出內容應包含伺服器,且
STATUS
為RUNNABLE
。確認您可以連線至 SQL 執行個體:
gcloud sql connect sqlserver-1 --user=sqluser-sample
系統會提示您輸入密碼。
正在清除所用資源
如果您不想保留執行個體,請使用 kubectl
刪除 Cloud SQL 執行個體、使用者和密鑰。
kubectl delete -n CC_NAMESPACE -f sql-server-example.yaml
kubectl delete -n CC_NAMESPACE -f example-secret.yaml
將 CC_NAMESPACE
替換為 Config Connector 管理資源的命名空間。
後續步驟
瞭解如何在 Config Connector 資源之間指定資源參照。
深入瞭解 Kubernetes 密鑰。