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 预配工作负载 复制链接链接已复制到粘贴板!
启用配置工作负载所需的服务。默认情况下,
辅助服务处于启用状态。默认情况下,cluster-api-provider-metal3-preview 和cluster-api-provider-openshift-assisted-preview是禁用的。确保所有三项服务都已启用: true。运行以下命令编辑资源:oc edit multiclusterengines.multicluster.openshift.io -n multicluster-engine在
.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- 启用中央基础设施管理服务。请参阅启用中央基础设施管理服务以了解具体过程。
使用
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>运行以下命令来应用 YAML 内容:
oc apply -f <filename>.yaml配置包含
distributionVersion、apiVIPs和 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<1>`.spec.openshiftAssistedConfigSpec.sshAuthorizedKey` 用于访问启动(也称为发现)阶段的节点。 <2>`.spec.config.sshAuthorizedKey` 用于访问已配置的 OpenShift Container Platform 节点。
应用 YAML 文件。运行以下命令:
oc apply -f <filename>.yaml- 如果您没有拉取密钥,则需要创建一个拉取密钥以使您的集群能够从容器注册表中拉取图像。完成以下步骤来创建拉取机密:
创建一个 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运行以下命令以应用该文件:
oc apply -f pull-secret.yaml配置
Metal3Cluster基础架构资源,其中包含与在裸机上部署集群相关的信息。apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 kind: Metal3Cluster metadata: name: <cluster-name> namespace: <cluster-namespace> spec: controlPlaneEndpoint: host: <cluster-name>.lab.home1 port: 6443 noCloudProvider: true- 1
主机是来自Cluster资源的clusterName的<clusterName>.<baseDomain>*值,baseDomain在OpenshiftAssistedControlPlane资源中定义。
应用该文件。运行以下命令:
oc apply -f <filename>.yaml为从
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 - 1
- 该图像与
distributionVersion值和OpenShift Container Platform Releases中的版本相匹配。 - 2
- 将
clusterName设置为与Cluster资源中的值相同的值
应用该文件。运行以下命令:
oc apply -f <filename>.yaml使用
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应用该文件。运行以下命令:
oc apply -f <filename>.yaml创建
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 创建
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>运行以下命令保存 YAML 文件并配置集群:
oc apply -f <filename>.yaml验证您的集群配置状态。
运行
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运行
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运行
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运行
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运行
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访问集群。
运行以下命令获取
kubeconfig文件:oc get secret -n test-capi cluster-name-admin-kubeconfig -o json | jq -r .data.kubeconfig | base64 --decode > kubeconfig-
运行以下命令,使用
kubeconfig文件访问集群:
export KUBECONFIG=$(realpath kubeconfig) oc get nodes