11.4. 使用 API 添加主机


您可以使用 Assisted Installer REST API 将主机添加到集群。

先决条件

  • 安装 Red Hat OpenShift Cluster Manager CLI (ocm)。
  • 以具有集群创建权限的用户身份登录 Red Hat OpenShift Cluster Manager
  • 安装 jq
  • 确保您要扩展的集群存在所有必需的 DNS 记录。
重要

在第 2 天操作中添加 control plane 节点时,请确保新节点共享与第 1 天网络相同的子网。子网在 install-config.yaml 文件的 machineNetwork 字段中指定。这个要求适用于集群管理的网络,如裸机或 vSphere,不适用于用户管理的网络。

流程

  1. 针对 Assisted Installer REST API 进行身份验证,并为会话生成 API 令牌。生成的令牌有效期仅为 15 分钟。
  2. 运行以下命令设置 $API_URL 变量:

    $ export API_URL=<api_url> 
    1
    Copy to Clipboard Toggle word wrap
    1
    <api_url> 替换为 Assisted Installer API URL,例如 https://api.openshift.com
  3. 运行以下命令导入集群:

    1. 设置 $CLUSTER_ID 变量:

      1. 登录到集群并运行以下命令:

        $ export CLUSTER_ID=$(oc get clusterversion -o jsonpath='{.items[].spec.clusterID}')
        Copy to Clipboard Toggle word wrap
      2. 显示 $CLUSTER_ID 变量输出:

        $ echo ${CLUSTER_ID}
        Copy to Clipboard Toggle word wrap
    2. 设置用于导入集群的 $CLUSTER_REQUEST 变量:

      $ export CLUSTER_REQUEST=$(jq --null-input --arg openshift_cluster_id "$CLUSTER_ID" \
      '{
        "api_vip_dnsname": "<api_vip>", 
      1
      
        "openshift_cluster_id": "<cluster_id>", 
      2
      
        "name": "<openshift_cluster_name>" 
      3
      
      }')
      Copy to Clipboard Toggle word wrap
      1
      <api_vip> 替换为集群 API 服务器的主机名。这可以是 API 服务器的 DNS 域,也可以是主机可访问的单一节点的 IP 地址。例如: api.compute-1.example.com
      2
      <cluster_id> 替换为前面子步骤中的 $CLUSTER_ID 输出。
      3
      <openshift_cluster_name> 替换为集群的纯文本名称。集群名称应与在第 1 天集群安装过程中设置的集群名称匹配。
    3. 导入集群并设置 $CLUSTER_ID 变量。运行以下命令:

      $ CLUSTER_ID=$(curl "$API_URL/api/assisted-install/v2/clusters/import" -H "Authorization: Bearer ${API_TOKEN}" -H 'accept: application/json' -H 'Content-Type: application/json' \
        -d "$CLUSTER_REQUEST" | tee /dev/stderr | jq -r '.id')
      Copy to Clipboard Toggle word wrap
  4. 运行以下命令,为集群生成 InfraEnv 资源并设置 $INFRA_ENV_ID 变量:

    1. 从位于 console.redhat.com 的 Red Hat OpenShift Cluster Manager 下载 pull secret 文件。
    2. 设置 $INFRA_ENV_REQUEST 变量:

      export INFRA_ENV_REQUEST=$(jq --null-input \
          --slurpfile pull_secret <path_to_pull_secret_file> \
      1
      
          --arg ssh_pub_key "$(cat <path_to_ssh_pub_key>)" \
      2
      
          --arg cluster_id "$CLUSTER_ID" '{
        "name": "<infraenv_name>", 
      3
      
        "pull_secret": $pull_secret[0] | tojson,
        "cluster_id": $cluster_id,
        "ssh_authorized_key": $ssh_pub_key,
        "image_type": "<iso_image_type>" 
      4
      
      }')
      Copy to Clipboard Toggle word wrap
      1
      <path_to_pull_secret_file> 替换为包含从位于 console.redhat.com 的 Red Hat OpenShift Cluster Manager 下载的 pull secret 的本地文件的路径。
      2
      <path_to_ssh_pub_key> 替换为访问主机所需的公共 SSH 密钥的路径。如果没有设置这个值,则无法在发现模式下访问主机。
      3
      <infraenv_name> 替换为 InfraEnv 资源的纯文本名称。
      4
      <iso_image_type> 替换为 ISO 镜像类型,可以是 full-isominimal-iso
    3. $INFRA_ENV_REQUEST 发布到 /v2/infra-envs API,并设置 $INFRA_ENV_ID 变量:

      $ INFRA_ENV_ID=$(curl "$API_URL/api/assisted-install/v2/infra-envs" -H "Authorization: Bearer ${API_TOKEN}" -H 'accept: application/json' -H 'Content-Type: application/json' -d "$INFRA_ENV_REQUEST" | tee /dev/stderr | jq -r '.id')
      Copy to Clipboard Toggle word wrap
  5. 运行以下命令,获取集群主机的发现 ISO 的 URL:

    $ curl -s "$API_URL/api/assisted-install/v2/infra-envs/$INFRA_ENV_ID" -H "Authorization: Bearer ${API_TOKEN}" | jq -r '.download_url'
    Copy to Clipboard Toggle word wrap

    输出示例

    https://api.openshift.com/api/assisted-images/images/41b91e72-c33e-42ee-b80f-b5c5bbf6431a?arch=x86_64&image_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NTYwMjYzNzEsInN1YiI6IjQxYjkxZTcyLWMzM2UtNDJlZS1iODBmLWI1YzViYmY2NDMxYSJ9.1EX_VGaMNejMhrAvVRBS7PDPIQtbOOc8LtG8OukE1a4&type=minimal-iso&version=4.12
    Copy to Clipboard Toggle word wrap

  6. 下载 ISO:

    $ curl -L -s '<iso_url>' --output rhcos-live-minimal.iso 
    1
    Copy to Clipboard Toggle word wrap
    1
    <iso_url> 替换为上一步中的 ISO URL。
  7. 从下载的 rhcos-live-minimal.iso 中引导新的 worker 主机。
  8. 获取没有安装的集群中的主机列表。继续运行以下命令,直到新主机显示:

    $ curl -s "$API_URL/api/assisted-install/v2/clusters/$CLUSTER_ID" -H "Authorization: Bearer ${API_TOKEN}" | jq -r '.hosts[] | select(.status != "installed").id'
    Copy to Clipboard Toggle word wrap

    输出示例

    2294ba03-c264-4f11-ac08-2f1bb2f8c296
    Copy to Clipboard Toggle word wrap

  9. 为新主机设置 $HOST_ID 变量,例如:

    $ HOST_ID=<host_id> 
    1
    Copy to Clipboard Toggle word wrap
    1
    <host_id> 替换为上一步中的主机 ID。
  10. 运行以下命令检查主机是否已就绪:

    注意

    确保复制整个命令,包括完整的 jq 表达式。

    $ curl -s $API_URL/api/assisted-install/v2/clusters/$CLUSTER_ID -H "Authorization: Bearer ${API_TOKEN}" | jq '
    def host_name($host):
        if (.suggested_hostname // "") == "" then
            if (.inventory // "") == "" then
                "Unknown hostname, please wait"
            else
                .inventory | fromjson | .hostname
            end
        else
            .suggested_hostname
        end;
    
    def is_notable($validation):
        ["failure", "pending", "error"] | any(. == $validation.status);
    
    def notable_validations($validations_info):
        [
            $validations_info // "{}"
            | fromjson
            | to_entries[].value[]
            | select(is_notable(.))
        ];
    
    {
        "Hosts validations": {
            "Hosts": [
                .hosts[]
                | select(.status != "installed")
                | {
                    "id": .id,
                    "name": host_name(.),
                    "status": .status,
                    "notable_validations": notable_validations(.validations_info)
                }
            ]
        },
        "Cluster validations info": {
            "notable_validations": notable_validations(.validations_info)
        }
    }
    ' -r
    Copy to Clipboard Toggle word wrap

    输出示例

    {
      "Hosts validations": {
        "Hosts": [
          {
            "id": "97ec378c-3568-460c-bc22-df54534ff08f",
            "name": "localhost.localdomain",
            "status": "insufficient",
            "notable_validations": [
              {
                "id": "ntp-synced",
                "status": "failure",
                "message": "Host couldn't synchronize with any NTP server"
              },
              {
                "id": "api-domain-name-resolved-correctly",
                "status": "error",
                "message": "Parse error for domain name resolutions result"
              },
              {
                "id": "api-int-domain-name-resolved-correctly",
                "status": "error",
                "message": "Parse error for domain name resolutions result"
              },
              {
                "id": "apps-domain-name-resolved-correctly",
                "status": "error",
                "message": "Parse error for domain name resolutions result"
              }
            ]
          }
        ]
      },
      "Cluster validations info": {
        "notable_validations": []
      }
    }
    Copy to Clipboard Toggle word wrap

  11. 当上一个命令显示主机就绪时,通过运行以下命令来使用 /v2/infra-envs/{infra_env_id}/hosts/{host_id}/actions/install API 开始安装:

    $ curl -X POST -s "$API_URL/api/assisted-install/v2/infra-envs/$INFRA_ENV_ID/hosts/$HOST_ID/actions/install"  -H "Authorization: Bearer ${API_TOKEN}"
    Copy to Clipboard Toggle word wrap
  12. 当安装继续进行时,安装会为主机生成待处理的证书签名请求 (CSR)。

    重要

    您必须批准 CSR 才能完成安装。

    运行以下 API 调用以监控集群安装:

    $ curl -s "$API_URL/api/assisted-install/v2/clusters/$CLUSTER_ID" -H "Authorization: Bearer ${API_TOKEN}" | jq '{
        "Cluster day-2 hosts":
            [
                .hosts[]
                | select(.status != "installed")
                | {id, requested_hostname, status, status_info, progress, status_updated_at, updated_at, infra_env_id, cluster_id, created_at}
            ]
    }'
    Copy to Clipboard Toggle word wrap

    输出示例

    {
      "Cluster day-2 hosts": [
        {
          "id": "a1c52dde-3432-4f59-b2ae-0a530c851480",
          "requested_hostname": "control-plane-1",
          "status": "added-to-existing-cluster",
          "status_info": "Host has rebooted and no further updates will be posted. Please check console for progress and to possibly approve pending CSRs",
          "progress": {
            "current_stage": "Done",
            "installation_percentage": 100,
            "stage_started_at": "2022-07-08T10:56:20.476Z",
            "stage_updated_at": "2022-07-08T10:56:20.476Z"
          },
          "status_updated_at": "2022-07-08T10:56:20.476Z",
          "updated_at": "2022-07-08T10:57:15.306369Z",
          "infra_env_id": "b74ec0c3-d5b5-4717-a866-5b6854791bd3",
          "cluster_id": "8f721322-419d-4eed-aa5b-61b50ea586ae",
          "created_at": "2022-07-06T22:54:57.161614Z"
        }
      ]
    }
    Copy to Clipboard Toggle word wrap

  13. 可选: 运行以下命令以查看集群的所有事件:

    $ curl -s "$API_URL/api/assisted-install/v2/events?cluster_id=$CLUSTER_ID" -H "Authorization: Bearer ${API_TOKEN}" | jq -c '.[] | {severity, message, event_time, host_id}'
    Copy to Clipboard Toggle word wrap

    输出示例

    {"severity":"info","message":"Host compute-0: updated status from insufficient to known (Host is ready to be installed)","event_time":"2022-07-08T11:21:46.346Z","host_id":"9d7b3b44-1125-4ad0-9b14-76550087b445"}
    {"severity":"info","message":"Host compute-0: updated status from known to installing (Installation is in progress)","event_time":"2022-07-08T11:28:28.647Z","host_id":"9d7b3b44-1125-4ad0-9b14-76550087b445"}
    {"severity":"info","message":"Host compute-0: updated status from installing to installing-in-progress (Starting installation)","event_time":"2022-07-08T11:28:52.068Z","host_id":"9d7b3b44-1125-4ad0-9b14-76550087b445"}
    {"severity":"info","message":"Uploaded logs for host compute-0 cluster 8f721322-419d-4eed-aa5b-61b50ea586ae","event_time":"2022-07-08T11:29:47.802Z","host_id":"9d7b3b44-1125-4ad0-9b14-76550087b445"}
    {"severity":"info","message":"Host compute-0: updated status from installing-in-progress to added-to-existing-cluster (Host has rebooted and no further updates will be posted. Please check console for progress and to possibly approve pending CSRs)","event_time":"2022-07-08T11:29:48.259Z","host_id":"9d7b3b44-1125-4ad0-9b14-76550087b445"}
    {"severity":"info","message":"Host: compute-0, reached installation stage Rebooting","event_time":"2022-07-08T11:29:48.261Z","host_id":"9d7b3b44-1125-4ad0-9b14-76550087b445"}
    Copy to Clipboard Toggle word wrap

  14. 登录到集群并批准待处理的 CSR 以完成安装。

验证

  • 检查新主机是否已成功添加到集群中,状态为 Ready

    $ oc get nodes
    Copy to Clipboard Toggle word wrap

    输出示例

    NAME                           STATUS   ROLES           AGE   VERSION
    control-plane-1.example.com    Ready    master,worker   56m   v1.25.0
    compute-1.example.com          Ready    worker          11m   v1.25.0
    Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat