In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Aliyun ECS SDK to create and manage preemptive examples, which can be used for reference by friends who need it. The following information is about ECS SDK.
Manage preemptive instances
Preparatory work
Before performing an action, you need to:
Understand the instance specifications and geographies that meet your business requirements.
Familiar with the basic knowledge and calling method of Aliyun ECS SDK. For more information, please refer to the instructions for using SDK.
Note:
Preemptive instance code needs to rely on ECS SDK version 4.2.0 or above. Take Java POM dependency as an example, modify and introduce pom dependency:
Com.aliyun aliyun-java-sdk-core 3.2.8 com.aliyun aliyun-java-sdk-ecs 4.2.0
Query regions and available instance specifications
Use OpenAPI DescribeZones queries to create the region of a preemptive instance and the available instance specifications. The sample code is shown below.
OpenApiCaller.javapublic class OpenApiCaller {IClientProfile profile; IAcsClient client; public OpenApiCaller () {profile = DefaultProfile.getProfile ("cn-hangzhou", AKSUtil.accessKeyId, AKSUtil.accessKeySecret); client = new DefaultAcsClient (profile);} public T doAction (AcsRequest var1) {try {return client.getAcsResponse (var1);} catch (Throwable e) {e.printStackTrace (); return null } DescribeZonesSample.javapublic class DescribeZonesSample {public static void main (String [] args) {OpenApiCaller caller = new OpenApiCaller (); DescribeZonesRequest request = new DescribeZonesRequest (); request.setRegionId ("cn-zhangjiakou"); / / you can obtain the RegionId request.setSpotStrategy ("SpotWithPriceLimit") of each region through DescribeRegionsRequest; / / request.setInstanceChargeType ("PostPaid") is required for querying whether preemptive instances can be purchased. / / postpaid mode. Preemptive instances must be postpaid mode DescribeZonesResponse response = caller.doAction (request); System.out.println (JSON.toJSONString (response));}}
The following is the output result. You can view the instance specifications, disk type, network type and other information available for each region.
{"requestId": "388D6321-E587-470C-8CFA-8985E2963DAE", "zones": [{"localName": "North China 3 availability Zone 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"]}]}
Query the historical price of preemptive instances
Use OpenAPI DescribeSpotPriceHistory to query the price change data of preemptive instances in the last 30 days to obtain the best cost-effective region and specifications. The sample code (DescribeSpotPriceHistorySample.java) is as follows.
Public class DescribeSpotPriceHistorySample {public static void main (String [] args) {OpenApiCaller caller = new OpenApiCaller (); List result = new ArrayList (); int offset = 0; while (true) {DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest (); request.setRegionId ("cn-hangzhou") / / you can obtain the RegionId request.setZoneId ("cn-hangzhou-b") of each region that can be purchased through DescribeRegionsRequest; / / request.setInstanceType ("ecs.sn2.medium") is required in the availability zone; / / request.setNetworkType ("vpc") is required for the instance type returned by DescribeZones; / / for the network type returned by DescribeZones, / / request.setIoOptimized ("optimized") is required. / / IoOptimized returned by DescribeZones. Select / / request.setStartTime ("2017-09-20T08:45:08Z"); / / start time of price, optional; default data within 3 days / / request.setEndTime ("2017-09-28T08:45:08Z"); / / end time of price, select 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 {}}
The following is an example of the returned result.
2017-09-26T06PUBG 28GV 55Z murmuri-> spotPrice:0.24---- > originPrice:1.22017-09-26T14GV 0000Z Rose-> spotPrice:0.36---- > originPrice:1.22017-09-26T15Rose 0000Z Rose-> spotPrice:0.24---- > originPrice:1.22017-09-27T14VRV 0000Z muri-> spotPrice:0.36---- > originPrice:1.22017-09-27T1500ZFI-> spotPrice:0.24 -> originPrice:1.22017-09-28T14GRV 00Z murmuri-> spotPrice:0.36---- > originPrice:1.22017-09-28T15VV 00ZMULY-> spotPrice:0.24---- > spotPrice:0.24---- > originPrice:1.22017-09-29T06GV 28RV 55Z muri-> spotPrice:0.24---- > originPrice:1.2
By repeating the above steps, you can determine the price trend and recent price of the specification resource in the availability zone.
Description:
You can decide whether it is acceptable to purchase the preemptive instance by the aPCge price and the maximum price, or you can analyze the historical price data through a more reasonable data model, and adjust the specifications and availability zones of the created resources at any time to achieve the best cost performance.
Create a preemptive instance
Before you can create a preemptive instance, you need to do the following:
If you use a custom image to create a preemptive instance, you must have created a custom image.
Create a security group in the console, or use OpenAPI CreateSecurityGroup to create a security group and get the security group ID (SecurityGroupId).
Create the VPC and switch in the console, or use OpenAPI CreateVpc and CreateVSwitch and get the switch ID (VSwitchId).
Use OpenAPI CreateInstance to create preemptive instances. The sample code (CreateInstaneSample.java) is as follows.
Public class CreateInstaneSample {public static void main (String [] args) {OpenApiCaller caller = new OpenApiCaller (); CreateInstanceRequest request = new CreateInstanceRequest (); request.setRegionId ("cn-hangzhou"); / / region ID request.setZoneId ("cn-hangzhou-b"); / / availability zone ID request.setSecurityGroupId ("sg-bp11nhf94ivkdxwb2gd4") / / Security group ID request.setImageId ("centos_7_03_64_20G_alibase_20170818.vhd") created in advance; / / it is recommended to select a custom image request.setVSwitchId ("vsw-bp164cyonthfudn9kj5br") that you have prepared in this region; / / switch ID request.setInstanceType ("ecs.sn2.medium") is required for VPC type. / / enter the specification request.setIoOptimized ("optimized") that you need to purchase after inquiry; / / refer to the DescirbeZones return parameter request.setSystemDiskCategory ("cloud_ssd"); / / refer to the DescirbeZones return parameter, and select one more cloud_ssd, cloud_efficiency, cloud request.setSystemDiskSize (40); request.setInstanceChargeType ("PostPaid"); / / Preemptive instance must be postpaid request.setSpotStrategy ("SpotWithPriceLimit"). / / SpotWithPriceLimit bidding model, SpotAsPriceGo does not need to bid, the highest pay-by-quantity price request.setSpotPriceLimit (0.25F); / / SpotWithPriceLimit bidding model takes effect, the highest price you can accept, in yuan per hour, must be higher than the current market transaction price to succeed CreateInstanceResponse response = caller.doAction (request); System.out.println (response.getInstanceId ());}}
Recovery preemptive instance
When preemptive instances may be forcibly recycled due to price factors or changes in market supply and demand. An interruption of a preemptive instance is triggered. Before release, the preemptive instance will enter the locked state, indicating that the instance will be automatically reclaimed. You can automatically process the exit logic of an instance based on the status of the instance.
Currently, you can obtain the interrupt lock status of preemptive instances in any of the following ways:
Obtained through instance metadata. Run the following command:
Curl 'http://100.100.100.200/latest/meta-data/instance/spot/termination-time'
If the return is empty, the instance is sustainable. If information similar to 2015-01-05T18:02:00Z format is returned (UTC time), the instance will be released at this time.
Use OpenAPI DescribeInstances to determine whether the instance is in the state of waiting for recycling based on the returned OperationLocks. The code example is as follows (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 ()) / / specify instance ID with the highest efficiency DescribeInstancesResponse response = caller.doAction (request); List 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;}
When the recycling is triggered, the output is as follows:
Instance:i-bp1ecbyds24ij63w146c-- > lockReason:Recycling,vmStatus:Stoppedspot instance will be recycled immediately, instance id:i-bp1ecbyds24ij63w146c
So much for sharing about the creation and management of preemptive instances by Aliyun ECS SDK. I hope the above content can be helpful to you and learn more. If you like this article, you might as well share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.