在 Huawei Cloud Stack 上管理节点

本文介绍如何在 Huawei Cloud Stack 平台上使用 Cluster API Machine 资源管理工作节点。

先决条件

WARNING

重要先决条件

  • 执行节点操作前,必须先部署控制平面。有关设置说明,请参阅创建集群。
  • 确保你拥有 HCS 平台的适当访问权限及所需权限。
  • 若要管理 global 集群本身的节点,必须先在 global 上安装 provider;请参阅在 Alauda OS 集群上管理节点。

使用本文中的 YAML 示例时,只需将 <> 中的值替换为特定于环境的值。除非集群策略要求使用其他值,否则请保留其余字段。使用 ac 创建的集群会将节点组 <cluster-name>-<group> 的 MachineDeployment、HCSMachineConfigPool 和机器模板命名为 KubeadmConfigTemplate <cluster-name>-<group>-config,其中 <group> 为 computes[].name;示例使用组 worker。未使用 ac 创建的集群可能采用其他名称;请从其 MachineDeployment 资源中查看这些名称。

概述

工作节点通过 Cluster API Machine 资源进行管理,从而实现声明式、自动化的节点生命周期管理。工作节点池由四种资源构成:

  1. Machine Configuration Pool - 工作节点的网络设置
  2. Machine Template - VM 规格
  3. Bootstrap Configuration - 节点初始化设置
  4. Machine Deployment - 节点创建与管理的编排
使用 ac 创建的集群

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

节点管理操作

使用 Cluster API 清单管理工作节点。若要向业务集群添加另一个工作节点池,请参阅添加工作节点组。

扩展工作节点

工作节点扩展允许你根据工作负载需求调整集群容量。

添加工作节点

增加工作节点数量,以处理增加的工作负载。

操作步骤:

  1. 检查当前节点状态

    ac config use-cluster global
    # List all machines in the cluster
    ac get machines.cluster.x-k8s.io -n cpaas-system
    
    # List machines for a specific MachineDeployment
    ac get machines.cluster.x-k8s.io -n cpaas-system -l cluster.x-k8s.io/deployment-name=<cluster-name>-worker
  2. 扩展配置池

    每增加一个节点,就向池中添加一个机器配置。为每个新条目设置与现有条目相同的 persistentDisks[],包括 /var/cpaas 磁盘。

    ac config use-cluster global
    ac get hcsmachineconfigpool <cluster-name>-worker -n cpaas-system -o yaml

    修改池以包含新的 IP 条目,然后应用更改:

    ac config use-cluster global
    ac apply -f <updated-pool-config.yaml>

    编辑池时,除非你有意添加新的磁盘槽位,否则请保持所有现有的 configs[] 条目及其已接受的 persistentDisks[] 条目不变。

  3. 扩展 MachineDeployment

    将 replicas 字段更新为所需的节点数量:

    ac config use-cluster global
    ac patch machinedeployment <cluster-name>-worker -n cpaas-system \
      --type='json' -p='[{"op": "replace", "path": "/spec/replicas", "value": <new-replica-count>}]'
  4. 监控扩展进度

    ac config use-cluster global
    # Watch machines being created
    ac get machines.cluster.x-k8s.io -n cpaas-system -w
    
    # Check MachineDeployment status
    ac get machinedeployment <cluster-name>-worker -n cpaas-system

    当 MachineDeployment 报告新的副本数已就绪时继续;否则请参阅故障排除。

移除工作节点

减少工作节点数量,以降低集群容量。

WARNING

数据丢失警告

缩容会移除工作节点及其 ECS 实例。模板拥有的 dataVolumes[] 不会保留。在 HCSMachineConfigPool.spec.configs[].persistentDisks[] 中声明的池管理持久磁盘仍由池跟踪;只要相应的主机名条目仍在池中,就可以重复使用这些磁盘。请确保:

  • 工作负载通过适当的副本机制能够容忍节点丢失
  • 关键数据不会仅存储在要移除的节点上
  • 应用程序按照水平扩展方式设计

操作步骤:

  1. 缩减 MachineDeployment

    ac config use-cluster global
    ac patch machinedeployment <cluster-name>-worker -n cpaas-system \
      --type='json' -p='[{"op": "replace", "path": "/spec/replicas", "value": <new-replica-count>}]'
  2. 监控移除进度

    ac config use-cluster global
    ac get machines.cluster.x-k8s.io -n cpaas-system -w

    当机器数量与新的副本数一致时继续;否则请参阅故障排除。

