استكشاف وتشغيل أحمال عمل Linux وPostgreSQL
في هذه الوحدة ستتعلم:
- نشر حساب Azure Blob Storage باستخدام قالب Bicep.
- إنشاء حاوية Blob Storage.
- ترحيل الصور إلى حساب Blob Storage.
- تحميل
tailwind.sqlإلى حساب Blob Storage. - الاتصال بجهاز 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
إضافة المستخدم الحالي إلى دور مالك بيانات Storage 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\""
},
...
]
تحميل التطبيق/البيانات/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
الاتصال بجهاز Azure الظاهري باستخدام الأمر az ssh
az ssh vm \
--resource-group 240900-linux-postgres \
--name vm-1
تنزيل ملف tailwind.sql من حساب التخزين
تعيين متغير STORAGE_ACCOUNT_NAME Bash إلى اسم حساب التخزين:
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 | ...
...
حدد مفتاح المسافة للصفحة عبر النتائج. أدخل 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 العام للجهاز الظاهري
الحصول على عنوان 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 للمساعدة في تأمين حركة المرور إلى نقطة النهاية.
استعراض نقطة نهاية واجهة برمجة التطبيقات العامة
افتح عنوان 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:
curl "http://${IP_ADDRESS}:8080"
تعرض نقطة النهاية هذه منتجا عشوائيا من قاعدة البيانات.
عرض الطلبات المسجلة إلى المحطة الطرفية
ارجع إلى المحطة الطرفية حيث تقوم بتشغيل التطبيق بشكل تفاعلي. يظهر الإخراج الطلب إلى نقطة نهاية واجهة برمجة التطبيقات:
{"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 الموارد يدويا عبر مدخل Microsoft 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 مجموعة الموارد سليمة، بحيث يمكنك نشر الموارد مرة أخرى باستخدام أمر واحد.