|
| 1 | +--- |
| 2 | +title: Static IP address for container group |
| 3 | +description: Create a container group in a virtual network and use an Azure application gateway to expose a static frontend IP address to a containerized web app |
| 4 | +ms.topic: article |
| 5 | +ms.date: 03/16/2020 |
| 6 | +--- |
| 7 | + |
| 8 | +# Expose a static IP address for a container group |
| 9 | + |
| 10 | +This article shows one way to expose a static, public IP address for a [container group](container-instances-container-groups.md) by using an Azure [application gateway](../application-gateway/overview.md). Follow these steps when you need a static entry point for an external-facing containerized app that runs in Azure Container Instances. |
| 11 | + |
| 12 | +In this article you use the Azure CLI to create the resources for this scenario: |
| 13 | + |
| 14 | +* An Azure virtual network |
| 15 | +* A container group deployed [in the virtual network (preview)](container-instances-vnet.md) that hosts a small web app |
| 16 | +* An application gateway with a public frontend IP address, a listener to host a website on the gateway, and a route to the backend container group |
| 17 | + |
| 18 | +As long as the application gateway runs and the container group exposes a stable private IP address in the network's delegated subnet, the container group is accessible at this public IP address. |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> Azure charges for an application gateway based on the amount of time that the gateway is provisioned and available, as well as the amount of data it processes. See [pricing](https://azure.microsoft.com/pricing/details/application-gateway/). |
| 22 | +
|
| 23 | +## Create virtual network |
| 24 | + |
| 25 | +In a typical case, you might already have an Azure virtual network. If you don't have one, create one as shown with the following example commands. The virtual network needs separate subnets for the application gateway and the container group. |
| 26 | + |
| 27 | +If you need one, create an Azure resource group. For example: |
| 28 | + |
| 29 | +```azureci |
| 30 | +az group create --name myResourceGroup --location eastus |
| 31 | +``` |
| 32 | + |
| 33 | +Create a virtual network with the [az network vnet create][az-network-vnet-create] command. This command creates the *myAGSubnet* subnet in the network. |
| 34 | + |
| 35 | +```azurecli |
| 36 | +az network vnet create \ |
| 37 | + --name myVNet \ |
| 38 | + --resource-group myResourceGroup \ |
| 39 | + --location eastus \ |
| 40 | + --address-prefix 10.0.0.0/16 \ |
| 41 | + --subnet-name myAGSubnet \ |
| 42 | + --subnet-prefix 10.0.1.0/24 |
| 43 | +``` |
| 44 | + |
| 45 | +Use the [az network vnet subnet create][az-network-vnet-subnet-create] command to create a subnet for the backend container group. Here it's named *myACISubnet*. |
| 46 | + |
| 47 | +```azurecli |
| 48 | +az network vnet subnet create \ |
| 49 | + --name myACISubnet \ |
| 50 | + --resource-group myResourceGroup \ |
| 51 | + --vnet-name myVNet \ |
| 52 | + --address-prefix 10.0.2.0/24 |
| 53 | +``` |
| 54 | + |
| 55 | +Use the [az network public-ip create][az-network-public-ip-create] command to create a static public IP resource. In a later step, this address is configured as the front end of the application gateway. |
| 56 | + |
| 57 | +```azurecli |
| 58 | +az network public-ip create \ |
| 59 | + --resource-group myResourceGroup \ |
| 60 | + --name myAGPublicIPAddress \ |
| 61 | + --allocation-method Static \ |
| 62 | + --sku Standard |
| 63 | +``` |
| 64 | + |
| 65 | +## Create container group |
| 66 | + |
| 67 | +Run the following [az container create][az-container-create] to create a container group in the virtual network you configured in the previous step. |
| 68 | + |
| 69 | +The group is deployed in the *myACISubnet* subnet and contains a single instance named *appcontainer* that pulls the `aci-helloworld` image. As shown in other articles in the documentation, this image packages a small web app written in Node.js that serves a static HTML page. |
| 70 | + |
| 71 | +```azurecli |
| 72 | +az container create \ |
| 73 | + --name appcontainer \ |
| 74 | + --resource-group myResourceGroup \ |
| 75 | + --image mcr.microsoft.com/azuredocs/aci-helloworld \ |
| 76 | + --vnet myVNet \ |
| 77 | + --subnet myACISubnet |
| 78 | +``` |
| 79 | + |
| 80 | +When successfully deployed, the container group is assigned a private IP address in the virtual network. For example, run the following [az container show][az-container-show] command to retrieve the group's IP address: |
| 81 | + |
| 82 | +```azurecli |
| 83 | +az container show \ |
| 84 | + --name appcontainer --resource-group myResourceGroup \ |
| 85 | + --query ipAddress.ip --output tsv |
| 86 | +``` |
| 87 | + |
| 88 | +Output is similar to: `10.0.2.4`. |
| 89 | + |
| 90 | +For use in a later step, save the IP address in an environment variable: |
| 91 | + |
| 92 | +```azurecli |
| 93 | +ACI_IP=$(az container show \ |
| 94 | + --name appcontainer \ |
| 95 | + --resource-group myResourceGroup \ |
| 96 | + --query ipAddress.ip --output tsv) |
| 97 | +``` |
| 98 | + |
| 99 | +## Create application gateway |
| 100 | + |
| 101 | +Create an application gateway in the virtual network, following the steps in the [application gateway quickstart](../application-gateway/quick-create-cli.md). The following [az network application-gateway create][az-network-application-gateway-create] command creates a gateway with a public frontend IP address and a route to the backend container group. See the [Application Gateway documentation](/azure/application-gateway/) for details about the gateway settings. |
| 102 | + |
| 103 | +```azurecli |
| 104 | +az network application-gateway create \ |
| 105 | + --name myAppGateway \ |
| 106 | + --location eastus \ |
| 107 | + --resource-group myResourceGroup \ |
| 108 | + --capacity 2 \ |
| 109 | + --sku Standard_v2 \ |
| 110 | + --http-settings-protocol http \ |
| 111 | + --public-ip-address myAGPublicIPAddress \ |
| 112 | + --vnet-name myVNet \ |
| 113 | + --subnet myAGSubnet \ |
| 114 | + --servers "$ACI_IP" |
| 115 | +``` |
| 116 | + |
| 117 | + |
| 118 | +It can take up to 15 minutes for Azure to create the application gateway. |
| 119 | + |
| 120 | +## Test public IP address |
| 121 | + |
| 122 | +Now you can test access to the web app running in the container group behind the application gateway. |
| 123 | + |
| 124 | +Run the [az network public-ip show][az-network-public-ip-show] command to retrieve the frontend public IP address of the gateway: |
| 125 | + |
| 126 | +```azurecli |
| 127 | +az network public-ip show \ |
| 128 | +--resource-group myresourcegroup \ |
| 129 | +--name myAGPublicIPAddress \ |
| 130 | +--query [ipAddress] \ |
| 131 | +--output tsv |
| 132 | +``` |
| 133 | + |
| 134 | +Output is a public IP address, similar to: `52.142.18.133`. |
| 135 | + |
| 136 | +To view the running web app when successfully configured, navigate to the gateway's public IP address in your browser. Successful access is similar to: |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +## Next steps |
| 141 | + |
| 142 | +* See a [quickstart template](https://github.com/Azure/azure-quickstart-templates/tree/master/201-aci-wordpress-vnet) to create a container group with a WordPress container instance as a backend server behind an application gateway. |
| 143 | +* You can also configure an application gateway with a certificate for SSL termination. See the [overview](../application-gateway/ssl-overview.md) and the [tutorial](../application-gateway/create-ssl-portal.md). |
| 144 | +* Depending on your scenario, consider using other Azure load-balancing solutions with Azure Container Instances. For example, use [Azure Traffic Manager](../traffic-manager/traffic-manager-overview.md) to distribute traffic across multiple container instances and across multiple regions. See this [blog post](https://aaronmsft.com/posts/azure-container-instances/). |
| 145 | + |
| 146 | +[az-network-vnet-create]: /cli/azure/network/vnet#az-network-vnet-create |
| 147 | +[az-network-vnet-subnet-create]: /cli/azure/network/vnet/subnet#az-network-vnet-subnet-create |
| 148 | +[az-network-public-ip-create]: /cli/azure/network/public-ip#az-network-public-ip-create |
| 149 | +[az-network-public-ip-show]: /cli/azure/network/public-ip#az-network-public-ip-show |
| 150 | +[az-network-application-gateway-create]: /cli/azure/network/application-gateway#az-network-application-gateway-create |
| 151 | +[az-container-create]: /cli/azure/container#az-container-create |
| 152 | +[az-container-show]: /cli/azure/container#az-container-show |
0 commit comments