Share via


針對容器群組的輸出和輸入流量設定單一公用 IP 位址

設定使用對外 IP 位址的容器群組,可讓外部用戶端使用此 IP 位址來存取群組中的容器。 例如,瀏覽器可以存取在容器中執行的 Web 應用程式。 不過,目前容器群組會對輸出流量使用不同的 IP 位址。 此輸出 IP 位址不會以程式設計方式公開,這會讓容器群組監視和用戶端防火牆規則設定更為複雜。

本文提供在與 Azure 防火牆整合的虛擬網路中設定容器群組的步驟。 設定容器群組和防火牆規則的使用者定義路由,您可以路由傳送和識別進出容器群組的流量。 容器群組輸入和輸出會使用防火牆的公用 IP 位址。 如果多個容器群組部署在委派給 Azure 容器執行個體的虛擬網路子網路中,則可以使用單一輸出 IP 位址。

在本文中,您會使用 Azure CLI 為此案例建立資源:

  • 部署在虛擬網路的委派子網路上的容器群組
  • 部署在網路中具有靜態公用 IP 位址的 Azure 防火牆
  • 容器群組子網路上的使用者定義的路由
  • 適用於防火牆輸入的 NAT 規則以及適用於輸出的應用程式規則

然後,您可以透過防火牆來驗證來自範例容器群組的輸入和輸出。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com ,從另一個瀏覽器索引標籤啟動 Cloud Shell。

當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。

登入 Azure

系統會在登入的初始帳戶下自動驗證 Cloud Shell。 使用下列指令碼透過不同的訂閱登入,並將 <Subscription ID> 取代為您的 Azure 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱以互動方式登入

注意

若要下載完整的指令碼,請移至完整指令碼

開始使用

本教學課程會使用隨機變數。 若使用現有的資源群組,請適當修改此變數的值。

resourceGroup=resourceGroup$RANDOM

Azure 資源群組:如果您還沒有 Azure 資源群組,請使用 az group create 命令建立資源群組。 適當修改位置值。

az group create --name $resourceGroup --location eastus

在虛擬網路中部署 ACI

在一般情況下,您可能已經有要在其中部署容器群組的 Azure 虛擬網路。 為了示範目的,下列命令會在建立容器群組時建立虛擬網路和子網路。 子網路會委派給 Azure 容器執行個體。

容器群組會從 aci-helloworld 映像執行小型 Web 應用程式。 如文件中其他文章所示,此映像會封裝以 Node.js 撰寫並提供靜態 HTML 網頁的小型 Web 應用程式。

使用 az container create 命令建立容器群組:

az container create \
  --name appcontainer \
  --resource-group $resourceGroup \
  --image mcr.microsoft.com/azuredocs/aci-helloworld \
  --vnet aci-vnet \
  --vnet-address-prefix 10.0.0.0/16 \
  --subnet aci-subnet \
  --subnet-address-prefix 10.0.0.0/24

提示

針對您的子網路中所需的 IP 位址空間,調整 --subnet address-prefix 的值。 最小支援的子網路是 /29,其可提供八個 IP 位址。 某些 IP 位址會保留供 Azure 使用。

若要在稍後的步驟中使用,請執行 [az container show][az-container-show] 命令,以取得容器群組的私人 IP 位址:

aciPrivateIp="$(az container show --name appcontainer \
  --resource-group $resourceGroup \
  --query ipAddress.ip --output tsv)"

在網路中部署 Azure 防火牆

在下列各節中,使用 Azure CLI 在虛擬網路中部署 Azure 防火牆。 如需背景,請參閱教學課程:使用 Azure 入口網站部署和設定 Azure 防火牆

首先,使用 az network vnet subnet create 為防火牆新增名為 AzureFirewallSubnet 的子網路。 AzureFirewallSubnet 是此子網路的「必要」名稱。

az network vnet subnet create \
  --name AzureFirewallSubnet \
  --resource-group $resourceGroup \
  --vnet-name aci-vnet   \
  --address-prefix 10.0.1.0/26

使用下列 Azure CLI 命令,以在子網路中建立防火牆。

如果尚未安裝,則請使用 az extension add 命令,以將防火牆延伸模組新增至 Azure CLI:

az extension add --name azure-firewall

使用 az network firewall create 命令建立防火牆資源:

az network firewall create \
  --name myFirewall \
  --resource-group $resourceGroup \
  --location eastus

az network public-ip create \
  --name fw-pip \
  --resource-group $resourceGroup \
  --location eastus \
  --allocation-method static \
  --sku standard
    
az network firewall ip-config create \
  --firewall-name myFirewall \
  --name FW-config \
  --public-ip-address fw-pip \
  --resource-group $resourceGroup \
  --vnet-name aci-vnet

使用 az network firewall update 命令更新防火牆設定:

az network firewall update \
  --name myFirewall \
  --resource-group $resourceGroup

使用 az network firewall ip-config list 命令取得防火牆的私人 IP 位址。 稍後的命令會使用此私人 IP 位址。

fwPrivateIp="$(az network firewall ip-config list \
  --resource-group $resourceGroup \
  --firewall-name myFirewall \
  --query "[].privateIpAddress" --output tsv)"

使用 az network public-ip show 命令取得防火牆的公用 IP 位址。 稍後的命令會使用此公用 IP 位址。

fwPublicIp="$(az network public-ip show \
  --name fw-pip \
  --resource-group $resourceGroup \
  --query ipAddress --output tsv)"

在 ACI 子網路上定義使用者定義的路由

