Skip to content

Latest commit

 

History

History

scripts

README.md

Scripts

The scripts in this folder create the Azure Kubernetes Service (AKS) cluster that the samples and tutorials run on, and install optional add-ons into it. They work the same way against Azure and against the LocalStack for Azure emulator: see Run against LocalStack.

Cluster Scripts

Every cluster script creates the local-aks-test cluster in the local-rg resource group, together with the localacrtest container registry that every sample pushes its image to, attached to the cluster with --attach-acr. The scripts are idempotent: an existing resource is kept, so a script can be re-run after a failure.

Script Description Features
01-basic-cluster.sh No questions: the smallest and fastest cluster to provision, for demos. To save time it turns off the Azure Files and Azure Disk CSI drivers and the CSI snapshot controller, so it runs every sample except web-app-file-storage and web-app-in-cluster-postgresql. System-assigned identity, Azure CNI Overlay with the Azure network policy, one node with the cluster autoscaler (1 to 3 nodes), Microsoft Entra Workload ID, Entra ID authentication with Azure RBAC (admin groups from AAD_ADMIN_GROUP_OBJECT_IDS), Advanced Container Networking Services (ACNS), container registry. Azure Files and Azure Disk CSI drivers and CSI snapshot controller off.
01-configurable-cluster.sh Asks which optional features to provision. Every question has a default and an environment variable, so the script also runs unattended. Pressing Enter at every question provisions the same features as the two scripts below. Each feature of the scripts below can be turned off, plus the choice of cluster identity and network policy. Optionally installs NGINX Gateway Fabric and cert-manager.
01-system-assigned-managed-identity.sh The full, production-like cluster, with a system-assigned identity. The samples' CI uses it. Custom virtual network with Azure Bastion, Key Vault with the CSI secrets provider, Container Insights, diagnostic settings on every resource, a user node pool across availability zones, cluster autoscaler, Workload ID, KEDA, managed Gateway API CRDs, Entra ID with Azure RBAC, ACNS, container registry.
01-user-assigned-managed-identity.sh The same full cluster with a pre-created user-assigned identity, which also gets the Contributor role on the node resource group. As above, with a user-assigned identity.

What the samples and tutorials need from the cluster:

Architecture

The following architecture is deployed by 01-user-assigned-managed-identity.sh and, with a user-assigned identity, by the Bicep and Terraform deployments.

Architecture of the AKS cluster

