Skip to content

Commit

Permalink
Add example for multiple AKS clusters. (pulumi#190)
Browse files Browse the repository at this point in the history
* Add example for multiple AKS clusters.
  • Loading branch information
clstokes committed Dec 11, 2018
1 parent d077ad9 commit d13331d
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 0 deletions.
4 changes: 4 additions & 0 deletions azure-ts-aks-multicluster/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/bin/
/node_modules/
key.rsa
key.rsa.pub
15 changes: 15 additions & 0 deletions azure-ts-aks-multicluster/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: azure-ts-aks-multicluster
runtime: nodejs
description: Create multiple Azure Kubernetes Service (AKS) clusters in different regions and with different node counts
template:
config:
azure:environment:
description: The Azure environment to use (`public`, `usgovernment`, `german`, `china`)
default: public
password:
description: Your cluster password
secret: true
sshPublicKey:
description: Your SSH public key (generate with `ssh-keygen -t rsa -f key.rsa`)
resourceGroupLocation:
description: The location to use for the Azure Resource Group
71 changes: 71 additions & 0 deletions azure-ts-aks-multicluster/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Multiple Azure Kubernetes Service (AKS) Clusters

This example demonstrates creating multiple Azure Kubernetes Service (AKS) clusters in different regions and with
different node counts. Please see https://docs.microsoft.com/en-us/azure/aks/ for more information about AKS.

# Prerequisites

Ensure you have [downloaded and installed the Pulumi CLI](https://pulumi.io/install).

We will be deploying to Azure, so you will need an Azure account. If you don't have an account,
[sign up for free here](https://azure.microsoft.com/en-us/free/).
[Follow the instructions here](https://pulumi.io/install/azure.html) to connect Pulumi to your Azure account.

# Running the Example

> **Note**: Due to an issue in the Azure Terraform Provider (https://github.com/terraform-providers/terraform-provider-azurerm/issues/1635) the
> creation of an Azure Service Principal, which is needed to create the Kubernetes cluster (see index.ts), is delayed and may not
> be available when the cluster is created. If you get a Service Principal not found error, as a work around, you should be able to run `pulumi up`
> again, at which time the Service Principal should have been created.
After cloning this repo, `cd` into it and run these commands.

1. Create a new stack, which is an isolated deployment target for this example:

```bash
$ pulumi stack init
```

2. Set the required configuration variables for this program:

```bash
$ pulumi config set azure:environment public
$ pulumi config set password --secret [your-cluster-password-here]
$ ssh-keygen -t rsa -f key.rsa
$ pulumi config set sshPublicKey < key.rsa.pub
```

3. Deploy everything with the `pulumi up` command. This provisions all the Azure resources necessary, including
an Active Directory service principal and AKS clusters:

```bash
$ pulumi up
```

4. After a couple minutes, your AKS clusters will be ready. The AKS cluster names will be printed as output variables
once `pulumi up` completes.

```bash
$ pulumi up
...

Outputs:
+ aksClusterNames: [
+ [0]: "akscluster-east513be264"
+ [1]: "akscluster-westece285c7"
]
...
```

5. At this point, you have multiple AKS clusters running in different regions. Feel free to modify your program, and
run `pulumi up` to redeploy changes. The Pulumi CLI automatically detects what has changed and makes the minimal
edits necessary to accomplish these changes.

6. Once you are done, you can destroy all of the resources, and the stack:

```bash
$ pulumi destroy
$ pulumi stack rm
```
11 changes: 11 additions & 0 deletions azure-ts-aks-multicluster/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.

import * as azure from "@pulumi/azure";
import * as pulumi from "@pulumi/pulumi";

// Parse and export configuration variables for this stack.
const config = new pulumi.Config();
export const password = config.require("password");
export const location = config.get("location") || "East US";
export const sshPublicKey = config.require("sshPublicKey");
export const resourceGroup = new azure.core.ResourceGroup("aks", {location: location});
57 changes: 57 additions & 0 deletions azure-ts-aks-multicluster/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.

import * as azure from "@pulumi/azure";
import * as pulumi from "@pulumi/pulumi";
import * as config from "./config";

// Per-Cluster Config
const aksClusterConfig = [
{
name: 'east',
location: "East US",
nodeCount: 2,
nodeSize: "Standard_D2_v2",
},
{
name: 'west',
location: "West US",
nodeCount: 5,
nodeSize: "Standard_D2_v2",
},
];

// Create the AD service principal for the K8s cluster.
const adApp = new azure.ad.Application("aks");
const adSp = new azure.ad.ServicePrincipal("aksSp", {applicationId: adApp.applicationId});
const adSpPassword = new azure.ad.ServicePrincipalPassword("aksSpPassword", {
servicePrincipalId: adSp.id,
value: config.password,
endDate: "2099-01-01T00:00:00Z",
});

// Create the individual clusters
const k8sClusters = aksClusterConfig.map((perClusterConfig, index) => {
const cluster = new azure.containerservice.KubernetesCluster(`aksCluster-${perClusterConfig.name}`, {
resourceGroupName: config.resourceGroup.name,
location: config.location,
agentPoolProfile: {
name: "aksagentpool",
count: perClusterConfig.nodeCount,
vmSize: perClusterConfig.nodeSize,
},
dnsPrefix: `${pulumi.getStack()}-kube`,
linuxProfile: {
adminUsername: "aksuser",
sshKeys: [{
keyData: config.sshPublicKey,
}],
},
servicePrincipal: {
clientId: adApp.applicationId,
clientSecret: adSpPassword.value,
},
});
return cluster;
});

export const aksClusterNames = k8sClusters.map(cluster => cluster.name);
13 changes: 13 additions & 0 deletions azure-ts-aks-multicluster/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "azure-ts-aks-multicluster",
"version": "0.1.0",
"devDependencies": {
"@types/node": "latest"
},
"dependencies": {
"@pulumi/azure": "latest",
"@pulumi/kubernetes": "latest",
"@pulumi/pulumi": "latest"
},
"license": "Apache-2.0"
}
21 changes: 21 additions & 0 deletions azure-ts-aks-multicluster/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"stripInternal": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true
},
"files": [
"index.ts"
]
}
12 changes: 12 additions & 0 deletions misc/test/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ func TestExamples(t *testing.T) {
})
},
}),
// TODO: This test fails due to a bug in the Terraform Azure provider in which the
// service principal is not available when attempting to create the K8s cluster.
// See the azure-ts-aks-multicluster readme for more detail and
// https://github.com/terraform-providers/terraform-provider-azurerm/issues/1635.
// base.With(integration.ProgramTestOptions{
// Dir: path.Join(cwd, "..", "..", "azure-ts-aks-multicluster"),
// Config: map[string]string{
// "azure:environment": azureEnviron,
// "password": "testTEST1234+_^$",
// "sshPublicKey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDeREOgHTUgPT00PTr7iQF9JwZQ4QF1VeaLk2nHKRvWYOCiky6hDtzhmLM0k0Ib9Y7cwFbhObR+8yZpCgfSX3Hc3w2I1n6lXFpMfzr+wdbpx97N4fc1EHGUr9qT3UM1COqN6e/BEosQcMVaXSCpjqL1jeNaRDAnAS2Y3q1MFeXAvj9rwq8EHTqqAc1hW9Lq4SjSiA98STil5dGw6DWRhNtf6zs4UBy8UipKsmuXtclR0gKnoEP83ahMJOpCIjuknPZhb+HsiNjFWf+Os9U6kaS5vGrbXC8nggrVE57ow88pLCBL+3mBk1vBg6bJuLBCp2WTqRzDMhSDQ3AcWqkucGqf dremy@remthinkpad",
// },
// }),
}

// Only include the long examples on non-Short test runs
Expand Down

0 comments on commit d13331d

Please sign in to comment.