Created k8s CI/CD deployment documentation

Modify k8s deployment script to support CD/CI deployment
Fix issue bootstrap lib not deployed to container in windows cli
This commit is contained in:
Ramón Tomás 2017-05-09 13:21:38 +02:00
parent 44ac659e32
commit fb9838a041
8 changed files with 130 additions and 59 deletions

48
README.CICD.k8s.md Normal file
View File

@ -0,0 +1,48 @@
# Kubernetes CI/CD VSTS
For k8s CI/CD pipeline delivery a series of tasks must be created in VSTS to deploy k8s in Azure
## Prerequisites
* A Kubernetes cluster. Follow Azure Container Service's [walkthrough](https://docs.microsoft.com/en-us/azure/container-service/container-service-kubernetes-walkthrough) to create one.
* A private Docker registry. Follow Azure Container Registry's [guide](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal) to create one.
* Optionally, previous steps can be skipped if you run gen-k8s-env.ps1 script to automatically create the azure environment needed for kubernetes deployment. Azure cli 2.0 must be previously installed [installation guide](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli). For example:
>```
>./gen-k8s-env -resourceGroupName k8sGroup -location westeurope -registryName k8sregistry -orchestratorName k8s-cluster -dnsName k8s-dns
>```
* An `Azure Blob storage`. It is needed for storing the kubernetes config file used by the hosted agent to access to Kubernetes cluster. Example:
<img src="img/k8s/blob_creation.png">
* Upload the `kubernetes config file` to the blob storage previously created. Execute the following command which will download the config file into the directory `c:\Users\<User>\.kube\` and then, upload it to your blob storage:
>```
>https://eshopk8s.blob.core.windows.net/k8s-config/config
>```
## Create the VSTS tasks
1. Create a `Download File` task to download the kubernetes binary `kubectl` to the hosted agent. For example:
>```
>https://storage.googleapis.com/kubernetes-release/release/v0.0.1.7.0-alpha.0/bin/windows/386/kubectl.exe
>```
<img src="img/k8s/get_kubectlbin_task.png">
2. Create a Download File task to download the kubernetes config file to the hosted agent. For example:
>```
>https://eshopk8s.blob.core.windows.net/k8s-config/config
>```
<img src="img/k8s/get_kubectlconfig_task.png">
3. Create a powershell task to execute the k8s deployment script. For example:
* Deployment script path
>```
>$(System.DefaultWorkingDirectory)/All Microservices/docker-compose/deploy.ps1
>```
* Deployment script path arguments. Where:
- userDockerHub: indicates if Docker Hub is used instead of ACR
- deployCI: indicates that it is a CI/CD deployment
- execPath: path where the k8s binary is stored
- kubeconfigPath: path where the k8s config file is stored
>```
>-deployCI $true -useDockerHub $true -execPath '$(System.DefaultWorkingDirectory)/' -kubeconfigPath '$(System.DefaultWorkingDirectory)/'
>```
<img src="img/k8s/deploy_script_task.png">

BIN
img/k8s/blob_creation.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -1,9 +1,27 @@
Param( Param(
[parameter(Mandatory=$true)][string]$registry, [parameter(Mandatory=$false)][string]$registry,
[parameter(Mandatory=$true)][string]$dockerUser, [parameter(Mandatory=$false)][string]$dockerUser,
[parameter(Mandatory=$true)][string]$dockerPassword [parameter(Mandatory=$false)][string]$dockerPassword,
[parameter(Mandatory=$false)][bool]$deployCI,
[parameter(Mandatory=$false)][bool]$useDockerHub,
[parameter(Mandatory=$false)][string]$execPath,
[parameter(Mandatory=$false)][string]$kubeconfigPath
) )
function ExecKube($cmd) {
if($deployCI) {
$kubeconfig = $kubeconfigPath + 'config';
$exp = $execPath + 'kubectl ' + $cmd + ' --kubeconfig=' + $kubeconfig
Invoke-Expression $exp
}
else{
$exp = $execPath + 'kubectl ' + $cmd
Invoke-Expression $exp
}
}
# Not used when deploying through CI VSTS
if(-not $deployCI) {
$requiredCommands = ("docker", "docker-compose", "kubectl") $requiredCommands = ("docker", "docker-compose", "kubectl")
foreach ($command in $requiredCommands) { foreach ($command in $requiredCommands) {
if ((Get-Command $command -ErrorAction SilentlyContinue) -eq $null) { if ((Get-Command $command -ErrorAction SilentlyContinue) -eq $null) {
@ -11,7 +29,10 @@ foreach ($command in $requiredCommands) {
exit exit
} }
} }
}
# Use ACR instead of DockerHub as image repository
if(-not $useDockerHub) {
Write-Host "Logging in to $registry" -ForegroundColor Yellow Write-Host "Logging in to $registry" -ForegroundColor Yellow
docker login -u $dockerUser -p $dockerPassword $registry docker login -u $dockerUser -p $dockerPassword $registry
if (-not $LastExitCode -eq 0) { if (-not $LastExitCode -eq 0) {
@ -20,17 +41,27 @@ if (-not $LastExitCode -eq 0) {
} }
# create registry key secret # create registry key secret
kubectl create secret docker-registry registry-key ` ExecKube -cmd 'create secret docker-registry registry-key `
--docker-server=$registry ` --docker-server=$registry `
--docker-username=$dockerUser ` --docker-username=$dockerUser `
--docker-password=$dockerPassword ` --docker-password=$dockerPassword `
--docker-email=not@used.com --docker-email=not@used.com'
}
# start sql, rabbitmq, frontend deployments # Removing previous services & deployments
kubectl create configmap config-files --from-file=nginx-conf=nginx.conf Write-Host "Removing existing services & deployments.." -ForegroundColor Yellow
kubectl label configmap config-files app=eshop ExecKube -cmd 'delete -f sql-data.yaml -f rabbitmq.yaml'
kubectl create -f sql-data.yaml -f rabbitmq.yaml -f services.yaml -f frontend.yaml ExecKube -cmd 'delete -f services.yaml -f frontend.yaml -f deployments.yaml'
ExecKube -cmd 'delete configmap config-files'
ExecKube -cmd 'delete configmap urls'
# start sql, rabbitmq, frontend deploymentsExecKube -cmd 'delete configmap config-files'
ExecKube -cmd 'create configmap config-files --from-file=nginx-conf=nginx.conf'
ExecKube -cmd 'label configmap config-files app=eshop'
ExecKube -cmd 'create -f sql-data.yaml -f rabbitmq.yaml -f services.yaml -f frontend.yaml'
# building and publishing docker images not necessary when deploying through CI VSTS
if(-not $deployCI) {
Write-Host "Building and publishing eShopOnContainers..." -ForegroundColor Yellow Write-Host "Building and publishing eShopOnContainers..." -ForegroundColor Yellow
dotnet restore ../eShopOnContainers-ServicesAndWebApps.sln dotnet restore ../eShopOnContainers-ServicesAndWebApps.sln
dotnet publish -c Release -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln dotnet publish -c Release -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln
@ -44,37 +75,43 @@ foreach ($service in $services) {
docker tag eshop/$service $registry/eshop/$service docker tag eshop/$service $registry/eshop/$service
docker push $registry/eshop/$service docker push $registry/eshop/$service
} }
}
Write-Host "Waiting for frontend's external ip..." -ForegroundColor Yellow Write-Host "Waiting for frontend's external ip..." -ForegroundColor Yellow
while ($true) { while ($true) {
$frontendUrl = kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}" $frontendUrl = & ExecKube -cmd 'get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"'
if ([bool]($frontendUrl -as [ipaddress])) { if ([bool]($frontendUrl -as [ipaddress])) {
break break
} }
Start-Sleep -s 15 Start-Sleep -s 15
} }
kubectl create configmap urls ` ExecKube -cmd 'create configmap urls `
--from-literal=BasketUrl=http://$($frontendUrl)/basket-api ` --from-literal=BasketUrl=http://$($frontendUrl)/basket-api `
--from-literal=CatalogUrl=http://$($frontendUrl)/catalog-api ` --from-literal=CatalogUrl=http://$($frontendUrl)/catalog-api `
--from-literal=IdentityUrl=http://$($frontendUrl)/identity ` --from-literal=IdentityUrl=http://$($frontendUrl)/identity `
--from-literal=OrderingUrl=http://$($frontendUrl)/ordering-api ` --from-literal=OrderingUrl=http://$($frontendUrl)/ordering-api `
--from-literal=MvcClient=http://$($frontendUrl)/webmvc ` --from-literal=MvcClient=http://$($frontendUrl)/webmvc `
--from-literal=SpaClient=http://$($frontendUrl) --from-literal=SpaClient=http://$($frontendUrl)'
kubectl label configmap urls app=eshop
ExecKube -cmd 'label configmap urls app=eshop'
Write-Host "Creating deployments..." Write-Host "Creating deployments..."
kubectl apply -f deployments.yaml ExecKube -cmd 'create -f deployments.yaml'
# not using ACR for pulling images when deploying through CI VSTS
if(-not $deployCI) {
# update deployments with the private registry before k8s tries to pull images # update deployments with the private registry before k8s tries to pull images
# (deployment templating, or Helm, would obviate this) # (deployment templating, or Helm, would obviate this)
kubectl set image -f deployments.yaml ` ExecKube -cmd 'set image -f deployments.yaml `
basket=$registry/eshop/basket.api ` basket=$registry/eshop/basket.api `
catalog=$registry/eshop/catalog.api ` catalog=$registry/eshop/catalog.api `
identity=$registry/eshop/identity.api ` identity=$registry/eshop/identity.api `
ordering=$registry/eshop/ordering.api ` ordering=$registry/eshop/ordering.api `
webmvc=$registry/eshop/webmvc ` webmvc=$registry/eshop/webmvc `
webspa=$registry/eshop/webspa webspa=$registry/eshop/webspa'
kubectl rollout resume -f deployments.yaml }
ExecKube -cmd 'rollout resume -f deployments.yaml'
Write-Host "WebSPA is exposed at http://$frontendUrl, WebMVC at http://$frontendUrl/webmvc" -ForegroundColor Yellow Write-Host "WebSPA is exposed at http://$frontendUrl, WebMVC at http://$frontendUrl/webmvc" -ForegroundColor Yellow

