

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Spot 執行個體

## 受管節點群組
<a name="_managed_nodegroups"></a>

 `eksctl` 支援[使用 EKS 受管節點群組的 Spot 工作者節點](https://aws.amazon.com/blogs/containers/amazon-eks-now-supports-provisioning-and-managing-ec2-spot-instances-in-managed-node-groups/)，這項功能可讓具有容錯應用程式的 EKS 客戶輕鬆地為其 EKS 叢集佈建和管理 EC2 Spot 執行個體。EKS 受管節點群組將根據 Spot 最佳實務設定並啟動 Spot 執行個體的 EC2 Autoscaling 群組，並在執行個體被 AWS 中斷之前自動耗盡 Spot 工作者節點。使用此功能無需額外收費，客戶只需支付使用 AWS 資源的費用，例如 EC2 Spot 執行個體和 EBS 磁碟區。

若要使用 Spot 執行個體建立具有受管節點群組的叢集，請傳遞 `--spot`旗標和選用的執行個體類型清單：

```console
eksctl create cluster --spot --instance-types=c3.large,c4.large,c5.large
```

若要在現有叢集上使用 Spot 執行個體建立受管節點群組：

```console
eksctl create nodegroup --cluster=<clusterName> --spot --instance-types=c3.large,c4.large,c5.large
```

若要透過組態檔案使用受管節點群組建立 Spot 執行個體：

```yaml
# spot-cluster.yaml

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: spot-cluster
  region: us-west-2

managedNodeGroups:
- name: spot
  instanceTypes: ["c3.large","c4.large","c5.large","c5d.large","c5n.large","c5a.large"]
  spot: true


# `instanceTypes` defaults to [`m5.large`]
- name: spot-2
  spot: true

# On-Demand instances
- name: on-demand
  instanceTypes: ["c3.large", "c4.large", "c5.large"]
```

```console
eksctl create cluster -f spot-cluster.yaml
```

**注意**  
未受管節點群組不支援 `spot`和 `instanceTypes` 欄位，而是使用 `instancesDistribution` 欄位來設定 Spot 執行個體。[請參閱以下內容](#spot-unmanaged) 

### 詳細資訊
<a name="_further_information"></a>
+  [EKS Spot 節點群組](https://aws.amazon.com/blogs/containers/amazon-eks-now-supports-provisioning-and-managing-ec2-spot-instances-in-managed-node-groups/) 
+  [EKS 受管節點群組容量類型](https://docs.aws.amazon.com/eks/latest/userguide/managed-node-groups.html#managed-node-group-capacity-types) 

## 未受管節點群組
<a name="spot-unmanaged"></a>

 `eksctl` 透過 Auto Scaling 群組的 MixedInstancesPolicy 支援 Spot 執行個體。

以下是使用 50% Spot 執行個體和 50% 隨需執行個體的節點群組範例：

```yaml
nodeGroups:
  - name: ng-1
    minSize: 2
    maxSize: 5
    instancesDistribution:
      maxPrice: 0.017
      instanceTypes: ["t3.small", "t3.medium"] # At least one instance type should be specified
      onDemandBaseCapacity: 0
      onDemandPercentageAboveBaseCapacity: 50
      spotInstancePools: 2
```

請注意，使用 `nodeGroups.X.instanceType` 欄位時不應設定 `instancesDistribution` 欄位。

此範例使用 GPU 執行個體：

```yaml
nodeGroups:
  - name: ng-gpu
    instanceType: mixed
    desiredCapacity: 1
    instancesDistribution:
      instanceTypes:
        - p2.xlarge
        - p2.8xlarge
        - p2.16xlarge
      maxPrice: 0.50
```

此範例使用容量最佳化 Spot 配置策略：

```yaml
nodeGroups:
  - name: ng-capacity-optimized
    minSize: 2
    maxSize: 5
    instancesDistribution:
      maxPrice: 0.017
      instanceTypes: ["t3.small", "t3.medium"] # At least one instance type should be specified
      onDemandBaseCapacity: 0
      onDemandPercentageAboveBaseCapacity: 50
      spotAllocationStrategy: "capacity-optimized"
```

此範例使用capacity-optimized-prioritized配置策略：

```yaml
nodeGroups:
  - name: ng-capacity-optimized-prioritized
    minSize: 2
    maxSize: 5
    instancesDistribution:
      maxPrice: 0.017
      instanceTypes: ["t3a.small", "t3.small"] # At least two instance types should be specified
      onDemandBaseCapacity: 0
      onDemandPercentageAboveBaseCapacity: 0
      spotAllocationStrategy: "capacity-optimized-prioritized"
```

使用 `capacity-optimized-prioritized` 配置策略，然後在啟動範本覆寫清單中設定執行個體類型的順序，從最高到最低的優先順序 （清單先到最後）。Amazon EC2 Auto Scaling 會盡力遵守執行個體類型的優先順序，但會先針對容量進行最佳化。對於必須將中斷可能性降至最低的工作負載而言，這是一個不錯的選項，但對於某些執行個體類型的偏好也很重要。如需詳細資訊，請參閱 [ASG 購買選項](https://docs.aws.amazon.com/autoscaling/ec2/userguide/asg-purchase-options.html#asg-spot-strategy)。

請注意，使用 `spotInstancePools` 欄位時不應設定 `spotAllocationStrategy` 欄位。如果`spotAllocationStrategy`未指定 ，EC2 預設會使用 `lowest-price`策略。

以下是一個最少的範例：

```yaml
nodeGroups:
  - name: ng-1
    instancesDistribution:
      instanceTypes: ["t3.small", "t3.medium"] # At least one instance type should be specified
```

若要區分 Spot 或隨需執行個體之間的節點，您可以使用具有 值`node-lifecycle`的 kubernetes 標籤`spot`，或`on-demand`取決於其類型。

### instancesDistribution 中的參數
<a name="_parameters_in_instancesdistribution"></a>

如需詳細資訊，請參閱叢集組態結構描述。