使用脚本创建自定义集群

注意

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

1. 创建自定义集群

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

  1. #!/bin/bash
  2. api_url='https://xxx.domain.com'
  3. api_token='token-5zgl2:tcj5nvfq67rf55r7xxxxxxxxxxx429xrwd4zx'
  4. cluster_name=''
  5. kubernetes_Version='v1.13.5-rancher1-2'
  6. network_plugin='canal'
  7. quota_backend_bytes=${quota_backend_bytes:-6442450944}
  8. auto_compaction_retention=${auto_compaction_retention:-240}
  9. ingress_provider=${ingress_provider:-nginx}
  10. ignoreDocker_Version=${ignoreDocker_Version:-true}
  11. monitoring_provider=${monitoring_provider:-metrics-server}
  12. service_NodePort_Range=${service_NodePort_Range:-'30000-32767'}
  13. create_Cluster=true
  14. add_Node=true
  15. create_cluster_data()
  16. {
  17. cat <<EOF
  18. {
  19. "amazonElasticContainerServiceConfig": null,
  20. "azureKubernetesServiceConfig": null,
  21. "dockerRootDir": "/var/lib/docker",
  22. "enableClusterAlerting": false,
  23. "enableClusterMonitoring": false,
  24. "googleKubernetesEngineConfig": null,
  25. "localClusterAuthEndpoint": {
  26. "enabled": true,
  27. "type": "/v3/schemas/localClusterAuthEndpoint"
  28. },
  29. "name": "$cluster_name",
  30. "rancherKubernetesEngineConfig": {
  31. "addonJobTimeout": 30,
  32. "addonsInclude":[ "https://raw.githubusercontent.com/rook/rook/master/cluster/examples/kubernetes/ceph/operator.yml"
  33. ],
  34. "authentication": {
  35. "strategy": "x509|webhook",
  36. "type": "/v3/schemas/authnConfig"
  37. },
  38. "authorization": {
  39. "type": "/v3/schemas/authzConfig"
  40. },
  41. "bastionHost": {
  42. "sshAgentAuth": false,
  43. "type": "/v3/schemas/bastionHost"
  44. },
  45. "cloudProvider": {
  46. "type": "/v3/schemas/cloudProvider"
  47. },
  48. "ignoreDockerVersion": "$ignoreDocker_Version",
  49. "ingress": {
  50. "provider": "$ingress_provider",
  51. "type": "/v3/schemas/ingressConfig"
  52. },
  53. "kubernetesVersion": "$kubernetes_Version",
  54. "monitoring": {
  55. "provider": "$monitoring_provider",
  56. "type": "/v3/schemas/monitoringConfig"
  57. },
  58. "network": {
  59. "options": {
  60. "flannel_backend_type": "vxlan"
  61. },
  62. "plugin": "$network_plugin",
  63. "type": "/v3/schemas/networkConfig"
  64. },
  65. "restore": {
  66. "restore": false,
  67. "type": "/v3/schemas/restoreConfig"
  68. },
  69. "services": {
  70. "etcd": {
  71. "backupConfig": {
  72. "enabled": true,
  73. "intervalHours": 12,
  74. "retention": 6,
  75. "s3BackupConfig": null,
  76. "type": "/v3/schemas/backupConfig"
  77. },
  78. "creation": "12h",
  79. "extraArgs": {
  80. "auto-compaction-retention": "$auto_compaction_retention",
  81. "election-timeout": "5000",
  82. "heartbeat-interval": "500",
  83. "quota-backend-bytes": "$quota_backend_bytes"
  84. },
  85. "retention": "72h",
  86. "snapshot": false,
  87. "type": "/v3/schemas/etcdService"
  88. },
  89. "kubeApi": {
  90. "alwaysPullImages": false,
  91. "podSecurityPolicy": false,
  92. "serviceNodePortRange": "$service_NodePort_Range",
  93. "type": "/v3/schemas/kubeAPIService"
  94. },
  95. "kubeController": {
  96. "extraArgs": {
  97. "node-monitor-grace-period": "20s",
  98. "node-monitor-period": "5s",
  99. "node-startup-grace-period": "30s",
  100. "pod-eviction-timeout": "1m"
  101. },
  102. "type": "/v3/schemas/kubeControllerService"
  103. },
  104. "kubelet": {
  105. "extraArgs": {
  106. "eviction-hard": "memory.available<300Mi,nodefs.available<10%,imagefs.available<15%,nodefs.inodesFree<5%",
  107. "kube-api-burst": "30",
  108. "kube-api-qps": "15",
  109. "kube-reserved": "memory=250Mi",
  110. "max-open-files": "2000000",
  111. "max-pods": "250",
  112. "network-plugin-mtu": "1500",
  113. "pod-infra-container-image": "rancher/pause:3.1",
  114. "registry-burst": "10",
  115. "registry-qps": "0",
  116. "serialize-image-pulls": "false",
  117. "sync-frequency": "3s",
  118. "system-reserved": "memory=250Mi"
  119. },
  120. "failSwapOn": false,
  121. "type": "/v3/schemas/kubeletService"
  122. },
  123. "kubeproxy": {
  124. "type": "/v3/schemas/kubeproxyService"
  125. },
  126. "scheduler": {
  127. "type": "/v3/schemas/schedulerService"
  128. },
  129. "type": "/v3/schemas/rkeConfigServices"
  130. },
  131. "sshAgentAuth": false,
  132. "type": "/v3/schemas/rancherKubernetesEngineConfig"
  133. }
  134. }
  135. EOF
  136. }
  137. curl -k -X POST \
  138. -H "Authorization: Bearer ${api_token}" \
  139. -H "Content-Type: application/json" \
  140. -d "$(create_cluster_data)" $api_url/v3/clusters

2. 生成注册命令

复制并保存以下内容为脚本文件,修改前三行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

3. 获取主机注册命令

复制并保存以下内容为脚本文件,修改前三行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