To understand what is service binding we first need to know any one of the two: reusable services or User provided services. Feel free to browse through this blog site for a post on reusable services .
Contents
Service Binding in Cloud Foundry
Once a service instance is created it needs to be bound to the application for which the service is created. Service binding makes the information or credentials needed by the application to access the service instance. Once the service instance is bound to the application, the service instance will expose some details and 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.
If you want to know what VCAP_SERVICES is, please refer our blog on VCAP_SERVICES environment variable and its use in cloud foundry applications
Applications can access VCAP_SERVICES as an environment variable and read information present from the VCAP_SERVICES at runtime or deployment time. If the information is changed we need to rebind the service instance to the application. It will update the VCAP_SERVICES environment variable. That does not make the application read the new information though. Many applications read environment variables at the time of application deployment and start up. That is why it’s always recommended to restart the application after a rebind operation. On the other hand, we can also do unbind. Be careful when you unbind a service instance from a running application. The application can start failing and even crash if the logic in the application is not written in such a way that the application will survive and handle the unavailability of a reusable backing service gracefully when the required reusable service instance is not bound.
Bind a service instance to an application in Cloud Foundry
There are two ways the binding can happen. Through CLI Commands explicitly binding an service instance to an application. The application programmer is generally doing this task. Another option is through the manifest file. We can specify the service instance names an application needs to be bound to in the manifest file. When the application will be deployed using the manifest the binding is also taken care of by the platform.
This command is used to bind the service instance to the application.
$ cf bind-service YOUR-APP-NAME YOUR-INSTANCE-NAME [-c PARAMETERS_AS_JSON] [--binding-name BINDING_NAME]
This example shows that the application myapp is being bound with the service instance myapp-application-logs . myapp-application-logs is an instance of reusable service application-logs that drain the logs applications prints to the service and save it for future exploration.
$ cf bind-service myapp myapp-application-logs
Once a service instance is bound to your application , the service exposes few details about itself through the VCAP_SERVICS environment variable, these details can be accessed by the application to communicate with the service instance. Credentials and the host and port are common examples of details when an application wants to perform read/write to a database service instance. See Cloud Foundry documentation for the explanation of the options that can be used in the command.
Unbind a service instance from an application in Cloud Foundry
Opposite to the bind operation, you can unbind the application and service instance by this command.
$ cf unbind-service APP_NAME SERVICE_INSTANCE
For example
$ cf unbind-service myapp myapp-application-logs
Unbinds the application myapp from the service instance myapp-application-logs
This post explains how we can use reusable service instances in our micro services or applications after we have successfully created a service instance. And also how to stop the application using the service instance.
Yorumlar