這篇文章主要介紹了阿里云ECS SDK創(chuàng)建并管理?yè)屨际綄?shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。如下資料是關(guān)于ECS SDK的內(nèi)容。
創(chuàng)新互聯(lián)長(zhǎng)期為上1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為烏審企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),烏審網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
管理?yè)屨际綄?shí)例
準(zhǔn)備工作
在執(zhí)行操作之前,您需要:
了解能滿足您業(yè)務(wù)要求的實(shí)例規(guī)格和地域。
熟悉了解阿里云 ECS SDK 的基礎(chǔ)知識(shí)和調(diào)用方法。詳細(xì)信息,請(qǐng)參考 SDK 使用說(shuō)明。
注意:
搶占式實(shí)例代碼需要依賴的 ECS SDK 版本 4.2.0 以上。以 Java POM 依賴為例,修改引入 pom 依賴:
com.aliyun aliyun-java-sdk-core 3.2.8 com.aliyun aliyun-java-sdk-ecs 4.2.0
查詢地域及可用的實(shí)例規(guī)格
使用 OpenAPI DescribeZones 查詢可以創(chuàng)建搶占式實(shí)例的地域以及可用的實(shí)例規(guī)格。示例代碼如下所示。
OpenApiCaller.java public class OpenApiCaller { IClientProfile profile; IAcsClient client; public OpenApiCaller() { profile = DefaultProfile.getProfile("cn-hangzhou", AKSUtil.accessKeyId, AKSUtil.accessKeySecret); client = new DefaultAcsClient(profile); } publicT doAction(AcsRequest 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");//可以通過(guò) DescribeRegionsRequest 獲取每個(gè)地域的 RegionId request.setSpotStrategy("SpotWithPriceLimit");//對(duì)于查詢是否可購(gòu)買搶占式實(shí)例此項(xiàng)必填 request.setInstanceChargeType("PostPaid");//后付費(fèi)模式,搶占式實(shí)例必須是后付費(fèi)模式 DescribeZonesResponse response = caller.doAction(request); System.out.println(JSON.toJSONString(response)); } }
以下為輸出結(jié)果,可以查看每個(gè)地域各個(gè)地域可供選擇的實(shí)例規(guī)格、磁盤類型、網(wǎng)絡(luò)類型等信息。
{ "requestId": "388D6321-E587-470C-8CFA-8985E2963DAE", "zones": [ { "localName": "華北 3 可用區(qū) 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" ] } ] }
查詢搶占式實(shí)例的歷史價(jià)格
使用 OpenAPI DescribeSpotPriceHistory 查詢搶占式實(shí)例最近 30 天的價(jià)格變化數(shù)據(jù),獲得最佳性價(jià)比的地域和規(guī)格信息,示例代碼(DescribeSpotPriceHistorySample.java)如下。
public class DescribeSpotPriceHistorySample { public static void main(String[] args) { OpenApiCaller caller = new OpenApiCaller(); Listresult = new ArrayList (); int offset = 0; while (true) { DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest(); request.setRegionId("cn-hangzhou"); //可以通過(guò) DescribeRegionsRequest 獲取可購(gòu)買的每個(gè)地域的 RegionId request.setZoneId("cn-hangzhou-b");//可用區(qū)必填 request.setInstanceType("ecs.sn2.medium");//參考 DescribeZones 返回的實(shí)例類型,必填 request.setNetworkType("vpc");//參考 DescribeZones 返回的網(wǎng)絡(luò)類型,必填 // request.setIoOptimized("optimized"); //是否 I/O 優(yōu)化類型,DescribeZones 返回的 IoOptimized,選填 // request.setStartTime("2017-09-20T08:45:08Z");//價(jià)格開始時(shí)間,選填,默認(rèn) 3 天內(nèi)數(shù)據(jù) // request.setEndTime("2017-09-28T08:45:08Z");//價(jià)格結(jié)束時(shí)間,選填 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 { } } }
以下為返回結(jié)果示例。
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
重復(fù)以上步驟,您可以判斷出該規(guī)格資源在可用區(qū)的價(jià)格變化趨勢(shì)和最近價(jià)格。
說(shuō)明:
您可以通過(guò)平均價(jià)格和最高價(jià)格來(lái)決定是否可以接受購(gòu)買該搶占式實(shí)例,也可以通過(guò)更加合理的數(shù)據(jù)模型來(lái)分析歷史價(jià)格數(shù)據(jù),隨時(shí)調(diào)整創(chuàng)建資源的規(guī)格和可用區(qū),到達(dá)最佳性價(jià)比。
創(chuàng)建搶占式實(shí)例
在創(chuàng)建搶占式實(shí)例之前,您需要完成以下工作:
如果您使用自定義鏡像創(chuàng)建搶占式實(shí)例,必須已經(jīng) 創(chuàng)建自定義鏡像。
在控制臺(tái) 創(chuàng)建安全組,或者使用 OpenAPI CreateSecurityGroup 創(chuàng)建安全組,并獲取安全組 ID(SecurityGroupId)。
在控制臺(tái)創(chuàng)建 VPC 和 交換機(jī),或者使用 OpenAPI CreateVpc 和 CreateVSwitch 創(chuàng)建,并獲取交換機(jī) ID(VSwitchId)。
使用 OpenAPI CreateInstance 創(chuàng)建搶占式實(shí)例。示例代碼(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"); //可用區(qū)ID request.setSecurityGroupId("sg-bp11nhf94ivkdxwb2gd4");//提前創(chuàng)建的安全組 ID request.setImageId("centos_7_03_64_20G_alibase_20170818.vhd"); //建議選擇您自己在該地域準(zhǔn)備的自定義鏡像 request.setVSwitchId("vsw-bp164cyonthfudn9kj5br");//VPC 類型需要交換機(jī) ID request.setInstanceType("ecs.sn2.medium"); //填入您詢價(jià)后需要購(gòu)買的規(guī)格 request.setIoOptimized("optimized");//參考 DescirbeZones 返回參數(shù) request.setSystemDiskCategory("cloud_ssd"); //參考 DescirbeZones 返回參數(shù),多選一 cloud_ssd, cloud_efficiency, cloud request.setSystemDiskSize(40); request.setInstanceChargeType("PostPaid");//搶占式實(shí)例必須后付費(fèi) request.setSpotStrategy("SpotWithPriceLimit"); //SpotWithPriceLimit 出價(jià)模式,SpotAsPriceGo 不用出價(jià),最高按量付費(fèi)價(jià)格 request.setSpotPriceLimit(0.25F);//SpotWithPriceLimit 出價(jià)模式生效,您能接受的最高價(jià)格,單位為元每小時(shí),必須高于當(dāng)前的市場(chǎng)成交價(jià)才能成功 CreateInstanceResponse response = caller.doAction(request); System.out.println(response.getInstanceId()); } }
回收搶占式實(shí)例
當(dāng)搶占式實(shí)例可能會(huì)因?yàn)閮r(jià)格因素或者市場(chǎng)供需變化而被強(qiáng)制回收。此時(shí)會(huì)觸發(fā)搶占式實(shí)例的中斷。釋放前,搶占式實(shí)例會(huì)進(jìn)入鎖定狀態(tài),提示實(shí)例將會(huì)被自動(dòng)回收。您可以針對(duì)實(shí)例回收狀態(tài)自動(dòng)化處理實(shí)例的退出邏輯。
目前,您可以通過(guò)以下任一種方式來(lái)獲取搶占式實(shí)例的中斷鎖定狀態(tài):
通過(guò) 實(shí)例元數(shù)據(jù) 獲取。運(yùn)行以下命令:
curl 'http://100.100.100.200/latest/meta-data/instance/spot/termination-time'
如果返回為空,說(shuō)明實(shí)例可持續(xù)使用。如果返回類似 2015-01-05T18:02:00Z 格式的信息(UTC 時(shí)間),說(shuō)明實(shí)例將于這個(gè)時(shí)間釋放。
使用 OpenAPI DescribeInstances,根據(jù)返回的 OperationLocks 判斷實(shí)例是否進(jìn)入 待回收 狀態(tài)。代碼示例如下(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());//指定實(shí)例 ID,效率最高 DescribeInstancesResponse response = caller.doAction(request); ListinstanceList = 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; } } } }
觸發(fā)回收時(shí)輸出結(jié)果如下:
instance:i-bp1ecbyds24ij63w146c-->lockReason:Recycling,vmStatus:Stopped spot instance will be recycled immediately, instance id:i-bp1ecbyds24ij63w146c
關(guān)于阿里云ECS SDK創(chuàng)建并管理?yè)屨际綄?shí)例就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果喜歡這篇文章,不如把它分享出去讓更多的人看到。