Using services with your app

Overview

Tsuru provide ways you to use external services, with that you can have adatabase, a storage and a lot of other services.

Using

The service workflow can be resumed to two steps:

  • Create a service instance
  • Bind the service instance to the app
    To start you have to list all services provided by tsuru:
  1. $ tsuru service-list
  2. +----------------+-----------+
  3. | Services | Instances |
  4. +----------------+-----------+
  5. | elastic-search | |
  6. | mysql | |
  7. +----------------+-----------+

The output from service-list above says that there are two availableservices: “elastic-search” and “mysql”, and no instances. To create our MySQLinstance, we should run the command service-instance-add:

  1. $ tsuru service-instance-add mysql db_instance
  2. Service successfully added.

Now, if we run service-list again, we will see our new service instance inthe list:

  1. $ tsuru service-list
  2. +----------------+---------------+
  3. | Services | Instances |
  4. +----------------+---------------+
  5. | elastic-search | |
  6. | mysql | db_instance |
  7. +----------------+---------------+

To bind the service instance to the application,we use the command service-instance-bind:

  1. $ tsuru service-instance-bind mysql db_instance -a myapp
  2. Instance blogsql is now bound to the app myapp.
  3.  
  4. The following environment variables are now available for use in your app:
  5.  
  6. - MYSQL_PORT
  7. - MYSQL_PASSWORD
  8. - MYSQL_USER
  9. - MYSQL_HOST
  10. - MYSQL_DATABASE_NAME
  11.  
  12. For more details, please check the documentation for the service, using service-doc command.

As you can see from bind output, we use environment variables to connect to theMySQL server. Next step is update your app to use these variables toconnect in the database.

After update it and deploy the new version your app will be able to comunicate with service.

More tools

To see more information about a service you should use service-info <service_name>:

  1. $ tsuru service-info mysql
  2. Info for "mysql"
  3.  
  4. Instances
  5. +-------------+---------+-------+
  6. | Instances | Plan | Apps |
  7. +-------------+---------+-------+
  8. | db_instance | default | myapp |
  9. +-----------------------+-------+
  10.  
  11. Plans
  12. +---------+------------+
  13. | Name | Description|
  14. +---------+------------+
  15. | medium | 2G Memory |
  16. | default | 1G Memory |
  17. +---------+------------+

After create a new service instance, sometimes it takes a while to be done.To see the state of a service instance you should useservice-instance-status <service_name> <service_instance>:

  1. $ tsuru service-instance-status mysql db_instance
  2. Service instance "db_instance" is pending

After service-instance-status command return up to instance,you are free to use it with your app.

原文: https://docs.tsuru.io/1.6/using/services.html