Preemptible instances are low-cost spot instances. You can bid for idle resources on Alibaba Cloud and obtain the resources as preemptible instances for running containers. When your bid is lower than the current market price, the resources are reclaimed. Preemptible instances greatly reduce the costs of using Elastic Container Instance (ECI) in some scenarios.
Description
Compared with a pay-as-you-go instance, the costs of using a preemptible instance are often lower. The market price of a preemptible instance fluctuates based on changes to the demand for and supply of its instance type. A preemptible instance is billed based on its actual usage duration. For more information, see Overview.
- After your preemptible instance runs for one hour, the system may release the preemptible instance and reclaim its resources due to high market price or insufficient resources.
- The system notifies you of the release through an event 3 minutes before the release.
- The resources of the preemptible instance are reclaimed upon the release. However, the information about the instance is retained. After the resources are reclaimed, the instance does not incur fees and its status changes to Expired.
When you use a preemptible instance, take note of the following:
- Offer a reasonable bid that can be higher than the fluctuated market price. In this way, the request to create a preemptible instance can be accepted and processed, and the preemptible instance will not be released due to the price after it is created. In addition, offer the bid based on the price that is affordable to your business.
- We recommend that you store important data in a storage medium that is not affected by the release of preemptible instances. For example, you can use an independent disk that will not be released along with the instance or an external storage, such as Apsara File Storage NAS.
You can create a preemptible instance by specifying the Elastic Compute Service (ECS) instance type or CPU and memory specifications.
- Specify the ECS instance type. The preemptible instance is billed based on the market price and discount for a pay-as-you-go ECS instance of the specified instance type at the time of purchase.
- Specify CPU and memory specifications. ECI automatically selects an ECS instance type that meets the specified CPU and memory specifications and the price that you offered. The preemptible instance is billed based on the market price and discount for a pay-as-you-go ECS instance of the selected instance type. It is not billed based on the prices of pay-as-you-go CPU and memory resources. Specifying CPU and memory specifications has the same effect as specifying the ECS instance type.
Scenarios
Preemptible instances are ideal for stateless applications, such as scalable web services, image rendering, big data analytics, and large-scale parallel computing. Preemptible instances are applicable to applications that require a high level of distribution, scalability, and fault tolerance capabilities. Preemptible instances help save costs and increase throughput of these applications.
You can use preemptible instances for the following business:
- Real-time analytics
- Big data processing
- Scalable websites
- Image and media encoding
- Scientific computing
- Geospatial surveys
- Web crawlers
- Testing
Kubernetes mode
You can add the k8s.aliyun.com/eci-spot-strategy and k8s.aliyun.com/eci-spot-price-limit annotations to the declaration of a pod to create a preemptible instance. The k8s.aliyun.com/eci-spot-strategy annotation has the following valid values:
- SpotAsPriceGo: specifies a preemptible instance priced at the market price at the time of purchase.
- SpotWithPriceLimit: specifies a preemptible instance with a maximum hourly price.
Add annotations as follows:
- k8s.aliyun.com/eci-spot-strategy: "SpotAsPriceGo"
- The k8s.aliyun.com/eci-spot-price-limit annotation specifies the maximum hourly price and only takes effect when the k8s.aliyun.com/eci-spot-strategy annotation is set to SpotWithPriceLimit. A maximum of three decimal places can be specified.
Specify the SpotAsPriceGo policy in the YAML file of a deployment
apiVersion: apps/v1beta2 # 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 an ECS instance type or CPU and memory specifications as needed.
k8s.aliyun.com/eci-spot-strategy: "SpotAsPriceGo" # A value of SpotAsPriceGo specifies a preemptible instance priced at the market price at the time of purchase.
spec:
# nodeSelector:
# env: test-team
containers:
- name: nginx
image: nginx:1.7.9 # replace it with your exactly <image_name:tags>
ports:
- containerPort: 80
Specify the SpotWithPriceLimit policy in the YAML file of a deployment
apiVersion: apps/v1beta2 # 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 or CPU and memory specifications as needed.
k8s.aliyun.com/eci-spot-strategy: "SpotWithPriceLimit" # A value of SpotWithPriceLimit specifies a preemptible instance with a maximum hourly price.
k8s.aliyun.com/eci-spot-price-limit: "0.250" # The maximum hourly 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
You can call the CreateContainerGroup operation to create a preemptible instance through Alibaba Cloud command-line interface (CLI), OpenAPI Explorer, or Alibaba Cloud SDK. For more information, see CreateContainerGroup.
- To create a preemptible instance priced at the market price at the time of purchase, set the SpotStrategy parameter to SpotAsPriceGo.
- To create a preemptible instance with a maximum hourly price, set the SpotStrategy parameter to SpotWithPriceLimit and set the SpotPriceLimit parameter.
#! /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)