使用密鑰儲存機密資料
您可以使用Secret,在 GKE 叢集中儲存密碼和存取金鑰等機密設定資訊。本頁面說明 Config Connector 如何在設定 Google Cloud 資源時使用 Secret 值。
總覽
本節概述如何搭配使用 Secret 與 Config Connector。
在建立資源時使用密鑰
建立資源時,可以使用 Secret 內的資料。在「將密鑰傳遞至資源」中,您會建立含有密碼的密鑰,然後在 Cloud SQL 資料庫中參照該密鑰,以供使用者存取。
更新密鑰
對 Secret 套用更新時,Config Connector 會在下次調解所需狀態時更新資源。舉例來說,如果您更新做為 SQLUser 密碼的 Secret,Config Connector 就會更新使用者的密碼。
刪除 Secret
如果您刪除資源正在使用的 Secret,Config Connector 不會移除資源對 Secret 的參照。舉例來說,如果您建立 SQLUser 時,密碼參照了 Secret,然後刪除該 Secret,密碼仍會保留在 SQLUser 中。
疑難排解
如果您建立的資源參照不存在的 Secret,Config Connector 會建立 DependencyNotFound 或 DependencyInvalid
事件。
Secret 和 Kubernetes 命名空間
由於 Kubernetes 不允許跨命名空間存取密鑰,因此如果您要為不同命名空間的資源使用相同的密鑰值,就必須在每個命名空間中套用密鑰。
將密鑰傳遞至資源
在本節中,您將建立 Cloud SQL 伺服器,並使用 Secret 設定預設使用者的密碼。
事前準備
如要完成這些步驟,您需要:
- 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.yamlkubectl 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 Secret。