|
| 1 | +import pulumi |
| 2 | +from pulumi import ResourceOptions |
| 3 | +from pulumi_azure.core import ResourceGroup |
| 4 | +from pulumi_azure.role import Assignment |
| 5 | +from pulumi_azure.ad import Application, ServicePrincipal, ServicePrincipalPassword |
| 6 | +from pulumi_azure.containerservice import KubernetesCluster, Registry |
| 7 | +from pulumi_azure.network import VirtualNetwork, Subnet |
| 8 | + |
| 9 | +config = pulumi.Config('azure-py-aks') |
| 10 | +PREFIX = config.require('prefix') |
| 11 | +PASSWORD = config.require('password') |
| 12 | +SSHKEY = config.require('sshkey') |
| 13 | +LOCATION = config.get('location') or 'east us' |
| 14 | + |
| 15 | +# create Azure AD Application for AKS |
| 16 | +app = Application( |
| 17 | + 'aks-app', |
| 18 | + name=PREFIX + 'aks-app' |
| 19 | +) |
| 20 | + |
| 21 | +# create service principal for the application so AKS can act on behalf of the application |
| 22 | +sp = ServicePrincipal( |
| 23 | + 'aks-sp', |
| 24 | + application_id=app.application_id |
| 25 | +) |
| 26 | + |
| 27 | +# create service principal password |
| 28 | +sppwd = ServicePrincipalPassword( |
| 29 | + 'aks-sp-pwd', |
| 30 | + service_principal_id=sp.id, |
| 31 | + end_date='2025-01-01T01:02:03Z', |
| 32 | + value=PASSWORD |
| 33 | +) |
| 34 | + |
| 35 | +rg = ResourceGroup( |
| 36 | + 'rg', |
| 37 | + name=PREFIX + 'rg', |
| 38 | + location=LOCATION |
| 39 | +) |
| 40 | + |
| 41 | +vnet = VirtualNetwork( |
| 42 | + 'vnet', |
| 43 | + name=PREFIX + 'vnet', |
| 44 | + location=rg.location, |
| 45 | + resource_group_name=rg.name, |
| 46 | + address_spaces=['10.0.0.0/8'] |
| 47 | +) |
| 48 | + |
| 49 | +subnet = Subnet( |
| 50 | + 'subnet', |
| 51 | + name=PREFIX + 'subnet', |
| 52 | + resource_group_name=rg.name, |
| 53 | + address_prefix='10.0.0.0/23', |
| 54 | + virtual_network_name=vnet.name |
| 55 | +) |
| 56 | + |
| 57 | +# create Azure Container Registry to store images in |
| 58 | +acr = Registry( |
| 59 | + 'acr', |
| 60 | + name=PREFIX + 'acr', |
| 61 | + location=rg.location, |
| 62 | + resource_group_name=rg.name, |
| 63 | + sku="basic" |
| 64 | +) |
| 65 | + |
| 66 | +# assignments are needed for AKS to be able to interact with those resources |
| 67 | +acr_assignment = Assignment( |
| 68 | + 'acr-permissions', |
| 69 | + principal_id=sp.id, |
| 70 | + role_definition_name='AcrPull', |
| 71 | + scope=acr.id |
| 72 | +) |
| 73 | + |
| 74 | +subnet_assignment = Assignment( |
| 75 | + 'subnet-permissions', |
| 76 | + principal_id=sp.id, |
| 77 | + role_definition_name='Network Contributor', |
| 78 | + scope=subnet.id |
| 79 | +) |
| 80 | + |
| 81 | +aks = KubernetesCluster( |
| 82 | + 'aks', |
| 83 | + name=PREFIX + 'aks', |
| 84 | + location=rg.location, |
| 85 | + resource_group_name=rg.name, |
| 86 | + kubernetes_version="1.12.5", |
| 87 | + dns_prefix="dns", |
| 88 | + agent_pool_profile=( |
| 89 | + { |
| 90 | + "name": "type1", |
| 91 | + "count": 3, |
| 92 | + "vmSize": "Standard_B2ms", |
| 93 | + "osType": "Linux", |
| 94 | + "maxPods": 110, |
| 95 | + "vnet_subnet_id": subnet.id |
| 96 | + } |
| 97 | + ), |
| 98 | + linux_profile=( |
| 99 | + { |
| 100 | + "adminUsername": "azureuser", |
| 101 | + "ssh_key": [ |
| 102 | + { |
| 103 | + "keyData": SSHKEY |
| 104 | + } |
| 105 | + ] |
| 106 | + } |
| 107 | + ), |
| 108 | + service_principal={ |
| 109 | + "clientId": app.application_id, |
| 110 | + "clientSecret": sppwd.value |
| 111 | + }, |
| 112 | + role_based_access_control={ |
| 113 | + "enabled": "true" |
| 114 | + }, |
| 115 | + network_profile=( |
| 116 | + { |
| 117 | + "networkPlugin": "azure", |
| 118 | + "serviceCidr": "10.10.0.0/16", |
| 119 | + "dns_service_ip": "10.10.0.10", |
| 120 | + "dockerBridgeCidr": "172.17.0.1/16" |
| 121 | + } |
| 122 | + ), __opts__=ResourceOptions(depends_on=[acr_assignment, subnet_assignment]) |
| 123 | +) |
| 124 | + |
| 125 | +pulumi.export('kubeconfig', aks.kube_config_raw) |
0 commit comments