Home

Awesome

Tailwind Traders Backend Services

Build status

This repository contains all code + deployment scripts for the Tailwind Traders Backend.

Table of contents

<a name="repositories"></a>Repositories

For this demo reference, we built several consumer and line-of-business applications and a set of backend services. You can find all repositories in the following locations:

<a name="deployment-scenarios"></a>Deployment scenarios

Tailwind Traders supports two deployment scenarios:

  1. Deploy Tailwind Traders Backend on Azure AKS and Azure resources (CosmosDb and Storage accounts)
  2. Deploy Tailwind Traders Backend on Windows and Linux containers in AKS

Service Principal

A Service Principal is needed for creating the AKS. If you use the CLI for create the resources, you can reuse a SP one passing to the script the id and password as optional parameters; if not, the script will create a new one for you and will print the details (id and password among them).

In case you use Azure Portal for the resources' creation, you can also reuse a SP or create manually a new one for passing the credentials to the template.

If you want to create a Service Principal, you can do so via the CLI

az ad sp create-for-rbac --skip-assignment --name myAKSClusterServicePrincipal

The output is similar to the following example. Make a note of your own appId and password. These values are used when you create an AKS cluster throughout this guide.

{
  "appId": "559513bd-0c19-4c1a-87cd-851a26afd5fc",
  "displayName": "myAKSClusterServicePrincipal",
  "name": "http://myAKSClusterServicePrincipal",
  "password": "e763725a-5eee-40e8-a466-dc88d980f415",
  "tenant": "72f988bf-86f1-41af-91ab-2d7cd011db48"
}

Deploy using one script

You can deploy all basics scenarios using one script under /Deploy folder.

.\Deploy-Unified.ps1 -resourceGroup <resource-group-name> -location <location> -clientId <service-principal-id> -password <service-principal-password> -subscription <subscription-id>

The process will take few minutes.

.\Deploy-Unified-WinLinux.ps1 -resourceGroup <resource-group-name> -location <location> -clientId <service-principal-id> -password <service-principal-password> -subscription <subscription-id> -deployWinLinux $true -rewardsResourceGroup <resource-group-rewards-name> -rewardsDbPassword <database-rewards-password>

The process will take few minutes, more than Linux scenario, it will create an Azure Kubernetes Service with Windows and Linux nodes.

In addition to the following documentation you can also deploy infrastructure and services step by step.

<a name="deploy-resources"></a>Deploy Tailwind Traders on AKS and Azure Resources (CosmosDb and Storage accounts)

To run Tailwind Traders you need to create the Azure infrastructure. There are two ways to do it. Using Azure portal or using a Powershell script.

<a name="create-infrastructure-portal"></a>Step 1 - Option 1: Creating infrastructure using Azure Portal

An ARM script is provided so you can automate the creation of the resources required for the backend services just clicking following button:

<a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FMicrosoft%2FTailwindTraders-Backend%2Fmain%2FDeploy%2Farm%2Fdeployment.json"><img src="Documents/Images/deploy-to-azure.png" alt="Deploy to Azure"/></a>

Azure portal will ask you for the following parameters:

The deployment could take more than 10 minutes, and once finished all needed resources will be created:

Resource group with all azure resources created

<a name="create-infrastructure-cli"></a>Step 1 - Option 2: Create the resources using the CLI

You can use the CLI to deploy the ARM script. Open a Powershell window from the /Deploy/powershell folder and run the Deploy-Arm-Azure.ps1 with following parameters:

You can optionally pass two additional parameters:

If these two parameters are not passed a new service principal will be created.

There is an additional optional parameters to control some aspects of what is created:

Once script finishes, everything is installed. If a service principal has been created, the script will output the service principal details - please, take note of the appId and password properties for use them in the AKS deployment

<a name="deploy-aks"></a>Step 2: Deploy Backend services on AKS

Pre-requisites for this deployment are to have:

Connecting kubectl to AKS

From the terminal type:

