1.10. 集群 API


Cluster API 提供声明性 API,可简化 Kubernetes 集群的配置、升级和管理。多集群引擎操作员安装核心 Cluster API 组件以及多个基础设施和引导程序提供程序,以支持不同的用例和平台。了解如何将 Cluster API 与独立多集群引擎操作员或 Red Hat Advanced Cluster Management 结合使用。请参阅以下流程:

1.10.1. 使用 Cluster API 安装托管集群

您可以使用metal3基础设施提供程序和 OpenShift Container Platform 辅助引导程序和控制平面提供程序,通过 Cluster API 安装 OpenShift Container Platform 管理的集群。

  • “ControlPlane”定义控制平面属性。
  • ClusterInfrastructure定义了集群级基础设施细节。

先决条件

  • 您需要来自 Red Hat Advanced Cluster Management 安装或多集群引擎操作员独立安装的MultiClusterEngine资源。
  • 以下 API 基本域必须指向静态 API VIP: api.<cluster_name>.<base_domain>
  • 以下应用程序基本域必须指向 Ingress VIP 的静态 IP 地址: *.apps.<cluster_name>.<base_domain>
  • 您需要集群的内部 API 端点: api-int.<baseDomain>

1.10.1.1. 使用 Cluster API 预配工作负载

  1. 启用配置工作负载所需的服务。默认情况下,辅助服务处于启用状态。默认情况下,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. .spec.overrides中为assisted-service组件、 cluster-api-provider-metal3-preview组件和cluster-api-provider-openshift-assisted-preview组件设置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是控制平面。
    2
    Metal3Cluster是基础设施。
  5. 运行以下命令来应用 YAML 内容:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  6. 配置包含distributionVersionapiVIPs和 SSH 密钥的OpenshiftAssistedControlPlane资源。在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:
      openshiftAssistedConfigSpec:
        sshAuthorizedKey: "{{ ssh_authorized_key }}" 
    1
    
        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. 如果您没有拉取密钥,则需要创建一个拉取密钥以使您的集群能够从容器注册表中拉取图像。完成以下步骤来创建拉取机密:
  9. 创建一个 YAML 文件来拉取图像。请参阅以下名为pull-secret.yaml的文件示例:

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

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

    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>.<baseDomain>*值, baseDomainOpenshiftAssistedControlPlane资源中定义。
  12. 应用该文件。运行以下命令:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  13. 为从OpenshiftAssistedControlPlane资源引用的控制平面节点配置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
    该图像与distributionVersion值和OpenShift Container Platform Releases中的版本相匹配。
    2
    clusterName设置为与Cluster资源中的值相同的值
  14. 应用该文件。运行以下命令:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  15. 使用MachineDeployment资源配置工作节点,该资源引用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
    引导配置也在下一个资源中被引用以进行安装。
    2
    基础设施参考指向要提供的资源。
  16. 应用该文件。运行以下命令:

    oc apply -f <filename>.yaml
    Copy to Clipboard Toggle word wrap
  17. 创建MachineDeployment中引用的OpenshiftAssistedConfigTemplate资源。以下 YAML 定义了工作节点的引导配置,并用于向辅助安装程序注册节点:

    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命令检查您的控制平面状态。

      查看以下输出和状态:

      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