在 Azure Kubernetes Service 上的 Istio 為基礎的服務網格附加元件中插入 CA 憑證

在基於 Istio 的 Azure Kubernetes Service 服務網狀外掛中,Istio 憑證授權中心(CA)預設會產生自簽的根憑證與金鑰,並用它們來簽署工作負載憑證。 若要保護根 CA 金鑰,您應該使用在安全的離線機器上執行的根 CA。 您可以使用根 CA,發送發出中繼憑證給在每個叢集中執行的 Istio CA 。 Istio CA 可以使用系統管理員指定的憑證和金鑰來簽署工作負載憑證,並將系統管理員指定的根憑證分發至工作負載作為信任的根。 本文說明如何在 Azure Kubernetes Service 的基於 Istio 服務網格附加元件中,帶入您自己的 Istio CA 憑證和金鑰。

顯示具有 Istio 之根 CA 和中繼 CA 的圖表。

本文說明如何使用 Azure Key Vault 將根憑證、簽署憑證和金鑰作為輸入提供給 Istio 型服務網格附加元件,進而設定 Istio 憑證授權單位

開始之前

確認 Azure CLI 版本

該附加元件需要安裝 Azure CLI 2.57.0 或更新版本。 您可以執行az --version以驗證版本。 要安裝或升級,請參見 Install Azure CLI

設定 Azure Key Vault

  1. 你需要一個 Azure Key Vault 資源來提供 Istio 外掛的憑證和金鑰輸入。

  2. 您需要離線產生根憑證、中繼憑證、中繼金鑰和憑證鏈結。 此處的步驟 1-3 具有說明如何產生這些檔案的範例。

  3. 在 Azure Key Vault 中利用憑證和金鑰創建機密資料:

    az keyvault secret set --vault-name $AKV_NAME --name root-cert --file <path-to-folder/root-cert.pem>
    az keyvault secret set --vault-name $AKV_NAME --name ca-cert --file <path-to-folder/ca-cert.pem>
    az keyvault secret set --vault-name $AKV_NAME --name ca-key --file <path-to-folder/ca-key.pem>
    az keyvault secret set --vault-name $AKV_NAME --name cert-chain --file <path-to-folder/cert-chain.pem>
    
  4. 啟用用於叢集的 Secret Store CSI 驅動程式的 Azure Key Vault 提供者

    az aks enable-addons --addons azure-keyvault-secrets-provider --resource-group $RESOURCE_GROUP --name $CLUSTER
    

    附註

    在輪換憑證時,可以使用 Azure Key Vault Secrets Provider 外掛的 --rotation-poll-interval 參數,以控制秘密同步到叢集的速度。 例如:az aks addon update --resource-group $RESOURCE_GROUP --name $CLUSTER --addon azure-keyvault-secrets-provider --enable-secret-rotation --rotation-poll-interval 20s

  5. 如果您的金鑰保存庫使用 Azure RBAC 作為權限模型,請依照此處的指示,將 金鑰保存庫 Secrets User 的 Azure 角色指派給增益集的使用者指派受控識別。 或者,如果您的金鑰庫正在使用金庫存取原則權限模型,請授權該附加元件的使用者指派受控識別,以使用存取原則存取 Azure Key Vault 資源:

    OBJECT_ID=$(az aks show --resource-group $RESOURCE_GROUP --name $CLUSTER --query 'addonProfiles.azureKeyvaultSecretsProvider.identity.objectId' -o tsv)
    
    az keyvault set-policy --name $AKV_NAME --object-id $OBJECT_ID --secret-permissions get
    