At this point if you type kubectl config current-context the name of your AKS cluster should be displayed. That means that kubectl is ready to use your AKS

Configuring services

Before deploying services using Helm, you need to setup the configuration. We refer to the configuration file with the name of gvalues file. This file contains all secrets and connection strings so beware to not commit in your repo accidentally.

An example of this file is in helm/gvalues.yaml. The deployment scripts use this file by default, but do not rely on editing this file. Instead create a copy of it a folder outside the repository and use the -valuesFile parameter of the deployment script.

Note: The folder /Deploy/helm/__values/ is added to .gitignore, so you can keep all your configuration files in it, to avoid accidental pushes.

Note: If you come from the Windows and Linux containers scenario you must add the Rewards database's connection string, in the values file you are using, for example:

inf:
(...)
  db:
  (...)
    rewards:
      host: tcp:*****.database.windows.net
      port: "1433"
      catalog: rewardsdb # you must not modify this name
      user: ttuser
      pwd: YourPassword
    (...)

Please refer to the comments of the file for its usage. Just ignore (but not delete) the tls section (it is used if TLS is enabled).

Auto generating the configuration file

Generating a valid gvalues file can be a bit harder, so there is a Powershell script that can do all work by you. This script assumes that all resources are deployed in the same resource group, and this resource group contains only the Tailwind Traders resources. Also assumes the Azure resources have been created using the tools provided in this repo.

Note The Generate-Config.ps1 uses the application-insights CLI extension to find the application insights id. Install it with az extension add --name application-insights

To auto-generate your gvalues file just go to /Deploy/powershell folder and from a Powershell window, type the following:

.\Generate-Config.ps1 -resourceGroup <your-resource-group> -outputFile ..\helm\__values\<name-of-your-file>

The parameters that Generate-Config.ps1 accepts are:

The script checks that all needed resources exists in the resource group. If some resource is missing or there is an unexpected resource, the script exits.

If you come from the Windows and Linux containers in AKS scenario and you want to use the rewards registration service you have to pass also the following parameters:

Otherwise the script will disable the rewards registration service.

Create secrets on the AKS

Docker images are stored in a ACR (a private Docker Registry hosted in Azure).

Before deploying anything on AKS, a secret must be installed to allow AKS to connect to the ACR through a Kubernetes' service account.

To do so from a Powershell terminal run the ./Create-Secret.ps1 inside powershell folder with following parameters:

This will create the secret in AKS using ACR credentials. If ACR login is not enabled you can create a secret by using a service principal. In case that ACR is not created with administrator rights you will have to provide the service principal clientId and secret:

Build & deploy images to ACR

You can manually use docker-compose to build and push the images to the ACR. If using compose you can set following environment variables:

Once set, you can use docker-compose build and docker-compose push to build and push the images.

Additionaly there is a Powershell script in the Deploy folder, named Build-Push.ps1. You can use this script for building and pushing ALL images to ACR. Parameters of this script are:

This script uses az CLI to get ACR information, and then uses docker-compose to build and push the images to ACR.

To build and push images tagged with v1 to a ACR named my-acr in resource group named my-rg, execute the following command inside /Deploy/powershell

.\Build-Push.ps1 -resourceGroup my-rg -dockerTag v1 -acrName my-acr

To just push the images (without building them before):

.\Build-Push.ps1 -resourceGroup my-rg -dockerTag v1 -acrName my-acr -dockerBuild $false

If you want to deploy the rewards registration image just call this command with the isWindows parameter set to true.

Notes:

Limit the used resources for the services

You can set the CPU and RAM limit and request consumption values for each one of the services, editing the values in its corresponding values.yaml, under the field resources:

resources:
  limits:
    cpu: "500m"
  requests:
    cpu: "100m"

Enabling SSL/TLS on the cluster (optional BUT highly recommended)

Tailwind Traders can be deployed with TLS (https) support. For this to work a TLS/SSL certificate must be installed on the Kubernetes cluster. Three options are provided:

