既存の Application Gateway を使用して Application Gateway イングレス コントローラー (AGIC) をインストールする

Application Gateway イングレス コントローラー (AGIC) は、Azure Kubernetes Service (AKS) クラスター内のポッドです。 AGIC では、Kubernetes イングレス リソースを監視し、Kubernetes クラスターの状態に基づいて Application Gateway 構成を作成して適用します。

ヒント

Application Gateway for Containers とは」も参照してください。

枠線

前提条件

このドキュメントは、次のツールとインフラストラクチャが既にインストールされていることを前提としています。

AGIC をインストールする前に、Application Gateway の構成をバックアップします

  1. Azure portal から、お使いの Application Gateway インスタンスに移動します。
  2. [Automation] セクションで、[テンプレートのエクスポート] を選んでから、[ダウンロード] を選びます。

ダウンロードした ZIP ファイルには、Application Gateway の復元が必要になったときに使用できる JSON テンプレート、bash、PowerShell スクリプトが含まれます

Helm のインストール

Helm は Kubernetes 用のパッケージ マネージャーであり、application-gateway-kubernetes-ingress パッケージのインストールに使われます。

Note

Cloud Shell を使用する場合は、Helm をインストールする必要はありません。 Azure Cloud Shell には Helm バージョン 3 が付属しています。 最初のステップをスキップして、AGIC Helm リポジトリを追加するだけです。

  1. Helm をインストールし、以下を実行して application-gateway-kubernetes-ingress Helm パッケージを追加します。

    • Kubernetes RBAC が有効 の AKS クラスター
    kubectl create serviceaccount --namespace kube-system tiller-sa
    kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller-sa
    helm init --tiller-namespace kube-system --service-account tiller-sa
    
  2. AGIC Helm リポジトリを追加します。

    helm repo add application-gateway-kubernetes-ingress https://appgwingress.blob.core.windows.net/ingress-azure-helm-package/
    helm repo update
    

Azure Resource Manager 認証

AGIC では、Kubernetes API サーバーと Azure Resource Manager と通信します。 これらの API にアクセスするには ID が必要です。

Microsoft Entra ワークロード ID を設定する

Microsoft Entra ワークロード ID は、認証を行い、他のサービスやリソースにアクセスするために、ソフトウェア ワークロードに割り当てる ID です。 AKS ポッドは、この ID を使って、他の Azure リソースでの認証を行うことができます。 この構成では、AGIC ポッドには ARM に HTTP 要求を行うための認可が必要です。

  1. Azure CLI [az アカウントセット] コマンドを使用して、特定のサブスクリプションを現在のアクティブなサブスクリプションに設定します。 次に、[az identity 作成] コマンドを使用してマネージド ID を作成します。 ノード リソースグループに ID を作成する必要があります。 ノード リソース グループには、MC_myResourceGroup_myAKSCluster_eastus などの名前が既定で割り当てられています。

    az account set --subscription "subscriptionID"
    
    az identity create --name "userAssignedIdentityName" --resource-group "resourceGroupName" --location "location" --subscription "subscriptionID"
    
  2. ロールの割り当てでは、次のコマンドを実行して、新しく作成された ID の principalId を特定します。

    $resourceGroup="resource-group-name"
    $identityName="identity-name"
    az identity list -g $resourceGroup --query "[?name == '$identityName'].principalId | [0]" -o tsv
    
  3. Application Gateway への共同作成者アクセスを、ID に許可します。 Application Gateway の次のような ID が必要です: /subscriptions/A/resourceGroups/B/providers/Microsoft.Network/applicationGateways/C。 最初に、次のコマンドを実行して、サブスクリプション内の Application Gateway ID の一覧を取得します。

    az network application-gateway list --query '[].id'
    

    ID に共同作成者アクセス権を割り当てるには、次のコマンドを実行します。

    $resourceGroup="resource-group-name"
    $identityName="identity-Name"
    # Get the Application Gateway ID
    $AppGatewayID=$(az network application-gateway list --query '[].id' -o tsv)
    $role="contributor"
    # Get the principal ID for the User assigned identity
    $principalId=$(az identity list -g $resourceGroup --query "[?name == '$identityName'].principalId | [0]" -o tsv)
    az role assignment create --assignee $principalId --role $role --scope $AppGatewayID
    
  4. Application Gateway リソース グループへの閲覧者アクセスを、ID に許可します。 リソース グループ ID は /subscriptions/A/resourceGroups/B のようになります。 すべてのリソース グループを取得するには、次を使用します: az group list --query '[].id'

    $resourceGroup="resource-group-name"
    $identityName="identity-Name"
    # Get the Application Gateway resource group
    $AppGatewayResourceGroup=$(az network application-gateway list --query '[].resourceGroup' -o tsv)
    # Get the Application Gateway resource group ID
    $AppGatewayResourceGroupID=$(az group show --name $AppGatewayResourceGroup --query id -o tsv)
    $role="Reader"
    # Get the principal ID for the User assigned identity
    $principalId=$(az identity list -g $resourceGroup --query "[?name == '$identityName'].principalId | [0]" -o tsv)
    # Assign the Reader role to the User assigned identity at the resource group scope
    az role assignment create --role $role --assignee $principalId  --scope $AppGatewayResourceGroupID
    

