在 VMware vSphere 上管理节点

本文介绍如何在基线集群运行后管理 VMware vSphere 上的工作节点。节点生命周期操作通过 VSphereMachineConfigPool、VSphereMachineTemplate、KubeadmConfigTemplate 和 MachineDeployment 资源进行管理。

要向业务集群添加其他工作节点组,请参阅添加工作节点组。

先决条件

开始之前,请确保满足以下条件:

WARNING

Day-2 apply 可能会被拒绝

发布和池规则适用于本页面执行的每次编辑:

  • 重新应用已归档的 MachineDeployment manifest 会重新提交整个对象,包括唯一预期更改为 replicas 的应用操作。在应用之前,请确认文件设置了 strategy.rollingUpdate.maxSurge: 0 和至少为 1 的 maxUnavailable,否则应用会被拒绝。strategy.type: OnDelete 无需满足这些要求。
  • KubeadmControlPlane 必须将 spec.replicas 设置为 1,或者在包含 spec.rolloutStrategy.rollingUpdate.maxSurge: 0 的情况下设置为 3 或更高值,否则对其进行编辑会被拒绝。
  • status.configStatuses[].state 为 InUse 或 Released 的槽位不能被移除,不能更改其 network.primary.ip 或 .ipv6,也不能从 persistentDisks[] 中移除磁盘,或更改磁盘的 sizeGiB 或 mountPath。添加槽位会被接受。请勿向此类槽位添加磁盘;请参阅已分配槽位限制。

请将拒绝消息与准入拒绝进行匹配。

Clusters created with ac

ac adm alpha cluster create 和 ac adm alpha global create 只会创建集群。要更改正在运行的集群,请按照本页所述更改其对象;不要更改 install-config.yaml 后再次运行 create。ac adm alpha cluster destroy 只会删除由 ac 渲染的对象,因此请删除你自行创建的模板。

步骤

扩展工作节点

添加更多工作节点时,请先更新工作节点机器配置池,再增加副本数。

工作节点扩展取决于 MachineDeployment.spec.replicas 与 VSphereMachineConfigPool.spec.configs[] 中可用节点槽位之间的关系。槽位数可以大于 replicas,空闲槽位不会影响正在运行的集群。如果 replicas 超过可用槽位数,CAPV 将无法正确分配新的工作节点。

  1. 在工作节点 VSphereMachineConfigPool 中添加一个或多个新节点槽位;对于使用 ac 创建的集群,则添加到 <cluster_name>-<group>。
  2. 在同名的 MachineDeployment 中更新 replicas。
  3. 先添加槽位,再增加副本数,如下所示。

以下示例添加了一个包含所需数据磁盘的工作节点槽位:

- hostname: "<worker_node_name_2>"
  datacenter: "<worker_02_datacenter>"
  network:
    primary:
      networkName: "<nic1_network_name>"
      ip: "<worker_02_nic1_ip>/<nic1_prefix>"
      gateway: "<nic1_gateway>"
      dns:
      - "<nic1_dns_1>"
  persistentDisks:
  - name: var-cpaas
    sizeGiB: <worker_var_cpaas_size_gib>
    mountPath: /var/cpaas
    fsFormat: ext4
  ephemeralDisks:
  - name: var-lib-kubelet
    sizeGiB: <worker_var_lib_kubelet_size_gib>
    mountPath: /var/lib/kubelet
    fsFormat: ext4
  - name: var-lib-containerd
    sizeGiB: <worker_var_lib_containerd_size_gib>
    mountPath: /var/lib/containerd
    fsFormat: ext4

此示例使用的是在 provider v1.0.17 上创建的集群的磁盘布局。请使 fsFormat 与现有槽位相匹配:ac 会将标准磁盘格式化为 xfs。在较早版本的 provider 上创建的池没有 ephemeralDisks[],而是在 persistentDisks[] 中声明 /var/cpaas、/var/lib/containerd 和 /var/lib/etcd;请与该池已有的槽位匹配,而不要在其中引入 ephemeralDisks[]。

若要附加其他原始磁盘,而不进行格式化或挂载,请省略 mountPath 和 fsFormat:

  persistentDisks:
  # ...standard persistent disks...
  - name: app-data
    sizeGiB: 50

原始磁盘可在来宾 OS 的 /dev/disk/by-capv/app-data 中使用。在滚动更新期间,同一个 VMDK 会重新附加到替换后的 VM,并重新创建符号链接。应用仍负责管理磁盘内容和运行时挂载。

然后增加工作节点副本数:

replicas: <worker_replicas>

请按以下顺序操作:

ac config use-cluster global
ac -n cpaas-system edit vspheremachineconfigpool <cluster_name>-<group>   # add the slots first
ac -n cpaas-system scale machinedeployment <cluster_name>-<group> --replicas=<worker_replicas>

注意:如果扩展或替换时必须保留逻辑池,请使用现有的同一个 VSphereMachineConfigPool.metadata.name。不同名称的池对象不会复用槽位和 VMDK。releaseDelayHours 是未使用的 Released 槽位的回收宽限期,而不是同一池内复用前的等待时间。

添加工作节点组

添加拥有独立槽位、VM 规格或放置位置的工作节点。global 集群不支持添加工作节点组。

在以下步骤中,<group> 是现有工作节点组,<new_group> 是新工作节点组,是一个其他组未使用的 DNS-1123 标签。control-plane 为保留值。

  1. 读取现有组的值:

    ac config use-cluster global
    ac -n cpaas-system get machinedeployment <cluster_name>-<group> \
      -o jsonpath='{.spec.template.spec.version}{"\n"}{.spec.template.spec.infrastructureRef.name}{"\n"}{.spec.template.spec.bootstrap.configRef.name}{"\n"}'
    ac -n cpaas-system get vspheremachinetemplate <machine_template_name> -o yaml
    ac -n cpaas-system get kubeadmconfigtemplate <bootstrap_template_name> -o yaml

    第一条命令会输出 <k8s_version>、<machine_template_name> 和 <bootstrap_template_name>。

  2. 编写工作节点对象:

    new-worker-group.yaml
    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: VSphereMachineConfigPool
    metadata:
      labels:
        cluster.x-k8s.io/cluster-name: <cluster_name>
      name: <cluster_name>-<new_group>
      namespace: cpaas-system
    spec:
      clusterRef:
        apiVersion: cluster.x-k8s.io/v1beta1
        kind: Cluster
        name: <cluster_name>
      datacenter: <datacenter>
      releaseDelayHours: 24
      configs:
      - hostname: <worker_node_name_1>
        datacenter: <datacenter>
        network:
          primary:
            deviceName: eth0
            networkName: <nic1_network_name>
            ip: <worker_01_nic1_ip>/<nic1_prefix>
            gateway: <nic1_gateway>
            dns:
            - <nic1_dns_1>
        persistentDisks:
        - name: var-cpaas
          datastore: <worker_data_datastore>
          sizeGiB: <worker_var_cpaas_size_gib>
          mountPath: /var/cpaas
          fsFormat: xfs
        ephemeralDisks:
        - name: var-lib-containerd
          datastore: <worker_data_datastore>
          sizeGiB: <worker_var_lib_containerd_size_gib>
          mountPath: /var/lib/containerd
          fsFormat: xfs
        - name: var-lib-kubelet
          datastore: <worker_data_datastore>
          sizeGiB: <worker_var_lib_kubelet_size_gib>
          mountPath: /var/lib/kubelet
          fsFormat: xfs
    ---
    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: VSphereMachineTemplate
    metadata:
      labels:
        cluster.x-k8s.io/cluster-name: <cluster_name>
      name: <cluster_name>-<new_group>
      namespace: cpaas-system
    spec:
      template:
        spec:
          server: <vsphere_server>
          datacenter: <datacenter>
          template: <template_name>
          cloneMode: linkedClone
          folder: <vm_folder>
          resourcePool: <resource_pool>
          datastore: <worker_system_datastore>
          diskGiB: <worker_system_disk_gib>
          memoryMiB: <worker_memory_mib>
          numCPUs: <worker_num_cpus>
          os: Linux
          powerOffMode: trySoft
          network:
            devices:
            - networkName: <nic1_network_name>
          machineConfigPoolRef:
            apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
            kind: VSphereMachineConfigPool
            name: <cluster_name>-<new_group>
            namespace: cpaas-system
    ---
    apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
    kind: KubeadmConfigTemplate
    metadata:
      labels:
        cluster.x-k8s.io/cluster-name: <cluster_name>
      name: <cluster_name>-<new_group>-config
      namespace: cpaas-system
    spec:
      template:
        spec:
          files:
          - content: |
              {
                "apiVersion": "kubelet.config.k8s.io/v1beta1",
                "kind": "KubeletConfiguration",
                "protectKernelDefaults": true,
                "staticPodPath": null,
                "streamingConnectionIdleTimeout": "5m",
                "tlsCertFile": "/etc/kubernetes/pki/kubelet.crt",
                "tlsPrivateKeyFile": "/etc/kubernetes/pki/kubelet.key"
              }
            owner: root:root
            path: /etc/kubernetes/patches/kubeletconfiguration0+strategic.json
            permissions: "0644"
          - content: |
              #!/bin/bash
              set -euo pipefail
              until mountpoint -q /var/lib/containerd; do
                echo "waiting for /var/lib/containerd mount"
                sleep 1
              done
              systemctl restart containerd
              until systemctl is-active --quiet containerd; do
                echo "waiting for containerd"
                sleep 1
              done
              if [ ! -d "/root/images" ]; then
                echo "ERROR: /root/images directory not found" >&2
                exit 1
              fi
              image_count=0
              for image_file in /root/images/*.tar; do
                if [ -f "$image_file" ]; then
                  echo "importing image: $image_file"
                  ctr -n k8s.io images import "$image_file"
                  image_count=$((image_count + 1))
                fi
              done
              if [ "$image_count" -eq 0 ]; then
                echo "ERROR: no tar files found in /root/images" >&2
                exit 1
              fi
              echo "imported $image_count images"
            owner: root:root
            path: /usr/local/bin/capv-load-local-images.sh
            permissions: "0755"
          - content: |
              nameserver <nic1_dns_1>
            owner: root:root
            path: /etc/resolv.conf
            permissions: "0644"
          joinConfiguration:
            nodeRegistration:
              criSocket: /var/run/containerd/containerd.sock
              ignorePreflightErrors:
              - ImagePull
              kubeletExtraArgs:
                cloud-provider: external
                volume-plugin-dir: /opt/libexec/kubernetes/kubelet-plugins/volume/exec/
              name: '{{ local_hostname }}'
            patches:
              directory: /etc/kubernetes/patches
          postKubeadmCommands:
          - chmod 600 /var/lib/kubelet/config.yaml
          preKubeadmCommands:
          - hostnamectl set-hostname "{{ ds.meta_data.hostname }}"
          - echo "::1         ipv6-localhost ipv6-loopback localhost6 localhost6.localdomain6" >/etc/hosts
          - echo "127.0.0.1   {{ ds.meta_data.hostname }} {{ local_hostname }} localhost localhost.localdomain localhost4 localhost4.localdomain4" >>/etc/hosts
          - while ! ip route | grep -q "default via"; do sleep 1; done; echo "NetworkManager started"
          - /usr/local/bin/capv-load-local-images.sh
          users:
          - name: boot
            sshAuthorizedKeys:
            - <ssh_public_key>
            sudo: ALL=(ALL) NOPASSWD:ALL
    ---
    apiVersion: cluster.x-k8s.io/v1beta1
    kind: MachineDeployment
    metadata:
      labels:
        cluster.x-k8s.io/cluster-name: <cluster_name>
      name: <cluster_name>-<new_group>
      namespace: cpaas-system
    spec:
      clusterName: <cluster_name>
      replicas: <worker_replicas>
      selector:
        matchLabels:
          cluster.x-k8s.io/cluster-name: <cluster_name>
          pool.name: <cluster_name>-<new_group>
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxSurge: 0
          maxUnavailable: 1
      template:
        metadata:
          labels:
            cluster.x-k8s.io/cluster-name: <cluster_name>
            pool.name: <cluster_name>-<new_group>
        spec:
          clusterName: <cluster_name>
          version: <k8s_version>
          nodeDeletionTimeout: 5m
          nodeDrainTimeout: 1m
          bootstrap:
            configRef:
              apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
              kind: KubeadmConfigTemplate
              name: <cluster_name>-<new_group>-config
              namespace: cpaas-system
          infrastructureRef:
            apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
            kind: VSphereMachineTemplate
            name: <cluster_name>-<new_group>
            namespace: cpaas-system
    • 为该池至少配置 replicas 个槽位。集群所有池中的主机名和主 IP 地址必须唯一,并且每个 MachineDeployment 都需要有自己的池;请参阅Provider Requirements。
    • 从 <machine_template_name> 中获取 template,以确保 VM 模板与 <k8s_version> 匹配。还要从中获取 server、folder 和 resourcePool。
    • 保留 maxSurge: 0:使用 Cluster API 默认值进行滚动更新时,如果池已满,更新会停滞。
    • 如果现有对象包含与示例不同的字段,请使用现有对象的值,例如 fsFormat,以及槽位的磁盘、第二个 NIC 或故障域。如果 <bootstrap_template_name> 与上述 spec.template.spec 不同,也请使用其值。例如,在 Kubernetes 1.35 及更高版本中,kubelet 配置补丁需要 "imagePullCredentialsVerificationPolicy": "NeverVerify";较早版本不接受此字段。
  3. 应用对象并监控该组:

    ac config use-cluster global
    ac apply -f new-worker-group.yaml
    ac -n cpaas-system get machines.cluster.x-k8s.io \
      -l cluster.x-k8s.io/deployment-name=<cluster_name>-<new_group> -w

    当该组的每个 Machine 都处于 Running 状态后再继续;然后确认集群上的节点已处于 Ready 状态。请将拒绝消息与准入拒绝进行匹配。

推出更新后的工作节点配置

需要更改工作节点 VM 规格时,请创建新的 VSphereMachineTemplate,并更新 MachineDeployment 以引用它。这会触发滚动更新,用新配置替换工作节点。

常见更改包括:

  • VM 模板名称(spec.template.spec.template)
  • CPU 或内存规格(numCPUs、memoryMiB)
  • 系统磁盘规格(diskGiB),仅适用于完整克隆:链接克隆会忽略此设置
  • VM 网络设备(network.devices)

在这些步骤中,只更改机器规格。不要更改 KubeadmControlPlane 或 MachineDeployment 的 spec.version,也不要切换到 Kubernetes 版本不同的 VM 模板。

持久磁盘是在 VSphereMachineConfigPool 槽位中声明的,而不是在 VSphereMachineTemplate 中声明,因此新模板不会更改它们。对于处于 InUse 或 Released 状态的槽位,更改现有磁盘的 sizeGiB 或 mountPath 会被拒绝;因此,若要更改现有磁盘的规格,必须使用新槽位,而不能编辑当前槽位。请参阅已分配槽位限制。

  1. 导出现有模板

    ac config use-cluster global
    ac get vspheremachinetemplate <cluster_name>-worker -n <namespace> -o yaml > new-worker-template.yaml
  2. 修改模板

    编辑 new-worker-template.yaml:

    • 将 metadata.name 设置为新的唯一名称(例如 <cluster_name>-worker-v2)
    • 更新所需的规格字段
    • 移除服务器生成的字段:metadata.resourceVersion、metadata.uid、metadata.generation、metadata.creationTimestamp、metadata.managedFields、metadata.annotations["kubectl.kubernetes.io/last-applied-configuration"] 和 status
  3. 应用新模板

    ac config use-cluster global
    ac apply -f new-worker-template.yaml
  4. 更新 MachineDeployment 引用

    ac config use-cluster global
    ac patch machinedeployment <cluster_name>-worker -n <namespace> \
      --type='merge' -p='{
        "spec": {
          "template": {
            "spec": {
              "infrastructureRef": {
                "name": "<new-template-name>"
              }
            }
          }
        }
      }'

    如果还需要更改引导设置,请参阅下文的更新引导模板。

  5. 监控滚动更新

    ac config use-cluster global
    ac -n <namespace> get machinedeployment <cluster_name>-worker -w
    ac -n <namespace> get machine

若要更改控制平面节点的机器规格,请使用相同的步骤,并改为修补 KubeadmControlPlane.spec.machineTemplate.infrastructureRef.name。

TIP

回滚失败的更新

如果滚动更新失败(例如,新 VM 无法启动),请将 MachineDeployment 引用恢复为先前的模板名称。只有在 Kubernetes 次要版本未更改时,此操作才有效。旧模板仍然存在,Cluster API 将回滚到该模板。

验证工作节点状态

运行以下命令,验证管理集群和业务集群的状态:

ac config use-cluster global
ac -n <namespace> get machinedeployment,machine,vspheremachine,vspherevm
ac config sync
ac config use-cluster <cluster_name>
ac get nodes -o wide
ac config use-cluster global

确认以下结果:

  • 已达到目标工作节点副本数。
  • 每个新工作节点都已加入集群。
  • 节点最终变为 Ready 状态。

更新 Bootstrap Templates

若要更新引导配置,请创建新的 KubeadmConfigTemplate 并更新 MachineDeployment 引用。

  1. 导出现有模板

    ac config use-cluster global
    ac get kubeadmconfigtemplate <cluster_name>-worker-config -n <namespace> -o yaml > new-bootstrap-template.yaml
  2. 修改模板

    编辑 new-bootstrap-template.yaml:

    • 将 metadata.name 设置为新的唯一名称(例如,<cluster_name>-worker-config-v2)
    • 更新所需的引导配置字段
    • 对于 Kubernetes 1.35 或更高版本,将必需的 kubelet 补丁设置添加到 /etc/kubernetes/patches/kubeletconfiguration0+strategic.json
    • 删除滚动更新工作节点配置步骤 2 中列出的相同服务器生成字段
  3. 应用新模板

    ac config use-cluster global
    ac apply -f new-bootstrap-template.yaml
  4. 更新 MachineDeployment 引用

    ac config use-cluster global
    ac patch machinedeployment <cluster_name>-worker -n <namespace> \
      --type='merge' -p='{
        "spec": {
          "template": {
            "spec": {
              "bootstrap": {
                "configRef": {
                  "name": "<new-bootstrap-template-name>"
                }
              }
            }
          }
        }
      }'

    Cluster API 控制器会触发滚动更新。现有机器在被替换之前会继续使用旧的引导配置。

运行时拓扑更改

网络、放置和磁盘拓扑属于机器模板和机器配置池的一部分。将这些字段的更改视为替换机器的滚动更新,而不是原地编辑 VM:

  1. 在增加副本数或开始替换之前,将所需的槽定义添加到现有的 VSphereMachineConfigPool。已分配的槽会拒绝某些更改;请参阅先决条件中的警告。
  2. 当 VM、网络、放置或系统磁盘字段发生更改时,创建新的 VSphereMachineTemplate。
  3. 当引导文件或节点配置发生更改时,创建新的 KubeadmConfigTemplate。
  4. 只有在新池容量和模板就绪后,才更新 MachineDeployment 引用。
  5. 监控替换的 Machine,并确认每个节点都变为 Ready 后,再移除旧模板。

关于确切的第二块 NIC 和持久磁盘字段,请参阅在 VMware vSphere 上创建集群中的可选拓扑。在 Day-2 更改期间,保持资源间的关系不变:

  • 更改 NIC 拓扑时,确保 VSphereMachineConfigPool.spec.configs[].network.additional 和 VSphereMachineTemplate.spec.template.spec.network.devices 保持一致。
  • 不要移除必需的 var-cpaas 持久磁盘、必需的 var-lib-kubelet 和 var-lib-containerd 临时磁盘,或控制平面的 var-lib-etcd 临时磁盘。
  • 新槽的磁盘列表必须与池中现有槽的列表完全一致。ephemeralDisks[] 从 provider v1.0.17 起可用,但在更早版本的 provider 上创建的池会在 persistentDisks[] 中声明 /var/cpaas、/var/lib/containerd 和 /var/lib/etcd;不要转换这些槽,也不要在同一个池中混用布局。
  • 不要调整已分配槽的结构或向其中添加磁盘。状态为 InUse 或 Released 的槽会拒绝移除持久磁盘,或更改其 sizeGiB 或 mountPath。调整大小意味着需要规划一个新槽。
  • 不要将 machineNamingStrategy、wipeFilesystem 或 releaseDelayHours 视为可在池之间移动磁盘或加快数据存储清理的开关。

从一个 NIC 扩展到两个 NIC 时,请同时更新以下所有字段:

  1. 将第二个 NIC 条目追加到 VSphereMachineConfigPool.spec.configs[].network.additional;保持 network.primary 不变。
  2. 在新模板的 VSphereMachineTemplate.spec.template.spec.network.devices 中添加第二个设备。

从两个 NIC 恢复到一个 NIC 时,请从两个字段中移除对应的第二个条目。将 network.additional 留空或完全移除该键,并滚动更新新的 VSphereMachineTemplate;不要直接编辑现有 VM。

故障排除

当工作节点管理失败时,请先执行以下检查:

  • 检查 VSphereMachine 的 MachineConfigPoolReady 条件。如果为 False,原因会说明槽位分配失败的原因:
    • PoolBoundToOtherConsumer:池已绑定到其他 KubeadmControlPlane 或 MachineDeployment。
    • NoAvailableSlots:没有槽位与所需的数据中心匹配。
  • 确认工作节点机器配置池仍有可用槽位。
  • 确认工作节点的 IP 地址、网关和 DNS 设置正确。
  • 确认工作节点 VM 模板仍符合所需的 Kubernetes 版本和 guest-tools 要求。
  • 当节点等待分配 IP 时,检查 VSphereVM.status.addresses。

后续步骤

有关集群创建和持久磁盘诊断,请参阅在 VMware vSphere 上创建集群及其持久磁盘生命周期。