One-Click Kubernetes Cluster Creation on Rancher

Previously, I was creating Kubernetes clusters on Rancher manually. Since I frequently create and delete clusters for testing purposes, doing this by hand every time gets tedious quickly.
In this post, I’ll walk through creating a Rancher cluster and the follow-up steps needed afterward.

Creating a Cluster

To deploy a new cluster on a fresh Rancher installation, the following steps are required:

  1. Create a Cloud Credential, which will be needed by the Node Template later.
  2. Create Kubernetes Master and Worker Node Templates. As of Rancher 2.3.5, you can manually select configuration options such as Datacenter, Datastore, Folder, and Network.
  3. Create a Cluster.
  4. (Optional) Configure the Kubernetes Cloud Provider.

This is a repetitive process, so I tried to codify these steps using Terraform — Infrastructure as Code. I’ve saved the code on GitHub: https://github.com/chengqing-su/vsphere-cluster-on-rancher

How to Use

First, clone the repository.

1
git clone git@github.com:chengqing-su/vsphere-cluster-on-rancher.git

Then, create a new file terraform/terraform.tfvars, and fill in the following content:

1
2
3
4
5
6
7
8
9
10
11
12
name = "<YOUR-CLUSTER-NAME>"
rancher_url = "<YOUR-RANCHER-URL>"
rancher_access_key = "<YOUR-RANCHER-ACCESS-KEY>"
rancher_secret_key = "<YOUR-RANCHER-SECRET-KEY>"

vcenter_server = "<YOUR-VCENTER-IP-OR-DOMAIN>"
vcenter_username = "<YOUR-VCENTER-USERNAME>"
vcenter_password = "<YOUR-VCENTER-PASSWORD>"
vcenter_datacenter = "<YOUR-VCENTER-DATACENTER>" #absolute path, like /datacenter
vcenter_datastore = "<YOUR-VCENTER-DATASTORE>" #absolute path, like /<your-datacenter-name>/datastore/<your-datastore-name>"
vcenter_folder = "<YOUR-VCENTER-VM-FOLDER>" #absolute path, like /<your-datacenter-name>/vm/<your-vm-folder>"
vcenter_resourcepool = "<YOUR-VCENTER-RESOURCE-POOL>" # absolute path, like /<your-datacenter-name>/host/<your-cluster-name>/Resources/<your-resource-pool>

Then, run the automation script auto/deploy to deploy with a single command.

When the cluster is no longer needed, run auto/destory to tear it down with a single command.

After Creating the Cluster

Once a cluster is up and running, a few follow-up steps can improve the experience.

Create a Default Storage Class

PersistentVolumes (PVs) can be provisioned in two ways: administrators can create them manually, or they can be dynamically provisioned through a StorageClass. As a result, creating a StorageClass is often necessary to make storage resources available.

Below is the StorageClass manifest:

1
2
3
4
5
6
7
8
9
10
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
annotations:
storageclass.kubernetes.io/is-default-class: "true"
name: vsphere
parameters:
datastore: <YOUR-DATASTORE>
diskformat: thin
provisioner: kubernetes.io/vsphere-volume

Apply the manifest above with kubectl to create a default StorageClass.

Configure a Load Balancer

When creating a Service of type LoadBalancer, I found that it would never succeed.
To resolve this, you can install an application called metallb from the App catalog. After installation, you also need to create a ConfigMap with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb
name: metallb-config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 192.168.0.10-192.168.0.50 #change this address range to your own