One of the main reasons to use Cloud Foundry as a PaaS is to be able to leverage the off-the-shelf reusable services that the platform and its partners offer. In this post we will see what are reusable services and how application developers can manage and use them for their application.
What is a reusable service?
A reusable service is something that is needed by the applications or micro-services to perform their job. While the applications can focus on the business logic the supporting technological capabilities are provided by the reusable services. A reusable service is generic capabilities that can be used by any application. Examples of reusable services are any database e.g. postgres, mongodb. The application-logs are one of the reusable service applications used to write their application logs that can be saved for long term. Messaging technology like rabbitmq is another reusable service that applications often use. The difference between using reusable service like a database from the platform and hosting our own database is that the platform manages the reusable services. Application developers do not have to manage the database, they just use it for their application. Making the database always available to be used is the job of the platform. That takes a lot of time and tasks away from application developers, because they no longer have to do database management. This is true for all reusable services in Cloud Foundry. There are different kinds of reusable services that are heavily developed by the partners of cloud foundry. Few of them are IBM, Pivotal, SAP and many more.
What is a service instance or reusable service instance in Cloud Foundry ?
You can think of reusable service as a class and a service instance as an object of a class. Let's assume we want to use a reusable service, for example mongodb for our application. We need to create a service instance of the reusable service mongodb. A service instance is a database created by the application developer and dedicated only to the application. Each service instance has their credentials that need to be known to access them. The application developer and the application can access these credentials in two ways. First option is by binding the service instance to the application then the credentials are available in the VCAP_SERVICES environment variable. Another option is by creating a service-key manually. What is a VCAP_SERVICES environment variable and what is a service-key ? and how to use them ? Those are two different blog topics. You will find them in this blog as a separate post. That's how the application gets its own personalized database. We also need to know that many reusable services also have capabilities that a service instance can be bound to more than one application.
How to use a reusable service in Cloud Foundry ?
Now you know how you can access the credentials of a reusable service instance. Now once you know the service you want to use, you have to create a service instance then bind the instance with your application. Once the service instance is bound to the application, the service instance will expose some details/ or credentials of the service instance through service binding. After the binding is done the details will be available in a special kind of environment variable named as VCAP_SERVICES. Your application can read this environment variable and extract key-value pairs present in the VCAP_SERVICES at runtime or at the time when the application is starting . For each service instance a JSON structure is made available in VCAP_SERVICES. Please refer our post on VCAP_SERVICES and its use in Cloud Foundry Applications here.
What is a service marketplace in Cloud Foundry ?
You should be able to search and list all possible reusable services available in the Cloud Foundry. You can see available reusable service in the service marketplace, either by the UI or by issuing CLI commands. The service marketplace showcases the available reusable services. All reusable services need to register them so that they can be discovered from the service marketplace. Along with the name and description of the service the market place also shows different plans a service has to offer. A plan is a typical T-shirt size that tells what you are entitled to get in a service instance. For example, a mongodb service instance can have a plan named small that offers maximum 3 GB of space. Some of the reusable services are free of cost but many cones with a cost. For that reason the admins of a Cloud Foundry Org sometimes do not allow developers to see and create instances of a reusable service. If you do not see a few of the reusable services in the list check with your Org Admin. That means don’t think that the list you see is all that the platform has to offer you. Admin control this through the entitlements. What are entitlements? Well that's another topic for another post.
How to discover available reusable services in Cloud Foundry ? How to list all reusable services in Cloud Foundry ?
The below CLI command make is possible. You need to login to a Cloud Foundry ORG and Space to be able to use this command.
$ cf marketplace [-s SERVICE] [--no-plans]
Use the -s option if you want to see information for a specific service. When we specify the [--no-plans] option the plan information is omitted. See the Cloud Foundry documentation version of the command.
How to create a service instance?
To use a reusable service we need to create a service instance first.
$ cf create-service SERVICE PLAN SERVICE-INSTANCE [-b BROKER] [-c PARAMETERS_AS_JSON] [-t TAGS]
This command is used to create an instance of reusable service. As a service can come with different flavour the flavour called PLAN, you can specify the plan that you want. SERVICE is the service name and SERVICE-INSTANCE is the instance name you will create for your application and later bind with your application. An example makes it clear
$ cf create-service application-logs standard myapp-application-logs
Here we are creating a services instance of the application-logs service. We are going to use the standard plan and the instance name will be myapp-application-logs . See the Cloud Foundry documentation version of the command for explanation of all the available options.
How to list all service instances in Cloud Foundry ?
As you can create many instances of a reusable service and there are many reusable services, you can list all of the instance names of all reusable services in the current space.
$ cf services
How to View Details of a Service Instance in Cloud Foundry?
You can use this command if you want to see details of a specific service instance
$ cf service SERVICE_INSTANCE
When you create a service instance of any reusable service you can customize it and configure it. The details of the configuration is described in each of the reusable services. You can use command line options or provide a json in a text field in UI to provide those details at the time of service instance creation.
Example of an mysql service instance is this
$cf service mysqldb
service instance: mysqldb
description: mysql databases on demand
service broker: mysql-broker
This service is not currently shared.
Showing status of last operation from service mysqldb
status: create succeeded
There are no bound apps for this service.
If there are any applications bound to this instance that also will be shown here.
How to delete a service instance from an application in Cloud Foundry ?
Last but not the least, you can delete a service instance using this command, but you need to make sure that there are no application bound to the service instance.
$ cf delete-service SERVICE-INSTANCE
In this post today we have learned what are reusable services in Cloud Foundry and how to list them, search them, and manage them.