使用密鑰儲存機密資料
您可以使用 Secret,在 GKE 叢集中儲存密碼和存取金鑰等機密設定資訊。本頁面說明 Config Connector 如何在設定 Google Cloud 資源時使用 Secret 值。
總覽
本節概述如何搭配使用 Secret 和 Config Connector。
安全性與 Secret 靜態資料加密
Kubernetes Secret 含有機密憑證和連線字串,因此請在叢集啟用應用程式層 Secret 加密功能。GKE 預設會加密靜態 Secret。應用程式層密鑰加密功能會使用 Cloud Key Management Service 信封加密,多添一層安全防護。
此外,請使用角色式存取控管 (RBAC),限制只有授權的叢集管理員才能讀取 Secret。對在 status 欄位中儲存機密值的 Config Connector 資源 (例如 IAMServiceAccountKey) 套用相同限制。
建立資源時使用 Secret
建立資源時,您可以使用 Secret 中的資料。在「將 Secret 傳遞至資源」中,您會建立包含密碼的 Secret,然後在 Cloud SQL 資料庫中參照使用者的 Secret。
更新密鑰
當您對 Secret 套用更新時,Config Connector 會在下次協調所需狀態時更新資源。舉例來說,如果您更新了參照為 SQLUser 密碼的 Secret,Config Connector 就會更新使用者的密碼。
刪除 Secret
如果刪除資源正在使用的 Secret,Config Connector 不會移除資源對 Secret 的參照。舉例來說,如果您建立 SQLUser,並使用參照 Secret 的密碼,然後刪除 Secret,SQLUser 仍會保留密碼。
疑難排解
如果您建立的資源參照了不存在的 Secret,Config Connector 會建立 DependencyNotFound 或 DependencyInvalid
事件。
Secret 和 Kubernetes 命名空間
Kubernetes 不允許跨命名空間存取 Secret,因此如果您在不同命名空間的資源中使用相同的 Secret 值,就必須在每個命名空間中套用 Secret。
將 Secret 傳遞至資源
在本節中,您將建立 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的 Secret。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。