diff --git a/README.k8s.md b/README.k8s.md index 28ebd49e8..c6f3eb322 100644 --- a/README.k8s.md +++ b/README.k8s.md @@ -23,6 +23,53 @@ The k8s directory contains Kubernetes configuration for the eShopOnContainers ap Once the user and password are retrieved, run the following script for deployment. For example: >``` ->./deploy.ps1 -registry myregistry.azurecr.io -dockerUser User -dockerPassword SecretPassword +>./deploy.ps1 -registry myregistry.azurecr.io -dockerUser User -dockerPassword SecretPassword -configFile file_with_config.json >``` -The script will build the code and corresponding Docker images, push the latter to your registry, and deploy the application to your cluster. You can watch the deployment unfold from the Kubernetes web interface: run `kubectl proxy` and open a browser to [http://localhost:8001/ui](http://localhost:8001/ui) + +The parameter `configFile` is important (and mandatory) because it contains the configuration used for the Pods in Kubernetes. This allow deploying Pods that use your own resources in Azure or any other cloud provider. A configuration file `local.json` is provided which configures Pods to use the infrastructure containers (that is sql server, rabbitmq, redis and mongodb must be deployed also in the k8s). + +The script will build the code and corresponding Docker images, push the later to your registry, and deploy the application to your cluster. You can watch the deployment unfold from the Kubernetes web interface: run `kubectl proxy` and open a browser to [http://localhost:8001/ui](http://localhost:8001/ui) + +### Pods configuration file + +When deploying to k8s the script needs the `configFile` with the location of a JSON configuration file. This file contains the configuration of the pods. The file is a JSON file. For reference another configuration file (cloud.json) is provided but without valid values. + +If you deploy the infrastructure containers use `local.json` as a value for `configFile` parameter. If you don't deploy the infrastructure containers use your own configuration file with the correct values. + +### Parameters of the deploy.ps1 script + +The script accepts following parameters: + ++ `registry`: Name of the Docker registry to use. If not passed DockerHub is assumed ++ `dockerUser`: Login to use for the Docker registry (if needed) ++ `dockerPassword`: Password to use for the Docker registry (if needed) ++ `execPath`: Location of `kubectl` (if not in the path). If passed must finish with the path character. ++ `kubeconfigPath`: Location of the `kubectl` configuration file. **This parameter is used only in the CI pipeline**, so you don't need to pass it when invoking the script using the CLI. ++ `configFile`: Location of the JSON file with the configuration of the pods. **This parameter is mandatory** ++ `imageTag`: Tag of the images to deploy to k8s. If not passed the name of the current branch is used. ++ `externalDns`: External DNS name of the k8s. This is only needed if you have configured a DNS that points to your k8s external IP. If you don't have any DNS configured do not pass this parameter. ++ `deployCI`: If `true` means that script is running under the context of a VSTS Hosted Build Agent. **You should never use this parameter from CLI** ++ `buildBits`: means that the source code of eShopOnContainers will be built. If you have built your code (and have all projects published in `obj/Docker/publish`) do not pass this parameter. Default value is `false` ++ `buildImages`: If `true` (default value) Docker images are built and pushed in the Docker registry. If you set this parameter to `false`, Docker images won't be built nor pushed in the Docker registry (but k8s' deployments and services will be redeployed). ++ `deployInfrastructure`: If `true` infrastructure containers (rabbitmq, mongo, redis, sql) will be deployed in k8s. If `false` those containers (and its related deployments and services in k8s) won't be deployed. ++ `dockerOrg`: Name of the organization in the registry where the images are (or will be pushed). Default value is `eshop` (which has images provided by Microsoft) + +### Typical usages of the script: + +Build all projects, and deploy all them in k8s including infrastructure containers in a organization called `foo` in Docker Hub. Images will be tagged with my current git branch and containers will use the configuration set in `local.json` file: + +``` +./deploy.ps1 -buildBits $true -dockerOrg foo -dockerUser MY_USER -dockerPassword MY_PASSWORD -configFile local.json +``` + +Do not build any project and don't rebuild docker images. Create k8s deployments that will pull images from my private repository, in the `foo` organization, using the tag `latest`. Containers will use the configuration set in `cloud.json` file. + +``` +./deploy.ps1 -buildImages false -dockerOrg foo -registry MY_REGISTRY_FQDN -dockerUser MY_USER -dockerPassword MY_PASSWORD -configFile cloud.json -imageTag master +``` + +Deploy k8s using public images that Microsoft provides: + +``` +./deploy.ps1 -buildImages false --configFile local.json -imageTag master +``` \ No newline at end of file diff --git a/k8s/deploy.ps1 b/k8s/deploy.ps1 index 5b6c5732f..95a62b790 100644 --- a/k8s/deploy.ps1 +++ b/k8s/deploy.ps1 @@ -8,8 +8,10 @@ Param( [parameter(Mandatory=$false)][string]$imageTag, [parameter(Mandatory=$false)][string]$externalDns, [parameter(Mandatory=$false)][bool]$deployCI=$false, - [parameter(Mandatory=$false)][bool]$buildImages=$false, - [parameter(Mandatory=$false)][bool]$deployInfrastructure=$true + [parameter(Mandatory=$false)][bool]$buildImages=$true, + [parameter(Mandatory=$false)][bool]$buildBits=$false, + [parameter(Mandatory=$false)][bool]$deployInfrastructure=$true, + [parameter(Mandatory=$false)][string]$dockerOrg="eshop" ) function ExecKube($cmd) { @@ -39,6 +41,7 @@ if(-not $deployCI) { } } else { + $buildBits = false; $buildImages = false; # Never build images through CI, as they previously built } @@ -61,27 +64,38 @@ if ($debugMode) { } # building and publishing docker images if needed -if($buildImages) { +if($buildBits) { Write-Host "Building and publishing eShopOnContainers..." -ForegroundColor Yellow dotnet restore ../eShopOnContainers-ServicesAndWebApps.sln dotnet publish -c Release -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln - +} +if ($buildImages) { Write-Host "Building Docker images tagged with '$imageTag'" -ForegroundColor Yellow $env:TAG=$imageTag docker-compose -p .. -f ../docker-compose.yml build - Write-Host "Pushing images to $registry..." -ForegroundColor Yellow + Write-Host "Pushing images to $registry/$dockerOrg..." -ForegroundColor Yellow $services = ("basket.api", "catalog.api", "identity.api", "ordering.api", "marketing.api","payment.api","locations.api", "webmvc", "webspa", "webstatus") + foreach ($service in $services) { - docker tag eshop/${service}:$imageTag $registry/eshop/${service}:$imageTag - docker push $registry/eshop/${service}:$imageTag + $imageFqdn = if ($useDockerHub) {"$dockerOrg/${service}"} else {"$registry/$dockerOrg/${service}"} + docker tag eshop/${service}:$imageTag ${imageFqdn}:$imageTag + docker push ${imageFqdn}:$imageTag } } -# Use ACR instead of DockerHub as image repository -if(-not $useDockerHub) { - Write-Host "Logging in to $registry" -ForegroundColor Yellow - docker login -u $dockerUser -p $dockerPassword $registry +# if we have login/pwd add the secret to k8s +if (-not [string]::IsNullOrEmpty($dockerUser)) { + $registryFDQN = if (-not $useDockerHub) {$registry} else {"index.docker.io/v1/"} + + Write-Host "Logging in to $registryFDQN as user $dockerUser" -ForegroundColor Yellow + if ($useDockerHub) { + docker login -u $dockerUser -p $dockerPassword + } + else { + docker login -u $dockerUser -p $dockerPassword $registryFDQN + } + if (-not $LastExitCode -eq 0) { Write-Host "Login failed" -ForegroundColor Red exit @@ -89,7 +103,7 @@ if(-not $useDockerHub) { # create registry key secret ExecKube -cmd 'create secret docker-registry registry-key ` - --docker-server=$registry ` + --docker-server=$registryFDQN ` --docker-username=$dockerUser ` --docker-password=$dockerPassword ` --docker-email=not@used.com' @@ -178,22 +192,23 @@ Write-Host "Creating deployments..." -ForegroundColor Yellow ExecKube -cmd 'create -f deployments.yaml' # update deployments with the correct image (with tag and/or registry) -Write-Host "Update Image containers to use prefix '$registry' and tag '$imageTag'" -ForegroundColor Yellow $registryPath = "" if (-not [string]::IsNullOrEmpty($registry)) { $registryPath = "$registry/" } -ExecKube -cmd 'set image deployments/basket basket=${registryPath}eshop/basket.api:$imageTag' -ExecKube -cmd 'set image deployments/catalog catalog=${registryPath}eshop/catalog.api:$imageTag' -ExecKube -cmd 'set image deployments/identity identity=${registryPath}eshop/identity.api:$imageTag' -ExecKube -cmd 'set image deployments/ordering ordering=${registryPath}eshop/ordering.api:$imageTag' -ExecKube -cmd 'set image deployments/marketing marketing=${registryPath}eshop/marketing.api:$imageTag' -ExecKube -cmd 'set image deployments/locations locations=${registryPath}eshop/locations.api:$imageTag' -ExecKube -cmd 'set image deployments/payment payment=${registryPath}eshop/payment.api:$imageTag' -ExecKube -cmd 'set image deployments/webmvc webmvc=${registryPath}eshop/webmvc:$imageTag' -ExecKube -cmd 'set image deployments/webstatus webstatus=${registryPath}eshop/webstatus:$imageTag' -ExecKube -cmd 'set image deployments/webspa webspa=${registryPath}eshop/webspa:$imageTag' +Write-Host "Update Image containers to use prefix '$registry$dockerOrg' and tag '$imageTag'" -ForegroundColor Yellow + +ExecKube -cmd 'set image deployments/basket basket=${registryPath}${dockerOrg}/basket.api:$imageTag' +ExecKube -cmd 'set image deployments/catalog catalog=${registryPath}${dockerOrg}/catalog.api:$imageTag' +ExecKube -cmd 'set image deployments/identity identity=${registryPath}${dockerOrg}/identity.api:$imageTag' +ExecKube -cmd 'set image deployments/ordering ordering=${registryPath}${dockerOrg}/ordering.api:$imageTag' +ExecKube -cmd 'set image deployments/marketing marketing=${registryPath}${dockerOrg}/marketing.api:$imageTag' +ExecKube -cmd 'set image deployments/locations locations=${registryPath}${dockerOrg}/locations.api:$imageTag' +ExecKube -cmd 'set image deployments/payment payment=${registryPath}${dockerOrg}/payment.api:$imageTag' +ExecKube -cmd 'set image deployments/webmvc webmvc=${registryPath}${dockerOrg}/webmvc:$imageTag' +ExecKube -cmd 'set image deployments/webstatus webstatus=${registryPath}${dockerOrg}/webstatus:$imageTag' +ExecKube -cmd 'set image deployments/webspa webspa=${registryPath}${dockerOrg}/webspa:$imageTag' Write-Host "Execute rollout..." -ForegroundColor Yellow ExecKube -cmd 'rollout resume deployments/basket'