Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

04. How to deploy your Windows Containers based apps into Kubernetes in Azure Container Service (Including CI CD)

James Montemagno edited this page Oct 25, 2023 · 5 revisions

Deploying eShopModernizedMVC to Kubernetes

Overview

Applications based on Windows Containers will soon need to use platforms going further from IaaS VMs in order to easily achieve a better automated scalability, high scalability and significant improvement in automated deployments and versioning. You can achieve those goals with the orchestrator Kubernetes, available in Azure Container Services.

Goals for this walkthrough

This post describes how to deploy eShopModernizedMVC to Kubernetes but deploying the eShopModernizedWebForms should be pretty similar, so you can use almost the same procedure but using different deployment .yml files.

Deploying to Kubernetes (K8s), from scratch, is a 2-step process:

  • Deploy a K8s cluster in Azure Container Service (Azure) or any other environment.
  • Deploy the application and related resources into the K8s cluster

However, before trying this scenario deploying to an orchestrator, it is recommended that you:

  1. Familiarize yourself with the previous sections about containerizing existing .NET Framework apps.
  2. You’ve been testing the sample applications (eShopModernizedMVC and/or eShopModernizedWebForms) and running them on a local Docker for Windows dev machine.

Scenarios

Scenario A: Direct deployment to K8s cluster from dev environment

image

Scenario B: Deployment to K8s cluster from CI/CD pipelines in VSTS

image

Prerequisites

  • Azure Subscription
  • Azure CLI installed on dev machine (explained below)
  • Kubectl (Kubernetes CLI) installed on dev machine and in PATH
  • Access to eShopModernizedMVC image at Docker Hub (or any other Docker Registry)

Position PowerShell into the eShopModernizedMVC-K8s\ServiceDeployments folder

Clone the Repo code into your hard drive. Open a PowerShell with Admin privileges and run: cd C:\Your-Path-to-eShopModernizing\Kubernetes\eShopModernizedMVC-K8s\ServiceDeployments

Connect to Azure from Azure CLI (PowerShell or Bash)

  1. Run Azure CLI: In order to run the Azure CLI locally, (you need Azure CLI version 2.0.4 or later) you need to install it if you don't have it. Follow this procedure: Install Azure CLI 2.0

    Then, check the version:

    az --version

  2. Login to Azure with:

    az login

  3. If you have multiple Azure subscriptions, select the one you whish to work with:

    az account set --subscription [my-azure-subscription-guid]

  4. Check your default subscription with:

    az account show

Deploying the Kubernetes Cluster in Azure Container Service (ACS)

In order to deploy Windows Containers we need a Kubernetes cluster with Windows nodes instead of Linux. However, the Master node will be a Linux node.

Yo can find additional generic info on Kubernetes deployment into ACS in this post: https://docs.microsoft.com/en-us/azure/container-service/kubernetes/container-service-kubernetes-windows-walkthrough

  1. Create an Azure Resource Group to hold everything related to the Kubernetes cluster and delete everything in a single step whenever you need to. Of course, you can choose your own name for the Resource Group:

    az group create --name eShopK8sWinResGrp --location westus

  2. Create the Kubernetes cluster (you should use your own cluster name, user name and password):

    az acs create --orchestrator-type=kubernetes --resource-group eShopK8sWinResGrp --name=eShopK8sWinCluster --agent-count=2 --generate-ssh-keys --windows --admin-username youruser --admin-password your@password

    If re-using already an created ssh key pair, you can add a pointer to the public key, something like the following:

    --ssh-key-value ~/.ssh/eshop_rsa.pub

  3. Install kubectl if you don't have it installed in your PC To connect to the Kubernetes cluster from your client computer, use kubectl, the Kubernetes command-line client.

    If you're using Azure CloudShell, kubectl is already installed.

    If you want to install it locally, you can use the az acs kubernetes install-cli command. The following Azure CLI example installs kubectl to your system. On Windows, run this command as an administrator:

    az acs kubernetes install-cli

    Don't forget to add the "C:\Program Files (x86)" folder path to your system environment PATH variable.

  4. Connect to the Kubernetes cluster and set the ssh key files to your local PC

    az acs kubernetes get-credentials --resource-group=eShopK8sWinResGrp --name=eShopK8sWinCluster

    Again, if re-using an already created ssh key pair, you can add a pointer to the private key, like the following:

    --ssh-key-file C:\Users\CESARDL\.ssh\eshop_rsa

  5. Check the connection with kubectl connecting to the cluster by retrieving nodes list:

    kubectl get nodes kubectl-get-nodes

  6. Open the Kubernetes Dashboard to check what’s going on in the cluster using a browser.

    • Open another PowerShell and run this command:

    kubectl proxy image

    • Now, open a browser and type the following to Connect to Kubernetes Dashboard:

    http://localhost:8001/ui

    image Leave it open so that you can check there, changes applied later.

In order to have a flexible deployment, instead of deploying the SQL container along with the applications, we have created separated deployment .yml files. This is a better option since the SQL container is heavier, a standard image and you don't need to re-deploy as often as the apps containers.

