Cara menginstal Application Gateway Ingress Controller (AGIC) Menggunakan Application Gateway Baru

Instruksi di bawah ini mengasumsikan Application Gateway Ingress Controller (AGIC) akan diinstal di lingkungan tanpa komponen yang sudah ada sebelumnya.

Alat Baris Perintah yang Diperlukan

Kami merekomendasikan penggunaan Azure Cloud Shell untuk semua operasi baris perintah di bawah. Luncurkan shell Anda dari shell.azure.com atau dengan mengklik tautan:

Atau, luncurkan Cloud Shell dari portal Microsoft Azure yang menggunakan ikon berikut:

Portal launch

Azure Cloud Shell Anda telah memiliki semua alat yang diperlukan. Jika Anda memilih untuk menggunakan lingkungan lain, pastikan alat baris perintah berikut diinstal:

Buat Identitas

Ikuti langkah-langkah di bawah ini untuk membuat objek perwakilan layanan Microsoft Entra. appIdRekam nilai , password, dan objectId - nilai ini akan digunakan dalam langkah-langkah berikut.

  1. Buat perwakilan layanan AD (Baca lebih lanjut tentang Azure RBAC):

    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)
    

    Nilai appId dan password dari output JSON akan digunakan dalam langkah-langkah berikut

  2. Gunakan appId dari output perintah sebelumnya untuk mendapatkan id perwakilan layanan baru:

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

    Output dari perintah ini adalah objectId, yang akan digunakan dalam templat Azure Resource Manager di bawah ini

  3. Buat file parameter yang akan digunakan dalam penyebaran templat Azure Resource Manager nantinya.

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

    Untuk menyebarkanRBAC Kube yang diaktifkan kluster, set aksEnableRBAC bidang ke true

Sebarkan Komponen

Langkah ini akan menambahkan komponen berikut ke langganan Anda:

  1. Unduh templat Azure Resource Manager dan ubah templat sesuai kebutuhan.

    wget https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/master/deploy/azuredeploy.json -O template.json
    
  2. Sebarkan templat Azure Resource Manager menggunakan Azure CLI. Penyebaran mungkin memakan waktu hingga 5 menit.

    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. Setelah penyebaran selesai, unduh output penyebaran ke dalam file yang dinamaideployment-outputs.json.

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

Siapkan Application Gateway Ingress Controller

Dengan instruksi di bagian sebelumnya, kami membuat dan mengkonfigurasi klaster AKS baru dan Application Gateway. Kami sekarang siap untuk menyebarkan aplikasi sampel dan pengontrol ingress ke infrastruktur Kubernetes baru kami.

Menyiapkan Kredensial Kubernetes

Untuk langkah-langkah berikut, kita memerlukan perintah kubectl penyiapan, yang akan kita gunakan untuk terhubung ke kluster Kubernetes baru. Cloud Shell telah kubectl terinstal. Kita akan menggunakan az CLI untuk mendapatkan kredensial untuk Kubernetes.

Dapatkan kredensial untuk AKS Anda yang baru disebarkan (baca lebih lanjutt):

# 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

Menginstal Identitas Pod Microsoft Entra

Microsoft Entra Pod Identity menyediakan akses berbasis token ke Azure Resource Manager (ARM).

Microsoft Entra Pod Identity akan menambahkan komponen berikut ke kluster Kubernetes Anda:

Untuk menginstal Identitas Pod Microsoft Entra ke kluster Anda:

  • RBAC Kube diaktifkan kluster AKS

    kubectl create -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/deployment-rbac.yaml
    
  • RBAC Kube dinonaktifkan kluster AKS

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

Menginstal Helm

Helm adalah manajer paket untuk Kube. Kami akan menggunakannya untuk menginstal application-gateway-kubernetes-ingress paket.

Catatan

Jika Anda menggunakan Cloud Shell, Anda tidak perlu menginstal Helm. Azure Cloud Shell dilengkapi dengan Helm versi 3. Lewati langkah pertama dan cukup tambahkan repositori AGIC Helm.

  1. Pasang Helm dan jalankan perintah berikut untuk menambahkan application-gateway-kubernetes-ingress paket helm:

    • RBAC Kube diaktifkan kluster 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
      
    • RBAC Kube dinonaktifkan kluster AKS

      helm init
      
  2. Tambahkan repositori AGIC Helm:

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

Pasang Bagan Helm Pengontrol Ingress

  1. Gunakan deployment-outputs.json file yang dibuat di atas dan buat variabel berikut.

    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. Unduh helm-config.yaml, yang akan mengkonfigurasikan AGIC:

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

    Atau salin file YAML di bawah ini:

    # 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. Edit helm-config.yaml yang baru diunduh dan isi bagian appgw dan 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
    

    Catatan

    Untuk menyebarkan ke Sovereign Clouds (misalnya, Azure Government), appgw.environment parameter konfigurasi harus ditambahkan dan diatur ke nilai yang sesuai seperti yang didokumentasikan di bawah ini.

    Nilai:

    • verbosityLevel: Set tingkat verbositas infrastruktur pembuatan log AGIC. Lihat Tingkat Pembuatan Log untuk kemungkinan nilai.
    • appgw.environment: Mengatur lingkungan cloud. Nilai yang mungkin: AZURECHINACLOUD, AZUREGERMANCLOUD, AZUREPUBLICCLOUD, AZUREUSGOVERNMENTCLOUD
    • appgw.subscriptionId: ID Langganan Azure tempat Application Gateway berada. Contoh: a123b234-a3b4-557d-b2df-a0bc12de1234
    • appgw.resourceGroup: Nama Grup Sumber Daya Azure tempat Application Gateway dibuat. Contoh: app-gw-resource-group
    • appgw.name: Nama Application Gateway. Contoh: applicationgatewayd0f0
    • appgw.shared: Bendera boolean ini harus diatur default ke false. Atur ke truejika Anda memerlukan Application Gateway Bersama.
    • kubernetes.watchNamespace: Tentukan namespace layanan yang harus ditonton AGIC. Nilai namespace dapat berupa nilai string tunggal, atau daftar namespace yang dipisahkan koma.
    • armAuth.type: bisa aadPodIdentity atau servicePrincipal
    • armAuth.identityResourceID: ID Sumber Daya Identitas Terkelola Azure
    • armAuth.identityClientID: ID Identitas Klien. Informasi selengkapnya tentang identityClientID disediakan di bawah ini.
    • armAuth.secretJSON: Hanya diperlukan ketika jenis Rahasia Perwakilan Layanan dipilih (ketika armAuth.type telah diatur ke servicePrincipal)

    Catatan

    identityResourceID dan identityClientID merupakan nilai yang dibuat selama tahap Komponen Sebar, dan dapat diperoleh lagi menggunakan perintah berikut:

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

    <resource-group> dalam perintah di atas adalah grup sumber daya Application Gateway Anda. <identity-name> adalah nama identitas yang dibuat. Semua identitas untuk langganan tertentu dapat didaftar menggunakan: az identity list

  4. Instal paket pengontrol ingress Application Gateway:

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

Instal Aplikasi Sampel

Sekarang setelah menginstal Application Gateway, AKS, dan AGIC, kami dapat menginstal aplikasi sampel melalui 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

Atau, Anda dapat:

  • Unduh file YAML di atas:

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

    kubectl apply -f aspnetapp.yaml
    

Contoh Lain

Panduan cara penggunaan ini memuat lebih banyak contoh tentang cara membuka layanan AKS melalui HTTP atau HTTPS, ke Internet dengan Application Gateway.