共用方式為


使用 Bicep 在 Azure Kubernetes Service(AKS)中部署 Open Service Mesh 附加元件

本文示範如何使用 Bicep範本,將 Open Service Mesh (OSM) 附加元件部署到 Azure Kubernetes Service(AKS)。

注意

由於雲端原生運算基礎(CNCF)即將淘汰 Open Service Mesh(OSM),建議您找出 OSM 設定,並移轉到對應的 Istio 設定。 如需從 OSM 移轉到 Istio 的資訊,請參閱 從 Open Service Mesh (OSM)設定移轉到 Istio 的指導

重要

根據叢集執行的 Kubernetes 版本,OSM 附加元件會安裝不同 OSM 版本。

Kubernetes 版本 已安裝 OSM 版本
1.24.0 或更新版本 1.2.5
介於 1.23.5 和 1.24.0 之間 1.1.3
1.23.5 之前 1.0.0

如果對應的 AKS 版本已達使用壽命年限,可能無法安裝或主動支援舊版 OSM。 如需 AKS 版本支援期限的相關資訊,您可以檢查 AKS Kubernetes 發行版本行事曆

Bicep 是使用宣告式語法來部署 Azure 資源的特定領域語言。 您可以使用 Bicep 來取代建立 Azure Resource Manager 範本,以部署基礎結構即程式碼 Azure 資源。

開始之前

開始之前,請先確認您已符合以下必要條件:

使用 Bicep 為新的 AKS 叢集安裝 OSM 附加元件

若要部署新的 AKS 叢集,您可以在叢集建立時啟用 OSM 附加元件。 下列指示會使用泛型 Bicep 範本,使用暫時性磁碟和 kubenet 容器網路介面來部署 AKS 叢集,然後啟用 OSM 附加元件。 如需更進階的部署情節,請參閱何謂 Bicep?

建立資源群組

  • 使用 az group create 命令建立資源群組。

    az group create --name <my-osm-bicep-aks-cluster-rg> --location <azure-region>
    

建立主要和參數 Bicep 檔案

  1. 建立目錄以儲存必要的 Bicep 部署檔案。 下列範例會建立名為 bicep-osm-aks-addon 的目錄,以及對目錄的變更:

    mkdir bicep-osm-aks-addon
    cd bicep-osm-aks-addon
    
  2. 建立主要檔案和參數檔案。

    touch osm.aks.bicep && touch osm.aks.parameters.json
    
  3. 開啟 osm.aks.bicep 檔案,並複製到以下內容:

    // https://learn.microsoft.com/azure/aks/troubleshooting#what-naming-restrictions-are-enforced-for-aks-resources-and-parameters
    @minLength(3)
    @maxLength(63)
    @description('Provide a name for the AKS cluster. The only allowed characters are letters, numbers, dashes, and underscore. The first and last character must be a letter or a number.')
    param clusterName string
    @minLength(3)
    @maxLength(54)
    @description('Provide a name for the AKS dnsPrefix. Valid characters include alphanumeric values and hyphens (-). The dnsPrefix can\'t include special characters such as a period (.)')
    param clusterDNSPrefix string
    param k8Version string
    param sshPubKey string
    param location string
    param adminUsername string
    
    
    resource aksCluster 'Microsoft.ContainerService/managedClusters@2021-03-01' = {
      name: clusterName
      location: location
      identity: {
        type: 'SystemAssigned'
      }
      properties: {
        kubernetesVersion: k8Version
        dnsPrefix: clusterDNSPrefix
        enableRBAC: true
        agentPoolProfiles: [
          {
            name: 'agentpool'
            count: 3
            vmSize: 'Standard_DS2_v2'
            osDiskSizeGB: 30
            osDiskType: 'Ephemeral'
            osType: 'Linux'
            mode: 'System'
          }
        ]
        linuxProfile: {
          adminUsername: adminUserName
          ssh: {
            publicKeys: [
              {
                keyData: sshPubKey
              }
            ]
          }
        }
        addonProfiles: {
            openServiceMesh: {
                enabled: true
                config: {}
          }
        }
      }
    }
    
  4. 開啟 osm.aks.parameters.json 檔案,並複製到以下內容。 別忘了以自己的值,取代部署參數值。

    注意

    osm.aks.parameters.json 是 Bicep 部署所需的範例範本參數檔案。 特別針對您的部署環境更新參數。 您需要新增值的參數包括:clusterNameclusterDNSPrefixk8VersionsshPubKeylocationadminUsername。 若要在您的區域中尋找可支援 Kubernetes 版本的清單,請使用 az aks get-versions --location <region> 命令。

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "clusterName": {
          "value": "<YOUR CLUSTER NAME HERE>"
        },
        "clusterDNSPrefix": {
          "value": "<YOUR CLUSTER DNS PREFIX HERE>"
        },
        "k8Version": {
          "value": "<YOUR SUPPORTED KUBERNETES VERSION HERE>"
        },
        "sshPubKey": {
          "value": "<YOUR SSH KEY HERE>"
        },
        "location": {
          "value": "<YOUR AZURE REGION HERE>"
        },
        "adminUsername": {
          "value": "<YOUR ADMIN USERNAME HERE>"
        }
      }
    }
    

