Start RStudio in kubernetes with mounted volume

11/23/2018

I try to start RStudio in docker container via kubernetes. All objects are created, but when I try to open rstudio using such commands in Ubuntu 18:

kubectl create -f rstudio-ing.yml    
IP=$(minikube ip)
xdg-open http://$IP/rstudio/

there is error: #RStudio initialization error: unable connect to service.

Usual docker command works fine:

docker run -d -p 8787:8787 -e PASSWORD=123 -v /home/aabor/r-projects:/home/rstudio aabor/rstudio

The same intended operation in kubernetes fails.

rstudio-ing.yml file creates all objects well. RStudio is accessible if I do not mount any folder. But if I add folder mounts it produces an error. Any suggestions?

The content of the rstudio-ing.yml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: r-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /rstudio/
        backend:
          serviceName: rstudio
          servicePort: 8787

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rstudio
spec:
  replicas: 1
  selector:
    matchLabels:
      service: rstudio
  template:
    metadata:
      labels:
        service: rstudio
        language: R
    spec:
      containers:
      - name: rstudio
        image: aabor/rstudio
        env:
        - name: PASSWORD
          value: "123"
        volumeMounts:
        - name: home-dir
          mountPath: /home/rstudio/
      volumes: 
      - name: home-dir
        hostPath: 
        #RStudio initialization error: unable connect to service
          path: /home/aabor/r-projects 

---

apiVersion: v1
kind: Service
metadata:
  name: rstudio
spec:
  ports:
  - port: 8787
  selector:
    service: rstudio

This is pod description:

    Name:           rstudio-689c4fd6c8-fgt7w
    Namespace:      default
    Node:           minikube/10.0.2.15
    Start Time:     Fri, 23 Nov 2018 21:42:35 +0300
    Labels:         language=R
                    pod-template-hash=2457098274
                    service=rstudio
    Annotations:    <none>
    Status:         Running
    IP:             172.17.0.9
    Controlled By:  ReplicaSet/rstudio-689c4fd6c8
    Containers:
    rstudio:
        Container ID:   docker://a6bdcbfdf8dc5489a4c1fa6f23fb782bc3d58dd75d50823cd370c43bd3bffa3c
        Image:          aabor/rstudio
        Image ID:       docker-pullable://aabor/rstudio@sha256:2326e5daa3c4293da2909f7e8fd15fdcab88b4eb54f891b4a3cb536395e5572f
        Port:           <none>
        Host Port:      <none>
        State:          Running
        Started:      Fri, 23 Nov 2018 21:42:39 +0300
        Ready:          True
        Restart Count:  0
        Environment:
        PASSWORD:  123
        Mounts:
        /home/rstudio/ from home-dir (rw)
        /var/run/secrets/kubernetes.io/serviceaccount from default-token-mrkd8 (ro)
    Conditions:
    Type           Status
    Initialized    True 
    Ready          True 
    PodScheduled   True 
    Volumes:
    home-dir:
        Type:          HostPath (bare host directory volume)
        Path:          /home/aabor/r-projects
        HostPathType:  
    default-token-mrkd8:
        Type:        Secret (a volume populated by a Secret)
        SecretName:  default-token-mrkd8
        Optional:    false
    QoS Class:       BestEffort
    Node-Selectors:  <none>
    Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                    node.kubernetes.io/unreachable:NoExecute for 300s
    Events:
    Type    Reason                 Age   From               Message
    ----    ------                 ----  ----               -------
    Normal  Scheduled              10s   default-scheduler  Successfully assigned rstudio-689c4fd6c8-fgt7w to minikube
    Normal  SuccessfulMountVolume  10s   kubelet, minikube  MountVolume.SetUp succeeded for volume "home-dir"
    Normal  SuccessfulMountVolume  10s   kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-mrkd8"
    Normal  Pulling                9s    kubelet, minikube  pulling image "aabor/rstudio"
    Normal  Pulled                 7s    kubelet, minikube  Successfully pulled image "aabor/rstudio"
    Normal  Created                7s    kubelet, minikube  Created container
    Normal  Started                6s    kubelet, minikube  Started container
-- Alexander
docker
kubernetes
rstudio

1 Answer

11/23/2018

You have created a service of type ClusterIP that can only be possible to access in the cluster not the outside. So to make it available outside of the cluster, change the service type LoadBalancer.

apiVersion: v1
kind: Service
metadata:
  name: rstudio
spec:
  ports:
  - port: 8787
  selector:
    service: rstudio
  type: LoadBalancer

In that case, the loadbalancer type service don't need the ingress and use the url as:

$ minikube service rstudio --url
-- Shudipta Sharma
Source: StackOverflow