With a system-assigned identity, Azure creates the cluster identity (item 6) together with the cluster. The architecture is composed of the following Azure resources:

  1. Azure Resource Group <prefix>-rg: A logical container scoping the resources below. AKS creates a second one for the cluster's infrastructure, the node resource group (item 10).

  2. Azure Key Vault <prefix>-kv-<suffix> with the Azure RBAC permission model: A vault whose secrets pods can mount as files through the Azure Key Vault Secrets Provider add-on. The add-on's identity (item 10) holds Key Vault Administrator on it. No sample needs it: tutorials/key-vault-csi-driver creates its own vault.

  3. Azure Log Analytics workspace <prefix>-log-analytics-<suffix> (PerGB2018 pricing tier, system-assigned identity): Stores the Container Insights data of the cluster, such as the ContainerLogV2, KubeEvents and KubePodInventory tables, and the resource logs and metrics that diagnostic settings named default send from the cluster, the virtual network, the Bastion host and its public IP address, the registry and the key vault. See Monitoring.

  4. Azure Virtual Network <prefix>-vnet-<suffix> (10.0.0.0/8): Hosts three subnets:

    • SystemSubnet (10.240.0.0/16): The nodes of the system node pool.
    • UserSubnet (10.241.0.0/16): The nodes of the user node pool.
    • AzureBastionSubnet (10.242.2.0/24): The Bastion host.

    The two node subnets have the Microsoft.Storage service endpoint, so that in NFS mode the storage account of web-app-file-storage can accept traffic from the nodes only.

  5. Azure Bastion <prefix>-bastion-<suffix> (Basic SKU), with the Standard static public IP address <prefix>-bastion-ip-<suffix>: Reaches the nodes over SSH from the Azure portal, without exposing them to the internet. On Azure, creating it takes several minutes.

  6. User-assigned managed identity <prefix>-aks-identity-<suffix>: The cluster identity, which the control plane uses to manage the cluster's Azure resources. It is created before the cluster and assigned to it. It holds Network Contributor on the virtual network and on SystemSubnet, which a cluster in a custom virtual network needs, and Contributor on the node resource group, which AKS grants.

  7. Azure Container Registry <prefix>acr<suffix> (Basic SKU, admin user enabled): Stores the container images of the samples. It is attached to the cluster: the kubelet identity (item 10) holds the AcrPull role on it, so the nodes pull the images without credentials.

  8. Azure Kubernetes Service (AKS) cluster <prefix>-aks-<suffix>, on the newest generally available Kubernetes version of the region:

    • Azure CNI Overlay networking: the nodes take their addresses from the subnets, the pods from 192.168.0.0/16 outside the virtual network, and the services from 172.16.0.0/16, with the cluster DNS service at 172.16.0.10. The network policy is Azure, Cilium (with the Cilium data plane) or Calico, as chosen in the scripts' menu, the networkOption parameter in Bicep or the network_option variable in Terraform.
    • Microsoft Entra ID authentication with Azure RBAC for Kubernetes authorization: the members of the cluster's admin groups are cluster administrators, and any other user or identity needs an Azure Kubernetes Service RBAC role, such as Azure Kubernetes Service RBAC Cluster Admin. kubectl signs in with kubelogin.
    • The OIDC issuer and Microsoft Entra Workload ID: a pod signs in to Azure services as a managed identity through a federated credential, without secrets. The samples that need it create their own identities and credentials.
    • Add-ons and features: Container Insights (the monitoring add-on), the Azure Key Vault Secrets Provider, KEDA, the managed Gateway API CRDs, Advanced Container Networking Services, and the Azure Disk and Azure Files CSI drivers with the snapshot controller.
  9. Node pools: the system node pool system in SystemSubnet, created with the cluster, and the user node pool user in UserSubnet, spread across availability zones 1, 2 and 3. Both run Azure Linux on Standard_D4ds_v5 virtual machines with 50 GiB ephemeral OS disks, up to 100 pods per node, and the cluster autoscaler between 1 and 3 nodes.

  10. Node resource group MC_<prefix>-rg_<prefix>-aks-<suffix>_<location>, created and managed by AKS: Holds the virtual machine scale sets of the two node pools, the Standard load balancer kubernetes with the public IP address of the cluster's outbound traffic, the network security group of the nodes, and the disks and storage accounts that the CSI drivers create for persistent volumes. It also holds two user-assigned managed identities: the kubelet identity <prefix>-aks-<suffix>-agentpool, which pulls the images from the registry (item 7), and azurekeyvaultsecretsprovider-<prefix>-aks-<suffix>, the identity of the Key Vault Secrets Provider (item 2).

  11. Data collection rule MSCI-<location>-<prefix>-aks-<suffix>, in the resource group, with its association to the cluster: Tells the Container Insights agent on the nodes what to collect, and sends the data to the workspace (item 3). The monitoring add-on creates it for the scripts; the Bicep and Terraform templates define it themselves.

Every deployment ends with az aks get-credentials, which merges the cluster into your kubeconfig. Each sample then pushes its image to the registry and deploys into its own namespace: the nodes pull the image through the kubelet identity, the samples that use Workload ID reach Azure services without secrets, and the logs of every pod land in the ContainerLogV2 table of the workspace, where the KQL queries in the sample READMEs read them.

Configurable script

Questions and variables

Each question is skipped when its variable is set. Yes/no variables accept true, false, yes, no, y and n, in any case. Setting the variable of a question that is not asked, such as CREATE_BASTION=true with CREATE_VNET=false, is an error.

Variable Default Effect
CLUSTER_IDENTITY system system, or user to create the local-aks-identity-test identity and assign it to the cluster.
NETWORK_OPTION 1 Azure CNI Overlay with 1 the Azure network policy, 2 the Cilium dataplane and network policy, 3 the Calico network policy.
SYSTEM_NODE_COUNT 1 Nodes in the system node pool.
ENABLE_CLUSTER_AUTOSCALER true The cluster autoscaler on both node pools, between 1 and 3 nodes (widened to include the node count).
ENABLE_WORKLOAD_IDENTITY true The OIDC issuer and Microsoft Entra Workload ID.
CREATE_KEY_VAULT true The local-kv-test vault, the azure-keyvault-secrets-provider add-on, and the Key Vault Administrator role for the add-on identity.
ENABLE_MONITORING true The local-log-analytics-test workspace, the monitoring add-on (Container Insights), and the default diagnostic settings of the cluster's resources.
ENABLE_FILE_CSI_DRIVER true The Azure Files CSI driver and its azurefile* storage classes. false passes --disable-file-driver.
ENABLE_DISK_CSI_DRIVER true The Azure Disk CSI driver and its managed-csi* storage classes. false passes --disable-disk-driver.
CREATE_VNET true The local-vnet-test virtual network with SystemSubnet, plus UserSubnet when there is a user node pool, the Microsoft.Storage service endpoint on the node subnets (only with the Azure Files CSI driver, whose NFS volumes need it), and the Network Contributor role for the cluster identity. Without it, AKS creates and manages the virtual network.
CREATE_BASTION true Asked only with the custom virtual network: AzureBastionSubnet, a public IP address and the local-bastion-test host.
CREATE_USER_NODE_POOL true The user node pool.
USER_NODE_COUNT 1 Asked only with the user node pool: its number of nodes.
USE_AVAILABILITY_ZONES true Asked only with the user node pool: spreads it across availability zones 1, 2 and 3.
ENABLE_KEDA true The KEDA add-on.
ENABLE_GATEWAY_API true The managed Gateway API CRDs (--enable-gateway-api). Needs azure-cli 2.86.0 or later, and no aks-preview extension.
INSTALL_GATEWAY_CONTROLLERS false Asked only with Gateway API: installs NGINX Gateway Fabric, which provides the nginx GatewayClass, and cert-manager with Helm. Needs helm.
ENABLE_ENTRA_RBAC true Microsoft Entra ID authentication with Azure RBAC for Kubernetes authorization.
AAD_ADMIN_GROUP_OBJECT_IDS <your-admin-group-object-id> Also read by 01-basic-cluster.sh, which always enables Entra ID. Asked only with Entra ID: the comma-separated object IDs of the admin groups. The placeholder works on the emulator; on Azure, set real group object IDs. On Azure, kubectl also needs kubelogin to sign in to a cluster with Entra ID (az aks install-cli installs it), and your account in one of these groups or in an Azure Kubernetes Service RBAC role on the cluster.
ASSUME_YES false The same as --yes: skip the confirmation prompt.
DRY_RUN false The same as --dry-run.

ACNS has no question: it is always on, as in the original scripts.

Running unattended

When stdin is closed, every question that has no variable takes its default:

# Every default: the same features as the original scripts
./01-configurable-cluster.sh --yes </dev/null

# A light demo cluster: a user-assigned identity and Workload ID, nothing else optional
CLUSTER_IDENTITY=user ENABLE_CLUSTER_AUTOSCALER=false CREATE_KEY_VAULT=false ENABLE_MONITORING=false CREATE_VNET=false \
  CREATE_USER_NODE_POOL=false ENABLE_KEDA=false ENABLE_GATEWAY_API=false ENABLE_ENTRA_RBAC=false \
  ./01-configurable-cluster.sh --yes

Dry run

--dry-run prints the configuration and then every command that a run against an empty resource group would execute, in order, without running any of them. It needs no az login, and values that a real run reads from Azure appear as placeholders such as <system-subnet-id>:

CREATE_VNET=false ./01-configurable-cluster.sh --dry-run </dev/null

01-basic-cluster.sh takes --dry-run and --yes too.

Differences from the original scripts

