Using NFS Volumes in PCF instances

Can I create files in PCF instances where it should read a file, create more files, create folder and after that ftp that files to another remote server. The answer is no with the default storage in an instance.

The solution to play with file handling in PCF Cloud is using NFS Volume Services. In PCF, a volume service provides a volume so your app can read or write to a reliable, non-ephemeral file system.

Developers want their applications to mount one or more volumes in order to write to a reliable, non-ephemeral file system. By integrating with service brokers and the Cloud Foundry runtime, providers can offer these services to developers through an automated, self-service, and on-demand user experience.

How to create Volume Services and mount to PCF instance

  1. cf marketplace -s nfs (this will list down nfs services are enabled)
  2. cf create-service nfs existing nfsservice -c input.json (this will create new service named nfsservice)
input.json {
“share”: “10.10.10.100/PCFCloud_Volume1”
}

3. cf bind-service nfsclientapp nfsservice -c bind.json (binds the nfs service with nfs client application. and specify a read write volume to be mounted to the path /var/vol1)

bind.json {
“mount”: “/var/vol1”
}

4. cf restage nfsclientapp

How to use this mounted volume in your application?

You can create a simple boot application and add this below code to your app and push this to PCF. The below code shows example for creating new file, deleting file, check if file exists, create directory.

private boolean filehandling() {
String filename = “test.txt”;
File file = new File(“/var/var1/” + filename);
try {
// create a new file
if (file.createNewFile()) {
return true;
}
// check file exists
if (file.exists()) {
return true;
}
// file delete
if (file.delete()) {
return true;
}
// return list
File file1 = new File(“/var/vol1/”);
String[] arr = file1.list();
System.out.println(arr);
//create directory
if(file1.mkdir()) {
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

Adding the Service in your application Manifest file.

Add the services in the manifest file of your application.

applications:
- name: nfsclientapp
stack: cflinuxfs3
buildpack: java_buildpack_offline
instances: 1
random-route: true
path: /target/globalapi-0.0.1-SNAPSHOT.jar
memory: 1GB
services:
- nfsservice