Skip to main content

Rancher is a powerful open-source platform that simplifies the deployment, management, and scaling of Kubernetes clusters across multiple environments. One of Rancher’s key features is its comprehensive API, which allows you to automate the creation and management of Kubernetes clusters. In this post, we’ll explore how you can use the Rancher API to automate the creation of a Kubernetes cluster, providing a step-by-step guide to get you started.

Why Automate Kubernetes Cluster Creation?

Automating the creation of Kubernetes clusters using Rancher API offers several benefits:

  • Consistency: Ensures that clusters are created with the same configuration every time.
  • Efficiency: Speeds up the deployment process, especially when managing multiple clusters across different environments.
  • Scalability: Simplifies the process of scaling up infrastructure by automating repetitive tasks.
  • Integration: Enables integration with CI/CD pipelines and other automation tools.

Prerequisites

Before you begin, ensure that you have the following in place:

  1. Rancher Server: A running Rancher server instance (Rancher version 2.x or higher).
  2. API Access: API access to the Rancher server, including an API token for authentication.
  3. HTTP Client: A tool to make HTTP requests, such as curl, Postman, or any programming language that supports HTTP requests (e.g., Python, Node.js).
  4. Cluster Configuration: The desired configuration for your Kubernetes cluster (e.g., number of nodes, node roles, cloud provider).

Step 1: Obtain an API Token

The first step in automating cluster creation is to obtain an API token from Rancher. This token will be used to authenticate your API requests.

  1. Log in to the Rancher UI.
  2. Navigate to User & Authentication > API & Keys.
  3. Click on Add Key.
  4. Give your key a name and click Create.
  5. Save the generated API token, as you’ll need it for the API requests.

Step 2: Define the Cluster Configuration

Next, you need to define the configuration of the Kubernetes cluster you want to create. This includes details like the cluster name, cloud provider, node pools, and networking options.

Here’s an example of a JSON configuration for an Amazon EKS cluster:

json
{
"type": "cluster",
"eksConfig": {
"region": "us-west-2",
"kubernetesVersion": "1.21",
"nodeGroups": [
{
"desiredSize": 3,
"instanceType": "t3.medium",
"name": "worker-group",
"diskSize": 20
}
]
},
"name": "my-eks-cluster"
}

For other cloud providers or custom configurations, the structure of the JSON payload may vary. Rancher’s API documentation provides detailed examples for different types of clusters (e.g., GKE, AKS, custom on-prem clusters).

Step 3: Create the Cluster via Rancher API

To create the cluster, you’ll send an HTTP POST request to the Rancher API with the cluster configuration JSON.

Here’s an example using curl:

bash
curl -k -X POST \
-H "Authorization: Bearer <your_api_token>" \
-H "Content-Type: application/json" \
-d '{
"type": "cluster",
"eksConfig": {
"region": "us-west-2",
"kubernetesVersion": "1.21",
"nodeGroups": [
{
"desiredSize": 3,
"instanceType": "t3.medium",
"name": "worker-group",
"diskSize": 20
}
] },
"name": "my-eks-cluster"
}'
\
https://<rancher_server_url>/v3/clusters

Replace <tyour_api_token> with your actual API token and <rancher_server_url> with your Rancher server URL.

This command will initiate the creation of a new Kubernetes cluster as defined in the JSON payload. The Rancher API will return a response containing details about the newly created cluster, including its status and cluster ID.

Step 4: Monitor the Cluster Creation Process

After sending the request, you can monitor the cluster creation process using the Rancher API. To do this, you can periodically check the status of the cluster by sending a GET request:

bash
curl -k -X GET \
-H "Authorization: Bearer <your_api_token>" \
https://<rancher_server_url>/v3/clusters/<cluster_id>

Replace <cluster_id> with the ID of the cluster you obtained from the creation response. This will return the current status of the cluster, such as provisioning, active, or failed.

Step 5: Configure Node Pools and Add Nodes (Optional)

If you’re using a custom cluster or want more control over node pools, you can further automate the process by creating node pools and adding nodes via the Rancher API.

Here’s an example JSON payload to create a node pool:

json
{
"type": "nodePool",
"clusterId": "<cluster_id>",
"nodeTemplateId": "<node_template_id>",
"quantity": 3,
"nodeTaints": [],
"hostnamePrefix": "worker-node"
}

You would send this JSON in a POST request to:

bash
https://<rancher_server_url>/v3/nodePools

Again, replace <cluster_id> and <node_template_id> with the respective IDs for your cluster and node template.

Step 6: Automate the Workflow with Scripts or CI/CD

To fully automate the creation process, you can integrate these API calls into a shell script, Python script, or a CI/CD pipeline. This allows you to trigger cluster creation as part of your deployment processes, ensuring a seamless and repeatable workflow.

For example, using Python and the requests library, you could write a script that sends these API requests and handles responses automatically. This script could be triggered by a CI/CD tool like Jenkins, GitLab CI, or GitHub Actions, making cluster creation a fully automated process.

Conclusion

Automating Kubernetes cluster creation using the Rancher API empowers you to deploy and manage clusters efficiently, ensuring consistency, scalability, and integration with your existing automation workflows. Whether you’re managing clusters across multiple environments or looking to streamline your CI/CD processes, Rancher’s API provides the flexibility and control you need to manage Kubernetes at scale.

By following the steps outlined in this post, you can start automating your Kubernetes deployments, freeing up time for other critical tasks and reducing the potential for human error. Happy automating!