Provisioning an RKE2 (Rancher Kubernetes Engine 2) cluster on vSphere
Tags: rancher, kubernetes, rke, vsphere
Published: 2023-06-04 23:07:22

In this article i will walk you down with steps to create RKE2 cluster on vSphere vCenter from Rancher UI.

## Pre-requisites

- Rancher nodes needs to communicate with vSphere vCenter on port 443.
- Rancher nodes needs to communicate with RKE2 cluster nodes on port 22.


## 1. Installing packages in the template VM.

Create a new Ubuntu VM, perform below steps and later convert it into a template. We will let rancher use this template to create VM's.

Ensure below packages are installed in the template,
•	curl
•	wget
•	git
•	net-tools
•	unzip
•	apparmor-parser
•	ca-certificates
•	cloud-init
•	cloud-guest-utils
•	cloud-image-utils
•	growpart
•	cloud-initramfs-growroot
•	open-iscsi
•	openssh-server
•	open-vm-tools


```
sudo apt-get update
sudo apt-get install -y curl wget git net-tools unzip ca-certificates cloud-init cloud-guest-utils cloud-image-utils cloud-initramfs-growroot open-iscsi openssh-server open-vm-tools net-tools apparmor
```

## 2. Configure the datasource for cloud-init in the template VM.

- Rancher will use cloud-init for things like setting hostname, creating a user, running a script, etc.
- Set the datasource for cloud-init using command “dpkg-reconfigure cloud-init”.

```
sudo dpkg-reconfigure cloud-init
```

- And ensure “NoCloud” datasource is selected like below, I have deselected all other datasources since my requirement for rancher is only “NoCloud”.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eoc64fqr77i029x56sbx.png)


Verify that changes are propagated to the config file,

```
root@:~# cat /etc/cloud/cloud.cfg.d/90_dpkg.cfg
# to update this file, run dpkg-reconfigure cloud-init
datasource_list: [ NoCloud ]
root@:~#
```

## 3. Convert the VM into a template.

Run below script to scrub the VM ( similar to sysprep on Windows ).
Save below contents to a file and execute it.

```
#!/bin/bash
# Cleaning logs.
if [ -f /var/log/audit/audit.log ]; then
  cat /dev/null > /var/log/audit/audit.log
fi
if [ -f /var/log/wtmp ]; then
  cat /dev/null > /var/log/wtmp
fi
if [ -f /var/log/lastlog ]; then
  cat /dev/null > /var/log/lastlog
fi

# Cleaning udev rules.
if [ -f /etc/udev/rules.d/70-persistent-net.rules ]; then
  rm /etc/udev/rules.d/70-persistent-net.rules
fi

# Cleaning the /tmp directories
rm -rf /tmp/*
rm -rf /var/tmp/*

# Cleaning the SSH host keys
rm -f /etc/ssh/ssh_host_*

# Cleaning the machine-id
truncate -s 0 /etc/machine-id
rm /var/lib/dbus/machine-id
ln -s /etc/machine-id /var/lib/dbus/machine-id

# Cleaning the shell history
unset HISTFILE
history -cw
echo > ~/.bash_history
rm -fr /root/.bash_history

# Truncating hostname, hosts, resolv.conf and setting hostname to localhost
truncate -s 0 /etc/{hostname,hosts,resolv.conf}
hostnamectl set-hostname localhost

# Clean cloud-init
cloud-init clean -s -l
```


Now clone the VM to a template.

## 4. Add vSphere vCenter credentials in Rancher.

Login into Rancher => Click on Burger menu => Click on Cluster Management => Click on Cloud Credentials.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9atiqyfui6h4vibb9gb.png)


Click Create and Click on VMware vSphere


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rsc2b5osekvglwpumalu.png)


Enter your vSphere vCenter credentials, we will use administrator account. For granular permissions, please refer rancher documentation.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6zj4h4ocmzmm1sy45ry.png)


## 5. Create RKE2 Cluster in vSphere

Go back to Rancher homepage, Click on Create.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1r5nqa633wj2yin5jhq.png)


Ensure you toggle the switch to RKE2 as highlighted below and click on VMware vSphere.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shselmmi7khjktua1pkn.png)



Enter the details,

Pool1, we will use to create "Control Plane Nodes". Ensure you select appropriate Data Center/Resource Pool/Data Store/Folder.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ui64gyx01icrf7zpxjmm.png)


Select the Template that you created in the initial steps, along with CPU/Memory/Networks/etc like below.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g9qau0cjd32hghi09q8.png)


Add another pool for worker node,



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eaxvxm9rzo5qqsex2c4x.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh7ywucllg0uenhgz7nd.png)


Fill the information like we did above for control plane nodes.

For the sake simplicity, we will keep the default values for the cluster and Click Create,



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/of4q6ncmmjwojhiwqdtr.png)


Cluster creation in progress,


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xq4yosz3gg64ho0w7lxc.png)


Wait for the nodes to be bootstrapped and cluster creation,


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxhyhm3r4gcq48d42z9j.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lb0bq9cz8g0j0q9q1v2n.png)


If you see VM settings, rancher would have mounted an iso called user-data.iso


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju2tn5ge3afilbha8z6f.png)


If you login to one of the node and navigate to /mnt, it will have user-data and meta-data and used by cloud-init (if you remember, this is the reason, we selected NoCloud as data source for cloud-init)



```
root@vsphere-rke2-test01-pool1-4a86ea2d-tf5jq:~# cd /mnt
root@vsphere-rke2-test01-pool1-4a86ea2d-tf5jq:/mnt# ls
meta-data  user-data
root@vsphere-rke2-test01-pool1-4a86ea2d-tf5jq:/mnt# cat meta-data
hostname: vsphere-rke2-test01-pool1-4a86ea2d-tf5jq
root@vsphere-rke2-test01-pool1-4a86ea2d-tf5jq:/mnt#
root@vsphere-rke2-test01-pool1-4a86ea2d-tf5jq:/mnt# cat user-data
#cloud-config
groups:
- staff
hostname: vsphere-rke2-test01-pool1-4a86ea2d-tf5jq
runcmd:
- sh /usr/local/custom_script/install.sh
set_hostname:
- vsphere-rke2-test01-pool1-4a86ea2d-tf5jq
users:
- create_groups: false
  groups: staff
  lock_passwd: true
  name: docker
  no_user_group: true
```




## 6. Add a new worker node.

Lets add a new worker node to existing cluster.
Go to Rancher home => Click burger menu => Click on Cluster Management => Click on your cluster.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pysun34gu0ekzgssg1m5.png)


Under pool2 dedicated to worker nodes, click on Plus icon,


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7udahvi94kc18s3adfvf.png)


events on vcenter of node getting created,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vudljfh12eoqljulo54.png)


Worker node2 successfully added.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ah0jv4vyc5arfo3p1yvv.png)