

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

# 使用節點群組
<a name="general-nodegroups"></a>

## 建立節點群組
<a name="_creating_nodegroups"></a>

除了與叢集一起建立的初始節點群組之外，您還可以新增一或多個節點群組。

若要建立其他節點群組，請使用：

```
eksctl create nodegroup --cluster=<clusterName> [--name=<nodegroupName>]
```

**注意**  
 `--version` 受管節點群組不支援 flag。它一律會從控制平面繼承版本。

根據預設，新的未受管節點群組會從控制平面 (`--version=auto`) 繼承版本，但您可以指定不同的版本，您也可以使用 `--version=latest`強制使用最新版本。

此外，您可以使用與 相同的組態檔案`eksctl create cluster`：

```
eksctl create nodegroup --config-file=<path>
```

### 從組態檔案建立節點群組
<a name="_creating_a_nodegroup_from_a_config_file"></a>

節點群組也可以透過叢集定義或組態檔案建立。根據下列範例組態檔案和名為 的現有叢集`dev-cluster`：

```
# dev-cluster.yaml
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: dev-cluster
  region: eu-north-1

managedNodeGroups:
  - name: ng-1-workers
    labels: { role: workers }
    instanceType: m5.xlarge
    desiredCapacity: 10
    volumeSize: 80
    privateNetworking: true
  - name: ng-2-builders
    labels: { role: builders }
    instanceType: m5.2xlarge
    desiredCapacity: 2
    volumeSize: 100
    privateNetworking: true
```

節點群組 `ng-1-workers`和 `ng-2-builders`可以使用此命令建立：

```
eksctl create nodegroup --config-file=dev-cluster.yaml
```

#### 負載平衡
<a name="_load_balancing"></a>

如果您已經準備好將現有的傳統負載平衡器或/和目標群組連接到節點群組，您可以在組態檔案中指定這些群組。建立節點群組時，傳統負載平衡器或/和目標群組會自動與 ASG 建立關聯。這僅支援透過 `nodeGroups` 欄位定義的自我管理節點群組。

```
# dev-cluster-with-lb.yaml
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: dev-cluster
  region: eu-north-1

nodeGroups:
  - name: ng-1-web
    labels: { role: web }
    instanceType: m5.xlarge
    desiredCapacity: 10
    privateNetworking: true
    classicLoadBalancerNames:
      - dev-clb-1
      - dev-clb-2
    asgMetricsCollection:
      - granularity: 1Minute
        metrics:
          - GroupMinSize
          - GroupMaxSize
          - GroupDesiredCapacity
          - GroupInServiceInstances
          - GroupPendingInstances
          - GroupStandbyInstances
          - GroupTerminatingInstances
          - GroupTotalInstances
  - name: ng-2-api
    labels: { role: api }
    instanceType: m5.2xlarge
    desiredCapacity: 2
    privateNetworking: true
    targetGroupARNs:
      - arn:aws:elasticloadbalancing:eu-north-1:01234567890:targetgroup/dev-target-group-1/abcdef0123456789
```

## 組態檔案中的節點群組選擇
<a name="_nodegroup_selection_in_config_files"></a>

若要僅對組態檔案中指定的節點群組子集執行 `create`或 `delete`操作，有兩個接受 glob 清單的 CLI 旗標，`0`以及 `1`，例如：

```
eksctl create nodegroup --config-file=<path> --include='ng-prod-*-??' --exclude='ng-test-1-ml-a,ng-test-2-?'
```

使用上述範例組態檔案，即可使用下列命令建立工作者節點群組以外的所有工作者節點群組：

```
eksctl create nodegroup --config-file=dev-cluster.yaml --exclude=ng-1-workers
```

或者，可以刪除具有下列項目的建置器節點群組：

```
eksctl delete nodegroup --config-file=dev-cluster.yaml --include=ng-2-builders --approve
```

在這種情況下，我們也需要提供 `--approve`命令，才能實際刪除節點群組。

### 包含和排除規則
<a name="node-include"></a>
+ 如果未指定 `--include`或 `--exclude` ，則會包含所有項目
+ 如果只指定 `--include` ，則只會包含符合這些 glob 的節點群組
+ 如果只指定 `--exclude` ，則會包含不符合這些 glob 的所有節點群組
+ 如果指定兩者，則`--exclude`規則優先於 `--include`（即兩個群組中符合規則的節點群組將被排除）

## 列出節點群組
<a name="_listing_nodegroups"></a>

若要列出節點群組或所有節點群組的詳細資訊，請使用：

```
eksctl get nodegroup --cluster=<clusterName> [--name=<nodegroupName>]
```

若要以 YAML 或 JSON 格式列出一或多個節點群組，其會輸出比預設日誌資料表更多的資訊，請使用：

```
# YAML format
eksctl get nodegroup --cluster=<clusterName> [--name=<nodegroupName>] --output=yaml

# JSON format
eksctl get nodegroup --cluster=<clusterName> [--name=<nodegroupName>] --output=json
```

## 節點群組不可變性
<a name="_nodegroup_immutability"></a>

