All Products
Search
Document Center

Create a preemptible elastic container instance

Last Updated: Sep 16, 2021

Preemptible elastic container instances are cost-effective. You can bid for unused Alibaba Cloud resources to create preemptible instances. When the market price exceeds your bid price for a preemptible instance or when the resource inventory is insufficient, the resources consumed by the instance are reclaimed and the instance is released. This reduces the costs of elastic container instances in some scenarios.

Background information

Preemptible instances are ideal for stateless scenarios such as scalable web services, image rendering, big data analytics, and large-scale parallel computing. Preemptible instances are applicable to highly distributed, scalable, and fault-tolerant applications. This can help reduce costs and increase throughput. For more information, see Overview.

Terms

Before you create a preemptible instance, take note of the following items:

  • Billing method

    The market price of a preemptible instance fluctuates based on the supply and demand for its instance type. When you create a preemptible instance, you must specify a maximum hourly price to bid for a specific instance type. If your bid price is higher than the real-time market price and the resource inventory of the instance type is sufficient, the preemptible instance is created. When a preemptible instance is created, it enters a protection period during which the instance is billed based on the market price at the time of purchase. After the protection period ends, the instance is billed based on the real-time market price.

    Note

    Preemptible instances are billed at lower prices compared with pay-as-you-go instances. The price of a preemptible instance fluctuates based on the supply and demand for its instance type. A preemptible instance is billed based on its actual duration of use. For more information, see the "Billing methods" section of Preemptible instances.

  • Protection period

    By default, each preemptible instance has a protection period of 1 hour. You can specify the protection period when you create a preemptible instance by using Kubernetes or by calling an API. You can set the protection period of a preemptible instance to 0, 1, 2, 3, 4, 5, or 6 hours. A value of 0 indicates that the preemptible instance does not have a protection period and may be released at any time. Shorter protection periods allow you to obtain lower instance prices.

  • Reclaim mechanism

    After the protection period of a preemptible instance ends, the system checks the market price and resource inventory of the instance type every 5 minutes. If the market price exceeds your bid price or if the resource inventory is insufficient, the instance is released.

    Note

    • A release event is generated 3 minutes before the resources of a preemptible instance are reclaimed.

    • After the resources of a preemptible instance are reclaimed, the information about the instance is retained, billing stops, and the instance state changes to Expired.

Precautions

When you use preemptible instances, take note of the following items:

  • Select a suitable instance type and place a bid.

    You can call an API to query information about preemptible instances over the last 30 days. Then, you can select a suitable instance type and place a bid. The following APIs are supported:

    Note

    When you place a bid, take the fluctuations in market price and the price range you are comfortable with into account. A good bid is usually higher than the average market price, but still within the range of your business requirements. In this way, requests for creating preemptible instances can be accepted and processed, and the created instances will not be released due to changes in market price.

  • We recommend that you store important data in a storage medium that will not be affected if preemptible instances are released. For example, you can use independent disks that are not to be released along with their associated instances, or use external storage resources such as Apsara File Storage NAS file systems.

Methods used to create preemptible instances

You can create a preemptible instance by specifying an Elastic Compute Service (ECS) instance type, or by specifying the number of vCPUs and memory size.

  • Specify an ECS instance type

    The preemptible instance is billed at the real-time discounted pay-as-you-go price of the specified instance type.

    If the protection period is set to 2, 3, 4, 5, or 6 hours, only the following ECS instance types are supported:

    • ecs.c5.large

    • ecs.c6.large

    • ecs.g5.large, ecs.g5.xlarge, ecs.g5.2xlarge, ecs.g5.4xlarge, and ecs.g5.8xlarge

    • ecs.g6.large, ecs.g6.xlarge, ecs.g6.2xlarge, ecs.g6.4xlarge, and ecs.g6.8xlarge

    • ecs.gn6e-c12g1.3xlarge, ecs.gn6e-c12g1.12xlarge, and ecs.gn6e-c12g1.24xlarge

    • ecs.gn5-c4g1.xlarge, ecs.gn5-c8g1.2xlarge, ecs.gn5-c8g1.4xlarge, and ecs.gn5-c28g1.7xlarge

    • ecs.gn6v-c8g1.2xlarge, ecs.gn6v-c8g1.8xlarge, ecs.gn6v-c8g1.16xlarge, and ecs.gn6v-c10g1.20xlarge

  • Specify the number of vCPUs and memory size

    This method has the same effect as specifying an ECS instance type, but the specified number of vCPUs must be 2 or larger.

    The system automatically selects an ECS instance type that meets the specified requirements on the number of vCPUs, memory size, and price. The created preemptible instance is billed at a discount on the market price of the selected ECS instance type, not on the pay-as-you-go price of Elastic Container Instance vCPU and memory resources.

Use Kubernetes to create a preemptible instance