Note

AGIC によって使用される ID に、Application Gateway のデプロイ先サブネットに委任された Microsoft.Network/virtualNetworks/subnets/join/action アクセス許可があることを確認してください。 カスタム ロールがこのアクセス許可で定義されていない場合、Microsoft.Network/virtualNetworks/subnets/join/action アクセス許可が含まれた組み込みの "ネットワーク共同作成者" ロールを使用できます。

サービス プリンシパルを使用する

Kubernetes シークレットを使って、ARM への AGIC アクセスを提供することもできます。

  1. Active Directory のサービス プリンシパルを作成し、base64 でエンコードします。 JSON BLOB を Kubernetes に保存するには、base64 エンコードが必要です。

    az ad sp create-for-rbac --role Contributor --sdk-auth | base64 -w0
    
  2. base64 でエンコードされた JSON BLOB を helm-config.yaml ファイルに追加します。 helm-config.yaml の詳細については、次のセクションを参照してください。

    armAuth:
        type: servicePrincipal
        secretJSON: <Base64-Encoded-Credentials>
    

Azure Application Gateway イングレス コントローラー アドオンをデプロイする

イングレス コントローラー配置マニフェストを作成する

---
# file: pet-supplies-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: pet-supplies-ingress
spec:
  ingressClassName: azure-application-gateway
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: store-front
            port:
              number: 80
      - path: /order-service
        pathType: Prefix
        backend:
          service:
            name: order-service
            port:
              number: 3000
      - path: /product-service
        pathType: Prefix
        backend:
          service:
            name: product-service
            port:
              number: 3002

イングレス コント ローラーをデプロイする

$namespace="namespace"
$file="pet-supplies-ingress.yaml"
kubectl apply -f $file -n $namespace

イングレス コントローラーを Helm Chart としてインストールする

