:::note 注意 jq 工具可能需要单独安装,安装方法参考:jq 安装 :::

1. Rancher UI 获取创建集群参数

  1. 访问Rancher_URL/v3/clusters/,单击右上角“Create”,创建导入集群:

    image-20191213210405727

  2. 在参数填写页面中,修改以下参数:

    • dockerRootDir 默认为/var/lib/docker,如果 dockerroot 路径有修改,需要修改此配置路径;

    • enableClusterAlerting(可选) 根据需要选择是否默认开启集群告警;

    • enableClusterMonitoring(可选) 根据需要选择是否默认开启集群监控;

    • name(必填) 设置集群名称,名称具有唯一性,不能与现有集群名称相同;

  3. 配置好参数后单击Show Request

  4. 在弹出的窗口中,复制API RequestHTTP Request:{}中的内容,此内容即为创建的集群的 API 参数;

    1. #!/bin/bash
    2. api_url='https://xxx.rancher.com/v3/clusters'
    3. api_token='token-vrdkx:mvnrxxxxxxxxxnxzfx4h2gjkdtzzv97sw7brz66454'
    4. cluster_name='test-import'
    5. create_cluster_data()
    6. {
    7. cat <<EOF
    8. {
    9. "aliyunEngineConfig": null,
    10. "amazonElasticContainerServiceConfig": null,
    11. "answers": null,
    12. "azureKubernetesServiceConfig": null,
    13. "baiduEngineConfig": null,
    14. "dockerRootDir": "/var/lib/docker",
    15. "enableClusterAlerting": true,
    16. "enableClusterMonitoring": true,
    17. "googleKubernetesEngineConfig": null,
    18. "huaweiEngineConfig": null,
    19. "localClusterAuthEndpoint": null,
    20. "name": "test-import",
    21. "rancherKubernetesEngineConfig": null,
    22. "tencentEngineConfig": null,
    23. "windowsPreferedCluster": false
    24. }
    25. EOF
    26. }
    27. curl -k -X POST \
    28. -H "Authorization: Bearer ${api_token}" \
    29. -H "Content-Type: application/json" \
    30. -d "$(create_cluster_data)" $api_url/v3/clusters

2. 创建集群

  1. 保存以上代码为脚本文件,修改前三行的变量与配置中的参数(比如:dockerRootDir ),最后执行脚本。

  2. 脚本执行完成后,集群状态如下所示,其状态为Provisioning;

    image-20191213212549253

3. 创建注册命令

复制并保存以下内容为脚本文件,修改前三行api_urltokencluster_name,然后执行脚本。

  1. #!/bin/bash
  2. api_url='https://xxx.domain.com'
  3. api_token='token-5zgl2:tcj5nvfq67rf55r7xxxxxxxxxxx429xrwd4zx'
  4. cluster_name=''
  5. # 获取集群ID
  6. cluster_ID=$( curl -s -k -H "Authorization: Bearer ${api_token}" $api_url/v3/clusters | jq -r ".data[] | select(.name == \"$cluster_name\") | .id" )
  7. # 生成注册命令
  8. create_token_data()
  9. {
  10. cat <<EOF
  11. {
  12. "clusterId": "$cluster_ID"
  13. }
  14. EOF
  15. }
  16. curl -k -X POST \
  17. -H "Authorization: Bearer ${api_token}" \
  18. -H 'Accept: application/json' \
  19. -H 'Content-Type: application/json' \
  20. -d "$(create_token_data)" $api_url/v3/clusterregistrationtokens

4. 获取主机注册命令

复制并保存以下内容为脚本文件,修改前三行api_urltokencluster_name,然后执行脚本。

  1. #!/bin/bash
  2. api_url='https://xxx.domain.com'
  3. api_token='token-5zgl2:tcj5nvfq67rf55r7xxxxxxxxxxx429xrwd4zx'
  4. cluster_name=''
  5. cluster_ID=$( curl -s -k -H "Authorization: Bearer ${api_token}" $api_url/v3/clusters | jq -r ".data[] | select(.name == \"$cluster_name\") | .id" )
  6. # nodeCommand
  7. curl -s -k -H "Authorization: Bearer ${api_token}" $api_url/v3/clusters/${cluster_ID}/clusterregistrationtokens | jq -r .data[].nodeCommand
  8. # command
  9. curl -s -k -H "Authorization: Bearer ${api_token}" $api_url/v3/clusters/${cluster_ID}/clusterregistrationtokens | jq -r .data[].command