添加工作节点组

添加具有独立地址、规格或可用区的工作节点。global 集群不支持添加工作节点组。

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

操作步骤:

  1. 准备子网

    工作节点使用的每个子网都必须列在 HCSCluster.spec.network.subnets 中。若要使用未列出的子网,请先在集群的 VPC 中创建该子网,然后将其添加到列表中:

    ac config use-cluster global
    ac -n cpaas-system patch hcscluster <cluster-name> --type=json \
      -p='[{"op":"add","path":"/spec/network/subnets/-","value":{"name":"<subnet-name>"}}]'
    ac -n cpaas-system get hcscluster <cluster-name> \
      -o jsonpath='{.status.conditions[?(@.type=="SubnetReady")].status}{"\n"}{.spec.network.subnets[*].id}{"\n"}'

    当状态为 True 且输出为每个子网列出一个 ID 时继续。

  2. 读取现有组的值

    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 hcsmachinetemplate <machine-template-name> -o yaml
    ac -n cpaas-system get kubeadmconfigtemplate <bootstrap-template-name> -o yaml

    第一个命令会输出 <kubernetes-version>、<machine-template-name> 和 <bootstrap-template-name>。

  3. 编写工作节点对象

    new-worker-group.yaml
    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: HCSMachineConfigPool
    metadata:
      labels:
        cluster.x-k8s.io/cluster-name: <cluster-name>
      name: <cluster-name>-<new-group>
      namespace: cpaas-system
    spec:
      configs:
      - hostname: <worker-hostname-1>
        networks:
        - subnetName: <subnet-name>
          ipAddress: <worker-ip-1>
        persistentDisks:
        - slot: 0
          size: 100
          type: <disk-type>
          mountPath: /var/cpaas
          format: xfs
    ---
    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: HCSMachineTemplate
    metadata:
      labels:
        cluster.x-k8s.io/cluster-name: <cluster-name>
      name: <cluster-name>-<new-group>
      namespace: cpaas-system
    spec:
      template:
        spec:
          imageName: <vm-image-name>
          flavorName: <instance-flavor>
          availabilityZone: <availability-zone>
          configPoolRef:
            name: <cluster-name>-<new-group>
          rootVolume:
            type: <disk-type>
            size: 100
          dataVolumes:
          - size: 100
            type: <disk-type>
            mountPath: /var/lib/containerd
            format: xfs
          - size: 100
            type: <disk-type>
            mountPath: /var/lib/kubelet
            format: xfs
    ---
    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"
          joinConfiguration:
            nodeRegistration:
              kubeletExtraArgs:
                protect-kernel-defaults: "true"
                volume-plugin-dir: /opt/libexec/kubernetes/kubelet-plugins/volume/exec/
            patches:
              directory: /etc/kubernetes/patches
          postKubeadmCommands:
          - chmod 600 /var/lib/kubelet/config.yaml
          users:
          - name: boot
            sshAuthorizedKeys:
            - <ssh-public-key>
    ---
    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: <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: <kubernetes-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: HCSMachineTemplate
            name: <cluster-name>-<new-group>
            namespace: cpaas-system
    • 为池至少提供 replicas 个条目,每个节点一个,并使用其他池未使用的主机名和地址。为每台主机重复 persistentDisks 条目,并在创建节点前完成规划;请参阅稍后更改渲染的对象。
    • 从 <machine-template-name> 获取 imageName,以使镜像与 <kubernetes-version> 匹配。flavorName 和 availabilityZone 是 provider 识别的 API 值,而不是租户 UI 中显示的名称;该组的每个工作节点都使用选定的可用区。
    • 不要设置 providerID 或 serverId。将 /var/cpaas 放入 persistentDisks[],而不是 dataVolumes[]。
    • 如果 <bootstrap-template-name> 中的字段与上述 spec.template.spec 不同,请使用其中的值。例如,在 Kubernetes 1.35 及更高版本中,kubelet 配置补丁需要 "imagePullCredentialsVerificationPolicy": "NeverVerify";早期版本不接受此字段。
  4. 应用对象并监控组

    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。

更改机器基础设施

若要更改工作机器规格(CPU、内存、磁盘、VM 镜像),请创建新的机器模板,并将 MachineDeployment 指向该模板。随后,工作节点会逐个替换。请执行以下步骤:

