共用方式為


使用 AGIC 在 AKS 叢集中啟用多重命名空間支援

Kubernetes 命名空間 可讓 Kubernetes 叢集進行分割,並配置給較大小組的子群組。 然後,這些子群組可以使用更精細的資源、安全性和組態控制來部署和管理基礎結構。 Kubernetes 允許在每個命名空間內獨立定義一或多個輸入資源。

從 0.7 版開始,應用程式閘道 Kubernetes 輸入控制器 (AGIC) 可以擷取和觀察多個命名空間的事件。 如果 Azure Kubernetes Service (AKS) 系統管理員決定使用 Azure 應用程式閘道 做為輸入,則所有命名空間都會使用相同的 應用程式閘道 部署。 AGIC 的單一安裝可監視可存取的命名空間,並設定與其相關聯的 應用程式閘道 部署。

AGIC 0.7 版會繼續獨佔觀察 default 命名空間,除非您明確地將它變更為 Helm 設定中的一或多個不同命名空間。

提示

請考慮針對 Kubernetes 輸入解決方案的容器 應用程式閘道。

啟用多重命名空間支援

  1. 使用下列其中一個方式修改 helm-config.yaml 檔案:

    • watchNamespace從 helm-config.yaml 完全刪除密鑰。 AGIC 會觀察所有命名空間。
    • watchNamespace 設為空字串。 AGIC 會觀察所有命名空間。
    • 新增以逗號分隔的多個命名空間(例如 , watchNamespace: default,secondNamespace。 AGIC 會以獨佔方式觀察這些命名空間。
  2. 執行 helm install -f helm-config.yaml application-gateway-kubernetes-ingress/ingress-azure以套用 Helm 樣本變更。

部署 AGIC 並能夠觀察多個命名空間之後,它會執行下列動作:

  • 列出來自所有可存取命名空間的輸入資源
  • 篩選以批註輸入資源 kubernetes.io/ingress.class: azure/application-gateway
  • 組合 應用程式閘道 組態
  • 透過 Azure Resource Manager 將設定套用至相關聯的 應用程式閘道 部署

處理衝突的組態

多個命名空間的輸入資源可以指示 AGIC 為單一 應用程式閘道 部署建立衝突的組態。 也就是說,兩個輸入可以宣告相同的網域。

在階層頂端,AGIC 可以建立接聽程式(IP 位址、埠和主機)和路由規則(系結接聽程式、後端集區和 HTTP 設定)。 多個命名空間和輸入可以共享它們。

另一方面,AGIC 可以只針對一個命名空間建立路徑、後端集區、HTTP 設定和 TLS 憑證,並移除重複專案。

例如,請考慮在 和 staging production 命名空間 www.contoso.com中定義的下列重複輸入資源:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: websocket-ingress
  namespace: staging
  annotations:
    kubernetes.io/ingress.class: azure/application-gateway
spec:
  rules:
    - host: www.contoso.com
      http:
        paths:
          - backend:
              serviceName: web-service
              servicePort: 80
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: websocket-ingress
  namespace: production
  annotations:
    kubernetes.io/ingress.class: azure/application-gateway
spec:
  rules:
    - host: www.contoso.com
      http:
        paths:
          - backend:
              serviceName: web-service
              servicePort: 80

雖然有兩個輸入資源要求將流量 www.contoso.com 路由傳送至個別 Kubernetes 命名空間,但只有一個後端可以傳送流量。 AGIC 會針對其中一個資源,以「先進先出」為基礎建立設定。 如果同時建立兩個輸入資源,則字母表中稍早的輸入資源優先。 根據此屬性,AGIC 會建立輸入的 production 設定。 應用程式閘道會使用下列資源進行設定:

  • 接聽程式:fl-www.contoso.com-80
  • 路由規則: rr-www.contoso.com-80
  • 後端集區: pool-production-contoso-web-service-80-bp-80
  • HTTP 設定: bp-production-contoso-web-service-80-80-websocket-ingress
  • 健康情況探查: pb-production-contoso-web-service-80-websocket-ingress

注意

除了接聽程式和路由規則之外,所建立 應用程式閘道 資源會包含 AGIC 為其建立它們的命名空間名稱(production)。

如果在不同時間點將這兩個輸入資源導入 AKS 叢集中,AGIC 可能會最終出現在重新設定 應用程式閘道,並將流量從 namespace-B 重新路由傳送至 namespace-A的案例中。

例如,如果您先新增staging,AGIC 會設定 應用程式閘道 將流量路由傳送至預備後端集區。 在之後的階段中,引入 production 輸入會導致 AGIC 將應用程式閘道的程式重新設計,開始將流量路由傳送至 production 後端集區。

限制對命名空間的存取

根據預設,AGIC 會根據任何命名空間內的批註輸入來設定 應用程式閘道。 如果您想要限制此行為,您有下列選項:

  • 藉由明確定義 AGIC 應該透過 watchNamespace helm-config.yamlYAML 密鑰觀察的命名空間,以限制命名空間。
  • 使用 Role 和 RoleBinding 物件 ,將 AGIC 限制為特定命名空間。

範例 Helm 組態檔

    # This file contains the essential configs for the ingress controller helm chart

    # Verbosity level of the App Gateway Ingress Controller
    verbosityLevel: 3
    
    ################################################################################
    # Specify which application gateway the ingress controller manages
    #
    appgw:
        subscriptionId: <subscriptionId>
        resourceGroup: <resourceGroupName>
        name: <applicationGatewayName>
    
        # Setting appgw.shared to "true" creates an AzureIngressProhibitedTarget CRD.
        # This prohibits AGIC from applying config for any host/path.
        # Use "kubectl get AzureIngressProhibitedTargets" to view and change this.
        shared: false
    
    ################################################################################
    # Specify which kubernetes namespace the ingress controller watches
    # Default value is "default"
    # Leaving this variable out or setting it to blank or empty string would
    # result in Ingress Controller observing all accessible namespaces.
    #
    # kubernetes:
    #   watchNamespace: <namespace>
    
    ################################################################################
    # Specify the authentication with Azure Resource Manager
    #
    # Two authentication methods are available:
    # - Option 1: AAD-Pod-Identity (https://github.com/Azure/aad-pod-identity)
    armAuth:
        type: aadPodIdentity
        identityResourceID: <identityResourceId>
        identityClientID:  <identityClientId>
    
    ## Alternatively you can use Service Principal credentials
    # armAuth:
    #    type: servicePrincipal
    #    secretJSON: <<Generate this value with: "az ad sp create-for-rbac --subscription <subscription-uuid> --role Contributor --sdk-auth | base64 -w0" >>
    
    ################################################################################
    # Specify if the cluster is Kubernetes RBAC enabled or not
    rbac:
        enabled: false # true/false
    
    # Specify aks cluster related information. THIS IS BEING DEPRECATED.
    aksClusterConfiguration:
        apiServerAddress: <aks-api-server-address>