-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx-custom-cluster-create.sh-OBSOLETE
executable file
·45 lines (45 loc) · 3.32 KB
/
x-custom-cluster-create.sh-OBSOLETE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
##
###Set the authentication#####
RANCHER_URL="https://rancher.domain.com" ##Modify
CATTLE_ACCESS_KEY="token-here" ##Modify
CATTLE_SECRET_KEY="secret-here" ##Modify
APITOKEN="$CATTLE_ACCESS_KEY:$CATTLE_SECRET_KEY"
##Adding Cluster Data####
CLUSTER_NAME="cluster-$((RANDOM % 90000 + 10000))"
#RKE2_K8S_VERSION=$(curl -s https://api.github.com/repos/rancher/rke2/releases/latest | grep -oP '"tag_name": "\K[^"]+' | head -1)
RKE2_K8S_VERSION="kubernetes-version-here" ##Modify
###Setting Authentication for Node####
SSH_USERNAME="username-here" ##Modify
read -r SSH_PASSWORD < /root/password.txt ##Modify
###
echo "Creating CLuster with given details"
CREATE_CLUSTER_RESPONSE=$(curl -k -u "${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}" -s $RANCHER_URL/v1/provisioning.cattle.io.clusters -H 'content-type: application/json' --data-raw '{"type":"provisioning.cattle.io.cluster","metadata":{"namespace":"fleet-default","name":"'$CLUSTER_NAME'"},"spec":{"rkeConfig":{"chartValues":{"rke2-calico":{}},"upgradeStrategy":{"controlPlaneConcurrency":"1","controlPlaneDrainOptions":{"deleteEmptyDirData":true,"disableEviction":false,"enabled":false,"force":false,"gracePeriod":-1,"ignoreDaemonSets":true,"skipWaitForDeleteTimeoutSeconds":0,"timeout":120},"workerConcurrency":"1","workerDrainOptions":{"deleteEmptyDirData":true,"disableEviction":false,"enabled":false,"force":false,"gracePeriod":-1,"ignoreDaemonSets":true,"skipWaitForDeleteTimeoutSeconds":0,"timeout":120}},"machineGlobalConfig":{"cni":"calico","disable-kube-proxy":false,"etcd-expose-metrics":false},"machineSelectorConfig":[{"config":{"protect-kernel-defaults":false}}],"etcd":{"disableSnapshots":false,"s3":null,"snapshotRetention":5,"snapshotScheduleCron":"0 */5 * * *"},"registries":{"configs":{},"mirrors":{}},"machinePools":[]},"machineSelectorConfig":[{"config":{}}],"kubernetesVersion":"'$RKE2_K8S_VERSION'","defaultPodSecurityAdmissionConfigurationTemplateName":"","localClusterAuthEndpoint":{"enabled":false,"caCerts":"","fqdn":""}}}')
##
sleep 10
echo "Fetching CLuster Id"
CLUSTERID=$(curl -s ${RANCHER_URL}/v3/clusters?name=${CLUSTER_NAME} -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" --insecure | jq -r .data[].id)
echo "CLuster ID is $CLUSTERID"
###
sleep 10
echo "Fetching Registration URL"
REGISTRATION_RESPONSE=$(curl -k -u "${CATTLE_ACCESS_KEY}:${CATTLE_SECRET_KEY}" --request GET $RANCHER_URL/v3/clusters/$CLUSTERID/clusterregistrationtokens --header 'Content-Type: application/json')
##
sleep 10
REGISTRATION_LINK=$(echo "${REGISTRATION_RESPONSE}" | jq -r '.data[0].insecureNodeCommand')
REGISTRATION_URL=""$REGISTRATION_LINK" --worker --controlplane --etcd"
echo "Registration URL is "$REGISTRATION_URL""
echo "Executing Registration URL in Node"
#####
input_file="ipaddress.txt"
remote_file=/etc/rancher/rke2/rke2.yaml
mkdir $CLUSTER_NAME
localpath=$CLUSTER_NAME/
mapfile -t ip_addresses < ipaddress.txt
for ip_address in "${ip_addresses[@]}"; do
echo "Running remote SSH command on $ip_address"
sshpass -p "$SSH_PASSWORD" ssh -v -o StrictHostKeyChecking=no "$SSH_USERNAME"@"$ip_address" "$REGISTRATION_URL"
sleep 30
sshpass -p "$SSH_PASSWORD" scp "$SSH_USERNAME"@"$ip_address":"$remote_file" "$localpath"
echo "RKE2 Cluster with name $CLUSTER_NAME created successfully on node $ip_address and the kube config file has been saved as $CLUSTER_NAME/config.yaml"
done