Skip to content

Commit 78a28d1

Browse files
committed
Additional documentation
1 parent da93d10 commit 78a28d1

7 files changed

+175
-17
lines changed

Build-Containers.ps1

+18
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,26 @@ Function Build-Container([string]$ContainerTag, [string]$BuildContextFolderName,
2323
$dockerfile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, "$BuildContextFolderName/Dockerfile"))
2424

2525
docker build -t $ContainerTag -f $dockerfile $buildContext
26+
27+
if ($LastExitCode -ne 0) {
28+
throw "Could not build the image $ContainerTag from file: $dockerfile for context: $buildContext"
29+
}
30+
2631
docker tag $ContainerTag $registryUrl/$ContainerTag
2732

33+
if ($LastExitCode -ne 0) {
34+
throw "Could not tag the image $ContainerTag for $registryUrl/$ContainerTag"
35+
}
36+
2837
Write-Host "[PUSH TO REPOSITORY] Pushing $ContainerTag container to $RegistryUrl..." -foreground "green"
2938

3039
docker push $RegistryUrl/$ContainerTag
3140

41+
if ($LastExitCode -ne 0) {
42+
throw "Could not push the image $RegistryUrl/$ContainerTag"
43+
}
44+
45+
3246
Write-Host "[FINISHED] Finished building $ContainerTag container" -foreground "green"
3347
}
3448

@@ -47,6 +61,10 @@ $registryUrl = ("cadevcache" + $EnvironmentTag + "registry.azurecr.io")
4761

4862
docker login $registryUrl -u $registryUsername -p $registryPassword
4963

64+
if ($LastExitCode -ne 0) {
65+
throw "Could not login to the docker registry"
66+
}
67+
5068
Build-Container -ContainerTag "devicecache-frontend:$VersionTag" -BuildContextFolderName "DeviceCache.Frontend" -RegistryUrl $registryUrl
5169
Build-Container -ContainerTag "devicecache-processor:$VersionTag" -BuildContextFolderName "DeviceCache.Processor" -RegistryUrl $registryUrl
5270
Build-Container -ContainerTag "devicecache-simulator:$VersionTag" -BuildContextFolderName "DeviceCache.Simulator" -RegistryUrl $registryUrl
File renamed without changes.

Create-Infrastructure.ps1

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ $eventHubTemplateFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine(
3333
$registryTemplateFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, "DeviceCache.Infrastructure/Registry.json"))
3434
$clusterTemplateFile = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PSScriptRoot, "DeviceCache.Infrastructure/Cluster.json"))
3535

36-
$automationKeyVaultName = "ca-automation-$EnvironmentTag"
37-
$automationKeyVault = Get-AzureRmKeyVault -VaultName $automationKeyVaultName -ErrorAction SilentlyContinue
38-
39-
if (-not $automationKeyVault) {
40-
throw "Automation key vault not found. Make sure you run the Create-Prerequisites.ps1 script first."
41-
}
42-
4336
$keyVaultName = "ca-devcache-$EnvironmentTag"
4437
Create-KeyVault -KeyVaultName $keyVaultName -ResourceGroupName $resourceGroupName -ResourceGroupLocation $ResourceGroupLocation
4538

@@ -54,6 +47,13 @@ $registryTemplateParameters["EnvironmentTag"] = $EnvironmentTag
5447
DeployTemplate -ResourceGroupName $resourceGroupName -TemplateFileFullPath $registryTemplateFile -TemplateParameters $registryTemplateParameters
5548

