Support

Custom load balancing for RStudio Server Pro

Follow

RStudio Server Pro gives you the ability to load balance by session, by user hash, or by server load. But it also gives you the ability to add your own custom load balancing script to make load balancing decisions. Specifics can be found in the RStudio Server admin guide.

First, configure RStudio Server Pro to accept custom load balancing in /etc/rstudio/load-balancer:

[config]
balancer = custom

Second, create a new bash script at:

/usr/lib/rstudio-server/bin/rserver-balancer

This script will be passed two environment variables: RSTUDIO_USERNAME — The user on behalf or which the new R session is being created. RSTUDIO_NODES — Comma separated list of the IP address and port of available nodes. The script should return the node to start the new session on using its standard output. Note that the format of the returned node should be identical to its format as passed to the script (i.e., include the IP address and port).

The example below load balances by randomly selecting an RStudio node for each new session: 

#!/usr/bin/env bash

# Assuming input is comma-separated, no spaces
# RSTUDIO_NODES="10.1.1.10:8787,10.1.1.11:8787,10.1.1.12:8787"

# reads node list into an array
IFS=',' read -r -a RSTUDIO_NODES_ARRAY <<< "$RSTUDIO_NODES"

# selects random node from array and returns it to stdout
echo "${RSTUDIO_NODES_ARRAY[$RANDOM % ${#RSTUDIO_NODES_ARRAY[@]}]}"

Comments