With every default, the configurable script creates the same resources as the original scripts, with these differences:

  • Automatic upgrades are off (--auto-upgrade-channel none --node-os-upgrade-channel None), so a demo cluster does not upgrade itself, and the Free tier is passed explicitly.
  • No Windows administrator credentials are set, because the cluster has no Windows node pool.
  • ~/.ssh/id_rsa.pub is passed when it exists; without it, the cluster is created with --no-ssh-key instead of failing.
  • The container registry, Log Analytics workspace, Key Vault and virtual network are created in parallel, and the Bastion host while the cluster is being created.
  • The summary names the subscription the resources go to before asking to proceed, because the same command reaches Azure or the emulator depending on the Azure CLI configuration.
  • A re-run keeps the Helm releases of NGINX Gateway Fabric and cert-manager that are already deployed instead of upgrading them.
  • When the cluster already exists but was built with different answers, the script stops before creating anything and lists the differences, because most of them cannot be changed on an existing cluster.

Monitoring

01-system-assigned-managed-identity.sh, 01-user-assigned-managed-identity.sh and 01-configurable-cluster.sh (with ENABLE_MONITORING=true, the default) create a diagnostic setting called default on each resource below. It sends the resource's platform metrics (AllMetrics) and these log categories to the local-log-analytics-test workspace. The configurable script skips the resources its answers leave out, and 01-basic-cluster.sh creates no workspace and no diagnostic settings.

Resource Log categories
AKS cluster local-aks-test kube-apiserver, kube-audit-admin, kube-controller-manager, kube-scheduler, cluster-autoscaler, cloud-controller-manager, guard, csi-azurefile-controller, csi-snapshot-controller
Virtual network local-vnet-test VMProtectionAlerts
Public IP address of the Bastion host local-bastion-ip-test DDoSProtectionNotifications, DDoSMitigationFlowLogs, DDoSMitigationReports
Bastion host local-bastion-test BastionAuditLogs
Container registry localacrtest ContainerRegistryRepositoryEvents, ContainerRegistryLoginEvents
Key vault local-kv-test AuditEvent, AzurePolicyEvaluationDetails

The AKS logs go to the resource-specific tables, such as AKSAuditAdmin and AKSControlPlane, instead of AzureDiagnostics. kube-audit is left out on purpose: it adds every get and list request to what kube-audit-admin collects, and Microsoft warns that it can incur substantial cost. To collect it anyway, add {"category": "kube-audit", "enabled": true} to the AKS cluster's --logs.

To list the diagnostic settings of a resource, such as the cluster:

az monitor diagnostic-settings list \
  --resource "$(az aks show --name local-aks-test --resource-group local-rg --query id --output tsv)"

tests/test-diagnostic-settings.sh checks that every resource of the cluster has its default diagnostic setting.

Add-on installers

Run these once the cluster is up.

Script Installs Notes
02-install-prometheus.sh The kube-prometheus-stack Helm chart in the prometheus namespace.
03-install-nginx-ingress-controller.sh The ingress-nginx controller in the ingress-basic namespace.
04-install-gateway-api.sh The standard-channel Gateway API CRDs, v1.5.1, managed by you. Do not run it on a cluster with the managed Gateway API CRDs: the original scripts and 01-configurable-cluster.sh with its defaults install those. It suits 01-basic-cluster.sh clusters.
05-install-nginx-gateway-fabric.sh NGINX Gateway Fabric, which creates the nginx GatewayClass. Needs the Gateway API CRDs. 01-configurable-cluster.sh installs it when INSTALL_GATEWAY_CONTROLLERS=true.
06-install-cert-manager.sh cert-manager with Gateway API support, and the letsencrypt-nginx ClusterIssuer for ingress-nginx. 01-configurable-cluster.sh installs cert-manager, without the ClusterIssuer, when INSTALL_GATEWAY_CONTROLLERS=true.

tutorials/gateway-api installs NGINX Gateway Fabric itself, so run it on a cluster created with INSTALL_GATEWAY_CONTROLLERS=false, the default.