Skip to content

Commit 17a1450

Browse files
4c74356b41hausdorff
authored andcommitted
azure aks example (#237)
* azure aks example * minor fix * minor fixes
1 parent ffc8e80 commit 17a1450

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

azure-py-aks/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

azure-py-aks/Pulumi.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: azure-py-aks
2+
runtime: python
3+
description: A minimal Azure Python Pulumi program

azure-py-aks/README.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
2+
3+
# Azure Kubernetes Service (AKS) Cluster
4+
5+
This example deploys an AKS cluster, virtual network and Azure Container Registry and grants AKS permissions to access and manage those.
6+
7+
## Deploying the App
8+
9+
To deploy your infrastructure, follow the below steps.
10+
11+
### Prerequisites
12+
13+
1. [Install Pulumi](https://pulumi.io/install)
14+
2. [Install Python 3.6](https://www.python.org/downloads/)
15+
3. [Configure Azure Credentials](https://pulumi.io/install/azure.html)
16+
4. [Generate SSH Key](https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key)
17+
18+
### Steps
19+
20+
After cloning this repo, from this working directory, run these commands:
21+
22+
1. Install the required Python packages packages:
23+
24+
```bash
25+
$ pip install -r requirements.txt
26+
```
27+
28+
2. Create a new stack, which is an isolated deployment target for this example:
29+
30+
```bash
31+
$ pulumi stack init
32+
```
33+
34+
3. Set the configuration variables for this program:
35+
36+
```bash
37+
$ pulumi config set prefix all_resources_will_be_prefixed_with_this_value
38+
$ pulumi config set password service_principal_password
39+
$ pulumi config set sshkey < ~/.ssh/id_rsa.pub
40+
$ # this has a default value, so you can skip it
41+
$ pulumi config set location any_valid_azure_location_for_aks
42+
```
43+
44+
4. Stand up the AKS cluster:
45+
46+
```bash
47+
$ pulumi up
48+
```
49+
50+
5. After 10-15 minutes, your cluster will be ready, and the kubeconfig YAML you'll use to connect to the cluster will be available as an output. You can save this kubeconfig to a file like so:
51+
52+
```bash
53+
$ pulumi stack output kubeconfig > kubeconfig.yaml
54+
```
55+
56+
Once you have this file in hand, you can interact with your new cluster as usual via `kubectl`:
57+
58+
```bash
59+
$ KUBECONFIG=./kubeconfig.yaml kubectl get nodes
60+
```
61+
6. From there, feel free to experiment. Simply making edits and running `pulumi up` will incrementally update your stack.
62+
63+
7. Once you've finished experimenting, tear down your stack's resources by destroying and removing it:
64+
65+
```bash
66+
$ pulumi destroy --yes
67+
$ pulumi stack rm --yes
68+
```

azure-py-aks/__main__.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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)

azure-py-aks/requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pulumi>=0.16.4
2+
pulumi_azure>=0.16.4

0 commit comments

Comments
 (0)