Deploy the SQL Server Windows Container into the Kubernetes Cluster

  1. In case you are positioned in a different place, make sure you move, in PowerShell, to your deployment file path:

    cd C:\Your-Path-to-eShopModernizing\Kubernetes\eShopModernizedSQL-K8s

  2. Run in PowerShell the kubectl to deploy the SQL Server container

    kubectl create -f eshop-modernized-sql-k8s-services-deployment.yml

Deploy the eShopModernizedMVC app into the Kubernetes Cluster

  1. In case you are positioned in a different place, make sure you move, in PowerShell, to your deployment file path:

    cd C:\Your-Path-to-eShopModernizing\Kubernetes\eShopModernizedMVC-K8s\ServiceDeployments

  2. Use or create a similar deployment file than this: eshop-modernized-mvc-k8s-services-deployment.yml

  3. Run in PowerShell within the path C:\Your-Path-to-eShopModernizing\Kubernetes\eShopModernizedMVC-K8s\ServiceDeployments

    kubectl create -f eshop-modernized-mvc-k8s-services-deployment.yml

    image

  4. Check it out from Kubectl CLI:

    kubectl get pods image

    kubectl get svc image

    You'll need to wait until you have an IP assigned. Check again until you see the IP with:

    kubectl get svc image

  5. Run eShopModernizedMVC from Kubernetes! IMPORTANT: The first time you might need to wait a few minutes until the SQL Server Docker image is pulled.

    You can check how the deployment is going by taking a look to the Kubernetes dashboard, like in the following screenshot where you can see how the containers are still being deployed (You need to set the kubectl proxy first):

    image

    You can also check the state of the deployment by typing kubectl get services where you'll see the EXTERNAL IP to use and ports to use when the deployment is finished.

    So, finally, you can test the app by typing the IP you get like in the following case:

    http://104.42.224.204/

    image

Scale-out the eShopModernizedMVC container/deployment to multiple containers

  1. To scale-out the eShopModernizedMVC container/deployment run the following command with Kubectl:

    kubectl scale --replicas=5 deployments/eshop-modernized-mvc image

  2. To see in the CLI the number of replicas per deployment, run:

    kubectl get deployments image

  3. You can also scale-out the eShopModernizedMVC container/deployment from the Kubernetes Web UI: image

  4. To see in the Kubernetes Web UI the number of replicas per deployment: image

You can delete everything from Azure with a simple Azure CLI 2 command:

az group delete --name eShopK8sWinResGrp --yes --no-wait

Or using the Azure portal, of course.

For further documentation on Kubernetes and Azure Container Service (ACS) check the following: https://docs.microsoft.com/en-us/azure/container-service/kubernetes/

Deploying from VSTS CD pipeline to Kubernetes in ACS

As a pre-requisite we need the images pushed to our container registry, as we have shown in the section 3 of the wiki about the CI CD for Azure VMs

Our pipeline will take as artifact the github repository, where we have the Kubernetes configuration files.

CD pipeline

CD artifact

The tasks of the release basically are deploying the services and then refreshing the pods to update the running versions of the pods to the new ones. First we deploy the SQL image using the 'Deploy to Kubernetes' task. We are simply passing here the configuration file of the SQL container for Kubernetes that we have stored in our repository.

Deploy SQL

The deployment of the MVC application is equal to the SQL deployment.

Deploy MVC

Finally we refresh the pod for MVC

Refresh pod

In the case of the CD for the WebForms application we repeat the process, but changing the MVC application by the Webforms one.

Deploy WebForms

Updating environment variables with VSTS variables

Because we do not want to store in the code reposistory the values for our cofiguration variables, we need to do some steps. What we will do here is to put the values in the Release definition and substitute them during the release. For Kubernetes we have seen that the yml files store the environment variables. We have created one specific configuration file for the CD on 'Kubernetes\eShopModernizedMVC-K8s\ServiceDeployments\eshop-modernized-mvc-k8s-services-CD-deployment.yml'. This file is the same as the original one, but with the variables set with a replace token:

(...)
env:
- name: ConnectionString
  value: "Server=sql-data;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word"
- name: UseMockData
  value: "False"
- name: UseCustomizationData
  value: "False"
- name: UseAzureStorage
  value: "#{use_azure_storage}#"
- name: StorageConnectionString
  value: "#{your_storage_connection_string}#"
- name: AppInsightsInstrumentationKey
  value: "#{your_app_insights_instrument_key}#"
- name: UseAzureActiveDirectory
  value: "#{use_azure_active_directory}#"
- name: AzureActiveDirectoryClientId
  value: "#{your_aad_client_id}#"
- name: AzureActiveDirectoryTenant
  value: "#{your_aad_directory_tenant}#"
- name: PostLogoutRedirectUri
  value: "#{your_aad_logout_refirect_uri}#"
(...)

We will set this value in the release process, using the 'Replace Tokens' task. We have set the task to take all the xml files and replace the pattern '{key_name}#' by the key_name if this one is found as a variable in the release.

Replace Tokens

In our example we have added this keys to the variables in the CD. As you can see the name of this variables corresponds to the replace tokens in the previous yml configuration file.

Environment variables

Clone this wiki locally