5649
if (-not $SkipCluster) {
50+
$automationKeyVaultName = "ca-automation-$EnvironmentTag"
51+
$automationKeyVault = Get-AzureRmKeyVault -VaultName $automationKeyVaultName -ErrorAction SilentlyContinue
52+
53+
if (-not $automationKeyVault) {
54+
throw "Automation key vault required for the cluster not found. Make sure you run the Create-CloudClusterPrerequisites script first."
55+
}
56+
5757
$clusterManagerId = (Get-AzureKeyVaultSecret -VaultName $automationKeyVaultName -SecretName servicePrincipalId).SecretValueText
5858
$clusterManagerKey = (Get-AzureKeyVaultSecret -VaultName $automationKeyVaultName -SecretName servicePrincipalPassword).SecretValue
5959
$sshPublicKey = (Get-AzureKeyVaultSecret -VaultName $automationKeyVaultName -SecretName machineSshPublicKey).SecretValueText

docs/cloud-prerequisites.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Prerequisites for cloud cluster deployment
2+
3+
In case you will be also deploying a remote cluster with ACS (Create-Infrastructure.ps1 script without the -SkipCluster flag), you will need to setup some prerequisites for such deployment:
4+
5+
- Subscription admin service principal credentials (**you need to provide this**)
6+
- Public SSH key (**you need to provide this**)
7+
8+
All the prerequistes will be automatically saved in a key vault which can be accessed later in other scripts.
9+
10+
## Getting a service principal credentials
11+
12+
To create and get the service principal credentials, simply follow these [instructions](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal)(scroll down to "Create an Azure Active Directory application" section). You will need the application ID and the key from your principal.
13+
14+
## Getting a public SSH key
15+
16+
For SSH access, you will need a private / public key pair. For setting these up under windows, you can find [some instructions here](https://docs.microsoft.com/en-us/azure/virtual-machines/linux/ssh-from-windows).
17+
18+
You could also use some of the tools like **Bitvise** which are capable of creating private / public keys themselves.
19+
20+
**Tip**: if you used Azure CLI to provision ACS clusters already, you can simply reuse the public key found in `C:\Users\<<your user>>\.ssh`
21+
22+
## Running the Create-CloudClusterPrerequisites.ps1 script
23+
24+
First, execute this line to securely enter your credentials for the service principal:
25+
26+
```powershell
27+
$servicePrincipalCredentials = Get-Credential
28+
```
29+
30+
Afterwards, you can execute the Create-Prerequisites.ps1 script itself:
31+
32+
``` powershell
33+
.\Create-CloudClusterPrerequisites.ps1 -EnvironmentTag <<your_env_tag>> -MachineSshPublicKey "ssh-rsa AAAA...6SkIQ0opBt" -ServicePrincipalCredentials $servicePrincipalCredentials
34+
```
35+
36+
If everything was setup correctly, you can verify the contents of your key vault in the cloud:
37+
38+
![Secrets inside key vault](keyvault_with_secrets.png)
39+
40+
Key vault name will be `ca-automation-<<your_env_tag>>`

docs/keyvault_with_secrets.png

16.4 KB
Loading

docs/roadmap.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Roadmap
2+
13
~~Infrastructure incl. scripting~~
24

35
~~Packaging / parametrization with Helm~~
@@ -14,6 +16,6 @@
1416

1517
Start to finish documentation with commands (in progress)
1618

17-
Prometheus / Grafana instance
19+
Prometheus / Grafana instance (in progress)
1820

1921
Exposing load metrics from services

docs/setup.md

+107-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Setup
2+
13
## Required Software:
24

35
**Editor**: Visual Studio 2017 / Visual Studio Code
@@ -12,23 +14,119 @@
1214

1315
**Minikube** (for local development): https://github.com/kubernetes/minikube
1416

15-
**Azure CLI 2.0**
17+
**Azure CLI 2.0** (can be run as [docker image](https://hub.docker.com/r/microsoft/azure-cli/))
18+
19+
## Concept of environments
20+
21+
All the PS scripts have the mandatory `-EnvironmentTag` flag which needs to be set. This flag is one of the best practices when writing Infrastructure-as-Code scripts to be able to reuse same scripts between multiple environments. It also helps when multiple people are using the same scripts for their "local" environments so that they get no conflicts in the cloud (each has its own environment).
22+
23+
Important here to know is:
24+
- choose a 2-4 letter [a-Z] tag which you want
25+
- provide the same tag to all the scripts you will be executing
26+
27+
## Prerequisites
28+
29+
This project can be run in two ways:
30+
- with a local cluster (e.g. minikube) while the PaaS components are in cloud (e.g. Event Hub)
31+
- completely in cloud (cluster with Azure Container Services)
32+
33+
Depending whether you want local or cloud deployment, there are some small differences in setup.
34+
35+
If you want to deploy the sample to Azure Container Services (cloud), you need to run the `Create-CloudClusterPrerequisites.ps1` script which should setup a KeyVault with deployment-time secrets (service principal, SSH Key, passwords and such) neccessary for Azure Container Services cluster to created.
36+
37+
More info here: [Runnning Create-CloudClusterPrerequisites script](cloud-prerequisites.md)
1638

1739
## Infrastructure Setup
1840

19-
For setting up the neccessary Azure Infrastructure (Infrastructure-as-Code) for the code to run, you can use the Create-Infrastructure.ps1 script. However, this script has a dependency on a Key Vault which should contain deployment-time secrets (service principal, SSH Key, passwords and such).
41+
For setting up the neccessary Azure Infrastructure (Infrastructure-as-Code) for the code to run, you can use the `Create-Infrastructure.ps1` script.
42+
43+
First, make sure you log in to your Azure Subscription with
44+
45+
```powershell
46+
Login-AzureRmAccount
47+
```
48+
49+
and, if necessary, switch to the correct subscription using
50+
51+
```powershell
52+
# to find out the subscirption id, run Get-AzureRmSubscription
53+
Select-AzureRmSubscription
54+
```
55+
56+
You can execute the `Create-Infrastructure.ps1` script now. In case if you will be working with a local cluster (e.g. minikube), run the following:
57+
58+
```powershell
59+
.\Create-Infrastructure.ps1 -EnvironmentTag <<set_tag_here>> -SkipCluster
60+
```
61+
62+
If you are going to be using a cloud cluster (ACS), omit the -SkipCluster flag.
63+
64+
```powershell
65+
# make sure you execute the Create-CloudClusterPrerequisites.ps1 script first!
66+
# more info above in this document, or read cloud-prerequisites.md
67+
.\Create-Infrastructure.ps1 -EnvironmentTag <<your_tag_here>>
68+
```
69+
70+
Script should finish without any errors.
2071

21-
To create such Key Vault with all required secrets, run the Create-Prerequisites.ps1 script.
22-
First, execute this like to securely enter your credentials for the service principal:
72+
## Initialize the cluster
73+
74+
Before you start deploying the microservices, your cluster needs to be "initialized" first. What this actually means is that we need to write in a secret for private docker registry connections and that we need to initialize [Helm](https://helm.sh/) for doing the actual deployments.
75+
76+
#### Local cluster
77+
78+
If you already installed minikube and are planning to deploy locally, you can go ahead and run the script.
79+
80+
```powershell
81+
.\Initialize-Cluster.ps1 -EnvironmentTag <<your_tag_here>>
82+
```
83+
84+
#### Cloud cluster
85+
86+
If you are planning to use the cloud cluster (ACS), you need to configure your kubectl tool first. Easiest way to do this is through Azure CLI:
87+
88+
```bash
89+
az acs kubernetes get-credentials --resource-group=... --name=...
90+
```
91+
92+
Afterwards, simple run the script:
93+
94+
```powershell
95+
.\Initialize-Cluster.ps1 -EnvironmentTag <<your_tag_here>>
96+
```
97+
98+
#### Kubectl & contexts
99+
100+
Important thing to realise here is that many of the tools like Helm and scripts you will be using in this project are supporting [Kubectl contexts](https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/) directly. One such context for your local cluster is setup when starting minikube, and the `az acs kubernetes get-credentials ... ` command above also sets up a context for your remote ACS cluster.
101+
102+
## Building the containers
103+
104+
**Short answer:**
23105

24106
```powershell
25-
$servicePrincipalCredentials = Get-Credential
107+
# builds all the containers and pushes them to a remove environment specific docker registry (ACR)
108+
.\Build-Containers.ps1 -EnvironmentTag <<your_tag_here>>
26109
```
27110

28-
Afterwards, you can execute the Create-Prerequisites.ps1 script itself:
111+
**Long answer:**
112+
113+
One of the things that was setup in previous step, was a private Docker Registry in form of Azure Container Registry. For both scenarios, local and cloud, we will be using this registry to roll out the containers onto the Kubernetes cluster.
114+
115+
Each of the microservices in this project has a Dockerfile which can be used out of the box and could do the classic docker build / docker tag / docker login / docker push if you wanted (or you can use something like [Draft](https://github.com/Azure/draft))
29116

30-
``` powershell
31-
.\Create-Prerequisites.ps1 -EnvironmentTag "white" -MachineSshPublicKey "ssh-rsa AAAA...6SkIQ0opBt" -ServicePrincipalCredentials $servicePrincipalCredentials
117+
## Deploying the application
118+
119+
Simply use the Deploy-Application.ps1 script:
120+
121+
```powershell
122+
# will use your current kubectl context for deployment target
123+
.\Deploy-Application.ps1 -EnvironmentTag <<your_tag_here>>
32124
```
33125

34-
Warning: make sure to delete the resources since they incure Azure costs! :)
126+
Once installed, you will be able to see the pods with `kubectl get pods`.
127+
128+
`helm list` will also show you your new release.
129+
130+
## Deleting the application
131+
132+
Simply run `helm delete <<release_name>>` with the release name you got from `helm list`.

0 commit comments

Comments
 (0)