這篇文章帶給大家的內容是關於如何使用阿里雲ECS SDK 合理快速地創建並管理搶佔式實例,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
管理搶佔式實例
準備工作
在執行操作之前,您需要:
了解能滿足您業務要求的執行個體規格和地理。
熟悉了解阿里雲 ECS SDK 的基礎知識和呼叫方法。詳細信息,請參考 SDK 使用說明。
注意:
搶佔式實例程式碼需要依賴的 ECS SDK 版本 4.2.0 以上。以Java POM 依賴為例,修改引入pom 依賴:
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-ecs</artifactId> <version>4.2.0</version> </dependency>
查詢地域及可用的實例規格
使用OpenAPI DescribeZones 查詢可以創建搶佔式執行個體的地域以及可用的執行個體規格。範例程式碼如下所示。
OpenApiCaller.java public class OpenApiCaller { IClientProfile profile; IAcsClient client; public OpenApiCaller() { profile = DefaultProfile.getProfile("cn-hangzhou", AKSUtil.accessKeyId, AKSUtil.accessKeySecret); client = new DefaultAcsClient(profile); } public <T extends AcsResponse> T doAction(AcsRequest<T> var1) { try { return client.getAcsResponse(var1); } catch (Throwable e) { e.printStackTrace(); return null; } } } DescribeZonesSample.java public class DescribeZonesSample { public static void main(String[] args) { OpenApiCaller caller = new OpenApiCaller(); DescribeZonesRequest request = new DescribeZonesRequest(); request.setRegionId("cn-zhangjiakou");//可以通过 DescribeRegionsRequest 获取每个地域的 RegionId request.setSpotStrategy("SpotWithPriceLimit");//对于查询是否可购买抢占式实例此项必填 request.setInstanceChargeType("PostPaid");//后付费模式,抢占式实例必须是后付费模式 DescribeZonesResponse response = caller.doAction(request); System.out.println(JSON.toJSONString(response)); } }
以下為輸出結果,可查看每個地理上各地理可供選擇的實例規格、磁碟類型、網路類型等資訊。
{ "requestId": "388D6321-E587-470C-8CFA-8985E2963DAE", "zones": [ { "localName": "华北 3 可用区 A", "zoneId": "cn-zhangjiakou-a", "availableDiskCategories": [ "cloud_ssd", "cloud_efficiency" ], "availableInstanceTypes": [ "ecs.e4.large", "ecs.n4.4xlarge", "ecs.sn2.medium", "ecs.i1.2xlarge", "ecs.se1.2xlarge", "ecs.n4.xlarge", "ecs.se1ne.2xlarge", "ecs.se1.large", "ecs.sn2.xlarge", "ecs.se1ne.xlarge", "ecs.xn4.small", "ecs.sn2ne.4xlarge", "ecs.se1ne.4xlarge", "ecs.sn1.medium", "ecs.n4.8xlarge", "ecs.mn4.large", "ecs.e4.2xlarge", "ecs.mn4.2xlarge", "ecs.mn4.8xlarge", "ecs.n4.2xlarge", "ecs.e4.xlarge", "ecs.sn2ne.large", "ecs.sn2ne.xlarge", "ecs.sn1ne.large", "ecs.n4.large", "ecs.sn1.3xlarge", "ecs.e4.4xlarge", "ecs.sn1ne.2xlarge", "ecs.e4.small", "ecs.i1.4xlarge", "ecs.se1.4xlarge", "ecs.sn2ne.2xlarge", "ecs.sn2.3xlarge", "ecs.i1.xlarge", "ecs.n4.small", "ecs.sn1ne.4xlarge", "ecs.mn4.4xlarge", "ecs.sn1ne.xlarge", "ecs.se1ne.large", "ecs.sn2.large", "ecs.i1-c5d1.4xlarge", "ecs.sn1.xlarge", "ecs.sn1.large", "ecs.mn4.small", "ecs.mn4.xlarge", "ecs.se1.xlarge" ], "availableResourceCreation": [ "VSwitch", "IoOptimized", "Instance", "Disk" ], "availableResources": [ { "dataDiskCategories": [ "cloud_ssd", "cloud_efficiency" ], "instanceGenerations": [ "ecs-3", "ecs-2" ], "instanceTypeFamilies": [ "ecs.mn4", "ecs.sn1", "ecs.sn2", "ecs.sn1ne", "ecs.xn4", "ecs.i1", "ecs.se1", "ecs.e4", "ecs.n4", "ecs.se1ne", "ecs.sn2ne" ], "instanceTypes": [ "ecs.n4.4xlarge", "ecs.sn2.medium", "ecs.i1.2xlarge", "ecs.se1.2xlarge", "ecs.n4.xlarge", "ecs.se1ne.2xlarge", "ecs.se1.large", "ecs.sn2.xlarge", "ecs.se1ne.xlarge", "ecs.xn4.small", "ecs.sn2ne.4xlarge", "ecs.se1ne.4xlarge", "ecs.sn1.medium", "ecs.n4.8xlarge", "ecs.mn4.large", "ecs.mn4.2xlarge", "ecs.mn4.8xlarge", "ecs.n4.2xlarge", "ecs.sn2ne.large", "ecs.sn2ne.xlarge", "ecs.sn1ne.large", "ecs.n4.large", "ecs.sn1.3xlarge", "ecs.sn1ne.2xlarge", "ecs.e4.small", "ecs.i1.4xlarge", "ecs.se1.4xlarge", "ecs.sn2ne.2xlarge", "ecs.sn2.3xlarge", "ecs.i1.xlarge", "ecs.n4.small", "ecs.sn1ne.4xlarge", "ecs.mn4.4xlarge", "ecs.sn1ne.xlarge", "ecs.se1ne.large", "ecs.sn2.large", "ecs.i1-c5d1.4xlarge", "ecs.sn1.xlarge", "ecs.sn1.large", "ecs.mn4.small", "ecs.mn4.xlarge", "ecs.se1.xlarge" ], "ioOptimized": true, "networkTypes": [ "vpc" ], "systemDiskCategories": [ "cloud_ssd", "cloud_efficiency" ] } ], "availableVolumeCategories": [ "san_ssd", "san_efficiency" ] } ] }
查詢搶佔式實例的歷史價格
使用OpenAPI DescribeSpotPriceHistory 查詢搶佔式實例最近30 天的價格變化數據,獲得最佳性價比的地域和規格信息,示例代碼(DescribeSpotPriceHistorySample.java)如下。
public class DescribeSpotPriceHistorySample { public static void main(String[] args) { OpenApiCaller caller = new OpenApiCaller(); List<DescribeSpotPriceHistoryResponse.SpotPriceType> result = new ArrayList<DescribeSpotPriceHistoryResponse.SpotPriceType>(); int offset = 0; while (true) { DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest(); request.setRegionId("cn-hangzhou"); //可以通过 DescribeRegionsRequest 获取可购买的每个地域的 RegionId request.setZoneId("cn-hangzhou-b");//可用区必填 request.setInstanceType("ecs.sn2.medium");//参考 DescribeZones 返回的实例类型,必填 request.setNetworkType("vpc");//参考 DescribeZones 返回的网络类型,必填 // request.setIoOptimized("optimized"); //是否 I/O 优化类型,DescribeZones 返回的 IoOptimized,选填 // request.setStartTime("2017-09-20T08:45:08Z");//价格开始时间,选填,默认 3 天内数据 // request.setEndTime("2017-09-28T08:45:08Z");//价格结束时间,选填 request.setOffset(offset); DescribeSpotPriceHistoryResponse response = caller.doAction(request); if (response != null && response.getSpotPrices() != null) { result.addAll(response.getSpotPrices()); } if (response.getNextOffset() == null || response.getNextOffset() == 0) { break; } else { offset = response.getNextOffset(); } } if (!result.isEmpty()) { for (DescribeSpotPriceHistoryResponse.SpotPriceType spotPriceType : result) { System.out.println(spotPriceType.getTimestamp() + "---> spotPrice:" + spotPriceType.getSpotPrice() + "----> originPrice:" + spotPriceType.getOriginPrice()); } System.out.println(result.size()); } else { } } }
以下為傳回結果範例。
2017-09-26T06:28:55Z--->spotPrice:0.24---->originPrice:1.2 2017-09-26T14:00:00Z--->spotPrice:0.36---->originPrice:1.2 2017-09-26T15:00:00Z--->spotPrice:0.24---->originPrice:1.2 2017-09-27T14:00:00Z--->spotPrice:0.36---->originPrice:1.2 2017-09-27T15:00:00Z--->spotPrice:0.24---->originPrice:1.2 2017-09-28T14:00:00Z--->spotPrice:0.36---->originPrice:1.2 2017-09-28T15:00:00Z--->spotPrice:0.24---->originPrice:1.2 2017-09-29T06:28:55Z--->spotPrice:0.24---->originPrice:1.2
重複上述步驟,您可以判斷出該規格資源在可用區的價格變動趨勢和最近價格。
說明:
您可以透過平均價格和最高價格來決定是否可以接受購買該搶佔式實例,也可以透過更合理的數據模型來分析歷史價格數據,隨時調整創建資源的規格和可用區,到達最佳性價比。
建立搶佔式實例
在建立搶佔式實例之前,您需要完成下列工作:
#如果您使用自訂鏡像建立搶佔式實例,必須已經建立自訂鏡像。
在控制台 建立安全性群組,或使用 OpenAPI CreateSecurityGroup 建立安全群組,並取得安全群組 ID(SecurityGroupId)。
在控制台建立 VPC 和 交換機,或使用 OpenAPI CreateVpc 和 CreateVSwitch 創建,並取得交換器 ID(VSwitchId)。
使用 OpenAPI CreateInstance 建立搶佔式實例。範例程式碼(CreateInstaneSample.java)如下。
public class CreateInstaneSample { public static void main(String[] args) { OpenApiCaller caller = new OpenApiCaller(); CreateInstanceRequest request = new CreateInstanceRequest(); request.setRegionId("cn-hangzhou");//地域 ID request.setZoneId("cn-hangzhou-b"); //可用区ID request.setSecurityGroupId("sg-bp11nhf94ivkdxwb2gd4");//提前创建的安全组 ID request.setImageId("centos_7_03_64_20G_alibase_20170818.vhd"); //建议选择您自己在该地域准备的自定义镜像 request.setVSwitchId("vsw-bp164cyonthfudn9kj5br");//VPC 类型需要交换机 ID request.setInstanceType("ecs.sn2.medium"); //填入您询价后需要购买的规格 request.setIoOptimized("optimized");//参考 DescirbeZones 返回参数 request.setSystemDiskCategory("cloud_ssd"); //参考 DescirbeZones 返回参数,多选一 cloud_ssd, cloud_efficiency, cloud request.setSystemDiskSize(40); request.setInstanceChargeType("PostPaid");//抢占式实例必须后付费 request.setSpotStrategy("SpotWithPriceLimit"); //SpotWithPriceLimit 出价模式,SpotAsPriceGo 不用出价,最高按量付费价格 request.setSpotPriceLimit(0.25F);//SpotWithPriceLimit 出价模式生效,您能接受的最高价格,单位为元每小时,必须高于当前的市场成交价才能成功 CreateInstanceResponse response = caller.doAction(request); System.out.println(response.getInstanceId()); } }
回收搶佔式實例
#當搶佔式實例可能會因為價格因素或市場供需變化而被強制回收。此時會觸發搶佔式執行個體的中斷。釋放前,搶佔式實例會進入鎖定狀態,提示實例會自動回收。您可以針對實例回收狀態自動化處理實例的退出邏輯。
目前,您可以透過以下任一種方式來取得搶佔式實例的中斷鎖定狀態:
透過 實例元資料 取得。執行以下指令:
curl 'http://100.100.100.200/latest/meta-data/instance/spot/termination-time'
如果傳回為空,表示實例可持續使用。如果傳回類似 2015-01-05T18:02:00Z 格式的資訊(UTC 時間),表示實例將於這個時間釋放。
使用 OpenAPI DescribeInstances,根據傳回的 OperationLocks 判斷執行個體是否進入 待回收 狀態。程式碼範例如下(DescribeInstancesSample.java)。
public class DescribeInstancesSample { public static void main(String[] args) throws InterruptedException { OpenApiCaller caller = new OpenApiCaller(); JSONArray allInstances = new JSONArray(); allInstances.addAll(Arrays.asList("i-bp18hgfai8ekoqwo0y2n", "i-bp1ecbyds24ij63w146c")); while (!allInstances.isEmpty()) { DescribeInstancesRequest request = new DescribeInstancesRequest(); request.setRegionId("cn-hangzhou"); request.setInstanceIds(allInstances.toJSONString());//指定实例 ID,效率最高 DescribeInstancesResponse response = caller.doAction(request); List<DescribeInstancesResponse.Instance> instanceList = response.getInstances(); if (instanceList != null && !instanceList.isEmpty()) { for (DescribeInstancesResponse.Instance instance : instanceList) { System.out.println ("result:instance:" + instance.getInstanceId() + ",az:" + instance.getZoneId()); if (instance.getOperationLocks() != null) { for (DescribeInstancesResponse.Instance.LockReason lockReason : instance.getOperationLocks()) { System.out.println("instance:" + instance.getInstanceId() + "--> lockReason:" + lockReason.getLockReason() + ",vmStatus:" + instance.getStatus()); if ("Recycling".equals(lockReason.getLockReason())) { //do your action System.out.println("spot instance will be recycled immediately, instance id:" + instance.getInstanceId()); allInstances.remove(instance.getInstanceId()); } } } } System.out.print("try describeInstances again later ..."); Thread.sleep(2 * 60 * 1000); } else { break; } } } }
觸發回收時輸出結果如下:
instance:i-bp1ecbyds24ij63w146c-->lockReason:Recycling,vmStatus:Stopped spot instance will be recycled immediately, instance id:i-bp1ecbyds24ij63w146c
以上是如何使用阿里雲 ECS SDK 合理快速地建立並管理搶佔式實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!