Introduction to Alibaba Cloud Spot Instance OpenApi-Alibaba Cloud Developer Community

objective

this topic describes how to use Alibaba Cloud ECS SDK to create SPOT SPOT instances.

Preparations:

  • first, you must be familiar with the basic knowledge and calling methods of Alibaba Cloud SDK. For more information, see SDK instructions.
  • Spot Spot instance code depends on ECS SDK version 4.2.0 or later,
  • take JAVA POM dependency as an example to modify and introduce the pom dependency:
<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>

Which regions and specifications can I purchase spot instances:

in a typical scenario, I need to create a batch of cheap and sufficient computing resources for business data processing.

However, I do not know which region to create the required computing resources. We need to plan the general requirements for computing resources:

  1. the region in which the spot instance is created.
  2. Specifications determine the type of instance based on your business needs

however, due to the different sales rates of aliyun in different regions, the specifications available for sale vary greatly in different regions.

We recommend that you call the DesribeZones operation to obtain the regions and specifications that can be created for your account.

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));

    }
}

output:

{
    "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"
            ]
        }
    ]
}

you can call this operation to query historical spot prices to obtain the most cost-effective regions and specifications.

In the previous step, we can basically determine which zone can be produced and which specifications can be produced. With this information, we can query the price history through the DescribeSpotPriceHistory interface, you can obtain price change data for up to 30 days.

  • API name: DescribeSpotPriceHistory
  • API documentation: https://help.aliyun.com/document_detail/60400.html
  • code example: 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");//是否Io优化类型,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 {
        }

    }

}

  • response
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
  • repeat the preceding steps to determine the price change trend and the latest price of the instance type in the zone.
  • A simple strategy determines whether the spot instance can be purchased based on the average price and the highest price.
  • At the minimum cost, we can analyze historical price data through a more reasonable data model, and adjust the specifications and zones of created resources at any time to achieve the best cost performance.

Create a Spot Instance

through the preceding process, we finally obtain the region information, specification attributes, disks, and network information of the resources that can be created. Now you can create a spot instance.

  • API name: CreateInstance
  • recommends that you in advance console or openapi way in need to create vm in resources created vpc proprietary network, get Create parameter vswitchId
  • we recommend that you create a security group in the region where you want to create vm resources in the console or by using openapi in advance. Obtain the creation parameters: SecurityGroupId
  • API reference: https://help.aliyun.com/document_detail/25499.html
  • Sample Code: 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());

    }
}

spot Instance interruption

  • spot Instances may forcibly recycle spot resources due to price factors or other internal inventory factors of ecs. In this case, spot interruptions are triggered.
  • Before the vm is released, the vm enters the locked state, indicating that the vm will be automatically recycled. You can automatically process the exit logic of the instance based on the vm recycling state.
  • Currently, the interrupt lock status of spot can be perceived in two ways.

vpc instances can be obtained through metadata

metadata access example:

curl 'http://100.100.100.200/latest/meta-data/instance/spot/termination-time'
返回格式示例:2015-01-05T18:02:00Z,时间为UTC时间,如果返回为空,说明可持续使用

check whether the describeInstances returned by the OperationLocks is ready to be recycled.

Sample code for obtaining lock status through openapi: 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;
            }
        }

    }
}

output results when recycling is triggered:

instance:i-bp1ecbyds24ij63w146c-->lockReason:Recycling,vmStatus:Stopped
spot instance will be recycled immediately, instance id:i-bp1ecbyds24ij63w146c

start, shut down, and release SPOT SPOT Instances

starting, stopping, and releasing instances Spot instances are no different from other instances. This topic does not describe this topic. For more information, see the related api documentation.

Purchase a Spot Instance

Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now