注意:替换期间不会保留 HCS dataVolumes[]。若要保留节点本地状态(例如 /var/cpaas),请在滚动替换开始前于 HCSMachineConfigPool.spec.configs[].persistentDisks[] 中声明该状态,保留 MachineDeployment.spec.strategy.rollingUpdate.maxSurge: 0,并确保池中没有空闲配置:只有当池中没有空闲配置时,替换节点才会获得其所替换节点的磁盘。如果集群仍在 HCSMachineTemplate.spec.template.spec.dataVolumes[] 中保留这些磁盘,请先完成迁移到池管理的持久磁盘。

  1. 创建新的机器模板

    复制现有的 HCSMachineTemplate 并修改所需值:

    • imageName - VM 镜像
    • flavorName - 实例类型
    • rootVolume.size - 系统磁盘大小
    • dataVolumes - 临时数据磁盘配置

    如果需要添加新的池管理持久磁盘,请先将其追加到工作节点的 HCSMachineConfigPool。provider 会创建或声明 EVS 磁盘,但运行中的 ECS 不会挂载该磁盘,直到此次滚动替换创建出替换工作节点。

    ac config use-cluster global
    ac get hcsmachinetemplate <current-template> -n cpaas-system -o yaml > new-template.yaml

    然后在应用前编辑 new-template.yaml:

    • 将 metadata.name 更改为 <new-template>
    • 不设置运行时身份字段,包括 spec.template.spec.providerID 和 spec.template.spec.serverId
    • 移除服务器生成的字段:
      • metadata.resourceVersion
      • metadata.uid
      • metadata.generation
      • metadata.creationTimestamp
      • metadata.managedFields
      • kubectl.kubernetes.io/last-applied-configuration 注解
      • status
  2. 部署新模板

    ac config use-cluster global
    ac apply -f new-template.yaml -n cpaas-system
  3. 更新 MachineDeployment

    修改 MachineDeployment 以引用新模板:

    ac config use-cluster global
    ac patch machinedeployment <cluster-name>-worker -n cpaas-system \
      --type='merge' -p='{"spec":{"template":{"spec":{"infrastructureRef":{"name":"<new-template>"}}}}}'
  4. 监控滚动更新

    ac config use-cluster global
    ac get machines.cluster.x-k8s.io -n cpaas-system -w

    当每台机器都使用新模板运行时继续;否则请参阅故障排除。

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

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

更新引导模板

若要更改工作节点的引导配置(例如 kubelet 设置或节点加入时运行的命令),请创建新的 KubeadmConfigTemplate,并将 MachineDeployment 指向该模板。直接修改现有模板不会对现有机器执行滚动更新;只有新创建的机器会应用更改。

  1. 导出现有模板

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

    编辑 new-bootstrap-template.yaml:

    • 将 metadata.name 设置为新的唯一名称 <new-bootstrap-template>
    • 移除更改机器基础设施中列出的服务器生成字段
    • 编辑需要更改的字段。保留 kubelet 补丁文件 /etc/kubernetes/patches/kubeletconfiguration0+strategic.json 的现有条目,包括证书路径,并且只更改必要的内容
    • 对于 Kubernetes 1.35 或更高版本,将所需的 kubelet 补丁设置添加到该文件中
  3. 应用新模板

    ac config use-cluster global
    ac apply -f new-bootstrap-template.yaml -n cpaas-system
  4. 更新 MachineDeployment

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

    随后开始滚动替换工作节点:

    ac config use-cluster global
    ac get machines.cluster.x-k8s.io -n cpaas-system -w

    当每台机器都使用新模板时继续;否则请参阅故障排除。

验证

部署工作节点后,验证部署:

ac config use-cluster global
# Check machine status
ac get machines.cluster.x-k8s.io -n cpaas-system

# Check MachineDeployment status
ac get machinedeployment -n cpaas-system

# Verify nodes are Ready
ac config sync
ac config use-cluster <cluster-name>
ac get nodes
ac config use-cluster global

当每台机器都为 Running 且每个节点都为 Ready 时继续;否则请参阅故障排除。

故障排除

查看控制器日志

ac config use-cluster global

# View HCS controller logs
ac logs -n cpaas-system deployment/cluster-api-provider-hcs-manager

# View machine details
ac describe hcsmachine <machine-name> -n cpaas-system

常见问题

症状操作
节点无法加入集群确认镜像与 Kubernetes 版本匹配、节点之间可以相互访问,并且配置池中有未使用的条目。
机器卡在配置中查看控制器日志中的错误。确认 HCS 拥有相应资源,并且凭据和权限有效。