根據設計，節點群組是不可變的。這表示如果您需要變更 AMI 或節點群組的執行個體類型等項目 （擴展除外），則需要建立具有所需變更的新節點群組、移動負載並刪除舊節點群組。請參閱[刪除和耗盡節點群組](#nodegroup-delete)一節。

## 擴展節點群組
<a name="_scaling_nodegroups"></a>

Nodegroup 擴展是一個可能需要幾分鐘的程序。未指定`--wait`旗標時， `eksctl` 會樂觀地預期節點群組會進行擴展，並在傳送 AWS API 請求後立即傳回。若要讓 `eksctl` 等待節點可用，請新增如下所示的`--wait`旗標。

**注意**  
向下/向內擴展節點群組 （即減少節點數量） 可能會導致錯誤，因為我們完全依賴 ASG 的變更。這表示移除/終止的節點 （未明確耗盡）。這可能是未來需要改進的領域。

直接呼叫更新受管節點群組組態的 EKS API，即可擴展受管節點群組。

### 擴展單一節點群組
<a name="_scaling_a_single_nodegroup"></a>

您可以使用 `eksctl scale nodegroup`命令來擴展節點群組：

```
eksctl scale nodegroup --cluster=<clusterName> --nodes=<desiredCount> --name=<nodegroupName> [ --nodes-min=<minSize> ] [ --nodes-max=<maxSize> ] --wait
```

例如，若要將 `ng-a345f4e1`中的節點群組擴展`cluster-1`至 5 個節點，請執行：

```
eksctl scale nodegroup --cluster=cluster-1 --nodes=5 ng-a345f4e1
```

也可以使用傳遞至 的組態檔案來擴展節點群組，`--config-file`並指定應使用 擴展的節點群組名稱`--name`。Eksctl 將搜尋組態檔案，並探索該節點群組及其擴展組態值。

如果所需的節點數量`NOT`在目前最小和目前最大數量節點的範圍內，則會顯示一個特定錯誤。這些值也可以`--nodes-max`分別與旗標 `--nodes-min`和 一起傳遞。

### 擴展多個節點群組
<a name="_scaling_multiple_nodegroups"></a>

Eksctl 可以探索和擴展使用 傳遞的組態檔案中找到的所有節點群組`--config-file`。

與擴展單一節點群組類似，相同的一組驗證也適用於每個節點群組。例如，所需的節點數量必須在節點數量下限和上限的範圍內。

## 刪除和耗盡節點群組
<a name="nodegroup-delete"></a>

若要刪除節點群組，請執行：

```
eksctl delete nodegroup --cluster=<clusterName> --name=<nodegroupName>
```

 [包含和排除規則](#node-include)也可以與此命令搭配使用。

**注意**  
這將在刪除執行個體之前耗盡該節點群組中的所有 Pod。

若要在耗盡過程中略過移出規則，請執行：

```
eksctl delete nodegroup --cluster=<clusterName> --name=<nodegroupName> --disable-eviction
```

所有節點都會封鎖，且所有 Pod 都會在刪除時從節點群組移出，但如果您需要耗盡節點群組而不將其刪除，請執行：

```
eksctl drain nodegroup --cluster=<clusterName> --name=<nodegroupName>
```

若要取消協調節點群組，請執行：

```
eksctl drain nodegroup --cluster=<clusterName> --name=<nodegroupName> --undo
```

若要忽略移出規則，例如 PodDisruptionBudget 設定，請執行：

```
eksctl drain nodegroup --cluster=<clusterName> --name=<nodegroupName> --disable-eviction
```

若要加速耗盡程序，您可以`--parallel <value>`為要平行耗盡的節點數量指定 。

## 其他功能
<a name="_other_features"></a>

您也可以為節點群組啟用 SSH、ASG 存取和其他功能，例如：

```
eksctl create nodegroup --cluster=cluster-1 --node-labels="autoscaling=enabled,purpose=ci-worker" --asg-access --full-ecr-access --ssh-access
```

### 更新標籤
<a name="_update_labels"></a>

中沒有特定命令`eksctl`可更新節點群組的標籤，但可以使用 輕鬆達成`kubectl`，例如：

```
kubectl label nodes -l alpha.eksctl.io/nodegroup-name=ng-1 new-label=foo
```

### SSH 存取
<a name="_ssh_access"></a>

您可以在節點群組組態`publicKeyPath`中設定其中一個 `publicKeyName`和 `publicKey`，以啟用節點群組的 SSH 存取。或者，您可以使用 [AWS Systems Manager (SSM)](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-sessions-start.html#sessions-start-cli) 在節點上 SSH，方法是使用 設定節點群組`enableSsm`：

```
managedNodeGroups:
  - name: ng-1
    instanceType: m5.large
    desiredCapacity: 1
    ssh: # import public key from file
      publicKeyPath: ~/.ssh/id_rsa_tests.pub
  - name: ng-2
    instanceType: m5.large
    desiredCapacity: 1
    ssh: # use existing EC2 key
      publicKeyName: ec2_dev_key
  - name: ng-3
    instanceType: m5.large
    desiredCapacity: 1
    ssh: # import inline public key
      publicKey: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDqZEdzvHnK/GVP8nLngRHu/GDi/3PeES7+Bx6l3koXn/Oi/UmM9/jcW5XGziZ/oe1cPJ777eZV7muEvXg5ZMQBrYxUtYCdvd8Rt6DIoSqDLsIPqbuuNlQoBHq/PU2IjpWnp/wrJQXMk94IIrGjY8QHfCnpuMENCucVaifgAhwyeyuO5KiqUmD8E0RmcsotHKBV9X8H5eqLXd8zMQaPl+Ub7j5PG+9KftQu0F/QhdFvpSLsHaxvBzA5nhIltjkaFcwGQnD1rpCM3+UnQE7Izoa5Yt1xoUWRwnF+L2TKovW7+bYQ1kxsuuiX149jXTCJDVjkYCqi7HkrXYqcC1sbsror someuser@hostname"
  - name: ng-4
    instanceType: m5.large
    desiredCapacity: 1
    ssh: # enable SSH using SSM
      enableSsm: true
```