# Programmatically Import Cluster (No Web UI)

**URL:** https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776
**Category:** SUSE Rancher Prime
**Created:** [September 15, 2018, 7:41pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776 "2018-09-15T19:41:53Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![Warren\_Kim](https://avatars.discourse-cdn.com/v4/letter/w/839c29/32.png) [@Warren\_Kim](https://forums.suse.com/u/Warren_Kim)
#### Post date: [September 15, 2018, 7:41pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/1 "2018-09-15T19:41:54Z")

</div>

I am able to programmatically install, configure, and create a default cluster. The issue is the final step still requires someone to login into the UI to obtain the import manifest, e.g.

“curl --insecure -sfL [https://10.93.97.36/v3/import/x8ktsbm8lcwkh9plbvq8vmgj6zrwwgdchk8l792mxbppddwpjwmbhr.yaml](https://10.93.97.36/v3/import/x8ktsbm8lcwkh9plbvq8vmgj6zrwwgdchk8l792mxbppddwpjwmbhr.yaml) | kubectl apply -f -”

I cant locate this in the API or docs. Is it possible to get this information from any CLI?

---

<div class="post-metadata">

### Author: ![superseb](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/superseb/32/2424_2.png) [@superseb](https://forums.suse.com/u/superseb)
#### Post date: [September 17, 2018, 8:20am UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/2 "2018-09-17T08:20:25Z")

</div>

Using the CLI:

```auto
rancher cluster create test --import
rancher cluster import test

```

---

<div class="post-metadata">

### Author: ![Warren\_Kim](https://avatars.discourse-cdn.com/v4/letter/w/839c29/32.png) [@Warren\_Kim](https://forums.suse.com/u/Warren_Kim)
#### Post date: [September 18, 2018, 10:30pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/3 "2018-09-18T22:30:38Z")

</div>

Thanks. Rancher CLI isn’t quite non-interactive (my fault, poorly worded question), but I imagine I can ‘expect’ some of the required prompts. In any case, appreciate the help!

---

<div class="post-metadata">

### Author: ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)
#### Post date: [September 20, 2018, 4:02am UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/4 "2018-09-20T04:02:35Z")

</div>

The registration command is in `/v3/clusters/<id>/clusterRegistrationTokens`. The UI is just an API client; anything it does you can.

---

<div class="post-metadata">

### Author: ![bkalai](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@bkalai](https://forums.suse.com/u/bkalai)
#### Post date: [September 19, 2019, 2:00pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/5 "2019-09-19T14:00:09Z")

</div>

After the cluster creation using rke I do rancher clusters create --import   
I then do rancher clusters import but it prints a curl command to run how do I avoid this as this is not scriptable

---

<div class="post-metadata">

### Author: ![superseb](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/superseb/32/2424_2.png) [@superseb](https://forums.suse.com/u/superseb)
#### Post date: [September 24, 2019, 11:58pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/6 "2019-09-24T23:58:45Z")

</div>

Importing clusters requires creating a cluster (with an empty config so its treated as a imported cluster), then deploying the needed resources into the cluster so Rancher can access it. This is done by providing a YAML file with the needed resources that can be deployed using kubectl (or with curl + kubectl if you are using self signed certificates). All of these steps can be done in an automated way, the command provided for the cluster can be retrieved and be automatically executed after you configured the correct kubeconfig for your cluster for kubectl.

---

<div class="post-metadata">

### Author: ![Sergio\_Hume](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/sergio_hume/32/5434_2.png) [@Sergio\_Hume](https://forums.suse.com/u/Sergio_Hume)
#### Post date: [September 26, 2019, 8:56pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/7 "2019-09-26T20:56:06Z")

</div>

Can you give us some samples please, i’m creating a K8S cluster with terraform inside a gitlab-ci pipeline, but later i wanna administer the cluster via Rancher.

Thanks a lot

---

<div class="post-metadata">

### Author: ![bkalai](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@bkalai](https://forums.suse.com/u/bkalai)
#### Post date: [October 31, 2019, 10:29pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/8 "2019-10-31T22:29:11Z")

</div>

use rancher2 provider to create imported i.e. “rancher2\_cluster” “foo-imported”  
look for the outputs and use the appropriate command (curl for self signed kubectl for signed ssl)  
I found out that creating the cluster directly in rancher2 provider works best

---

<div class="post-metadata">

### Author: ![superseb](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/superseb/32/2424_2.png) [@superseb](https://forums.suse.com/u/superseb)
#### Post date: [November 1, 2019, 11:18am UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/9 "2019-11-01T11:18:51Z")

</div>

For CLI:

```bash
# create cluster
rancher cluster create my-imported-cluster --import
# kubectl command
rancher cluster import my-imported-cluster -q | head -1
# kubectl insecure command (self signed certificates on Rancher)
rancher cluster import my-imported-cluster -q | tail -1
# wait til cluster is ready
rancher wait my-imported-cluster

```

Terraform:

```bash
provider "rancher2" {
  api_url = "https://x.x.x.x"
  access_key = "token-xxxxx"
  secret_key = "x"
  insecure = "true"
}

resource "rancher2_cluster" "my-imported-cluster-tf" {
  name = "my-imported-cluster-tf"
}

output "kubectl-command" {
  value = ["${rancher2_cluster.my-imported-cluster-tf.cluster_registration_token.0.command}"]
}

output "insecure-kubectl-command" {
  value = ["${rancher2_cluster.my-imported-cluster-tf.cluster_registration_token.0.insecure_command}"]
}

```

Curl/bash: [https://github.com/superseb/katacoda-scenarios/blob/master/rancher2-k3s-import/setup-rancher.sh](https://github.com/superseb/katacoda-scenarios/blob/master/rancher2-k3s-import/setup-rancher.sh)

---

<div class="post-metadata">

### Author: ![kumarkung](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@kumarkung](https://forums.suse.com/u/kumarkung)
#### Post date: [March 24, 2020, 10:19am UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/10 "2020-03-24T10:19:27Z")

</div>

Hi, I want to perform below steps via api for rancher 2.x, please provide the api info

1. Add cluster (import eks cluster)
2. Create project and associate namespace to it
3. Enable monitoring for cluster and project

---

<div class="post-metadata">

### Author: ![johnmorsley](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/johnmorsley/32/5960_2.png) [@johnmorsley](https://forums.suse.com/u/johnmorsley)
#### Post date: [June 4, 2020, 8:40pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/11 "2020-06-04T20:40:49Z")

</div>

Cracking code @superseb, but I get a ‘No api\_url’. Any idea why?

---

<div class="post-metadata">

### Author: ![superseb](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/superseb/32/2424_2.png) [@superseb](https://forums.suse.com/u/superseb)
#### Post date: [June 4, 2020, 9:04pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/12 "2020-06-04T21:04:56Z")

</div>

I assume you are using the Terraform example, what Terraform version, what provider.rancher2 version and what Rancher version are you using? And what Terraform code if its not a copy paste of above.

---

<div class="post-metadata">

### Author: ![johnmorsley](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/johnmorsley/32/5960_2.png) [@johnmorsley](https://forums.suse.com/u/johnmorsley)
#### Post date: [June 4, 2020, 11:50pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/13 "2020-06-04T23:50:41Z")

</div>

Hi @superseb,

Terraform: 0.12.26  
Rancher2: 1.8.3  
AWS: 2.64.0

Code:

```
data "aws_eks_cluster" "import" {
    name = var.eks_cluster_name
}

provider "rancher2" {
    alias = "admin"
    api_url = var.rancher_url
    token_key = var.rancher_token
    insecure = true
}

resource "rancher2_cluster" "eks_cluster" {
    name = local.cluster_name
}

output "rancher2_cluster_name" {
    value = local.cluster_name
}

output "eks_cluster_registration_token" {
    value = rancher2_cluster.eks_cluster.cluster_registration_token
}

locals {
    cluster_name = format("%s-%s", data.aws_eks_cluster.import.name, var.region)
}

variable "region" {
    type = string
   default = "eu-west-2" # London
}

variable "rancher_url" {
    type = string
    default = "https://rancher.morsley.io/"
}

variable "rancher_token" {
    type = string
}

variable "eks_cluster_name" {
    type = string
    default = "eks-morsley-io"
}

```

I supply the token as output by my bootstrap code, which was:

```
rancher2_bootstrap.admin.token

```

And I get:

```
data.aws_eks_cluster.import: Refreshing state...

Error: [ERROR] No api_url provided

  on <empty> line 0:
  (source code not available)

```

Many thanks, as I’m really stuck now…

---

<div class="post-metadata">

### Author: ![rajbir\_singh](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/rajbir_singh/32/6471_2.png) [@rajbir\_singh](https://forums.suse.com/u/rajbir_singh)
#### Post date: [June 15, 2020, 6:16am UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/14 "2020-06-15T06:16:48Z")

</div>

can I enable cluster monitoring using Rancher CLI? Right now m using rancher terraform to import and enable cluster monitoring

---

<div class="post-metadata">

### Author: ![bill](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/bill/32/6505_2.png) [@bill](https://forums.suse.com/u/bill)
#### Post date: [June 26, 2020, 1:57pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/15 "2020-06-26T13:57:57Z")

</div>

Finally I made the script successfully run Rancher in Kind (kubernetes in docker)

The file [add-cluster.sh](https://github.com/ozbillwang/rancher-in-kind/blob/master/add-cluster.sh) has the most curl command you need to login.

reference: [GitHub - ozbillwang/rancher-in-kind: Scripts to start up Rancher with Kind (kubernetes in docker) cluster](https://github.com/ozbillwang/rancher-in-kind)

1. Run Rancher UI
2. create Kubernetes cluster (by kind)
3. change rancher admin’s password
4. update server url in rancher
5. create a imported cluster in rancher
6. add new kubernetes cluster’s nodes into Rancher. You should see 4 nodes are added automatically

 ![image](https://us1.discourse-cdn.com/flex022/uploads/suse/original/2X/0/0d51be07906bf9ed285fbffd4eaabf89b50dfaaa.png)

---

<div class="post-metadata">

### Author: ![tvanderwert](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/tvanderwert/32/7751_2.png) [@tvanderwert](https://forums.suse.com/u/tvanderwert)
#### Post date: [July 6, 2021, 9:06pm UTC](https://forums.suse.com/t/programmatically-import-cluster-no-web-ui/11776/16 "2021-07-06T21:06:49Z")

</div>

Hello @Warren_Kim,

It looks like you’ve included an API Key in your post. API keys are private and should not be shared on public forums such as this. If this is a key that is used in a production cluster please consider changing it immediately as it could lead to an exposure and a security risk. API keys should never be needed for support requests - so please make sure to remove them from your posts to reduce the potential for security issues.

Please let us know if you have any questions or concerns about this.  
Thanks!
