This repository is a companion to a presentation given the Cognizant organization on Sept. 23 2021 entitled "Modern Application development – Container services, AKS and Functions"
Install Visual Studio Code from the following link: https://code.visualstudio.com/Download
Install the following VsCode extensions:
- Azure Account
- Azure Tools
- C#
For Windows Choclatey is the easiest way to install, follow these instructions to install Choclatey, https://chocolatey.org/install
Execute the following script in Windows Powershell or Windows Powershell ISE.
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Ensure choco was added to your User or system path, if not add.
C:\ProgramData\chocolatey\bin
Open a command prompt and Install the Azure CLI
choco install azure-cli
In advanced settings update your user PATH environment variable to include:
C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin
After completing keep the dialog open as you will be adding more here.
To install the dotnet CLI, install .Net Core from https://docs.microsoft.com/en-us/dotnet/core/install/windows?tabs=net50
Ensure the dotnet CLI is added to your user or system PATH
C:\Program Files (x86)\dotnet\
Windows: https://www.docker.com/products/docker-desktop
Install docker desktop, this will also require the Windows Subsystem for Linx (WSL2) to be installed.
This will most likely require a restart.
When installing Docker Desktop the Docker CLI will also be installed
Follow the instructions at https://kubernetes.io/docs/tasks/tools/install-kubectl-windows/ to install
Add the following to your User PATH environment variable
%USERPROFILE%\.azure-kubectl
%USERPROFILE%\.azure-kubelogin
Follow the instructions at: https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=v3%2Cwindows%2Ccsharp%2Cportal%2Cbash%2Ckeda#v2 to install the Azure Functions Core Tools
Ensure your User or system PATH is updated with C:\Program Files\Microsoft\Azure Functions Core Tools\
This ensures that your PATH environment variable is correct
First, ensure you have an subscription that you can deploy your resources to
Ensure you can login to Azure using az login
Use az account show --output table
to find what your active directory and subscription is.
List your subscriptions with:
az account list --output table
Set your active subscription with:
az account set --subscription "My Demos"
Read more here: https://docs.microsoft.com/en-us/cli/azure/manage-azure-subscriptions-azure-cli
Look at your registered providers with:
Mac/Linux: az provider list --output table | grep " Registered"
Windows: az provider list --output table | findstr /C:" Registered"
Ensure the following providers are registered:
- Microsoft.Storage
- Microsoft.OperationsManagement
- Microsoft.OperationalInsights
- Microsoft.ContainerService
- Microsoft.ManagedIdentity
- microsoft.insights
- Microsoft.ContainerRegistry
- Microsoft.Compute
- Microsoft.Network
If not registered register with:
az provider register --namespace {provider-string}
e.g. az provider register --namespace Microsoft.Network
Ensure that docker cli is working
- open a new terminal
- run
docker --version
- if not working follow the docker desktop install instructions
Instructions
- Open VsCode
- Create a folder/directory and use VsCode Open Folder
- Open a new terminal from the terminal menu
- Your terminal should be in the folder you opened, if not cd to it
- Run
dotnet new webapi -o forecast.api -n forecast.api
- cd to the new directory
cd forecast.api
- Terminal: run
vscode .
, this will open vscode to the new application directory - VsCode Command Pallette (ctl+shift+P): find
add docker files to workspace
and click - A new dockerfile should be added to your directory, view to ensure it looks correct
- Terminal:
docker image list
, no images should exist - VsCode Command Pallette (ctl+shift+P):
Docker Images: Build Image
- Terminal: Run
docker image list
, you should now see your image - Terminal:
docker run -p 5000:5000 --name forecastapi forecastapi
- Menu/Terminal->New Terminal: call api with curl
curl http://localhost:5000/WeatherForecast
- Ensure you see a response from the API with weather forecast data
- Terminal: show running containers
docker ps
- Terminal: stop running container
docker stop forecastapi
- Create ACR in the the terminal with az cli
Create a resource group
az group create --location eastus --resource-group rg-ipldemo
- From here on you will use a name for your ACR such as:
{YourInitials}IplDemo
JklAcrDemo to ensure no DNS conflicts - Terminal: Create an Azure Container Registry resource
az acr create --location eastus --name {YourAcrName} --resource-group rg-ipldemo --sku basic
- Terminal: Update Docker CLI config to include our new ACR repository
az acr login --name {YourAcrName}
- Terminal: publish our local image to ACR
docker tag forecastapi:latest {YourAcrName}.azurecr.io/forecastapi:latest
docker push {YourAcrName}.azurecr.io/forecastapi:latest
- Navigate to the Azure Portal -> All Resources
- Find your ACR
- Select
repositories
in the left menu - View your published image
- Create a new AKS cluster
az group create --name rg-k8s --location eastus
az aks create --resource-group rg-k8s --name aks-ipldemo --node-count 1 --enable-addons monitoring --generate-ssh-keys
az aks get-credentials --resource-group rg-k8s --name aks-ipldemo
- Give AKS access to ACR to pull images
az aks update -n aks-ipldemo -g rg-k8s --attach-acr {YourAcrName}
- Azure portal: Find your cluster, namespaces menu, see there are no namespaces
- VsCode: review
forecast.yml
from the same directory as this Readme
Notice the service, deployment, and loadbalancer - VsCode->Terminal: View running pods
kubectl get pods
- VsCode->Terminal: view k8s namespaces
kubectl get namespaces
- VsCode: Open Folder, folder that contains the forecast.yml file
- VsCode->Terminal: Create our k8s resources
kubectl apply -f forecast.yml
- Azure portal: Find and view your AKS cluster
Click workloads and now see a running pod, forecast, browse around and you can more details - Azure portal: View the updated load balancer
Find your resource group such as:MC_rg-k8s_aks-ipldemo_eastus
Click the Load Balancer
Click the Load Balancing Rules menu
Click the one load balancing rule and notice it load balacing port 80
- Terminal: Create a new Azure function shell project
func init forecast.func --dotnet
- Terminal:
cd forecast.func
code .
- Terminal: create a new HTTP triggered function in your project
func new --name weatherforecast --template "HTTP trigger" --authlevel "anonymous"
- VsCode: View the created files in the File Explorer
- VsCode: Copy the contents of azurefuncforecast.cs to
weatherforecast.cs
file - VsCode: F5 to debug the project
- Ctrl+Click the URL in the output window to show the forecast data
- Terminal: Create a new Azure Function resource (Watch for replacement {your initials})
az group create --name rg-azurefunc --location eastus
az storage account create --name stazurefunc{your initials} --location eastus --resource-group rg-azurefunc --sku Standard_LRS
az functionapp create --resource-group rg-azurefunc --consumption-plan-location eastus --runtime dotnet --functions-version 3 --name forecastfunc --storage-account stazurefunc{your initials}
- Terminal: Publish your file to the Azure Function
func azure functionapp publish forecastfunc
- hit the url in the output window
- go to the azure portal
- view the function app in All Resources
show the function, click it
show various aspects of the function
cant be modified since its deployed with a package
- delete resource group
az group delete --resource-group rg-ipldemo
- delete k8s deployment + service
kubectl delete deployment forecast
kubectl delete service forecast
- delete images from acr
docker rmi $(docker images -q) -f
docker system prune
- Delete Azure Function
az group delete --resource-group rg-azurefunc
- Delete AKS cluster
az group delete --resource-group rg-k8s