Skip to content

Instantly share code, notes, and snippets.

@superseb
Last active June 4, 2024 20:16
Show Gist options
  • Save superseb/c363247c879e96c982495daea1125276 to your computer and use it in GitHub Desktop.
Save superseb/c363247c879e96c982495daea1125276 to your computer and use it in GitHub Desktop.
Add custom node to Rancher 2.0 (from v2.0.0-alpha26 and up)
#!/bin/bash
docker run -d -p 80:80 -p 443:443 --name rancher-server rancher/rancher:latest
while ! curl -k https://localhost/ping; do sleep 3; done
# Login
LOGINRESPONSE=`curl -s 'https://127.0.0.1/v3-public/localProviders/local?action=login' -H 'content-type: application/json' --data-binary '{"username":"admin","password":"admin"}' --insecure`
LOGINTOKEN=`echo $LOGINRESPONSE | jq -r .token`
# Change password
curl -s 'https://127.0.0.1/v3/users?action=changepassword' -H 'content-type: application/json' -H "Authorization: Bearer $LOGINTOKEN" --data-binary '{"currentPassword":"admin","newPassword":"thisisyournewpassword"}' --insecure
# Create API key
APIRESPONSE=`curl -s 'https://127.0.0.1/v3/token' -H 'content-type: application/json' -H "Authorization: Bearer $LOGINTOKEN" --data-binary '{"type":"token","description":"automation"}' --insecure`
# Extract and store token
APITOKEN=`echo $APIRESPONSE | jq -r .token`
# Set server-url
RANCHER_SERVER=https://your_rancher_server.com
curl -s 'https://127.0.0.1/v3/settings/server-url' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" -X PUT --data-binary '{"name":"server-url","value":"'$RANCHER_SERVER'"}' --insecure > /dev/null
# Create cluster
CLUSTERRESPONSE=`curl -s 'https://127.0.0.1/v3/cluster' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" --data-binary '{"dockerRootDir":"/var/lib/docker","enableNetworkPolicy":false,"type":"cluster","rancherKubernetesEngineConfig":{"addonJobTimeout":30,"ignoreDockerVersion":true,"sshAgentAuth":false,"type":"rancherKubernetesEngineConfig","authentication":{"type":"authnConfig","strategy":"x509"},"network":{"type":"networkConfig","plugin":"canal"},"ingress":{"type":"ingressConfig","provider":"nginx"},"monitoring":{"type":"monitoringConfig","provider":"metrics-server"},"services":{"type":"rkeConfigServices","kubeApi":{"podSecurityPolicy":false,"type":"kubeAPIService"},"etcd":{"snapshot":false,"type":"etcdService","extraArgs":{"heartbeat-interval":500,"election-timeout":5000}}}},"name":"yournewcluster"}' --insecure`
# Extract clusterid to use for generating the docker run command
CLUSTERID=`echo $CLUSTERRESPONSE | jq -r .id`
# Create token
curl -s 'https://127.0.0.1/v3/clusterregistrationtoken' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" --data-binary '{"type":"clusterRegistrationToken","clusterId":"'$CLUSTERID'"}' --insecure > /dev/null
# Set role flags
ROLEFLAGS="--etcd --controlplane --worker"
# Generate nodecommand
AGENTCMD=`curl -s 'https://127.0.0.1/v3/clusterregistrationtoken?id="'$CLUSTERID'"' -H 'content-type: application/json' -H "Authorization: Bearer $APITOKEN" --insecure | jq -r '.data[].nodeCommand' | head -1`
# Concat commands
DOCKERRUNCMD="$AGENTCMD $ROLEFLAGS"
# Echo command
echo $DOCKERRUNCMD
@superseb
Copy link
Author

rancher/rancher-agent is coupled with rancher/rancher and will be automatically filled in in the generated AGENTCOMMAND.

@ggMartinez
Copy link

ggMartinez commented Jan 31, 2019

Hi superseb! First of all, thank you for this work.

Did you get some documentation about Rancher 2.x API? Because I'm looking for it since long, and haven't found a thing yet.

I'm trying to use this script, but the server name never get filled in rancher. The curl for "Set server-url" don't fails, and in the output show the stuff seemingly right, but the value seems to set empty, and, for example, when trying to get nodeCommand, the parameter for "--server" is empty.

Do you have any idea why this problem could be happening?

Very best regards.

Gonzalo

@mehdi-sh-hm
Copy link

I think in Generate nodecommand section query param is clusterId not id
thanks

@jbw815
Copy link

jbw815 commented Jun 22, 2021

How do I login/authenticate when rancher is configured with github authentication? I tried this: /v3-public/githubProviders/github?action=login
I got this to work for Active Directory: https://$RancherENV/v3-public/activeDirectoryProviders/activedirectory?action=login

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment