How to use SparklyR on Azure with AZTK
This document is a guide for how to create an Apache Spark cluster for an R user. In this tutorial, we install and setup Rstudio Server.
This guide does not require any knowledge about Azure or cloud infrastructure.
The goal for this tutorial is to:
- Provision a Spark cluster to use R with
- To interact with said Spark cluster with a RStudio Server
- To achieve the above goals quickly, cheaply, and easily
For this tutorial, we assume that you have the following requirements:
Setup AZTK
In your working directory, run aztk spark init
to initialize AZTK. This command will create a .aztk/
folder in your working directory. Fill out the .aztk/secrets.yaml
file with your Azure Batch and Azure Storage account secrets. We also recommend setting your ssh-key here.
For more details, see this section
For this tutorial, please copy the folder aztk/custom-scripts
into your working directory. This should be located in AZTK repo that you cloned when installing AZTK. (If you are working directly in the cloned repo, you can skip this step as the /custom-scripts
folder is already there.)
Provision your cluster
Set up your Spark cluster with the aztk/r-base Docker image and a custom script
To provision your Spark cluster for R users, you will need to edit .aztk/cluster.yaml
. You need to set the parameters docker_repo
and custom_scripts
as follows:
# .aztk/cluster.yaml
...
docker_repo: aztk/r-base:latest
custom_scripts:
- script: custom-scripts/rstudio_server.sh
runOn: master
...
This will tell AZTK to use the aztk/r-base image to build your cluster as well as configure Rstudio Server to run seamlessly after the cluster is set up. This requires custom-scripts/rstudio_server.sh
to be a valid path from your working directory
Feel free to modify the other parameters as needed.
Create cluster through command-line (recommended)
You can now run the aztk cluster create command:
aztk spark cluster create --id <my_spark_cluster> --size 10
This command will automatically use the contents that we modified in .aztk/cluster.yaml
(and .aztk/spark-defaults.conf
).
Interact with Sparklyr
After you've run your aztk spark cluster create
command, you will need to wait a few minutes for your cluster to be ready.
Once it is ready, you can use the following command to ssh into your cluster's master node. This command will also port forward the necessary ports to start using RStudio Server and the standard Spark UIs:
aztk spark cluster ssh --id <my_spark_cluster>
Once you've ssh'ed in, you'll be in the master node of your cluster. You can open up your favorite browser and go to localhost:8787
to use RStudio Server. We have created a default user 'rstudio' with 'rstudio' as the password.
Connecting to your cluster
library(sparklyr)
# Getting ip address of the master node
cluster_url <- paste0("spark://", system("hostname -i", intern = TRUE), ":7077")
sc <- spark_connect(master = cluster_url)
Once you've connected to your sparklyr, you can visit localhost:8080
to see the SparkUI and monitor the state of your cluster.
For more information about using sparklyr, here's the link.