在 ACI 子網路上定義使用者定義的路由,以將流量轉移至 Azure 防火牆。 如需詳細資訊,請參閱路由網路流量

建立路由表

首先,執行下列 az network route-table create 命令來建立路由表。 在與虛擬網路相同的區域中建立路由表。

az network route-table create \
  --name Firewall-rt-table \
  --resource-group $resourceGroup \
  --location eastus \
  --disable-bgp-route-propagation true

建立路由

執行 az network-route-table route create 以在路由表中建立路由。 若要將流量路由傳送至防火牆,請將下一個躍點類型設定為 VirtualAppliance,並將防火牆的私人 IP 位址傳遞為下一個躍點位址。

az network route-table route create \
  --resource-group $resourceGroup \
  --name DG-Route \
  --route-table-name Firewall-rt-table \
  --address-prefix 0.0.0.0/0 \
  --next-hop-type VirtualAppliance \
  --next-hop-ip-address $fwPrivateIp

建立路由表與 ACI 子網路的關聯

執行 az network vnet subnet update 命令,將路由表關聯至委派給 Azure 容器執行個體的子網路。

az network vnet subnet update \
  --name aci-subnet \
  --resource-group $resourceGroup \
  --vnet-name aci-vnet \
  --address-prefixes 10.0.0.0/24 \
  --route-table Firewall-rt-table

在防火牆上設定規則

根據預設,Azure 防火牆會拒絕 (封鎖) 輸入和輸出流量。

在 ACI 子網路的防火牆上設定 NAT 規則

在防火牆上建立 NAT 規則,以轉譯和篩選您先前在網路中所啟動應用程式容器的輸入網際網路流量。 如需詳細資料,請參閱使用 Azure 防火牆 DNAT 篩選輸入網際網路流量

使用 az network firewall nat-rule create 命令建立 NAT 規則和集合:

az network firewall nat-rule create \
  --firewall-name myFirewall \
  --collection-name myNATCollection \
  --action dnat \
  --name myRule \
  --protocols TCP \
  --source-addresses '*' \
  --destination-addresses $fwPublicIp \
  --destination-ports 80 \
  --resource-group $resourceGroup \
  --translated-address $aciPrivateIp \
  --translated-port 80 \
  --priority 200

視需要新增 NAT 規則,以篩選子網路中其他 IP 位址的流量。 例如,子網路中的其他容器群組可能會公開輸入流量的 IP 位址,或者,可以在重新啟動之後將其他內部 IP 位址指派給容器群組。

在防火牆上建立輸出應用程式規則

執行下列 az network firewall application-rule create 命令,以在防火牆上建立輸出規則。 此範例規則允許從委派給 Azure 容器執行個體的子網路存取 FQDN checkip.dyndns.org。 後續步驟會使用網站的 HTTP 存取,以確認來自 Azure 容器執行個體的輸出 IP 位址。

az network firewall application-rule create \
  --collection-name myAppCollection \
  --firewall-name myFirewall \
  --name Allow-CheckIP \
  --protocols Http=80 Https=443 \
  --resource-group $resourceGroup \
  --target-fqdns checkip.dyndns.org \
  --source-addresses 10.0.0.0/24 \
  --priority 200 \
  --action Allow

透過防火牆測試容器群組存取

下列各節會確認已在 Azure 防火牆後方正確設定委派給 Azure 容器執行個體的子網路。 先前的步驟已透過防火牆路由傳送子網路的連入流量以及子網路的連出流量。

測試到達容器群組的輸入

瀏覽至防火牆的公用 IP 位址,以測試虛擬網路中所執行 appcontainer 的輸入存取。 之前,您已將公用 IP 位址儲存至 $FW_PUBLIC_IP 變數:

echo $fwPublicIp

輸出會類似:

52.142.18.133

如果已正確設定防火牆上的 NAT 規則,則您在瀏覽器中輸入防火牆的公用 IP 位址時會看到下列內容:

Browse to firewall's public IP address

測試來自容器群組的輸出

將下列範例容器部署至虛擬網路。 執行時,會將單一 HTTP 要求傳送至 http://checkip.dyndns.org,以顯示傳送者的 IP 位址 (輸出 IP 位址)。 如果已正確設定防火牆上的應用程式規則,則會傳回防火牆的公用 IP 位址。

az container create \
  --resource-group $resourceGroup \
  --name testegress \
  --image mcr.microsoft.com/azuredocs/aci-tutorial-sidecar \
  --command-line "curl -s http://checkip.dyndns.org" \
  --restart-policy OnFailure \
  --vnet aci-vnet \
  --subnet aci-subnet

檢視容器記錄,以確認 IP 位址與防火牆的公用 IP 位址相同。

az container logs \
  --resource-group $resourceGroup \
  --name testegress 

輸出會類似:

<html><head><title>Current IP Check</title></head><body>Current IP Address: 52.142.18.133</body></html>

清除資源

若不再需要,則可使用 az group delete 移除資源群組和所有相關資源,如下所示。 --no-wait 參數不會等待作業完成,就會將控制項傳回給提示字元。 --yes 參數會確認您想要刪除資源,而不另外對您提示將要進行此作業。

az group delete --name $resourceGroup --yes --no-wait

下一步

在本文中,您會在 Azure 防火牆後方的虛擬網路中設定容器群組。 您已在防火牆上設定使用者定義的路由以及 NAT 和應用程式規則。 您可以使用此設定來設定單一靜態 IP 位址,以處理來自 Azure 容器執行個體的輸入和輸出。

如需管理流量以及保護 Azure 資源的詳細資訊,請參閱 Azure 防火牆文件。