

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用节点组
<a name="general-nodegroups"></a>

## 创建节点组
<a name="_creating_nodegroups"></a>

除了与集群一起创建的初始节点组外，您还可以添加一个或多个节点组。

要创建其他节点组，请使用：

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

**注意**  
 `--version`托管节点组不支持标志。它总是从控制平面继承版本。

默认情况下，新的非托管节点组从控制平面继承版本 (`--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-2-builders`可以使用以下命令创建节点组`ng-1-workers`和：

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

#### 负载均衡
<a name="_load_balancing"></a>

如果您已经准备好将现有的经典负载均衡器 or/and 目标组附加到节点组，则可以在配置文件中指定这些目标组。创建节点组时，经典负载均衡器 or/and 目标组会自动与 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`操作，有两个接受全局列表的 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`则仅包含与这些全局匹配的节点组
+ 如果只指定`--exclude`了，则包括所有与这些全局不匹配的节点组
+ 如果同时指定了两者，则`--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>

节点组扩展过程最多可能需要几分钟。如果未指定该`--wait`标志，则`eksctl`乐观地期望节点组能够扩展，并在发送 AWS API 请求后立即返回。要`eksctl`等到节点可用，请添加一个`--wait`标志，如下例所示。

**注意**  
缩放节点组 down/in （即减少节点数量）可能会导致错误，因为我们完全依赖于对 ASG 的更改。这意味着被清空的节点并 removed/terminated 未被显式耗尽。这可能是 future 需要改进的领域。

扩展托管节点组是通过直接调用更新托管节点组配置的 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`和来为节点组启用 SSH 访问。`publicKey`或者，您可以使用 [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
```