Come installare un controller di ingresso (AGIC) di gateway applicazione usando un nuovo gateway applicazione

Le istruzioni seguenti presuppongono che gateway applicazione Controller in ingresso (AGIC) venga installato in un ambiente senza componenti preesistenti.

Strumenti da riga di comando necessari

È consigliabile usare Azure Cloud Shell per tutte le operazioni della riga di comando seguenti. Avviare la shell da shell.azure.com o facendo clic sul collegamento:

In alternativa, avviare Cloud Shell da portale di Azure usando l'icona seguente:

Portal launch

Azure Cloud Shell dispone già di tutti gli strumenti necessari. Se si sceglie di usare un altro ambiente, assicurarsi che siano installati gli strumenti da riga di comando seguenti:

Creare un'identità

Seguire questa procedura per creare un oggetto entità servizio Microsoft Entra. Registrare i appIdvalori , passworde objectId . Questi valori verranno usati nei passaggi seguenti.

  1. Creare un'entità servizio di Active Directory (altre informazioni sul controllo degli accessi in base al ruolo di Azure):

    az ad sp create-for-rbac --role Contributor --scopes /subscriptions/mySubscriptionID -o json > auth.json
    appId=$(jq -r ".appId" auth.json)
    password=$(jq -r ".password" auth.json)
    

    I appId valori e password dell'output JSON verranno usati nei passaggi seguenti

  2. Usare l'oggetto appId dell'output del comando precedente per ottenere l'oggetto id della nuova entità servizio:

    objectId=$(az ad sp show --id $appId --query "id" -o tsv)
    

    L'output di questo comando è objectId, che verrà usato nel modello di Azure Resource Manager seguente

  3. Creare il file di parametri che verrà usato nella distribuzione del modello di Azure Resource Manager in un secondo momento.

    cat <<EOF > parameters.json
    {
      "aksServicePrincipalAppId": { "value": "$appId" },
      "aksServicePrincipalClientSecret": { "value": "$password" },
      "aksServicePrincipalObjectId": { "value": "$objectId" },
      "aksEnableRBAC": { "value": false }
    }
    EOF
    

    Per distribuire un cluster abilitato per il controllo degli accessi in base al ruolo di Kubernetes, impostare il aksEnableRBAC campo su true

Distribuire componenti

Questo passaggio aggiungerà i componenti seguenti alla sottoscrizione:

  1. Scaricare il modello di Azure Resource Manager e modificare il modello in base alle esigenze.

    wget https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/master/deploy/azuredeploy.json -O template.json
    
  2. Distribuire il modello di Azure Resource Manager usando l'interfaccia della riga di comando di Azure. La distribuzione potrebbe richiedere fino a 5 minuti.

    resourceGroupName="MyResourceGroup"
    location="westus2"
    deploymentName="ingress-appgw"
    
    # create a resource group
    az group create -n $resourceGroupName -l $location
    
    # modify the template as needed
    az deployment group create \
            -g $resourceGroupName \
            -n $deploymentName \
            --template-file template.json \
            --parameters parameters.json
    
  3. Al termine della distribuzione, scaricare l'output della distribuzione in un file denominato deployment-outputs.json.

    az deployment group show -g $resourceGroupName -n $deploymentName --query "properties.outputs" -o json > deployment-outputs.json
    

Configurare gateway applicazione controller di ingresso

Con le istruzioni riportate nella sezione precedente, è stato creato e configurato un nuovo cluster del servizio Azure Kubernetes e un gateway applicazione. È ora possibile distribuire un'app di esempio e un controller di ingresso nella nuova infrastruttura Kubernetes.

Configurare le credenziali di Kubernetes

Per i passaggi seguenti, è necessario configurare il comando kubectl , che verrà usato per connettersi al nuovo cluster Kubernetes. Cloud Shell è kubectl già installato. Si userà l'interfaccia az della riga di comando per ottenere le credenziali per Kubernetes.

Ottenere le credenziali per il servizio Azure Kubernetes appena distribuito (altre informazioni):

# use the deployment-outputs.json created after deployment to get the cluster name and resource group name
aksClusterName=$(jq -r ".aksClusterName.value" deployment-outputs.json)
resourceGroupName=$(jq -r ".resourceGroupName.value" deployment-outputs.json)

az aks get-credentials --resource-group $resourceGroupName --name $aksClusterName

Installare l'identità pod di Microsoft Entra

Microsoft Entra Pod Identity fornisce l'accesso basato su token ad Azure Resource Manager (ARM).

Microsoft Entra Pod Identity aggiungerà i componenti seguenti al cluster Kubernetes:

Per installare Microsoft Entra Pod Identity nel cluster:

  • Cluster del servizio Azure Kubernetes abilitato per il controllo degli accessi in base al ruolo

    kubectl create -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/deployment-rbac.yaml
    
  • Cluster del servizio Azure Kubernetes disabilitato

    kubectl create -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/deployment.yaml
    

Installare Helm

Helm è un gestore di pacchetti per Kubernetes. Verrà usato per installare il application-gateway-kubernetes-ingress pacchetto.

Nota

Se si usa Cloud Shell, non è necessario installare Helm. Azure Cloud Shell include Helm versione 3. Ignorare il primo passaggio e aggiungere semplicemente il repository Helm AGIC.

  1. Installare Helm ed eseguire quanto segue per aggiungere application-gateway-kubernetes-ingress il pacchetto Helm:

    • Cluster del servizio Azure Kubernetes abilitato per il controllo degli accessi in base al ruolo

      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
      
    • Cluster del servizio Azure Kubernetes disabilitato

      helm init
      
  2. Aggiungere il repository Helm AGIC:

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