最初の手順で、ご利用の Kubernetes クラスターに Helm の Tiller をインストールします。 Cloud Shell を使用して、AGIC Helm パッケージをインストールします。

  1. application-gateway-kubernetes-ingress Helm リポジトリを追加し、Helm の更新を実行します

    helm repo add application-gateway-kubernetes-ingress https://appgwingress.blob.core.windows.net/ingress-azure-helm-package/
    helm repo update
    
  2. AGIC を構成する helm-config.yaml をダウンロードします。

    wget https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/master/docs/examples/sample-helm-config.yaml -O helm-config.yaml
    

    または、次の YAML ファイルをコピーします。

    # 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 must manage
    #
    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 must watch
    # 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: Azure-AD-workload-identity
    armAuth:
        type: workloadIdentity
        identityClientID:  <identityClientId>
    
    ## Alternatively you can use Service Principal credentials
    # armAuth:
    #    type: servicePrincipal
    #    secretJSON: <<Generate this value with: "az ad sp create-for-rbac --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>
    
  3. helm-config.yaml を編集し、appgwarmAuth の値を入力します。

    Note

    <identity-client-id> は、前のセクションで設定した Microsoft Entra ワークロード ID のプロパティです。 az identity show -g <resourcegroup> -n <identity-name> コマンドを実行することで、この情報を取得できます。<resourcegroup> は、AKS クラスター、Application Gateway、マネージド ID に関連するインフラストラクチャ リソースをホストしているリソース グループです。

  4. 前の手順の helm-config.yaml 構成を使用して、Helm Chart application-gateway-kubernetes-ingress をインストールします。

    helm install -f <helm-config.yaml> application-gateway-kubernetes-ingress/ingress-azure
    

    または、1 つの手順の中で helm-config.yaml と Helm コマンドを組み合わせることもできます。

    helm install ./helm/ingress-azure \
         --name ingress-azure \
         --namespace default \
         --debug \
         --set appgw.name=applicationgatewayABCD \
         --set appgw.resourceGroup=your-resource-group \
         --set appgw.subscriptionId=subscription-uuid \
         --set appgw.shared=false \
         --set armAuth.type=servicePrincipal \
         --set armAuth.secretJSON=$(az ad sp create-for-rbac --role Contributor --sdk-auth | base64 -w0) \
         --set rbac.enabled=true \
         --set verbosityLevel=3 \
         --set kubernetes.watchNamespace=default \
         --set aksClusterConfiguration.apiServerAddress=aks-abcdefg.hcp.westus2.azmk8s.io
    
  5. 新しく作成されたポッドのログを確認して、正常に開始したかどうかを確認します

Azure Application Gateway を使用して、AKS サービスを HTTP または HTTPS 経由でインターネットに公開する方法については、この攻略ガイドを参照してください。

共有 Application Gateway

既定では、AGIC はリンク先の Application Gateway の完全な所有権を持っています。 AGIC バージョン 0.8.0 以降では、1 つの Application Gateway を他の Azure コンポーネントと共有できます。 たとえば、仮想マシン スケール セットでホストされているアプリと AKS クラスターに対して、同じ Application Gateway を使用できます。

この設定を有効にする前に、Application Gateway の構成をバックアップします

  1. Azure portal から、お使いの Application Gateway のインスタンスに移動します
  2. [Automation] セクションで、[テンプレートのエクスポート] を選んでから、[ダウンロード] を選びます。

ダウンロードした ZIP ファイルには、Application Gateway の復元に使用できる JSON テンプレート、bash、PowerShell スクリプトが含まれます

シナリオ例

2 つの Web サイトのトラフィックを管理する架空の Application Gateway を見てみましょう。

既定の設定では、AGIC は、それが指し示す Application Gateway の所有権を 100% 持っています。 AGIC では、すべての Application Gateway の構成が上書きされます。 prod.contoso.com 用のリスナーを、Kubernetes イングレス内に定義するのではなく、(Application Gateway 上で) 手動で作成すると、prod.contoso.com の構成は AGIC によって数秒以内に削除されます。

また、AGIC をインストールし、仮想マシン スケール セットのマシンから prod.contoso.com を提供するには、dev.contoso.com のみを構成するように AGIC を制限する必要があります。 これは、次の CRD をインスタンス化すると容易になります。

cat <<EOF | kubectl apply -f -
apiVersion: "appgw.ingress.k8s.io/v1"
kind: AzureIngressProhibitedTarget
metadata:
  name: prod-contoso-com
spec:
  hostname: prod.contoso.com
EOF

上記のコマンドによって、AzureIngressProhibitedTarget オブジェクトが作成されます。 これにより、AGIC (バージョン 0.8.0 以降) では、prod.contoso.com の Application Gateway 構成の存在を認識し、そのホスト名に関連付けられた構成を変更しないように明示的に指示します。