You can add annotations to metadata in the configuration file of the pod to create a preemptible elastic container instance. Take note of the following annotations:

  • k8s.aliyun.com/eci-spot-strategy: specifies the bidding policy for the preemptible instance. Valid values:

    • SpotWithPriceLimit: You must set the maximum hourly price that you are willing to pay for the preemptible instance.

    • SpotAsPriceGo: The system places bids based on the real-time market price.

  • k8s.aliyun.com/eci-spot-price-limit: specifies the maximum hourly price of the preemptible instance. This value can be accurate to three decimal places. You must specify k8s.aliyun.com/eci-spot-price-limit when k8s.aliyun.com/eci-spot-strategy is set to SpotWithPriceLimit.

  • k8s.aliyun.com/eci-spot-duration: specifies the protection period of the preemptible instance. Valid values: 0, 1, 2, 3, 4, 5, and 6. Unit: hours.

Example 1: Specify an ECS instance type, set k8s.aliyun.com/eci-spot-strategy to SpotWithPriceLimit, and set k8s.aliyun.com/eci-spot-duration.

apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment-basic
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        k8s.aliyun.com/eci-use-specs : "ecs.c5.large"  # Specify an ECS instance type.
        k8s.aliyun.com/eci-spot-strategy: "SpotWithPriceLimit"  # Set k8s.aliyun.com/eci-spot-strategy to SpotWithPriceLimit.
        k8s.aliyun.com/eci-spot-price-limit: "0.250"   # Specify a maximum hourly price for the preemptible instance.
        k8s.aliyun.com/eci-spot-duration: "2"   # Specify a protection period for the preemptible instance.
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9 # replace it with your exactly <image_name:tags>
        ports:
        - containerPort: 80

Example 2: Specify the number of vCPUs and memory size, and set k8s.aliyun.com/eci-spot-strategy to SpotAsPriceGo.

apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment-basic
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        k8s.aliyun.com/eci-use-specs : "2-4Gi"  # Specify the number of vCPUs and memory size.
        k8s.aliyun.com/eci-spot-strategy: "SpotAsPriceGo"  # The SpotAsPriceGo bidding policy is used. This policy specifies that the system places bids based on the real-time market price.
 
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9 # replace it with your exactly <image_name:tags>
        ports:
        - containerPort: 80

Call an API to create a preemptible instance

When you can call the CreateContainerGroup API to create a preemptible elastic container instance, you can use the SpotStrategy parameter to specify the bidding policy, the SpotPriceLimit parameter to specify a maximum hourly price, and the SpotDuration parameter to specify a protection period. The following table describes these parameters. For more information, see CreateContainerGroup.

Parameter

Type

Example

Description

SpotStrategy

String

SpotPriceLimit

The bidding policy for the instance. Valid values:

  • NoSpot: The instance is a regular pay-as-you-go instance.

  • SpotWithPriceLimit: The instance is a preemptible instance with a user-defined maximum hourly price.

  • SpotAsPriceGo: The instance is a preemptible instance for which the system places bids based on the real-time market price.

Default value: NoSpot.

SpotPriceLimit

Float

0.2

The maximum hourly price of the preemptible instance. This price can be accurate to three decimal places. You must specify this parameter when SpotStrategy is set to SpotWithPriceLimit.

SpotDuration

Integer

1

The protection period of the preemptible instance. Unit: hours. Valid values: 0, 1, 2, 3, 4, 5, and 6. Default value: 1.

Notice

If you set SpotStrategy to SpotAsPriceGo, the maximum hourly price may reach the pay-as-you-go price when resources of the specified instance type within the specified zone are strained.

You can use an SDK to create a preemptible instance. The following sample code provides an example on how to use SDK for Python to create a preemptible instance:

#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkeci.request.v20180808.CreateContainerGroupRequest import CreateContainerGroupRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')
request = CreateContainerGroupRequest()
request.set_accept_format('json')
request.set_SecurityGroupId("sg-xxx")
request.set_VSwitchId("vsw-xxx")
request.set_ContainerGroupName("test-spot")
request.set_SpotStrategy('SpotAsPriceGo')
request.set_InstanceType('ecs.c5.large')
request.set_Containers([
  {
    "Image": "nginx",
    "Name": "nginx"
  }
])
response = client.do_action_with_exception(request)
print(response)

Use the console to create a preemptible instance

When you create a preemptible instance on the instance buy page in the Elastic Container Instance console, you can set Billing Method to Preemptible Instance.

Preemptible instance

When you create a preemptible instance in the Elastic Container Instance console, take note of the following items:

  • Preemptible instances can be created only by specifying the number of vCPUs and memory size. The specified number of vCPUs must be 2 or larger.

    The system automatically selects an ECS instance type that meets the specified requirements on the number of vCPUs, memory size, and price. The created preemptible instance is billed at a discount on the market price of the selected ECS instance type, not on the pay-as-you-go price of Elastic Container Instance vCPU and memory resources.

  • You cannot configure the bidding policy. By default, the SpotAsPriceGo bidding policy is used. This policy specifies that the system places bids based on the real-time market price.

  • You cannot configure the protection period. The default protection period of the preemptible instance is 1 hour.