部署 Bicep 檔案

  1. 開啟終端,並使用 az login 命令向 Azure CLI 驗證您的 Azure 帳戶。

  2. 使用 az deployment group create 命令部署 Bicep 檔案。

    az deployment group create \
      --name OSMBicepDeployment \
      --resource-group osm-bicep-test \
      --template-file osm.aks.bicep \
      --parameters @osm.aks.parameters.json
    

驗證 OSM 附加元件的安裝

  1. 查詢叢集的附加元件設定檔,以檢查已安裝附加元件的啟用狀態。 下列命令應該會傳回 true

    az aks list -g <my-osm-aks-cluster-rg> -o json | jq -r '.[].addonProfiles.openServiceMesh.enabled'
    
  2. 使用下列 kubectl 命令取得 osm-controller 的狀態。

    kubectl get deployments -n kube-system --selector app=osm-controller
    kubectl get pods -n kube-system --selector app=osm-controller
    kubectl get services -n kube-system --selector app=osm-controller
    

存取 OSM 附加元件設定

您可以使用 OSM MeshConfig 資源設定 OSM 控制器,也可以使用 Azure CLI 檢視 OSM 控制器的組態設定。

  • 使用 kubectl get 命令檢視 OSM 控制器的組態設定。

    kubectl get meshconfig osm-mesh-config -n kube-system -o yaml
    

    以下是 MeshConfig 輸出範例:

    apiVersion: config.openservicemesh.io/v1alpha1
    kind: MeshConfig
    metadata:
      creationTimestamp: "0000-00-00A00:00:00A"
      generation: 1
      name: osm-mesh-config
      namespace: kube-system
      resourceVersion: "2494"
      uid: 6c4d67f3-c241-4aeb-bf4f-b029b08faa31
    spec:
      certificate:
        serviceCertValidityDuration: 24h
      featureFlags:
        enableEgressPolicy: true
        enableMulticlusterMode: false
        enableWASMStats: true
      observability:
        enableDebugServer: true
        osmLogLevel: info
        tracing:
          address: jaeger.osm-system.svc.cluster.local
          enable: false
          endpoint: /api/v2/spans
          port: 9411
      sidecar:
        configResyncInterval: 0s
        enablePrivilegedInitContainer: false
        envoyImage: mcr.microsoft.com/oss/envoyproxy/envoy:v1.18.3
        initContainerImage: mcr.microsoft.com/oss/openservicemesh/init:v0.9.1
        logLevel: error
        maxDataPlaneConnections: 0
        resources: {}
      traffic:
        enableEgress: true
        enablePermissiveTrafficPolicyMode: true
        inboundExternalAuthorization:
          enable: false
          failureModeAllow: false
          statPrefix: inboundExtAuthz
          timeout: 1s
        useHTTPSIngress: false
    

    請注意,enablePermissiveTrafficPolicyMode 會設定為 true。 在 OSM 中,寬鬆流量原則模式會略過 SMI 流量原則強制執行。 在此模式中,OSM 會自動探索屬於服務網格一部分的服務。 探索到的服務會在每個 Envoy Proxy 側車上設定流量原則規則,以允許這些服務之間的通訊。

    警告

    繼續之前,請確認您的寬鬆流量原則模式已設定為 true。 如果還沒,請使用以下命令將其變更為 true

    kubectl patch meshconfig osm-mesh-config -n kube-system -p '{"spec":{"traffic":{"enablePermissiveTrafficPolicyMode":true}}}' --type=merge
    

清除資源

下一步

本文已說明如何在 AKS 叢集上,安裝並確認 OSM 附加元件的安裝和執行。 在叢集上安裝 OSM 附加元件後,您可以部署範例應用程式上架現有的應用程式,並使用 OSM 網格。