View File

@ -13,13 +13,6 @@
<DockerComposeProjectPath>..\..\..\docker-compose.dcproj</DockerComposeProjectPath> <DockerComposeProjectPath>..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="wwwroot\lib\bootstrap\**" />
<Content Remove="wwwroot\lib\bootstrap\**" />
<EmbeddedResource Remove="wwwroot\lib\bootstrap\**" />
<None Remove="wwwroot\lib\bootstrap\**" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.0" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.0" /> <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.0" />

View File

@ -72,13 +72,6 @@
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" /> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
</ItemGroup> </ItemGroup>
<!-- workaround for https://github.com/aspnet/websdk/issues/114 -->
<Target Name="AddGeneratedContentItems" BeforeTargets="AssignTargetPaths" DependsOnTargets="PrepareForPublish">
<ItemGroup>
<Content Include="wwwroot/**" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);@(Content)" />
</ItemGroup>
</Target>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\BuildingBlocks\HealthChecks\src\Microsoft.AspNetCore.HealthChecks\Microsoft.AspNetCore.HealthChecks.csproj" /> <ProjectReference Include="..\..\BuildingBlocks\HealthChecks\src\Microsoft.AspNetCore.HealthChecks\Microsoft.AspNetCore.HealthChecks.csproj" />
<ProjectReference Include="..\..\BuildingBlocks\HealthChecks\src\Microsoft.Extensions.HealthChecks\Microsoft.Extensions.HealthChecks.csproj" /> <ProjectReference Include="..\..\BuildingBlocks\HealthChecks\src\Microsoft.Extensions.HealthChecks\Microsoft.Extensions.HealthChecks.csproj" />