-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deployment.Kubernetes.ps1
164 lines (119 loc) · 6.22 KB
/
Deployment.Kubernetes.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[Alias('a')]
[ValidateSet('deployToProd', 'deleteDeployments', 'getInfo', 'getLogs')]
[string]$action = "getInfo",
[Parameter(Mandatory=$false)] # The appName is used as the kubernete deployment/service name and cannot contains '.'
[string]$appName = "donation.restapi.entrance", # fdotnetcorewebapp, donation-restapi-entrance
[Parameter(Mandatory=$false)]
[string]$appVersion = "1.0.4", # 1.0.3
[Parameter(Mandatory=$false)]
[string]$appUrl = "/api/info/getinfo",
[Parameter(Mandatory=$false)]
[bool]$deployService = $true,
[Parameter(Mandatory=$false)]
[bool]$waitForStatefullsets = $false,
# Fred Azure Container Registry Information
[Parameter(Mandatory=$false)]
[string]$acrName = "FredContainerRegistry", # Consider that the Azure Container `FredContainerRegistry` already exist
[Parameter(Mandatory=$false)]
[string]$myResourceGroup = "FredContainerRegistryResourceGroup",
[Parameter(Mandatory=$false)] # The full login server name for your Azure container registry. az acr show --name $acrName --query loginServer --output table
[string]$acrLoginServer = "fredcontainerregistry.azurecr.io",
[Parameter(Mandatory=$false)] # The Azure Container Registry has default username which is the name of the registry, but there is a password required when pushing a image
[string]$azureContainerRegistryPassword = $env:azureContainerRegistryPassword,
[Parameter(Mandatory=$false)]
[Alias('cls')]
[bool]$clearScreen = $true,
[Parameter(Mandatory=$false)]
[bool]$traceKubernetesCommand = $false,
[Parameter(Mandatory=$false)]
[string]$APP_ENDPOINT_IP,
[Parameter(Mandatory=$false)]
[string]$APP_ENDPOINT_PORT
)
# https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-aks
#az acr show --name $acrName --resource-group $myResourceGroup --query "id" --output tsv
#az acr show --name $acrName --query loginServer --output tsv
$appName = $appName.Replace(".", "-")
if($null -eq (Get-Module Util)) {
Import-Module "$(if($PSScriptRoot -eq '') {'.'} else {$PSScriptRoot})\Util.psm1" -Force
}
Import-Module "$(if($PSScriptRoot -eq '') {'.'} else {$PSScriptRoot})\KubernetesManager.psm1" -Force
function getKubernetesYamlFile($postFixFileName) {
return "$(getCurrentScriptPath)\Kubernetes.Templates\$appName--$postFixFileName"
}
function getAppMetadatJsonFileName() {
return [System.IO.Path]::Combine($env:TEMP, "$($appName.replace(".","-")).json")
}
function deployRelease([Hashtable]$context, [string]$message, [bool]$deployService) {
Write-HostColor $message DarkYellow
# Deploy the web app $appName from docker image on x pods
$processedFile = processFile $context (getKubernetesYamlFile("Deployment.{Params}.yaml")) # TODO:Script
$json = $kubernetesManager.create($processedFile, $true)
$kubernetesManager.printKubectlOutpuResourceInfos($json)
#$kubernetesManager.waitForDeployment($deploymentName)
if($waitForStatefullsets) {
# $kubernetesManager.waitForStatefullsets() # not implemented
}
if($deployService) {
# Deploy service/loadBalancer for the x pods
$processedFile = processFile $context (getKubernetesYamlFile("Service.{Params}.yaml"))
$serviceName = $kubernetesManager.createService($processedFile)
$kubernetesManager.waitForService($serviceName)
$waitTime = 30
Write-HostColor "Waiting $waitTime seconds before testing the web site home url"
Start-Sleep -s $waitTime # I noticed that it takes some time for the machine to be ready
# Retreive ip + port and verify home url
$loadBlancerIp = $kubernetesManager.GetServiceLoadBalancerIP($serviceName)
$loadBlancerPort = $kubernetesManager.GetServiceLoadBalancerPort($serviceName)
Write-HostColor "LoadBalancer Ip:$($loadBlancerIp), port:$($loadBlancerPort)" DarkYellow
updateJsonFileWithProperty (getAppMetadatJsonFileName) "EndPointIP" $loadBlancerIp
updateJsonFileWithProperty (getAppMetadatJsonFileName) "EndPointPort" $loadBlancerPort
$testUrl = "http://$loadBlancerIp`:$loadBlancerPort$($context.TEST_URL)"
validateUrl $testUrl
}
}
function deleteRelease([Hashtable]$context, [string]$message, [bool]$deployService) {
Write-HostColor $message DarkYellow
# Deploy the web app $appName from docker image on x pods
$processedFile = processFile $context (getKubernetesYamlFile("Deployment.{Params}.yaml")) # TODO:Script
$kubernetesManager.delete($processedFile)
if($deployService) {
# Deploy service/loadBalancer for the x pods
$processedFile = processFile $context (getKubernetesYamlFile("Service.{Params}.yaml"))
$kubernetesManager.delete($processedFile)
}
}
if($clearScreen) {
cls
}
else {
Write-Host ""
}
Write-Host "Deployment.Kubernetes " -ForegroundColor Yellow -NoNewline
Write-HostColor "action:$action, appName:$appName - $appVersion" DarkYellow
# For now pick the first cluster available
$kubernetesManager = GetKubernetesManagerInstance $acrName $acrLoginServer $azureContainerRegistryPassword ($action -eq "deployToProd") $traceKubernetesCommand
$context = @{ ENVIRONMENT = "prod"; APP_VERSION = $appVersion; APP_NAME = $appName; TEST_URL = $appUrl; APP_ENDPOINT_IP = $APP_ENDPOINT_IP; APP_ENDPOINT_PORT = $APP_ENDPOINT_PORT }
switch($action) {
deployToProd {
deployRelease $context "`r`n*** Deploy initial version v$($context.APP_VERSION) to $($context.ENVIRONMENT) ***" $deployService
}
getInfo {
$deploymentName = "$appName-deployment-$appVersion"
Write-HostColor $kubernetesManager.getForDeploymentInformation($deploymentName)
$serviceName = "$appName-service-prod"
Write-HostColor $kubernetesManager.getForServiceInformation($serviceName)
}
deleteDeployments {
deleteRelease $context "`r`n*** Delete version v$($context.APP_VERSION) to $($context.ENVIRONMENT) ***" $deployService
}
getLogs {
Write-Host "getLogs"
$podNames = $kubernetesManager.getPodNames()
$kubernetesManager.writeLogs($podNames)
}
}
Write-Host "Deployment.Kubernetes done" -ForegroundColor DarkYellow