API for External Kubernetes Cluster Creation

Hello Experts,

I am looking you advices how can I create external Kubernetes cluster with API URL, Token and kubeconfig as same capability of GUI mode.

I tried using Cluster API ( https://apidocs.morpheusdata.com/v6.0.1/reference/addcluster ) it doesn’t have options to pass those values. So, I firstly created the cluster with external Kubernetes layout Id and default service plan first. And then I tired to update this cluster with additional parameters by using update cluster API ( https://apidocs.morpheusdata.com/v6.0.1/reference/updatecluster ). However, it gives me error messages and doesn’t allow to put serviceUrl and serviceToken.

{“errors”:{“serviceUrl”:“Update for field not allowed or unrecognized”

Regards,
Aung

Hi Aung,

Below is an example of the API payload that you can use to create an External Kubernetes Cluster.

CURL COMMAND
curl -XPOST "https://<your_appliance_url>/api/clusters" \
  -H "Authorization: Bearer <api_access_token>" \
  -H "Content-Type: application/json" \
  -d '{
  "cluster": {
    "type": "external-kubernetes-cluster",
    "group": {
      "id": 1
    },
    "name": "dgeks02",
    "cloud": {
      "id": 1
    },
    "layout": {
      "id": 162
    },
    "server": {
      "config": {
        "apiUrl": "<api_url>",
        "apiToken": "<api_token>",
        "serviceAccess": "<kube_config>",
        "createUser": true
      },
      "name": "dgeks02",
      "plan": {
        "id": 99,
        "code": "external-default",
        "options": {
        }
      },
      "securityGroups": [

      ],
      "visibility": "private",
      "networkDomain": null,
      "hostname": "dgeks02"
    }
  }
}'

A new line should be appended for every line in the kube_config, like this,

"apiVersion: v1 \nclusters: \n- cluster: \n    certificate-authority-data: \nLS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvakNDQWVhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJek1EY3dPdk5xOXpaSkw1NXdPQTBlN0JQeHZHZDRZT0EKUFlVPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==\n    server: https://A764AAB27ADDA33CE4ABC4FAA7973E90.gr7.us-east-1.eks.amazonaws.com \n  name: dgeks01 \ncontexts: \n- context: \n    cluster: dgeks01 \n    user: dgeks01 \n  name: dgeks01 \ncurrent-context: dgeks01 \nkind: Config \npreferences: {} \nusers: \n- name: dgeks01 \n  user: \n    token: \neyJhbGciOiJSUzI1NiIsImtpZCI6IlJQSGRQaVNkb0YtRGxCd1NEeDZLMXJDUFk2NE1CZU8xZkR4U25RPL6joLXuoAQ82uRCIuMkqdopdbTuXYXWtmFuKPgjaluCUEUod2_ohbo49HC5JoHAJ9ZykwhBjiqIX_x2Y70RpbHEWxsANmzC057L41M4HwoBp3aapNBBVSWA1moEbFOIdzM9wn13eyx8A7UdGVNQ7q6WPbrz6klp8D9W5mHf-FnG_0pJDj-JPnPNtJ5EA"

In my example I have used the 1.22 layout hence the layout.id is 162. Here is the list of ids.

Layout Options

  • External Kubernetes 1.26 [227]
  • External Kubernetes 1.25 [226]
  • External Kubernetes 1.24 [225]
  • External Kubernetes 1.23 [163]
  • External Kubernetes 1.22 [162]
  • External Kubernetes 1.21 [161]
  • External Kubernetes 1.20 [160]

You will also need to replace the plan.id with the Default External plan id in your environment.

You can also use the Morpheus CLI to generate the API payload.
https://clidocs.morpheusdata.com/en/latest/CLI-Manual.html?highlight=cluster#clusters-add

Thanks
Deepti

Thank you @dgaharwar … I will try it.

Hello @dgaharwar Deepti,

As per your guidance, I can add external kubernetes cluster via API and also see objects . However, when I try kubectl get command in Cluster > Control section of Morpheus portal, it shows error messages as below. And I also can’t create namespace via portal. Is it kubeconfig format issue when I created via API. Do you also have sample yaml file for external Kubernetes cluster integration using morpheus command --payload.

error: error loading config file “/var/opt/morpheus/morpheus-ui/workspace/kubernetes/runtime/fb0634fd-abdf-4f49-a619-5cad713322ca/config”: yaml: mapping values are not allowed in this context.

Thanks for you help.

Regards,
Aung