If Let's Encrypt is choose, then cert-manager is used. Cert-manager allows auto-provisioning of TLS certificates using Let's Encrypt and ACME protocol. The certificate is requested, created and installed on the server without any manual intervention.

Using Let's Encrypt and Cert manager

To enable SSL/TLS support you must do it before deploying your images. The first step is to add cert-manager to the cluster by running ./Add-Cert-Manager.ps1. It has no parameters and use helm to configure cert-manager in the cluster. This needs to be done only once.

Then you should run ./Enable-Ssl.ps1 with following parameters:

Output of the script will be something like following:

NAME:   tailwindtraders-ssl
LAST DEPLOYED: Fri Dec 21 11:32:00 2018
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1alpha1/Certificate
NAME             AGE
tt-cert-staging  0s

==> v1alpha1/Issuer
NAME                 AGE
letsencrypt-staging  0s

You can verify that the issuer object is created using kubectl get issuers:

PS> kubectl get issuers
NAME                  AGE
letsencrypt-staging   4m

You can verify that the certificate object is created using kubectl get certificates:

PS> kubectl get certificates
NAME              AGE
tt-cert-staging   4m

The certificate object is not the real SSL/TLS certificate but a definition on how get one from Let's Encrypt. The certificate itself is stored in a secret, called letsencrypt-staging (or letsencrypt-prod). You should see a secret named tt-letsencrypt-xxxx (where xxxx is either staging or prod).

PS> kubectl get secrets
NAME                  TYPE                                  DATA      AGE
acr-auth              kubernetes.io/dockerconfigjson        1         2d
default-token-6tm9t   kubernetes.io/service-account-token   3         3d
letsencrypt-prod      Opaque                                1         3h
letsencrypt-staging   Opaque                                1         4h
tt-letsencrypt-prod   kubernetes.io/tls                     2         5m
ttsa-token-rkjlg      kubernetes.io/service-account-token   3         2d

The SSL/TLS secret names are:

At this point the support for SSL/TLS is installed, and you can install Tailwind Traders Backend on the cluster.

Note: You don't need to do this again, unless you want to change the domain of the SSL/TLS certificate. In this case you need to remove the issuer and certificate objects (using helm delete tailwindtraders-ssl --purge and then reinstall again)

Remember Staging certificates are not trusted, so browsers will complain about it, exactly in the same way that they complain about a self-signed certificate. The only purpose is to test all the deployment works, but in any production environment you must use the prod environment. In development/test environments is recommended to install the staging certificates and then trust those certificates in the developers' machines. You can download the Let's Encrypt staging certificates from their web.

Another way to validate your certificate deployment is doing a kubectl describe cert tt-cert-staging (or tt-cert-prod). In the Events section you should see that the certificate has been obtained:

Events:
  Type    Reason          Age   From          Message
  ----    ------          ----  ----          -------
  Normal  CreateOrder     10m   cert-manager  Created new ACME order, attempting validation...
  Normal  DomainVerified  9m    cert-manager  Domain "e43cd6ae16f344a093dc.eastus.aksapp.io" verified with "http-01" validation
  Normal  IssueCert       9m    cert-manager  Issuing certificate...
  Normal  CertObtained    9m    cert-manager  Obtained certificate from ACME server
  Normal  CertIssued      9m    cert-manager  Certificate issued successfully

Use custom Certificate

If you already have a TLS certificate from your certificate authority you can deploy it on the server. Using Powershell, run the Enable-Ssl.ps1 script with following parameters:

If you have a .pfx file you need to convert it to the separate .crt and .key files:

# Extract encrypted key from pfx file
openssl pkcs12 -in certfile.pfx -nocerts -out keyfile-encrypted.key
# Unencrypt key file
openssl rsa -in keyfile-encrypted.key -out keyfile.key
# Extract certificate file from pfx file
openssl pkcs12 -in certfile.pfx -clcerts -nokeys -out certfile.crt

Deploying services

Note: If you want to add SSL/TLS support on the cluster (needed to use https on the web) please read Enabling SSL/TLS on the cluster section before installing the backend.

