สํารวจและเรียกใช้ปริมาณงาน Linux และ PostgreSQL

เสร็จสมบูรณ์เมื่อ

ในหน่วยนี้ คุณจะ:

  • ปรับใช้บัญชีที่เก็บข้อมูล Azure Blob โดยใช้เทมเพลต Bicep
  • สร้างคอนเทนเนอร์ที่เก็บข้อมูล Blob
  • ย้ายรูปภาพไปยังบัญชีที่เก็บข้อมูล Blob
  • อัปโหลด tailwind.sql ไปยังบัญชีที่เก็บข้อมูล Blob
  • เชื่อมต่อไปยังเครื่องเสมือน Azure โดยใช้ Azure CLI
  • ดาวน์โหลดไฟล์จากบัญชีเก็บข้อมูล
  • เชื่อมต่อไปยังเซิร์ฟเวอร์ PostgreSQL โดยใช้ psql และนําเข้าไฟล์ 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 Data Storage

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\""
  },
  ...
]

อัปโหลดแอป/ข้อมูล/โพสต์/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

เชื่อมต่อไปยังเครื่องเสมือน Azure โดยใช้คําสั่ง az ssh

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 ไปยังเครื่องเสมือน Azure โดยใช้คําสั่ง az storage blob download:

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

นําเข้า tailwind.sql โดยใช้ psql

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         | ...
...

เลือก Spacebar เพื่อไปยังหน้าผ่านผลลัพธ์ ใส่ q เพื่อออกจาก pager

ออกจาก psql

\q

เรียกใช้แอปพลิเคชันแบบโต้ตอบผ่านบรรทัดคําสั่ง

บนเครื่องระยะไกล เปลี่ยนไปยังไดเรกทอรีที่ประกอบด้วยแอปพลิเคชัน:

cd tailwind-traders-go/app

เรียกใช้แอปพลิเคชันแบบโต้ตอบจากบรรทัดคําสั่ง:

go run main.go app:serve

ผลลัพธ์ต่อไปนี้จะปรากฏขึ้น:

$ go run main.go app:serve
Listening on :8080

ค้นหาที่อยู่ IP สาธารณะของ VM

รับที่อยู่ 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": "..."
}

อีกวิธีหนึ่งคือ คุณสามารถทําการร้องขอไปยังจุดสิ้นสุด API โดยใช้ curl:

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 และฐานข้อมูล Azure สําหรับ PostgreSQL (เซิร์ฟเวอร์แบบยืดหยุ่น) เรียบร้อยแล้ว

ล้างทรัพยากร Azure

หลังจากที่คุณเสร็จสิ้นการสํารวจปริมาณงาน Linux และ PostgreSQL ให้ล้างข้อมูลทรัพยากรเพื่อประหยัดค่าใช้จ่าย

คุณสามารถลบกลุ่มทรัพยากร 240900-linux-postgres ด้วยตนเองผ่านทางพอร์ทัล Azure หรือเรียกใช้คําสั่ง 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 ไม่เปลี่ยนแปลง ดังนั้นคุณสามารถปรับใช้ทรัพยากรอีกครั้งโดยใช้คําสั่งเดียว

ทรัพยากร