All Products
Search
Document Center

Create a preemptible instance

Last Updated: May 24, 2021

Preemptible elastic container instances are cost-effective. You can bid for unused Alibaba Cloud resources to create preemptible instances for running containers. When the market price exceeds your bid price for a created 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 applications that are highly distributed, scalable, and fault-tolerant to help reduce costs and increase throughput. For more information, see Overview.

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 current 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. By default, the protection period is 1 hour. After the protection period ends, the instance is billed based on its 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 usage duration. For more information, see Preemptible instances.

  • Reclaiming 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

    • An 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 of the instance is retained, billing stops, and the status of the instance changes to Expired.

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

  • Make a bid that exceeds expected fluctuations in market price but remains but is still within a reasonable price range. This way, requests to create 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 such as Apsara File Storage NAS.

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 market price of the specified instance type.

  • 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 cannot be less than two.

    The system automatically selects an ECS instance type that has the specified number of vCPUs and memory size and meets the price requirements. 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 prices of vCPUs and memory resources.

Kubernetes mode

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

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

    • SpotAsPriceGo: The system places bids based on the current market price.

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

  • 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 thk8s.aliyun.com/eci-spot-price-limit when k8s.aliyun.com/eci-spot-strategy is set to SpotWithPriceLimit.

Examples:

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

    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.
        spec:
        #  nodeSelector:
        #    env: test-team
          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 system places bids based on the current market price.
     
        spec:
        #  nodeSelector:
        #    env: test-team
          containers:
          - name: nginx
            image: nginx:1.7.9 # replace it with your exactly <image_name:tags>
            ports:
            - containerPort: 80

API mode

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

Parameter

Type

Required

Example

Description

SpotStrategy

String

No

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 market price at the time of purchase is used as the bid price.

Default value: NoSpot.

SpotPriceLimit

Float

No

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.

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

Console mode

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

Preemptible Instance

When you create preemptible instances 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 cannot be less than two.

    The system automatically selects an ECS instance type that has the specified number of vCPUs and memory size and meets the price requirements. 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 prices of vCPUs 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 current market price.