新しい AGIC インストールで有効にする

AGIC (バージョン 0.8.0 以降) を Application Gateway の構成のサブセットに制限するには、helm-config.yaml テンプレートを変更します。 appgw: セクションで、shared キーを追加して true に設定します。

appgw:
    subscriptionId: <subscriptionId>    # existing field
    resourceGroup: <resourceGroupName>  # existing field
    name: <applicationGatewayName>      # existing field
    shared: true                        # <<<<< Add this field to enable shared Application Gateway >>>>>

Helm の変更を適用します:

  1. AzureIngressProhibitedTarget CRD がインストールされていることを確認します:

    kubectl apply -f https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/7b55ad194e7582c47589eb9e78615042e00babf3/crds/AzureIngressProhibitedTarget-v1-CRD-v1.yaml
    
  2. Helm を更新します:

    helm upgrade \
        --recreate-pods \
        -f helm-config.yaml \
        ingress-azure application-gateway-kubernetes-ingress/ingress-azure
    

その結果、AKS クラスターには prohibit-all-targets という AzureIngressProhibitedTarget の新しいインスタンスが作成されます。

kubectl get AzureIngressProhibitedTargets prohibit-all-targets -o yaml

オブジェクト prohibit-all-targets では、名前が示すように、AGIC が "いかなる" ホストおよびパスの構成も変更することが禁止されます。 appgw.shared=true を指定して Helm をインストールすると AGIC がデプロイされますが、Application Gateway は変更されません。

アクセス許可を拡大する

appgw.shared=true と既定の prohibit-all-targets を使用した Helm では、AGIC による構成の適用がブロックされるため、AGIC のアクセス許可を拡大します。

  1. 特定のセットアップを含む次のスニペットを使って、AzureIngressProhibitedTarget という名前の新しい YAML ファイルを作成します。

    cat <<EOF | kubectl apply -f -
    apiVersion: "appgw.ingress.k8s.io/v1"
    kind: AzureIngressProhibitedTarget
    metadata:
      name: your-custom-prohibitions
    spec:
      hostname: your.own-hostname.com
    EOF
    
  2. 独自のカスタム禁止を作成した後にのみ、既定のものを削除できますが、これは過度な拡大です:

    kubectl delete AzureIngressProhibitedTarget prohibit-all-targets
    

既存の AGIC インストールに対して有効にする

AKS クラスター、Application Gateway、クラスター内の構成済みの AGIC が既に動作しているとします。 prod.contoso.com のイングレスがあり、AKS クラスターからそれに対するトラフィックを正常に提供しています。 既存の Application Gateway に staging.contoso.com を追加しますが、VM でホストする必要があります。 既存の Application Gateway を再利用し、staging.contoso.com のリスナーとバックエンド プールを手動で構成します。 しかし、Application Gateway の構成を (ポータルARM API、または Terraform を使って) 手動で調整すると、AGIC が持っている完全な所有権と競合します。 変更を適用しても、AGIC によってすぐに上書きまたは削除されます。

AGIC が構成のサブセットに変更を加えることを禁止できます。

  1. 次のスニペットを含む AzureIngressProhibitedTarget という名前の新しい YAML ファイルを作成します。

    cat <<EOF | kubectl apply -f -
    apiVersion: "appgw.ingress.k8s.io/v1"
    kind: AzureIngressProhibitedTarget
    metadata:
      name: manually-configured-staging-environment
    spec:
      hostname: staging.contoso.com
    EOF
    
  2. 新しく作成されたオブジェクトを表示します:

    kubectl get AzureIngressProhibitedTargets
    
  3. Azure portal から Application Gateway の構成を変更し、リスナー、ルーティング規則、バックエンドなどを追加します。作成した新しいオブジェクト (manually-configured-staging-environment) では、AGIC が staging.contoso.com に関連する Application Gateway の構成を上書きすることは禁止されます。