Installare il grafico Helm del controller di ingresso

  1. Usare il deployment-outputs.json file creato in precedenza e creare le variabili seguenti.

    applicationGatewayName=$(jq -r ".applicationGatewayName.value" deployment-outputs.json)
    resourceGroupName=$(jq -r ".resourceGroupName.value" deployment-outputs.json)
    subscriptionId=$(jq -r ".subscriptionId.value" deployment-outputs.json)
    identityClientId=$(jq -r ".identityClientId.value" deployment-outputs.json)
    identityResourceId=$(jq -r ".identityResourceId.value" deployment-outputs.json)
    
  2. Scaricare helm-config.yaml, che configurerà AGIC:

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

    In alternativa, copiare il file YAML seguente:

    # 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 will manage
    #
    appgw:
        subscriptionId: <subscriptionId>
        resourceGroup: <resourceGroupName>
        name: <applicationGatewayName>
    
        # Setting appgw.shared to "true" will create 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 will watch
    # Default value is "default"
    # Leaving this variable out or setting it to blank or empty string would
    # result in Ingress Controller observing all acessible 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>
    
  3. Modificare il file helm-config.yaml appena scaricato e compilare le sezioni appgw e armAuth.

    sed -i "s|<subscriptionId>|${subscriptionId}|g" helm-config.yaml
    sed -i "s|<resourceGroupName>|${resourceGroupName}|g" helm-config.yaml
    sed -i "s|<applicationGatewayName>|${applicationGatewayName}|g" helm-config.yaml
    sed -i "s|<identityResourceId>|${identityResourceId}|g" helm-config.yaml
    sed -i "s|<identityClientId>|${identityClientId}|g" helm-config.yaml
    

    Nota

    Per la distribuzione in cloud sovrani (ad esempio, Azure per enti pubblici), il appgw.environment parametro di configurazione deve essere aggiunto e impostato sul valore appropriato, come documentato di seguito.

    Valori:

    • verbosityLevel: imposta il livello di dettaglio dell'infrastruttura di registrazione AGIC. Per i valori possibili, vedere Livelli di registrazione.
    • appgw.environment: imposta l'ambiente cloud. Valori possibili: AZURECHINACLOUD, AZUREGERMANCLOUD, AZUREPUBLICCLOUD, AZUREUSGOVERNMENTCLOUD
    • appgw.subscriptionId: ID sottoscrizione di Azure in cui risiede gateway applicazione. Esempio: a123b234-a3b4-557d-b2df-a0bc12de1234
    • appgw.resourceGroup: nome del gruppo di risorse di Azure in cui è stato creato gateway applicazione. Esempio: app-gw-resource-group
    • appgw.name: nome del gateway applicazione. Esempio: applicationgatewayd0f0
    • appgw.shared: questo flag booleano deve essere impostato per impostazione predefinita su false. Impostare su true se è necessario un gateway applicazione condiviso.
    • kubernetes.watchNamespace: specificare lo spazio dei nomi che deve essere guardato da AGIC. Il valore dello spazio dei nomi può essere un singolo valore stringa o un elenco delimitato da virgole di spazi dei nomi.
    • armAuth.type: può essere aadPodIdentity o servicePrincipal
    • armAuth.identityResourceID: ID risorsa dell'identità gestita di Azure
    • armAuth.identityClientID: ID client dell'identità. Di seguito sono riportate altre informazioni su identityClientID .
    • armAuth.secretJSON: necessario solo quando viene scelto il tipo di segreto dell'entità servizio (quando armAuth.type è stato impostato su servicePrincipal)

    Nota

    identityClientID E identityResourceID sono valori creati durante la procedura Distribuisci componenti e possono essere ottenuti di nuovo usando il comando seguente:

    az identity show -g <resource-group> -n <identity-name>
    

    <resource-group>nel comando precedente è il gruppo di risorse del gateway applicazione. <identity-name> è il nome dell'identità creata. È possibile elencare tutte le identità per una determinata sottoscrizione usando: az identity list

  4. Installare il pacchetto del controller di ingresso del gateway applicazione:

    helm install -f helm-config.yaml --generate-name application-gateway-kubernetes-ingress/ingress-azure
    

Installare un'app di esempio

Dopo aver installato gateway applicazione, servizio Azure Kubernetes e AGIC, è possibile installare un'app di esempio tramite Azure Cloud Shell:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: aspnetapp
  labels:
    app: aspnetapp
spec:
  containers:
  - image: "mcr.microsoft.com/dotnet/samples:aspnetapp"
    name: aspnetapp-image
    ports:
    - containerPort: 8080
      protocol: TCP

---

apiVersion: v1
kind: Service
metadata:
  name: aspnetapp
spec:
  selector:
    app: aspnetapp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: aspnetapp
  annotations:
    kubernetes.io/ingress.class: azure/application-gateway
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Exact
        backend:
          service:
            name: aspnetapp
            port:
              number: 80
        pathType: Exact
EOF

In alternativa, è possibile:

  • Scaricare il file YAML precedente:

    curl https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/master/docs/examples/aspnetapp.yaml -o aspnetapp.yaml
    
  • Applicare il file YAML:

    kubectl apply -f aspnetapp.yaml
    

Altri esempi

Questa guida pratica contiene altri esempi su come esporre un servizio servizio Azure Kubernetes tramite HTTP o HTTPS a Internet con gateway applicazione.