Data ScienceOpenShiftR

R using RStudio Server with OpenShift

Introduction

[ UPDATE ] A new and improved approached to standing up a RStudio Server as a container instance can be found here.

To know R is to love R. RStudio Server is a popular IDE tool that makes the R experience even better. And OpenShift makes it even easier again. R is for Riki. This lab is another in the OpenShift MiniLabs series.

riki

Objectives

Let’s demonstrate hosting an RStudio Server instance as a container managed by OpenShift. Moreover, the guest account home directory is mapped to an external volume such that any installed packages are preserved on container restart.

r_logo-svg

Setup

Initial Attempt

This tutorial assumes you have completed the OpenShift MiniLabs installation procedure. Then refresh before continuing.

Repeat Attempt

To reset your environment to repeat this tutorial do the following:

$ cd ~/containersascode
$ ./oc-cluster-wrapper/oc-cluster up containersascode
$ oc login -u system:admin
$ oc delete persistentvolumeclaim rstudioguestclaim
$ oc delete persistentvolume rstudiovolume
$ oc login -u developer -p developer
$ oc delete project rstudio

Instructions

This demonstration begins by creating a persistent volume that can be later claimed by a container instance. This step is something typically done by an Administrator.

Create the Persistent Volume

Replace $VOLUMEPATH below with your preferred host-path location, e.g. /Users/johndoe/oc/volumes

$ oc login -u system:admin

$ oc get pv
$ oc create -f - << EOF!
apiVersion: v1
kind: PersistentVolume
metadata:
  name: rstudiovolume
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  hostPath:
    path: $VOLUMEPATH
EOF!
$ oc get pv

Create Project

Let’s create a project for our new application. The RStudio Server container needs some extra privileges so will assign that as follows:

$ oc login -u developer -p developer
$ oc new-project rstudio --display-name='RStudio Server' --description='RStudio Server' 
$ oc login -u system:admin
$ oc adm policy add-scc-to-user anyuid -z default

Create Application from Dockerfile

You can create an OpenShift container straight from a Docker file. Do the following for the pre-supplied Docker file that will build an RStudio Server instance on Centos:

$ oc login -u developer -p developer
$ wget https://bitbucket.org/emergile/MLOps/src/master/rstudio/Dockerfile
$ oc new-app . -l name='server' --name='server' 
$ oc deploy server --cancel 
$ oc expose service server

Claim the Storage

$ oc login -u developer -p developer 
$ oc project rstudio

$ oc set volume dc/server --add \
    --overwrite \
    --name=work \
    --type=persistentVolumeClaim \
    --mount-path=/home/guest \
    --claim-size=2Gi \
    --claim-name=rstudioguestclaim \
    --containers=server

Build the Image

Make sure you are in the same directory as the Dockerfile. The first build attempt will take some time. You can observe progress from the Console at https://127.0.0.1:8443/console/project/rstudio/browse/builds/server/server-2?tab=logs

$ oc login -u developer -p developer 
$ oc project rstudio
$ oc start-build server --from-dir=. 
$ oc status -v

Verify Lab Success

Once deployed, visit your new RStudio Server instance at http://server-rstudio.127.0.0.1.nip.io/ using credentials guest/guest. Try and few R commands but more interestingly install a new package, e.g. plyr.

Now restart the container by scaling the pod down and up again at: https://127.0.0.1:8443/console/project/rstudio/overview . Then revisit the RStudio Console and verify the previously installed package remains by issuing the command installed.packages() inside the RStudio console.

Trivia

Some users on  Chrome may encounter an “R Session Disconnected” problem after login to http://server-rstudio.127.0.0.1.nip.io/. The cause of this error is undetermined but a workaround is to just use an alternate browser such as Firefox instead.

Check sites such as https://www.rstudio.com/products/rstudio/ and https://support.rstudio.com/hc/en-us  for more on RStudio Server. Managing R libraries in RStudio server is described at https://support.rstudio.com/hc/en-us/articles/215733837-Managing-libraries-for-RStudio-Server

Leave a Reply