Alibaba Cloud CLI kullanarak bir Elastic Compute Service (ECS) örneğini bölgeler arası geçirin. Kaynak örneğin özel bir görüntüsünü oluşturun, bunu hedef bölgeye kopyalayın ve kopyalanan görüntüden yeni bir örnek başlatın.
İşleyiş şekli
Bölgeler arası geçiş, ECS örnek disklerinizi yeni bir bölgede çoğaltmak için özel bir görüntü kullanır.
Kaynak örnekten özel bir görüntü oluşturun. Bu işlem, tüm disklerdeki işletim sistemi, uygulamalar ve verileri yakalar.
Özel görüntüyü hedef bölgeye kopyalayın.
Hedef bölgede kopyalanan görüntüden yeni bir örnek oluşturun.
Tüm uygulamaların ve hizmetlerin beklendiği gibi çalıştığından emin olmak için yeni örneği doğrulayın.
Gereksiz maliyetlerden kaçınmak için kaynak kaynakları serbest bırakın.
Başlamadan önce
Sınırlamalar
Anlık görüntü oluşturulamayan yerel diske sahip örnekler desteklenmez.
Bu yöntem, yerel SSD'ye sahip örneklerden sistem disklerinin ve veri disklerinin anlık görüntülerini içeren görüntüler oluşturmayı desteklemez. Daha fazla bilgi için bkz. Yerel SSD'ye sahip örnek aileleri (i serisi).
Kaynak örnek klasik ağda veya VPC'de olabilir. Hedef örnek bir VPC'de olmalıdır.
Örnek türü, hedef alanda mevcut olmalıdır.
Faturalama
Özel görüntünün bir parçası olarak oluşturulan anlık görüntüler ücretlendirilir. Daha fazla bilgi için bkz. Anlık görüntü faturalaması.
Planlama hususları
Hedef bölgede bir VPC, vSwitch ve güvenlik grubu oluşturun.
Yeni örnek farklı meta verilere (örnek kimliği, özel IP adresi, MAC adresi) sahiptir. MAC adresine bağlı lisanslar veya küme IP yapılandırmaları gibi bu meta verilere bağlı olan uygulamaları veya betikleri güncelleyin. Daha fazla bilgi için bkz. Örnek meta verileri.
Ön koşullar
Alibaba Cloud CLI'yı yükleyin ve yapılandırın. Daha fazla bilgi için bkz. Kurulum kılavuzu ve Kimlik bilgilerini yapılandırma ve yönetme.
Gerekli ECS API işlemleri için (
CreateImage,CopyImage,RunInstancesveDeleteInstance) izinlere sahip olduğunuzdan emin olun.
Adım 1: Kaynak örnekten özel bir görüntü oluşturun
Kaynak örneğinizin özel bir görüntüsünü oluşturmak için CreateImage işlemini çağırın. Daha fazla bilgi için bkz. Bir örnekten özel görüntü oluşturma.
Kaynak örnek çalışıyorsa, anlık görüntü verilerinin tutarlılığını sağlamak için bu işlem örneği birkaç dakika durdurur.
aliyun ecs CreateImage \
--RegionId 'cn-hangzhou' \
--ImageName 'source-image-for-migration' \
--InstanceId 'i-bp1g6zv0ce8oghu7****'
Yanıt, yeni görüntü kimliğini döndürür.
{
"ImageId": "m-bp146shijn7hujku****",
"RequestId": "C8B26B44-0189-443E-9816-*******"
}
Bir sonraki adım için ImageId değerini kaydedin.
Adım 2: Görüntüyü hedef bölgeye kopyalayın
Kopyalamadan önce görüntü durumunun Available olması için bekleyin. Durumu kontrol etmek için DescribeImages işlemini çağırın.
Görüntüyü hedef bölgeye kopyalamak için CopyImage işlemini çağırın. Bu örnekte, bir görüntü Çin (Hangzhou) bölgesinden Çin (Pekin) bölgesine kopyalanmaktadır.
aliyun ecs CopyImage \
--RegionId 'cn-hangzhou' \
--ImageId 'm-bp146shijn7hujku****' \
--DestinationRegionId 'cn-beijing' \
--DestinationImageName 'migrated-image-beijing'
Yanıt, hedef bölgedeki kopyalanan görüntü için yeni bir ImageId döndürür.
{
"ImageId": "m-bp1h46wfpjsjastd****",
"RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E"
}
Bir sonraki adım için bu yeni ImageId değerini kaydedin.
Adım 3: Hedef örneği oluşturun
Hedef bölgede kopyalanan görüntüden yeni bir örnek oluşturmak için RunInstances işlemini çağırın. Daha fazla bilgi için bkz. Özel Başlatma.
aliyun ecs RunInstances \
--RegionId 'cn-beijing' \
--ImageId 'm-bp1h46wfpjsjastd****' \
--InstanceType 'ecs.g6.large' \
--SecurityGroupId 'sg-2zea9dbddva****' \
--VSwitchId 'vsw-2zep7vc25mjc1****' \
--InstanceName 'migrated-instance-beijing' \
--PasswordInherit true \
--InternetChargeType 'PayByTraffic' \
--SystemDisk.Size '40' \
--SystemDisk.Category 'cloud_essd' \
--InstanceChargeType 'PostPaid' \
--InternetMaxBandwidthOut '10'
PasswordInherit değeri true olarak ayarlandığından, yeni örnek kaynak örnekle aynı parolayı kullanır.
Yanıt, yeni örnek kimliğini döndürür.
{
"RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3****",
"InstanceIdSets": {
"InstanceIdSet": [
"i-bp67acfmxazb4pd2****"
]
}
}
Adım 4: Hedef örneği doğrulayın
Yeni örnek başladıktan sonra bağlanın ve aşağıdakileri doğrulayın:
Sistem ve veri disklerindeki dosya sistemi ve veriler kaynak örnekle eşleşir.
Uygulamalar ve hizmetler doğru şekilde başlar ve çalışır.
Örnek meta verilerine (IP adresleri, MAC adresleri) bağlı kaynak ilişkileri güncellenir.
Adım 5: Kaynak kaynakları serbest bırakın
Yeni örneğin doğru çalıştığını doğruladıktan sonra gereksiz ücretlerden kaçınmak için orijinal kaynakları serbest bırakın.
Örnekleri serbest bırakmak ile görüntüleri ve anlık görüntüleri silmek geri alınamaz. Devam etmeden önce veri geçişinizi doğrulayın.
-
Kaynak örneği serbest bırakın: DeleteInstance işlemini çağırın. Daha fazla bilgi için bkz. Örnek serbest bırakma.
aliyun ecs DeleteInstance --InstanceId 'i-bp1g6zv0ce8oghu7****' --Force true -
Özel görüntüleri silin: Hem kaynak hem de kopyalanan görüntüler için DeleteImage işlemini çağırın. Daha fazla bilgi için bkz. Özel görüntü silme.
# Delete the source region image aliyun ecs DeleteImage --RegionId 'cn-hangzhou' --ImageId 'm-bp146shijn7hujku****' # Delete the destination region image aliyun ecs DeleteImage --RegionId 'cn-beijing' --ImageId 'm-bp1h46wfpjsjastd****' -
İlgili anlık görüntüleri silin: Görüntüleme işlemi tarafından oluşturulan her anlık görüntü için DeleteSnapshot işlemini çağırın.
aliyun ecs DeleteSnapshot --SnapshotId 's-bp1c0doj0taqyzzl****'
Geçişi betiklerle otomatikleştirin
Aşağıdaki betikler, geçişiniz için uyarlayabileceğiniz temel bir çerçeve sağlar.
Betik örnekleri:
Bash
Bu betiği çalıştırmak için jq aracının yüklü olması gerekir.
#!/usr/bin/env bash
# Source instance ID
SRC_INSTANCE_ID="i-bp1g6zv0ce8oghu7****"
# Source instance region ID
SRC_REGION_ID="cn-hangzhou"
# Destination region ID
DST_REGION_ID="cn-beijing"
# Destination instance zone ID
DST_ZONE_ID="cn-beijing-h"
# Source instance type
SRC_INSTANCE_TYPE="ecs.e-c1m1.large"
# Source instance system disk category
SRC_SYSTEM_DISK_CATEGORY="cloud_auto"
# Source instance system disk size
SRC_SYSTEM_DISK_SIZE=40
# Output log
function log {
local level="$1"
local message="$2"
echo "$(date +'%Y-%m-%d %H:%M:%S') [$level] $message"
}
# Wrap Alibaba Cloud CLI
function invoke_aliyun_command() {
local service="$1"
local action="$2"
shift 2
local -a params=("$@")
response=$(aliyun "$service" "$action" "${params[@]}")
exit_code=$?
if [ $exit_code -eq 0 ]; then return 0; fi
log "ERROR" "Failed to invoke aliyun command: aliyun $service $action ${params[*]}"
exit 1
}
# Wait for resource to be available
function wait_resource_available() {
local service="$1"
local action="$2"
local region_id="$3"
local resource_type="$4"
local resource_id="$5"
local -a params=(
"--region" "$region_id"
"--RegionId" "$region_id"
"--${resource_type}Id" "$resource_id"
)
if [ "$resource_type" == "Image" ]; then
params+=("--Status" "Creating,Waiting,Available")
fi
local current_status
local current_progress
local timeout=1200
local interval=20
local end_time=$(( $(date +%s) + timeout ))
while (( $(date +%s) < end_time )); do
invoke_aliyun_command "$service" "$action" "${params[@]}"
current_status=$(echo "$response" | jq -r '.. | .Status? // empty' | head -n1)
current_progress=$(echo "$response" | jq -r '.. | .Progress? // empty' | head -n1)
log "INFO" "${resource_type} status: $current_status"
if [[ -n "$current_progress" ]]; then
log "INFO" "Creation progress: $current_progress"
fi
if [[ "$current_status" == "Available" ]]; then return 0; fi
sleep "$interval"
done
}
# Create image
log "INFO" "Creating source image from instance '$SRC_INSTANCE_ID'"
src_img_params=(
"--region" "$SRC_REGION_ID"
"--RegionId" "$SRC_REGION_ID"
"--InstanceId" "$SRC_INSTANCE_ID"
"--ImageName" "cli-src-img"
)
invoke_aliyun_command ecs CreateImage "${src_img_params[@]}"
src_img_id=$(echo "$response" | jq -r .ImageId)
wait_resource_available ecs DescribeImages "$SRC_REGION_ID" "Image" "$src_img_id"
log "INFO" "Source image created: '$src_img_id'"
# Copy image to destination region
log "INFO" "Copying image to region: '$DST_REGION_ID'"
dst_img_params=(
"--region" "$SRC_REGION_ID"
"--RegionId" "$SRC_REGION_ID"
"--DestinationRegionId" "$DST_REGION_ID"
"--ImageId" "$src_img_id"
"--DestinationImageName" "cli-dst-img"
)
invoke_aliyun_command ecs CopyImage "${dst_img_params[@]}"
dst_img_id=$(echo "$response" | jq -r .ImageId)
wait_resource_available ecs DescribeImages "$DST_REGION_ID" "Image" "$dst_img_id"
log "INFO" "Destination image copied: '$dst_img_id'"
# Create VPC
log "INFO" "Creating VPC"
dst_vpc_params=(
"--region" "$DST_REGION_ID"
"--RegionId" "$DST_REGION_ID"
"--CidrBlock" "10.0.0.0/8"
"--VpcName" "cli-dst-vpc"
)
invoke_aliyun_command ecs CreateVpc "${dst_vpc_params[@]}"
dst_vpc_id=$(echo "$response" | jq -r .VpcId)
wait_resource_available "Vpc" "DescribeVpcAttribute" "$DST_REGION_ID" "Vpc" "$dst_vpc_id"
log "INFO" "VPC created: '$dst_vpc_id'"
# Create a vSwitch
log "INFO" "Creating VSwitch"
dst_vsw_params=(
"--region" "$DST_REGION_ID"
"--RegionId" "$DST_REGION_ID"
"--ZoneId" "$DST_ZONE_ID"
"--VpcId" "$dst_vpc_id"
"--CidrBlock" "10.1.1.0/24"
)
invoke_aliyun_command vpc CreateVSwitch "${dst_vsw_params[@]}"
dst_vsw_id=$(echo "$response" | jq -r .VSwitchId)
wait_resource_available "vpc" "DescribeVSwitchAttributes" "$DST_REGION_ID" "VSwitch" "$dst_vsw_id"
log "INFO" "VSwitch created: '$dst_vsw_id'"
# Create security group
log "INFO" "Creating security group"
dst_sg_params=(
"--region" "$DST_REGION_ID"
"--RegionId" "$DST_REGION_ID"
"--SecurityGroupName" "cli-dst-sg"
"--VpcId" "$dst_vpc_id"
"--SecurityGroupType" "normal"
)
invoke_aliyun_command ecs CreateSecurityGroup "${dst_sg_params[@]}"
dst_sg_id=$(echo "$response" | jq -r .SecurityGroupId)
log "INFO" "Security group created: '$dst_sg_id'"
# Create a pay-as-you-go ECS instance in the destination region
log "INFO" "Creating ECS instance in zone: '$DST_ZONE_ID'"
dst_instance_params=(
"--region" "$DST_REGION_ID"
"--RegionId" "$DST_REGION_ID"
"--ImageId" "$dst_img_id"
"--SecurityGroupId" "$dst_sg_id"
"--VSwitchId" "$dst_vsw_id"
"--InstanceType" "$SRC_INSTANCE_TYPE"
"--InstanceName" "cli-dst-ecs"
"--PasswordInherit" "true"
"--SystemDisk.Category" "$SRC_SYSTEM_DISK_CATEGORY"
"--SystemDisk.Size" "$SRC_SYSTEM_DISK_SIZE"
"--InstanceChargeType" "PostPaid"
)
invoke_aliyun_command ecs RunInstances "${dst_instance_params[@]}"
dst_instance_id=$(echo "$response" | jq -r '.InstanceIdSets.InstanceIdSet[0]')
log "INFO" "ECS instance created: '$dst_instance_id'"
Powershell
# Set error exit policy
$ErrorActionPreference = "Stop"
# Set character encoding
chcp 65001 | Out-Null
[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8
# Source instance ID
$SRC_INSTANCE_ID = "i-bp1g6zv0ce8oghu7****"
# Source instance region ID
$SRC_REGION_ID = "cn-hangzhou"
# Destination region ID
$DST_REGION_ID = "cn-beijing"
# Destination instance zone ID
$DST_ZONE_ID = "cn-beijing-h"
# Source instance type
$SRC_INSTANCE_TYPE = "ecs.e-c1m1.large"
# Source instance system disk category
$SRC_SYSTEM_DISK_CATEGORY = "cloud_auto"
# Source instance system disk size
$SRC_SYSTEM_DISK_SIZE = 40
# Output log
function Log {
param ([string]$Level, [string]$Message)
Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [$Level] $Message"
}
# Wrap Alibaba Cloud CLI
function Invoke-AliyunCommand {
param ([string]$Service, [string]$Action, [array]$Params)
$response = aliyun $Service $Action $Params
if ($LASTEXITCODE -eq 0) { return $response | ConvertFrom-Json }
Log "ERROR" "Failed to call aliyun command: aliyun $Service $Action $Params"
exit 1
}
# Get nested property value
function Get-NestedPropertyValue {
param (
[object]$Object,
[string]$PropertyPath
)
$parts = $PropertyPath -split '\.'
$value = $Object
foreach ($part in $parts) {
$value = $value.$part
}
return $value
}
# Wait for resource to be available
function Wait-ResourceAvailable {
param (
[string]$Service,
[string]$Action,
[object]$Params,
[string]$StatusPath,
[string]$ProgressPath
)
$timeout = 1200
$interval = 20
$endTime = (Get-Date).AddSeconds($timeout)
while ((Get-Date) -lt $endTime) {
$response = Invoke-AliyunCommand $Service $Action $Params
$currentStatus = Get-NestedPropertyValue $response $StatusPath
Log "INFO" "Resource status: $currentStatus"
if ($ProgressPath) {
$currentProgress = Get-NestedPropertyValue $response $ProgressPath
Log "INFO" "Creation progress: $currentProgress"
}
if ("Available" -eq $currentStatus) { return }
Start-Sleep -Seconds $interval
}
}
# Create image
Log "INFO" "Creating source image from instance '$SRC_INSTANCE_ID'"
$srcImgId = (Invoke-AliyunCommand -Service "ecs" -Action "CreateImage" -Params @(
"--region", $SRC_REGION_ID,
"--RegionId", $SRC_REGION_ID,
"--InstanceId", $SRC_INSTANCE_ID,
"--ImageName", "cli-src-img"
)).ImageId
Wait-ResourceAvailable -Service "ecs" -Action "DescribeImages" -Params @(
"--region", $SRC_REGION_ID,
"--RegionId", $SRC_REGION_ID,
"--ImageId", $srcImgId,
"--Status", "Creating,Waiting,Available"
) -StatusPath "Images.Image.Status" -ProgressPath "Images.Image.Progress"
Log "INFO" "Source image created: '$srcImgId'"
# Copy image to destination region
Log "INFO" "Copying image to region: '$DST_REGION_ID'"
$dstImgId = (Invoke-AliyunCommand -Service "ecs" -Action "CopyImage" -Params @(
"--region", $SRC_REGION_ID,
"--RegionId", $SRC_REGION_ID,
"--DestinationRegionId", $DST_REGION_ID,
"--ImageId", $srcImgId,
"--DestinationImageName", "cli-dst-img"
)).ImageId
Wait-ResourceAvailable -Service "ecs" -Action "DescribeImages" -Params @(
"--region", $DST_REGION_ID,
"--RegionId", $DST_REGION_ID,
"--ImageId", $dstImgId,
"--Status", "Creating,Waiting,Available"
) -StatusPath "Images.Image.Status" -ProgressPath "Images.Image.Progress"
Log "INFO" "Destination image copied: '$dstImgId'"
# Create VPC
Log "INFO" "Creating VPC"
$dstVpcId = (Invoke-AliyunCommand -Service "vpc" -Action "CreateVpc" -Params @(
"--region", $DST_REGION_ID,
"--RegionId", $DST_REGION_ID,
"--CidrBlock", "10.0.0.0/8",
"--VpcName", "cli-dst-vpc"
)).VpcId
Wait-ResourceAvailable -Service "Vpc" -Action "DescribeVpcAttribute" -Params @(
"--region", $DST_REGION_ID,
"--RegionId", $DST_REGION_ID,
"--VpcId", $dstVpcId
) -StatusPath "Status"
Log "INFO" "VPC created: '$dstVpcId'"
# Create a vSwitch
Log "INFO" "Creating VSwitch"
$dstVSwitchId = (Invoke-AliyunCommand vpc CreateVSwitch @(
"--region", $DST_REGION_ID,
"--RegionId", $DST_REGION_ID,
"--ZoneId", $DST_ZONE_ID,
"--VpcId", $dstVpcId,
"--CidrBlock", "10.1.1.0/24"
)).VSwitchId
Wait-ResourceAvailable -Service "Vpc" -Action "DescribeVSwitchAttributes" -Parameters @(
"--region", $DST_REGION_ID,
"--RegionId", $DST_REGION_ID,
"--VSwitch", $dstVSwitchId
) -StatusPath "Status"
Log "INFO" "VSwitch created: '$dstVSwitchId'"
# Create security group
Log "INFO" "Creating security group"
$dstSecurityGroupId = (Invoke-AliyunCommand ecs CreateSecurityGroup @(
"--region", $DST_REGION_ID,
"--RegionId", $DST_REGION_ID,
"--SecurityGroupName", "cli-dst-sg",
"--VpcId", $dstVpcId,
"--SecurityGroupType", "normal"
)).SecurityGroupId
Log "INFO" "Security group created: '$dstSecurityGroupId'"
# Create a pay-as-you-go ECS instance in the destination region
Log "INFO" "Creating ECS instance in zone: '$DST_ZONE_ID'"
$dstInstanceId = (Invoke-AliyunCommand ecs RunInstances @(
"--region", $DST_REGION_ID,
"--RegionId", $DST_REGION_ID,
"--ImageId", $dstImgId,
"--SecurityGroupId", $dstSecurityGroupId,
"--VSwitchId", $dstVSwitchId,
"--InstanceType", $SRC_INSTANCE_TYPE,
"--InstanceName", "cli-dst-ecs",
"--PasswordInherit", "true",
"--SystemDisk.Category", $SRC_SYSTEM_DISK_CATEGORY,
"--SystemDisk.Size", $SRC_SYSTEM_DISK_SIZE,
"--InstanceChargeType", "PostPaid"
)).InstanceIdSets.InstanceIdSet[0]
Log "INFO" "ECS instance created: '$dstInstanceId'"
Log "INFO" "Script execution completed successfully."
İlgili konular
Özel görüntü ile örnekleri bölgeler arası çoğaltma (konsol tabanlı yöntem)