1.11. 集群 API


集群 API 提供声明 API,以简化 Kubernetes 集群的置备、升级和管理。多集群引擎 operator 安装核心 Cluster API 组件以及多个基础架构和 bootstrap 供应商,以支持不同的用例和平台。了解如何在独立多集群引擎 operator 或 Red Hat Advanced Cluster Management 中使用 Cluster API。请参见以下进程:

1.11.1. 使用 Cluster API 安装受管集群

您可以使用 metal3 基础架构供应商和 OpenShift Container Platform 支持的 bootstrap 和 control plane 供应商使用 Cluster API 安装 OpenShift Container Platform 受管集群。

  • 'ControlPlane 'defines the control plane 属性。
  • ClusterInfrastructure 定义集群级别的基础架构详情。

先决条件

  • 您需要 Red Hat Advanced Cluster Management 安装中的 MultiClusterEngine 资源,或 multicluster engine operator 独立安装。
  • 以下 API 基域必须指向静态 API VIP: api.<cluster_name>.<base_domain >。
  • 以下应用程序基域必须指向 Ingress VIP 的静态 IP 地址:.< apps.<cluster_name>.<base_domain >。
  • 您需要集群的内部 API 端点: api-int.<baseDomain&gt;。

1.11.1.1. 使用 Cluster API 置备工作负载

  1. 启用置备工作负载所需的服务。默认情况下启用 assisted-service。默认情况下,禁用 cluster-api-provider-metal3-preview 和 cluster-api-provider-openshift-assisted-preview。确保启用了所有三个服务 : true。运行以下命令编辑资源:

    oc edit multiclusterengines.multicluster.openshift.io -n multicluster-engine
    Copy to Clipboard Toggle word wrap
  2. assisted-service 组件、cluster-api-provider-metal3-preview 组件和 cluster-api-provider-openshift-assisted-preview 组件的 .spec.overrides 中设置 enabled: true。请参阅以下 configOverrides 值:

        - configOverrides: {}
          enabled: true
          name: assisted-service
        - configOverrides: {}
          enabled: true
          name: cluster-api
    ...
        - configOverrides: {}
          enabled: true
          name: cluster-api-provider-metal3-preview
        - configOverrides: {}
          enabled: true
          name: cluster-api-provider-openshift-assisted-preview
    Copy to Clipboard Toggle word wrap
  3. 启用中央基础架构管理服务。请参阅启用中央基础架构管理服务
  4. 使用 clusterNetwork 规格、controlPlaneRef 规格和 infrastructureRef 规格配置集群资源。请参阅以下 集群资源

    apiVersion: cluster.x-k8s.io/v1beta1
    kind: Cluster
    metadata:
      name: <cluster-name>
      namespace: <cluster-namespace>
    spec:
      clusterNetwork:
        pods:
          cidrBlocks:
            - 172.18.0.0/20
        services:
          cidrBlocks:
            - 10.96.0.0/12
      controlPlaneRef: 
    1
    
        apiVersion: controlplane.cluster.x-k8s.io/v1alpha2
        kind: OpenshiftAssistedControlPlane
        name:  <cluster-name>
        namespace:  <cluster-namespace>
      infrastructureRef: 
    2
    
        apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
        kind: Metal3Cluster
        name: <cluster-name>
        namespace: <cluster-namespace>
    Copy to Clipboard Toggle word wrap
    1
    OpenshiftAssistedControlPlane 是 control plane。
    2
    Metal3Cluster 是基础架构。
  5. 运行以下命令来应用 YAML 内容:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  6. 配置 OpenshiftAssistedControlPlane 资源,其中包含 distributionVersionapiVIP 和 SSH 密钥。在 distributionVersion 字段中指定 OpenShift Container Platform 版本。

    注: distributionVersion 的值与 OpenShift Container Platform Releases 中的镜像匹配。请参阅以下 YAML 资源:

    apiVersion: controlplane.cluster.x-k8s.io/v1alpha2
    kind: OpenshiftAssistedControlPlane
    metadata:
      name: <cluster-name>
      namespace: <cluster-namespace>
      annotations: {}
    spec: 
    1
    
      openshiftAssistedConfigSpec:
        sshAuthorizedKey: "{{ ssh_authorized_key }}"
        nodeRegistration:
          kubeletExtraLabels:
          - 'metal3.io/uuid="${METADATA_UUID}"'
      distributionVersion: <4.x.0>
      config:
        apiVIPs:
        - 192.168.222.40
        ingressVIPs:
        - 192.168.222.41
        baseDomain: lab.home
        pullSecretRef:
          name: "pull-secret"
        sshAuthorizedKey: "{{ ssh_authorized_key }}" 
    2
    
      machineTemplate:
        infrastructureRef:
          apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
          kind: Metal3MachineTemplate
          name: <cluster-name-control-plane>
          namespace: <cluster-namespace>
      replicas: 3
    Copy to Clipboard Toggle word wrap
    1
    .spec.openshiftAssistedConfigSpec.sshAuthorizedKey 用于访问 引导 阶段的节点,也称为 发现阶段
    2
    .spec.config.sshAuthorizedKey 用于访问置备的 OpenShift Container Platform 节点。
  7. 应用 YAML 文件。运行以下命令:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  8. 如果没有 pull secret,则需要创建一个 pull secret,以便集群从容器 registry 中拉取镜像。完成以下步骤以创建 pull secret:
  9. 创建 YAML 文件以拉取镜像。请参阅以下名为 pull-secret.yaml 的文件示例:

    apiVersion: v1
    kind: Secret
    metadata: 
    1
    
      name: pull-secret
      namespace: <cluster-namespace>
    data: 
    2
    
      .dockerconfigjson: <encoded_docker_configuration>
    type: kubernetes.io/dockerconfigjson
    Copy to Clipboard Toggle word wrap
    1
    确保 &lt ;cluster-namespace& gt; 值与目标命名空间匹配。
    2
    将 base64 编码的配置文件指定为 < encoded_docker_configuration > 的值。
  10. 运行以下命令以应用该文件:

    oc apply -f pull-secret.yaml
    Copy to Clipboard Toggle word wrap
  11. 配置 Metal3Cluster 基础架构资源,其中包含与 baremetal 上部署集群相关的信息。

    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: Metal3Cluster
    metadata:
      name: <cluster-name>
      namespace: <cluster-namespace>
    spec:
      controlPlaneEndpoint:
        host: <cluster-name>.lab.home 
    1
    
        port: 6443
      noCloudProvider: true
    Copy to Clipboard Toggle word wrap
    1
    主机 是带有 Cluster 资源的 clusterName 的 < clusterName & gt;.<baseDomain>* 值,baseDomainOpenshiftAssistedControlPlane 资源中定义。
  12. 应用该文件。运行以下命令:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  13. 为从 OpenshiftAssistedControlPlane 资源引用的 control plane 节点配置 Metal3MachineTemplate 资源。请参见以下 YAML 示例:

    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: Metal3MachineTemplate
    metadata:
      name: <cluster-name>
      namespace: <cluster-namespace>
    spec:
      nodeReuse: false
      template:
        spec:
          automatedCleaningMode: disabled
          dataTemplate:
            name: <cluster-name-template>
          image: 
    1
    
            checksum: https://mirror.openshift.com/pub/openshift-v4/x86_64/dependencies/rhcos/4.19/4.19.0/sha256sum.txt
            checksumType: sha256
            url: https://mirror.openshift.com/pub/openshift-v4/x86_64/dependencies/rhcos/4.19/4.19.0/rhcos-4.19.0-x86_64-nutanix.x86_64.qcow2
            format: qcow2
    ---
    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: Metal3DataTemplate
    metadata:
       name: <cluster-name-template>
       namespace: <cluster-namespace>
    spec:
       clusterName: <cluster-name> 
    2
    Copy to Clipboard Toggle word wrap
    1
    镜像与 OpenShift Container Platform Releases 中的 distributionVersion 值和版本匹配。
    2
    clusterName 设置为在 Cluster 资源中相同的值
  14. 应用该文件。运行以下命令:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  15. 使用 MachineDeployment 资源配置 worker 节点,该资源引用 Metal3MachineTemplate。请参阅以下 YAML 示例:

    apiVersion: cluster.x-k8s.io/v1beta1
    kind: MachineDeployment
    metadata:
      name: <cluster-name-worker>
      namespace: <cluster-namespace>
      labels:
        cluster.x-k8s.io/cluster-name: <cluster-name>
    spec:
      clusterName: <cluster-name>
      replicas: 2
      selector:
        matchLabels:
          cluster.x-k8s.io/cluster-name: <cluster-name>
      template:
        metadata:
          labels:
            cluster.x-k8s.io/cluster-name: <cluster-name>
        spec:
          clusterName: <cluster-name>
          bootstrap: 
    1
    
            configRef:
              name: <cluster-name-worker>
              apiVersion: bootstrap.cluster.x-k8s.io/v1alpha1
              kind: OpenshiftAssistedConfigTemplate
          infrastructureRef: 
    2
    
            name: <cluster-name-workers-2>
            apiVersion: infrastructure.cluster.x-k8s.io/v1alpha3
            kind: Metal3MachineTemplate
    Copy to Clipboard Toggle word wrap
    1
    在下一个资源中也会引用 bootstrap 配置来安装。
    2
    基础架构引用指向要调配的资源。
  16. 应用该文件。运行以下命令:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  17. 创建 MachineDeployment 中引用的 OpenshiftAssistedConfigTemplate 资源。以下 YAML 为 worker 节点定义 bootstrap 配置,用于使用 Assisted Installer 注册节点:

    apiVersion: bootstrap.cluster.x-k8s.io/v1alpha1
    kind: OpenshiftAssistedConfigTemplate
    metadata:
      name: <cluster-name-worker>
      namespace: <cluster-namespace>
      labels:
        cluster.x-k8s.io/cluster-name: cluster-name
    spec:
      template:
        spec:
          nodeRegistration:
            kubeletExtraLabels:
              - 'metal3.io/uuid="${METADATA_UUID}"'
    1
    
          sshAuthorizedKey: "{{ ssh_authorized_key }}" 
    2
    Copy to Clipboard Toggle word wrap
    1
    kubeletExtraLabels 无法从这个值中改变。
    2
    输入您的 sshAuthorizedKey
  18. 创建 MachineDeployment 资源中引用的 Metal3MachineTemplate。请参见以下示例:

    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: Metal3MachineTemplate
    metadata:
       name: <cluster-name-workers-2>
       namespace: <cluster-namespace>
    spec:
       nodeReuse: false
       template:
          spec:
             automatedCleaningMode: metadata
             dataTemplate:
                name: <cluster-name-workers-template>
          image:
            checksum: https://mirror.openshift.com/pub/openshift-v4/x86_64/dependencies/rhcos/4.19/4.19.0/sha256sum.txt
            checksumType: sha256
            url: https://mirror.openshift.com/pub/openshift-v4/x86_64/dependencies/rhcos/4.19/4.19.0/rhcos-4.19.0-x86_64-nutanix.x86_64.qcow2
            format: qcow2
    ---
    apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
    kind: Metal3DataTemplate
    metadata:
       name: <cluster-name-workers-template>
       namespace: <cluster-namespace>
    spec:
       clusterName: <cluster-name>
    Copy to Clipboard Toggle word wrap
  19. 运行以下命令来保存 YAML 文件并置备集群:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  20. 验证集群置备状态。

    1. 运行 oc get cluster --namespace <cluster-namespace> <cluster-name> -o yaml 以检查您的集群资源状态。

      请参见以下输出和状态:

      status:
        conditions:
        - lastTransitionTime: "2025-06-25T11:23:38Z"
          status: "True"
          type: Ready
        - lastTransitionTime: "2025-06-25T11:23:38Z"
          status: "True"
          type: ControlPlaneReady
        - lastTransitionTime: "2025-06-25T10:37:03Z"
          status: "True"
          type: InfrastructureReady
      Copy to Clipboard Toggle word wrap
    2. 运行 oc get metal3cluster --namespace <cluster-namespace> <cluster-name> -o yaml 命令检查集群基础架构状态。

      请参见以下输出和状态:

      status:
        conditions:
        - lastTransitionTime: "2025-06-25T10:37:03Z"
          status: "True"
          type: Ready
        - lastTransitionTime: "2025-06-25T10:37:03Z"
          status: "True"
          type: BaremetalInfrastructureReady
      Copy to Clipboard Toggle word wrap
    3. 运行 oc get openshiftassistedcontrolplane --namespace <cluster-namespace> <cluster-name> -o yaml 命令检查 control plane 状态。

      请参见以下输出和状态:

      status:
        conditions:
        - lastTransitionTime: "2025-06-25T11:23:38Z"
          status: "True"
          type: Ready
        - lastTransitionTime: "2025-06-25T11:23:38Z"
          status: "True"
          type: ControlPlaneReady
        - lastTransitionTime: "2025-06-25T10:45:48Z"
          status: "True"
          type: KubeconfigAvailable
        - lastTransitionTime: "2025-06-25T10:38:25Z"
          status: "True"
          type: MachinesCreated
        - lastTransitionTime: "2025-06-25T11:10:54Z"
          status: "True"
          type: MachinesReady
        - lastTransitionTime: "2025-06-25T11:23:38Z"
          status: "True"
          type: UpgradeCompleted
      Copy to Clipboard Toggle word wrap
    4. 运行 oc get machinedeployment --namespace <cluster-namespace> <cluster-name> -o yaml 命令检查机器部署状态。

      请参见以下输出和状态:

      status:
        conditions:
        - lastTransitionTime: "2025-06-25T11:10:29Z"
          status: "True"
          type: Ready
        - lastTransitionTime: "2025-06-25T11:10:29Z"
          status: "True"
          type: Available
        - lastTransitionTime: "2025-06-25T11:10:29Z"
          status: "True"
          type: MachineSetReady
      Copy to Clipboard Toggle word wrap
    5. 运行 kubectl get machine -l cluster.x-k8s.io/cluster-name=cluster-name -n test-capi -o yaml 命令检查您的机器。

      请参见以下输出和状态:

     status:
        conditions:
        - lastTransitionTime: "2025-06-25T11:09:57Z"
      	status: "True"
      	type: Ready
        - lastTransitionTime: "2025-06-25T10:38:20Z"
      	status: "True"
      	type: BootstrapReady
        - lastTransitionTime: "2025-06-25T11:09:57Z"
      	status: "True"
      	type: InfrastructureReady
        - lastTransitionTime: "2025-06-25T11:10:29Z"
      	status: "True"
      	type: NodeHealthy
    Copy to Clipboard Toggle word wrap
  21. 访问集群。

    1. 运行以下命令来获取 kubeconfig 文件:

      oc get secret -n test-capi  cluster-name-admin-kubeconfig -o json | jq -r .data.kubeconfig | base64 --decode > kubeconfig
      Copy to Clipboard Toggle word wrap
    2. 运行以下命令使用 kubeconfig 文件来访问集群:
    export KUBECONFIG=$(realpath kubeconfig)
    oc get nodes
    Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat