Managed Kubernetes - Programmatic Worker Node spinup

May 22, 2019 247 views
Kubernetes

I've been testing out the managed kubernetes, and it seems like one has to manually add "Node Pools" from the web control panel to add nodes to my cluster. Is there a way to do this programmatically? Basically, I want to be able to autoscale (spin up new nodes), and have it join the cluster.

2 Answers
asb MOD May 23, 2019
Accepted Answer

You can add nodes to a node pool programmatically via the DigitalOcean API. For example, you can list the node pools in your cluster by sending a GET request to https://api.digitalocean.com/v2/kubernetes/clusters/<k8s-cluster-id>/node_pools. Using cURL it would look like:

$ curl -X GET -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DO_TOKEN" \
  "https://api.digitalocean.com/v2/kubernetes/clusters/c801fb70-7086-4686-b4f0-95788435d032/node_pools" \
  | jq .

{
  "node_pools": [
    {
      "id": "c6561251-78c7-4824-830e-92610d1d7e5e",
      "name": "example-cluster-01-default-pool",
      "size": "s-1vcpu-2gb",
      "count": 3,
      "tags": [
        "k8s",
        "k8s:c801fb70-7086-4686-b4f0-95788435d032",
        "k8s:worker"
      ],
      "nodes": [
        {
          "id": "8d077fb6-8350-4564-936d-cfca4ceab9ee",
          "name": "example-cluster-01-default-pool-yqdj",
          "status": {
            "state": "running"
          },
          "created_at": "2019-05-21T15:22:36Z",
          "updated_at": "2019-05-21T15:26:02Z"
        },
        {
          "id": "a35e9b2b-d0d5-4790-976f-0ceba17479e9",
          "name": "example-cluster-01-default-pool-yqdr",
          "status": {
            "state": "running"
          },
          "created_at": "2019-05-21T15:22:36Z",
          "updated_at": "2019-05-21T15:26:02Z"
        },
        {
          "id": "cc32b6b5-5a05-49b1-b12e-8adb971ef3c9",
          "name": "example-cluster-01-default-pool-yqdb",
          "status": {
            "state": "running"
          },
          "created_at": "2019-05-21T15:22:36Z",
          "updated_at": "2019-05-21T15:26:02Z"
        }
      ]
    }
  ]
}

Now that we have the ID for the node pool, we can send a PUT request to https://api.digitalocean.com/v2/kubernetes/clusters/<k8s-cluster-id>/node_pools/<node-pool-id> adding an additional node:

$ curl -X PUT -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DO_TOKEN" \
  -d '{"name": "example-cluster-01-default-pool-yqdj","count": 4}' \
  "https://api.digitalocean.com/v2/kubernetes/clusters/c801fb70-7086-4686-b4f0-95788435d032/node_pools/c6561251-78c7-4824-830e-92610d1d7e5e" \
  | jq .

You can remove nodes using the same technique or delete a specific node if you want finer control.

Thanks a lot. Its great DO API has kubernetes endpoints

Have another answer? Share your knowledge.