使用外掛程式 CA 憑證設定 Istio 型服務網格附加元件

  1. 啟用你現有 AKS 叢集的 Istio 服務網格附加元件,並引用之前建立的 Azure Key Vault 機密:

    az aks mesh enable --resource-group $RESOURCE_GROUP --name $CLUSTER \
    --root-cert-object-name root-cert \
    --ca-cert-object-name ca-cert \
    --ca-key-object-name ca-key \
    --cert-chain-object-name cert-chain \
    --key-vault-id /subscriptions/$SUBSCRIPTION/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.KeyVault/vaults/$AKV_NAME
    

    附註

    對於使用由 Istio CA 產生的自簽署根憑證且具有 Istio 附加元件的現有叢集,不支援切換至外掛程式 CA。 您必須先在這些叢集上停用網格,然後使用上述指令再次啟用網格,以傳遞外掛程式 CA 輸入。

  2. 驗證 Istio 控制平面已挑選自訂憑證授權單位:

    kubectl logs deploy/istiod-asm-1-17 -c discovery -n aks-istio-system | grep -v validationController | grep x509
    

    預期的輸出應該會類似:

    2023-11-06T15:49:15.493732Z     info    x509 cert - Issuer: "CN=Intermediate CA - A1,O=Istio,L=cluster-A1", Subject: "", SN: e191d220af347c7e164ec418d75ed19e, NotBefore: "2023-11-06T15:47:15Z", NotAfter: "2033-11-03T15:49:15Z"
    2023-11-06T15:49:15.493764Z     info    x509 cert - Issuer: "CN=Root A,O=Istio", Subject: "CN=Intermediate CA - A1,O=Istio,L=cluster-A1", SN: 885034cba2894f61036f2956fd9d0ed337dc636, NotBefore: "2023-11-04T01:40:02Z", NotAfter: "2033-11-01T01:40:02Z"
    2023-11-06T15:49:15.493795Z     info    x509 cert - Issuer: "CN=Root A,O=Istio", Subject: "CN=Root A,O=Istio", SN: 18e2ee4089c5a7363ec306627d21d9bb212bed3e, NotBefore: "2023-11-04T01:38:27Z", NotAfter: "2033-11-01T01:38:27Z"
    

憑證授權單位輪替

基於安全性或原則的緣故,您可能需要定期輪替憑證授權單位。 本區段會逐步引導您處理中繼 CA 和根 CA 輪替情境。

中繼憑證授權單位輪替

  1. 您可以輪替中繼 CA,同時保持根 CA 不變。 更新 Azure Key Vault 資源中的秘密,並使用新的憑證和金鑰檔案:

    az keyvault secret set --vault-name $AKV_NAME --name root-cert --file <path-to-folder/root-cert.pem>
    az keyvault secret set --vault-name $AKV_NAME --name ca-cert --file <path-to-folder/ca-cert.pem>
    az keyvault secret set --vault-name $AKV_NAME --name ca-key --file <path-to-folder/ca-key.pem>
    az keyvault secret set --vault-name $AKV_NAME --name cert-chain --file <path/cert-chain.pem>
    
  2. 等候--rotation-poll-interval的持續時間。 根據 Azure Key Vault 資源上更新的新中繼 CA,檢查叢集上的憑證是否已重新整理:

    kubectl logs deploy/istiod-asm-1-17 -c discovery -n aks-istio-system | grep -v validationController
    

    預期的輸出應該會類似:

    2023-11-07T06:16:21.091844Z     info    Update Istiod cacerts
    2023-11-07T06:16:21.091901Z     info    Using istiod file format for signing ca files
    2023-11-07T06:16:21.354423Z     info    Istiod has detected the newly added intermediate CA and updated its key and certs accordingly
    2023-11-07T06:16:21.354910Z     info    x509 cert - Issuer: "CN=Intermediate CA - A2,O=Istio,L=cluster-A2", Subject: "", SN: b2753c6a23b54d8364e780bf664672ce, NotBefore: "2023-11-07T06:14:21Z", NotAfter: "2033-11-04T06:16:21Z"
    2023-11-07T06:16:21.354967Z     info    x509 cert - Issuer: "CN=Root A,O=Istio", Subject: "CN=Intermediate CA - A2,O=Istio,L=cluster-A2", SN: 17f36ace6496ac2df88e15878610a0725bcf8ae9, NotBefore: "2023-11-04T01:40:22Z", NotAfter: "2033-11-01T01:40:22Z"
    2023-11-07T06:16:21.355007Z     info    x509 cert - Issuer: "CN=Root A,O=Istio", Subject: "CN=Root A,O=Istio", SN: 18e2ee4089c5a7363ec306627d21d9bb212bed3e, NotBefore: "2023-11-04T01:38:27Z", NotAfter: "2033-11-01T01:38:27Z"
    2023-11-07T06:16:21.355012Z     info    Istiod certificates are reloaded
    
  3. 工作負載會收到來自 Istio 控制平面的憑證 (預設為 24 小時有效)。 如果您未重新啟動 Pod,全部工作負載都會在 24 小時內根據新的中繼 CA 取得新的葉憑證。 如果您想要強迫全部這些工作負載立即從新的中繼 CA 取得新的葉憑證,則需要重新啟動工作負載。

    kubectl rollout restart deployment <deployment name> -n <deployment namespace>
    