You can deploy Tailwind Traders using a custom domain or in the domain created by Http Application Routing (if enabled). If you are using a custom domain be sure to:

If tlsHost is not passed, the script will assume that Http Application Routing is installed in the AKS. If the script has problems detecting the host name verify that the AKS has http_application_routing enabled.

More information

You need to use Powershell and run ./Deploy-Images-Aks.ps1 with following parameters:

This script will install all services using Helm and your custom configuration from the configuration file set by -valuesFile parameter.

The parameter charts allow for a selective installation of charts. Is a list of comma-separated values that mandates the services to deploy in the AKS. Values are:

So, using charts pp,st will only install the popular products and the stock api.

If you want to deploy the whole win-linux environment (with rewards registration pod) use -charts "*,rr.

Deploying the images on the storage

To deploy the needed images on the Azure Storage account just run the /Deploy/Deploy-Pictures-Azure.ps1 script, with following parameters:

Script will create blob containers and copy the images (located in /Deploy/tailwindtraders-images folder) to the storage account.


<a name="deploy-win-linux-containers"></a>Using AKS with Windows and Linux containers

This version allows us to deploy Windows and Linux containers. We need to create and Azure Kubernetes Service (AKS) with 1.14 version. This AKS version is in preview, so you must execute the following command:

az extension add --name aks-preview

We have added an ARM template so you can automate the creation of the resources required for the backend services.

Click the following button to deploy:

<a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FMicrosoft%2FTailwindTraders-Backend%2Fmain%2FDeploy%2Farm%2Fdeployment-dual-nodes.json"><img src="./Documents/Images/deploy-to-azure.png" alt="Deploy to Azure"/></a>

For mixed (Windows and Linux containers) scenario we need to deploy Tailwind Traders Rewards. The data base deployed in Tailwind Traders Rewards is used by a WCF service of this project.

Follow the Step 2: Deploy AKS to deploy the services to AKS.

| Note: In code is important to set RegisterUsers variable true to test all the features.


<a name="run-backend-locally"></a>Run Backend Services Locally

The easiest way to run your backend services locally is using Compose. To run the services type docker-compose up from terminal located in ./Source folder. This will build (if needed) the Docker images and bring up all the containers.

Note: Only Linux containers are supported currently.

Configurate containers

There are some services that connect to a CosmosDb database, hence you require to provide cosmosdb host and key using environment variables, or even better, through an .env file.

To do so, just create a file named .env in the same ./Source folder with following content pointing to your previously created in the Azure portal:

COSMOSDB_HOST=<Url of your CosmosDb>
COSMOSDB_AUTHKEY=<AuthKey of your CosmosDb>

Connect to CosmosDb emulator from docker container

If you are using Windows, you can run the CosmosDb emulator. If using it, follow this steps.

