Using Rancher API to create EKS, AKS and GKE
Tags: kubernetes, rancher, aws, eks
Published: 2022-04-15 13:56:29


Rancher can be used to create Kubernetes clusters on the public cloud through the Rancher web portal or API.

You need to create an [API ](https://rancher.com/docs/rancher/v2.5/en/user-settings/api-keys/)token with no scope to be able to create new clusters. 

In the below commands update your cloud credentials depending on the cloud provider with the credential id  "cattle-global-data:cc-XXXX"

**Create EKS cluster**
```
curl -k -s 'https://rancher.example.com/v3/cluster' -H 'content-type: application/json' -H "Authorization: Bearer $TOKEN" --data-binary '{"dockerRootDir":"/var/lib/docker","enableClusterAlerting":false,"enableClusterMonitoring":false,"enableNetworkPolicy":false,"windowsPreferedCluster":false,"type":"cluster","name":"test","eksConfig":{"imported":false,"amazonCredentialSecret":"cattle-global-data:cc-XXXX","displayName":"test","kmsKey":"","kubernetesVersion":"1.21","loggingTypes":[],"nodeGroups":[{"desiredSize":2,"diskSize":20,"ec2SshKey":"","gpu":false,"imageId":"","instanceType":"t3.medium","labels":{},"maxSize":2,"minSize":2,"nodegroupName":"test","requestSpotInstances":false,"resourceTags":{},"spotInstanceTypes":[],"subnets":[],"tags":{},"type":"nodeGroup","userData":"","launchTemplate":null,"version":"1.21"}],"privateAccess":false,"publicAccess":true,"publicAccessSources":[],"region":"eu-central-1","secretsEncryption":false,"securityGroups":[],"serviceRole":"","subnets":[],"tags":{},"type":"eksclusterconfigspec"},"labels":{}}'
```

**Create AKS cluster**

```
curl -k -s 'https://rancher.example.com/v3/cluster' -H 'content-type: application/json' -H "Authorization: Bearer $TOKEN" --data-binary '{"dockerRootDir":"/var/lib/docker","enableClusterAlerting":false,"enableClusterMonitoring":false,"enableNetworkPolicy":false,"windowsPreferedCluster":false,"type":"cluster","name":"test","aksConfig":{"imported":false,"azureCredentialSecret":"cattle-global-data:cc-XXXXX","clusterName":"test","dnsPrefix":"test","kubernetesVersion":"1.21.9","linuxAdminUsername":"azureuser","loadBalancerSku":"Standard","networkPlugin":"kubenet","nodePools":[{"availabilityZones":["1","2","3"],"count":1,"enableAutoScaling":false,"maxPods":110,"mode":"System","name":"agentpool","orchestratorVersion":"1.21.9","osDiskSizeGB":128,"osDiskType":"Managed","osType":"Linux","type":"aksnodepool","vmSize":"Standard_DS2_v2","isNew":true}],"privateCluster":false,"resourceGroup":"test","resourceLocation":"northeurope","tags":{},"type":"aksclusterconfigspec"},"labels":{}}'
```

**Create GKE cluster**
```
curl -k -s 'https://rancher.example.com/v3/cluster' -H 'content-type: application/json' -H "Authorization: Bearer $TOKEN" --data-binary '{"dockerRootDir":"/var/lib/docker","enableClusterAlerting":false,"enableClusterMonitoring":false,"enableNetworkPolicy":false,"windowsPreferedCluster":false,"type":"cluster","name":"test","gkeConfig":{"imported":false,"clusterAddons":{"horizontalPodAutoscaling":true,"httpLoadBalancing":true,"networkPolicyConfig":false},"clusterIpv4Cidr":"","clusterName":"test","description":"","enableKubernetesAlpha":false,"googleCredentialSecret":"cattle-global-data:cc-XXXX","ipAllocationPolicy":{"clusterIpv4CidrBlock":null,"clusterSecondaryRangeName":null,"createSubnetwork":false,"nodeIpv4CidrBlock":null,"servicesIpv4CidrBlock":null,"servicesSecondaryRangeName":null,"subnetworkName":null,"useIpAliases":true,"clusterIpv4Cidr":""},"kubernetesVersion":"1.21.6-gke.1500","labels":{},"locations":["europe-west3-a","europe-west3-b","europe-west3-c"],"loggingService":"logging.googleapis.com/kubernetes","maintenanceWindow":"","masterAuthorizedNetworks":{"enabled":false},"monitoringService":"monitoring.googleapis.com/kubernetes","network":"default","networkPolicyEnabled":false,"nodePools":[{"autoscaling":{"enabled":false,"maxNodeCount":null,"minNodeCount":null},"config":{"diskSizeGb":100,"diskType":"pd-standard","imageType":"COS","labels":{},"localSsdCount":0,"machineType":"n1-standard-2","oauthScopes":["https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append"],"preemptible":false,"taints":null,"tags":null},"initialNodeCount":1,"management":{"autoRepair":true,"autoUpgrade":true},"maxPodsConstraint":110,"name":"test","version":"1.21.6-gke.1500","type":"gkenodepoolconfig","isNew":true}],"privateClusterConfig":{"enablePrivateEndpoint":false,"enablePrivateNodes":false,"masterIpv4CidrBlock":null},"projectID":"XXXXXX","region":"europe-west3","subnetwork":"default","type":"gkeclusterconfigspec","zone":""},"labels":{},"annotations":{}}'
```

**Get the information about the cluster**, ex: test
```
curl -k -s -u $TOKEN -H 'Content-Type: application/json' 'https://rancher.example.com/v3/clusters?name=test' | jq -r .data[]
```

**Delete the Cluster**
```
curl -k -s -u $TOKEN -X DELETE -H 'Accept: application/json' 'https://rancher.example.com/v1/provisioning.cattle.io.clusters/fleet-default/CLUSTER-ID'
```