Monday 29 November 2021

How to deploy an Azure Function app in AKS using the Azure CLI

 

Create the Function app project

In a terminal or command prompt, run the following command to create a new Azure Function app project. Use the docker flag to generate a Dockerfile that will be used to build the Function App docker image.

# create a dummy function that is triggered by a HTTP GET
func init aksexample --worker-runtime dotnet --docker
cd aksexample
func new --name HttpExample --template "HTTP trigger"
# to check if everything works
dotnet build

Create the Azure infrastructure

Next, create the Azure infrastructure:

# global variables
resourceGroup=k8s-example-rg
location=westeurope
acrName=exampleacr01
storageAccount=examplesa01
functionAppName=example01
fullAcrName=$acrName.azurecr.io
imageName=$fullAcrName/example:v1.0.0
aksName=exampleaks01
# create a resource group
az group create --name $resourceGroup --location $location
# create a container registry for hosting your docker image
az acr create --resource-group $resourceGroup --name $acrName --sku Basic
# grab the login credentials, username defaults to acr name
az acr login --name $acrName
az acr update -n $acrName --admin-enabled true
password=$(az acr credential show -n $acrName --query 'passwords[0].value' -o tsv)
# create the infra for the function app
az storage account create --name $storageAccount --location $location --resource-group $resourceGroup --sku Standard_LRS

Create the AKS

Now we can create the azure kubernetes service in Azure:

# create the aks and install the cli so we can use kubectl
az aks create --resource-group $resourceGroup --name $aksName --node-count 1 --enable-addons monitoring --generate-ssh-keys
az aks get-credentials --resource-group $resourceGroup --name $aksName
az aks install-cli
# create a secret for the storage account
storageConnectionString=$(az storage account show-connection-string --resource-group $resourceGroup --name $storageAccount --query connectionString --output tsv)
kubectl create secret generic storageaccountconnectionstring --from-literal=storageAccountConnectionString=$storageConnectionString
# create a secret for the acr credentials
kubectl create secret docker-registry containerregistrysecret --docker-server=$fullAcrName --docker-username=$acrName --docker-password=$password

Deploy the Azure Function app to AKS

Finally we can deploy our Azure Function app to the AKS:

# attach the aks to the acr so it can grab the image from the acr
az aks update --name $aksName --resource-group $resourceGroup --attach-acr $acrName
# func kubernetes deploy runs a docker build, pushes the image to the container registry and deploys it to AKSfunc kubernetes deploy --name $functionAppName --registry $fullAcrName

Test if everything works

If everything went successful you should see logging like:

Getting loadbalancer ip for the service: example-http
Waiting for the service to be ready: example01-http
HttpExample - [httpTrigger]
Invoke url:
http://20.56.225.47/api/httpexample

You can then invoke the url with:

curl http://20.56.225.47/api/httpexample?name=bla

Which should give you the expected result.

Wrapping up

Now we have deployed our first Function app to AKS we would like to automate the whole process in an Azure DevOps pipeline. Let's use Terraform for that.

Don't forget to remove your azure infrastructure because the AKS will munch away your MSDN credits at no time otherwise.

Deploying Web App to Azure using Azure CLI

 

  1. Deploy to azure web app in docker container
- az group create --name myResourceGroup --location "mylocation"- az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku B1 --is-linux- az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <app-name> --deployment-container-image-name <azure-container-registry-name>.azurecr.io/mydockerimage:v1.0.0

2. Deploy to azure web app from local git

- az group create --name myResourceGroup --location mylocation- az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku FREE- az webapp create --name myWebApp --resource-group myResourceGroup --plan myAppServicePlan --deployment-local-git- git remote add azure <local-git-url>- git push azure master

3. Deploy to azure web app from local folder

- az group create --name myResourceGroup --location mylocation- az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku FREE- az webapp up --name myWebApp --plan myAppServicePlan --sku FREE

4. Deploy a zip to azure web app

- az webapp deployment source config-zip --resource-group myResourceGroup --name myWebApp --src zipFilePathLocation

5. Deploy to azure web app from git

az webapp deployment source config --branch master --manual-integration --name myWebApp --repo-url <gith url> --resource-group myResourceGroup

How to Create a private container registry using the Azure CLI?

 Azure Container Registry is a private registry service for building, storing, and managing container images and related artifacts. In this quickstart, you create an Azure container registry instance with the Azure CLI. Then, use Docker commands to push a container image into the registry, and finally pull and run the image from your registry.

This quickstart requires that you are running the Azure CLI (version 2.0.55 or later recommended). Run az --version to find the version. If you need to install or upgrade, see Install Azure CLI.

You must also have Docker installed locally. Docker provides packages that easily configure Docker on any macOSWindows, or Linux system.

Because the Azure Cloud Shell doesn't include all required Docker components (the dockerd daemon), you can't use the Cloud Shell for this quickstart.

Create a resource group

Create a resource group with the az group create command. An Azure resource group is a logical container into which Azure resources are deployed and managed.

The following example creates a resource group named myResourceGroup in the eastus location.

Azure CLI
az group create --name myResourceGroup --location eastus

Create a container registry

In this quickstart you create a Basic registry, which is a cost-optimized option for developers learning about Azure Container Registry. For details on available service tiers, see Container registry service tiers.

Create an ACR instance using the az acr create command. The registry name must be unique within Azure, and contain 5-50 alphanumeric characters. In the following example, myContainerRegistry007 is used. Update this to a unique value.

Azure CLI
az acr create --resource-group myResourceGroup \
  --name myContainerRegistry007 --sku Basic

When the registry is created, the output is similar to the following:

JSON
{
  "adminUserEnabled": false,
  "creationDate": "2019-01-08T22:32:13.175925+00:00",
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.ContainerRegistry/registries/myContainerRegistry007",
  "location": "eastus",
  "loginServer": "mycontainerregistry007.azurecr.io",
  "name": "myContainerRegistry007",
  "provisioningState": "Succeeded",
  "resourceGroup": "myResourceGroup",
  "sku": {
    "name": "Basic",
    "tier": "Basic"
  },
  "status": null,
  "storageAccount": null,
  "tags": {},
  "type": "Microsoft.ContainerRegistry/registries"
}

Take note of loginServer in the output, which is the fully qualified registry name (all lowercase). Throughout the rest of this quickstart <registry-name> is a placeholder for the container registry name, and <login-server> is a placeholder for the registry's login server name.

 Tip

In this quickstart, you create a Basic registry, which is a cost-optimized option for developers learning about Azure Container Registry. Choose other tiers for increased storage and image throughput, and capabilities such as connection using a private endpoint. For details on available service tiers (SKUs), see Container registry service tiers.

Log in to registry

Before pushing and pulling container images, you must log in to the registry. To do so, use the az acr login command. Specify only the registry resource name when logging in with the Azure CLI. Don't use the fully qualified login server name.

Azure CLI
az acr login --name <registry-name>

Example:

Azure CLI
az acr login --name mycontainerregistry

The command returns a Login Succeeded message once completed.

Push image to registry

To push an image to an Azure Container registry, you must first have an image. If you don't yet have any local container images, run the following docker pull command to pull an existing public image. For this example, pull the hello-world image from Microsoft Container Registry.

docker pull mcr.microsoft.com/hello-world

Before you can push an image to your registry, you must tag it with the fully qualified name of your registry login server. The login server name is in the format <registry-name>.azurecr.io (must be all lowercase), for example, mycontainerregistry.azurecr.io.

Tag the image using the docker tag command. Replace <login-server> with the login server name of your ACR instance.

docker tag mcr.microsoft.com/hello-world <login-server>/hello-world:v1

Example:

docker tag mcr.microsoft.com/hello-world mycontainerregistry.azurecr.io/hello-world:v1

Finally, use docker push to push the image to the registry instance. Replace <login-server> with the login server name of your registry instance. This example creates the hello-world repository, containing the hello-world:v1 image.

docker push <login-server>/hello-world:v1

After pushing the image to your container registry, remove the hello-world:v1 image from your local Docker environment. (Note that this docker rmi command does not remove the image from the hello-world repository in your Azure container registry.)

docker rmi <login-server>/hello-world:v1

List container images

The following example lists the repositories in your registry:

Azure CLI
az acr repository list --name <registry-name> --output table

Output:

Result
----------------
hello-world

The following example lists the tags on the hello-world repository.

Azure CLI
az acr repository show-tags --name <registry-name> --repository hello-world --output table

Output:

Result
--------
v1

Run image from registry

Now, you can pull and run the hello-world:v1 container image from your container registry by using docker run:

docker run <login-server>/hello-world:v1  

Example output:

Unable to find image 'mycontainerregistry.azurecr.io/hello-world:v1' locally
v1: Pulling from hello-world
Digest: sha256:662dd8e65ef7ccf13f417962c2f77567d3b132f12c95909de6c85ac3c326a345
Status: Downloaded newer image for mycontainerregistry.azurecr.io/hello-world:v1

Hello from Docker!
This message shows that your installation appears to be working correctly.

[...]

Clean up resources

When no longer needed, you can use the az group delete command to remove the resource group, the container registry, and the container images stored there.

Azure CLI
az group delete --name myResourceGroup