根憑證授權單位輪替

  1. 您需要使用根憑證檔案來更新 Azure Key Vault 密碼,讓舊根憑證和新根憑證串接:

    az keyvault secret set --vault-name $AKV_NAME --name root-cert --file <path-to-folder/root-cert.pem>
    az keyvault secret set --vault-name $AKV_NAME --name ca-cert --file <path-to-folder/ca-cert.pem>
    az keyvault secret set --vault-name $AKV_NAME --name ca-key --file <path-to-folder/ca-key.pem>
    az keyvault secret set --vault-name $AKV_NAME --name cert-chain --file <path/cert-chain.pem>
    

    root-cert.pem的內容遵循下列格式:

    -----BEGIN CERTIFICATE-----
    <contents of old root certificate>
    -----END CERTIFICATE-----
    -----BEGIN CERTIFICATE-----
    <contents of new root certificate>
    -----END CERTIFICATE-----
    

    一旦憑證同步至叢集後,檢查istiod記錄。

    kubectl logs deploy/istiod-asm-1-17 -c discovery -n aks-istio-system 
    

    預期輸出:

    2023-11-07T06:42:00.287916Z     info    Updating new ROOT-CA
    2023-11-07T06:42:00.287928Z     info    update root cert and generate new dns certs
    2023-11-07T06:42:00.288254Z     info    Update trust anchor with new root cert
    2023-11-07T06:42:00.288279Z     info    trustBundle     updating Source IstioCA with certs
    2023-11-07T06:42:00.288298Z     info    Istiod has detected the newly added intermediate CA and updated its key and certs accordingly
    2023-11-07T06:42:00.288303Z     info    Istiod certificates are reloaded
    
  2. 您必須等候 24 小時 (葉憑證有效性的預設時間),或強制重新啟動全部工作負載。 如此一來,全部工作負載就會同時辨識新舊憑證授權單位以進行 mTLS 驗證

    kubectl rollout restart deployment <deployment name> -n <deployment namespace>
    
  3. 你現在可以只用新的 CA(不含舊的 CA)來更新 Azure Key Vault 的秘密:

    az keyvault secret set --vault-name $AKV_NAME --name root-cert --file <path-to-folder/root-cert.pem>
    az keyvault secret set --vault-name $AKV_NAME --name ca-cert --file <path-to-folder/ca-cert.pem>
    az keyvault secret set --vault-name $AKV_NAME --name ca-key --file <path-to-folder/ca-key.pem>
    az keyvault secret set --vault-name $AKV_NAME --name cert-chain --file <path/cert-chain.pem>
    

    一旦憑證同步至叢集後,檢查istiod記錄。

    kubectl logs deploy/istiod-asm-1-17 -c discovery -n aks-istio-system | grep -v validationController
    

    預期輸出:

    2023-11-07T08:01:17.780299Z     info    x509 cert - Issuer: "CN=Intermediate CA - B1,O=Istio,L=cluster-B1", Subject: "", SN: 1159747c72cc7ac7a54880cd49b8df0a, NotBefore: "2023-11-07T07:59:17Z", NotAfter: "2033-11-04T08:01:17Z"
    2023-11-07T08:01:17.780330Z     info    x509 cert - Issuer: "CN=Root B,O=Istio", Subject: "CN=Intermediate CA - B1,O=Istio,L=cluster-B1", SN: 2aba0c438652a1f9beae4249457023013948c7e2, NotBefore: "2023-11-04T01:42:12Z", NotAfter: "2033-11-01T01:42:12Z"
    2023-11-07T08:01:17.780345Z     info    x509 cert - Issuer: "CN=Root B,O=Istio", Subject: "CN=Root B,O=Istio", SN: 3f9da6ddc4cb03749c3f43243a4b701ce5eb4e96, NotBefore: "2023-11-04T01:41:54Z", NotAfter: "2033-11-01T01:41:54Z"
    

    從本文所顯示的範例輸出中,您可以看到我們已從根 A (在啟用附加元件時使用) 移至根 B。