Generate and install a cert that allows host.docker.internal to be trusted

  1. Run PowerShell as Administrator
  2. Go to CosmosDb emulator folder cd "c:\Program Files\Azure Cosmos DB Emulator"
  3. Generate certificate for docker dns .\Microsoft.Azure.Cosmos.Emulator.exe /GenCert=host.docker.internal
  4. To export the certificate you can follow this guide (https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator-export-ssl-certificates#export-emulator-certificate)
  5. Rename the .cer exported certificate to cosmosdbcert.crt and place it in the /Source folder of the repo
  6. Modify Product.Api and Profile.Api Dockerfiles in line 7 to install the certificate
WORKDIR /usr/local/share/ca-certificates
COPY ./cosmosdbcert.crt .
RUN update-ca-certificates
  1. Enable network access on emulator: To enable network access for the first time, shut down the emulator and delete the emulator's data directory %LOCALAPPDATA%\CosmosDBEmulator. Then, run .\Microsoft.Azure.Cosmos.Emulator.exe /FailOnSslCertificateNameMismatch /allownetworkaccess /Key=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==

  2. add following .env file:

COSMOSDB_HOST=https://host.docker.internal:8081/
COSMOSDB_AUTHKEY=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==
  1. Hit F5 and the collections should be created in the emulator. (https://localhost:8081/_explorer/index.html)

Running using Visual Studio

To run the Backend using Visual Studio, just open the Tailwind.Traders.Backend.sln, and set "Docker-compose" as startup project and run the solution. Visual Studio will use the compose file to build and run all the containers.

<a name="run-bridge2ks"></a>Running using Bridge to Kubernetes

Tailwind Traders supports Bridge to Kubernetes. Follow the steps in this document to use Bridge to kubernetes with Tailwind Traders.

Requirements

Note Tailwind Traders has been tested with Bridge to Kubernetes version:

Bridge to Kubernetes
v1.0.120210126

Configuring Bridge to Kubernetes

Bridge to Kubernetes will work with the following Tailwind Traders APIs in this repo and serve as examples of how easy it is to get started debugging Kubernetes Microservices:

In each of the above folders, you will find a .vscode folder with the following files:

These files already contain the configuration code for Visual Studio Code to connect the existing debuggers (.NET or Node) to Bridge to Kubernetes. Note: These examples assume you have Tailwind Traders deployed to a Kubernetes namespace with the name: twt. If you have them deployed to a namespace with a different name, you will have to update the "targetNamespace" in the tasks.json folder for your API of choice.

Here are some samples of the launch.json:

nodeJS debug with Kubernetes sample:
{
    "type": "node",
    "request": "launch",
    "name": "Launch Program with Kubernetes",
    "skipFiles": [
        "<node_internals>/**"
    ],
    "program": "${workspaceFolder}/bin/www",
    "preLaunchTask": "bridge-to-kubernetes.service",
    "env": {
        "GRPC_DNS_RESOLVER": "native"
    }
},
.NET 5.0 debug with Kubernetes sample:
{
    "name": ".NET Launch with Kubernetes",
    "type": "coreclr",
    "request": "launch",
    "preLaunchTask": "bridge-to-kubernetes.compound",
    "program": "${workspaceFolder}/bin/Debug/net5.0/Tailwind.Traders.Login.Api.dll",
    "args": [],
    "cwd": "${workspaceFolder}",
    "console": "internalConsole",
    "stopAtEntry": false,
    "env": {
        "GRPC_DNS_RESOLVER": "native",
        "ASPNETCORE_URLS": "http://+:5000"
    }
},

The tasks.json will contain the tasks to launch the bridge-to-kubernetes.service with isolated addresses. Here are some samples:

nodeJS bridge-to-kubernetes.service sample:
{
  "label": "bridge-to-kubernetes.service",
  "type": "bridge-to-kubernetes.service",
  "service": "cart",
  "ports": [
    3000
  ],
  "targetCluster": "tailwindtradersakscnnn2hudd2oae",
  "targetNamespace": "twt",
  "isolateAs": "twtsample-f325"
}

.NET 5.0 bridge-to-kubernetes.service sample:
{
    "label": "bridge-to-kubernetes.service",
    "type": "bridge-to-kubernetes.service",
    "service": "login",
    "ports": [
        5000
    ],
    "targetCluster": "tailwindtradersakscnnn2hudd2oae",
    "targetNamespace": "twt",
    "isolateAs": "twtsample-8bbd"
},
{
    "label": "bridge-to-kubernetes.compound",
    "dependsOn": [
        "bridge-to-kubernetes.service",
        "build"
    ],
    "dependsOrder": "sequence"
}

Creating your own configurations

First, you will need to make sure your local system has access to your Kubernetes Cluster, and that you are in the namespace with your application deployed.

Second, you will need to ensure you have a Run and Debug configuration (launch.json) existing for your application.

Once you have your launch.json for your application code working locally, you are ready to add your own Bridge to Kubernetes configuration.

  1. Open your command palette. (cmd + shift + p on macOS or ctrl + shift + p on Windows)
  2. Search for Bridge to Kubernetes: Configure, hit enter
  3. Bridge to Kubernetes will beging to search your Kubernetes cluster to find which services exist; you will then need to select which service you want to route traffic for.
  4. You will then be prompted to enter a port for which your application serves local traffic.

Example:

The nodeJS samples provided for Tailwind Traders, by default, serve traffic on port 3000. If you were to debug locally you would use port 3000; thus, you would enter port 3000 in the Bridge to Kubernetes box.

Alternatively, the .NET 5.0 sample provided for Tailwind Traders, by default, serve traffic on port 5000. Thus, would use enter port 5000 in the Bridge to Kubernetes box.

  1. Lastly, you will be asked if you want to isolate traffic for the service you selected. The samples provided above assume you will want to route traffic, which will create an isolated environment, with a DNS prefix, where you can test your selected service. If you do not wisht to isolate traffic, you would select no; if you do wish to isolate traffic, you would select yes. A default "isolateAs" will be added to your .vscode/tasks.json. If you wish, you may change this value to anything you want.

Using Bridge to Kubernetes

First, you will need to make sure you have access to the cluster you have Tailwind Traders deployed, and you are in the current namespace where the Tailwind Traders services are running.

Second, you will need to make sure you are using Visual Studio Code with the API folder you wish to debug. If you are using macOS, it is recommended you launch Visual Studio Code from a terminal so you have access to all tools in your path.

Example:

From within a Terminal Session, you would navigate to the folder you wish to open and debug within Visual Studio Code:

cd TailwindTraders-Backend/Source/Services/Tailwind.Traders.Cart.Api && code .

Once Visual Studio Code opens and you see your Tailwind Traders API code in the Explorer, you will then navigate to the debugger and select "Launch Program with Kubernetes."

Visual Studio Code will begin to handle the routing and creation of your debugging services.

Example:

Visual Studio Code Routing Manager Starting

If you are using isolation, you will be prompted to update your machine's hosts file to match your Kubernetes cluster environment - you will need to allow Visual Studio Code to make temporary changes to your hosts file.

Once your debugger has started, navigate to the bottom of your screen within Visual Studio Code and find the small icon with a person standing in the middle of some circles, next to that you should see, "Kubernetes: twt", where twt is equal to whatever yourname space is where you have Tailwind Traders deployed.

Visual Studio Code Bridge to Kubernetes Extension

If you click that icon, you will see a popup offering you FQDN addresses where you can easily navigate to your now isolated service in Kubernetes.

Visual Studio Code Bridge to Kubernetes FQDN Example

Find the service you are working in (I.E. Cart, Coupon, or Login), and select it. Your browser should launch and you should see your own sandbox where you can begin to debug.

To test this, set a break point at one (or more) of the following places:

If you are debugging the Tailwind Traders Cart Api:

If you are debugging the Tailwind Traders Coupon Api:

If you are debugging the Tailwind Traders Login Api:

Once you breakpoint is set, you can refresh your browser and hit your breakpoints by doing the following:

Cart Api

  1. Login to Tailwind Traders using: admin@tailwindtraders.com as the username and password as the password.

  2. Find any item in the store, select it, and then click Add to cart. You should then hit your breakpoint you set in the shoppingCartDao.js. You will notice you hit your breakpoint, you can now see your callstack from within Visual Studio Code, and you can use IntelliSense.

Cart Debugging Example

Coupon Api

  1. Login to Tailwind Traders using: admin@tailwindtraders.com as the username and password as the password.
  2. From the home screen, after you have logged in, click, "See my coupons". You will notice you have hit your breakpoint, you can now see your callstack from within Visual Studio Code, and you can use IntelliSense.

Coupon Debugging Example

Login Api

  1. Login to Tailwind Traders using: admin@tailwindtraders.com as the username and password as the password.
  2. You will notice you have hit your breakpoint, you can now see your callstack from within Visual Studio Code, and you can use IntelliSense.

Login Debugging Example

<a name="contributing"></a>Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.