探索和執行 Linux 和 PostgreSQL 工作負載
在本單元中,您將會:
- 使用 Bicep 範本部署 Azure Blob 儲存體帳戶。
- 建立 Blob 儲存體容器。
- 將映像移轉至 Blob 儲存體帳戶。
- 將
tailwind.sql上傳至 Blob 儲存體帳戶。 - 使用 Azure CLI 連線到 Azure 虛擬機器。
- 從儲存體帳戶下載檔案。
- 使用
psql連線到 PostgreSQL 伺服器,並匯入 SQL 檔案。 - 透過命令列以互動方式執行應用程式。
- 確認應用程式有正確執行。
使用 deploy/vm-postgres.bicep 部署儲存帳戶
在本機電腦上執行下列命令:
az deployment group create \
--resource-group 240900-linux-postgres \
--template-file deploy/vm-postgres.bicep \
--parameters \
deployVm=false \
deployPostgres=false \
deployStorage=true
將目前的使用者新增至 [儲存體 Blob 資料擁有者] 角色
STORAGE_ACCOUNT_ID=$(az storage account list \
--resource-group 240900-linux-postgres \
--query '[0].id' \
-o tsv)
USER_ID=$(az ad signed-in-user show \
--query id \
-o tsv)
az role assignment create \
--role "Storage Blob Data Owner" \
--assignee $USER_ID \
--scope $STORAGE_ACCOUNT_ID
在儲存體帳戶中建立名為 container1 的容器
STORAGE_ACCOUNT_NAME=$(az storage account list \
--resource-group 240900-linux-postgres \
--query '[0].name' \
-o tsv)
echo "STORAGE_ACCOUNT_NAME: $STORAGE_ACCOUNT_NAME"
az storage container create \
--account-name $STORAGE_ACCOUNT_NAME \
--auth-mode login \
--name container1
將影像遷移至儲存帳戶的子資料夾中
az storage blob upload-batch \
--account-name $STORAGE_ACCOUNT_NAME \
--auth-mode login \
--overwrite \
--destination container1/images \
--source app/data/images
會出現下列輸出:
[
{
"Blob": "https://storageji2dbe.blob.core.windows.net/container1/images/wrench_set.jpg",
"Last Modified": "...",
"Type": "image/jpeg",
"eTag": "\"0x8DCE0CA938AF41B\""
},
{
"Blob": "https://storageji2dbe.blob.core.windows.net/container1/images/planer.jpg",
"Last Modified": "...",
"Type": "image/jpeg",
"eTag": "\"0x8DCE0CA939DF18B\""
},
...
]
將 app/data/postgres/tailwind.sql 上傳至儲存體帳戶
az storage blob upload \
--account-name $STORAGE_ACCOUNT_NAME \
--auth-mode login \
--container-name container1 \
--file app/data/postgres/tailwind.sql \
--name tailwind.sql
使用 az ssh 命令連線到 Azure 虛擬機器
az ssh vm \
--resource-group 240900-linux-postgres \
--name vm-1
從儲存體帳戶下載 tailwind.sql 檔案
將 Bash 變數 STORAGE_ACCOUNT_NAME 設定為儲存體帳戶名稱:
STORAGE_ACCOUNT_NAME=$(az storage account list \
--resource-group 240900-linux-postgres \
--query '[0].name' \
-o tsv)
echo "STORAGE_ACCOUNT_NAME: $STORAGE_ACCOUNT_NAME"
使用 tailwind.sql 命令將 az storage blob download 下載至 Azure 虛擬機器:
az storage blob download \
--account-name $STORAGE_ACCOUNT_NAME \
--auth-mode login \
--container-name container1 \
--file tailwind.sql \
--name tailwind.sql
在遠端電腦上設定 psql 的環境變數
MANAGED_IDENTITY_NAME=240900-linux-postgres-identity
export AZURE_CLIENT_ID=$(az identity show --resource-group 240900-linux-postgres --name $MANAGED_IDENTITY_NAME --query "clientId" -o tsv)
PG_NAME=$(az postgres flexible-server list --resource-group 240900-linux-postgres --query "[0].name" -o tsv)
# Set psql environment variables
export PGHOST="${PG_NAME}.privatelink.postgres.database.azure.com"
export PGPASSWORD=$(curl -s "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fossrdbms-aad.database.windows.net&client_id=${AZURE_CLIENT_ID}" -H Metadata:true | jq -r .access_token)
export PGUSER=$MANAGED_IDENTITY_NAME
export PGDATABASE=postgres
使用 psql 匯入 tailwind.sql
psql -f tailwind.sql
連線到 postgres 伺服器以確認匯入成功
psql
列出資料表
\dt
會出現下列輸出:
postgres=> \dt
List of relations
Schema | Name | Type | Owner
--------+----------------------+-------+--------------------------------
public | cart_items | table | 240900-linux-postgres-identity
public | checkouts | table | 240900-linux-postgres-identity
public | collections | table | 240900-linux-postgres-identity
public | collections_products | table | 240900-linux-postgres-identity
public | customers | table | 240900-linux-postgres-identity
public | delivery_methods | table | 240900-linux-postgres-identity
public | product_types | table | 240900-linux-postgres-identity
public | products | table | 240900-linux-postgres-identity
public | shipment_items | table | 240900-linux-postgres-identity
public | shipments | table | 240900-linux-postgres-identity
public | store_inventory | table | 240900-linux-postgres-identity
public | stores | table | 240900-linux-postgres-identity
public | suppliers | table | 240900-linux-postgres-identity
public | supply_orders | table | 240900-linux-postgres-identity
(14 rows)
執行列出資料表的 SQL 查詢
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public';
會出現下列輸出:
postgres=> SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public';
table_name
----------------------
collections
stores
customers
cart_items
product_types
products
suppliers
collections_products
checkouts
shipments
delivery_methods
shipment_items
store_inventory
supply_orders
(14 rows)
開啟展開模式,然後從產品資料表中進行選取
在 postgres=> 提示字元中,開啟展開模式:
\x
從產品資料表中進行選取:
select * from products;
下列提示隨即出現:
postgres=> \x
Expanded display is on.
postgres=> select * from products;
產品清單隨即出現:
id | 1
product_type_id | 1
supplier_id | 2
sku | brush_cleaner
name | Meltdown Brush Cleaner
price | 12.99
description | We all leave our brushes sitting around, full of old dry paint. Don't worry! The Meltdown Brush Cleaner can remove just about anything.
image | brush_cleaner.jpg
digital | f
unit_description | 1 - 10oz Jar
package_dimensions | 4x8x2
weight_in_pounds | 3.2
reorder_amount | 10
status | in-stock
requires_shipping | t
warehouse_location | Zone 1, Shelf 12, Slot 6
created_at | ...
updated_at | ...
...
選取空格鍵以逐頁查看結果。 輸入 q 以結束頁面巡覽區。
結束 psql
\q
透過命令列以互動方式執行應用程式
在遠端電腦上,變更為包含該應用程式的目錄:
cd tailwind-traders-go/app
從命令列以互動方式執行應用程式:
go run main.go app:serve
會出現下列輸出:
$ go run main.go app:serve
Listening on :8080
尋找 VM 的公用 IP 位址
取得虛擬機器的公用 IP 位址:
IP_ADDRESS=$(az network public-ip show \
--resource-group 240900-linux-postgres \
--name vm-1-ip \
--query ipAddress \
--out tsv)
將 URL 輸出至終端機:
echo "Your URL is: http://${IP_ADDRESS}:8080"
本單元使用連接埠 8080 進行互動式開發/測試用途。 在生產環境中,您會使用連接埠 443,並需要 TLS 憑證來協助保護端點的流量。
瀏覽公用 API 端點
在網頁瀏覽器中開啟 URL。 會出現下列輸出:
{
"id": 5,
"product_type_id": 1,
"supplier_id": 2,
"sku": "drafting_tools",
"name": "Bespoke Drafting Set",
"price": 45,
"description": "Build your next bridge (or tunnel) using our Bespoke Drafting Set. Everyone drives across *regular* bridges everyday - but they'll rememeber yours - because it's _bespoke_.",
"image": "drafting_tools.jpg",
"digital": false,
"unit_description": "Tools and carrying case",
"package_dimensions": "5x10x3",
"weight_in_pounds": "1.2",
"reorder_amount": 10,
"status": "in-stock",
"requires_shipping": true,
"warehouse_location": "Zone 1, Shelf 4, Slot 1",
"created_at": "...",
"updated_at": "..."
}
或者,您也可以使用 curl 向 API 端點提出要求:
curl "http://${IP_ADDRESS}:8080"
此端點會顯示資料庫中的隨機產品。
檢視記錄在終端機上的請求
返回您在其中以互動方式執行應用程式的終端機。 輸出會顯示對 API 端點提出的要求:
{"time":"...","level":"INFO","msg":"httpLog","remoteAddr":"[::1]:58592","method":"GET","url":"/"}
{"time":"...","level":"INFO","msg":"httpLog","remoteAddr":"[::1]:59414","method":"GET","url":"/"}
{"time":"...","level":"INFO","msg":"httpLog","remoteAddr":"[::1]:59414","method":"GET","url":"/favicon.ico"}
如果這些要求成功,則表示您已成功將應用程式工作負載移轉至 Azure 虛擬機器和適用於 PostgreSQL 的 Azure 資料庫 (彈性伺服器)。
清除 Azure 資源
完成 Linux 和 PostgreSQL 工作負載的探索後,請清除資源以節省成本。
您可以透過 Azure 入口網站手動刪除資源群組 240900-linux-postgres,或執行下列 Azure CLI 命令:
az group delete \
--name 240900-linux-postgres \
--yes \
--no-wait
另一個可用的選項是使用 empty.bicep 範本來刪除 vm-postgres.bicep 檔案所建立的資源。 使用 az deployment group create 執行 --mode Complete 時會移除範本所未定義的資源。 因為 empty.json 沒有資源,所以命令會刪除每個資源。
az deployment group create \
--resource-group 240900-linux-postgres \
--template-file deploy/empty.bicep \
--mode Complete
部署 empty.json 會讓 240900-linux-postgres 資源群組保持不變,因此您可使用單一命令再次部署該資源。