MongoDB services

Apcera provides the mongodb service type and gateway for integrating with MongoDB.

Creating MongoDB services

The mongodb service gateway creates an internal MongoDB service to which you can bind jobs. The MongoDB service type does not use a provider. You only have to create the service and the binding. See Requirements for using MongoDB services.

For example, the following APC command creates the mongodb service, and binds the job myapp to this service:

apc service create mongodb --type mongodb --job myapp
╭────────────────────────────────────────────────────╮
│             Service Creation Settings              │
├─────────────────┬──────────────────────────────────┤
│        Service: │ service::/sandbox/admin::mongodb │
│ Job to bind to: │ myapp                            │
│        Binding: │ (will be auto-generated)         │
│   Service Type: │ mongodb                          │
╰─────────────────┴──────────────────────────────────╯
apc service show mongodb
╭────────────────────┬─────────────────────────────────────────╮
│ Service:           │ mongodb                                 │
├────────────────────┼─────────────────────────────────────────┤
│ FQN:               │ service::/sandbox/admin::mongodb        │
│ Type:              │ mongodb                                 │
│ Completion Status: │ created                                 │
│                    │                                         │
│ Created by:        │ admin@apcera.me                         │
│ Created at:        │ 2016-04-05 05:19:52.144944701 +0000 UTC │
╰────────────────────┴─────────────────────────────────────────╯

The myapp job will have an environment variable named MONGODB_URI set on its container instance that includes a connection string to the MongoDB instance. For example: MONGODB_URI=mongodb://169.254.0.20:10000/db.

apc app show myapp
╭──────────────────────┬─────────────────────────────────────────────╮
│ Job:                 │ myapp                                       │
├──────────────────────┼─────────────────────────────────────────────┤
│ FQN:                 │ job::/sandbox/admin::myapp                  │
│ State:               │ started                                     │
│                      │                                             │
│ Bound Services       │                                             │
│                      │                                             │
│ 1. Service:          │ service::/sandbox/admin::mongodb            │
│    Environment:      │ MONGODB_URI                                 │
│                      │ MONGODB_URI                                 │
│                      │                                             │
│ Linked Jobs          │                                             │
│                      │                                             │
│ 1. Name:             │ mongodb                                     │
│    Job:              │ job::/sandbox/admin::mongodb/mongodb        │
│    Port:             │ 27017                                       │
│    Environment:      │ MONGODB_URI                                 │
│                      │ MONGODB_URI                                 │
╰──────────────────────┴─────────────────────────────────────────────╯

Creating persistent MongoDB services

The MongoDB service gateway supports the use of the persistence_provider option. You can use this option to provide a persistent backend for your MongoDB services.

apc service create NAME --type mongodb -- --persistence_provider PROVIDER_FQN

For example, to use an NFS provider to create a persistent MongoDB service:

apc service create mongodb --type mongodb -- --persistence_provider provider::/apcera/providers::apcfs
╭──────────────────────────────────────────────────╮
│            Service Creation Settings             │
├───────────────┬──────────────────────────────────┤
│      Service: │ service::/sandbox/admin::mongodb │
│ Service Type: │ mongodb                          │
╰───────────────┴──────────────────────────────────╯

Is this correct? [Y/n]: 
Creating service... done
Success!

See Apcera File Share services for details on using NFS persistence.

Connecting to an existing MongoDB server

You can use the Generic service type and gateway to connect to an existing MongoDB server.

Requirements for implementing MongoDB services

This section describes the requirements for implementing MongoDB services.

Service gateway

To create MongoDB services, the mongodb service gateway is required.

To verify that you have the mongodb service gateway:

apc gateway list -ns /
Working in "/"
╭──────────┬──────────┬──────────────────────────┬─────────┬───────────╮
│ Name     │ Type     │ Namespace                │ Status  │ Instances │
├──────────┼──────────┼──────────────────────────┼─────────┼───────────┤
│ generic  │ generic  │ /apcera/service-gateways │ started │ 1/1       │
│ gnatsd   │ gnatsd   │ /apcera/service-gateways │ started │ 1/1       │
│ http     │ http     │ /apcera/service-gateways │ started │ 1/1       │
│ ipm      │ ipm      │ /apcera/service-gateways │ started │ 1/1       │
│ memcache │ memcache │ /apcera/service-gateways │ started │ 1/1       │
│ mongodb  │ mongodb  │ /apcera/service-gateways │ started │ 1/1       │
│ mysql    │ mysql    │ /apcera/service-gateways │ started │ 1/1       │
│ network  │ network  │ /apcera/service-gateways │ started │ 1/1       │
│ nfs      │ nfs      │ /apcera/service-gateways │ started │ 1/1       │
│ postgres │ postgres │ /apcera/service-gateways │ started │ 1/1       │
│ rabbitmq │ rabbitmq │ /apcera/service-gateways │ started │ 1/1       │
│ redis    │ redis    │ /apcera/service-gateways │ started │ 1/1       │
│ s3       │ s3       │ /apcera/service-gateways │ started │ 1/1       │
╰──────────┴──────────┴──────────────────────────┴─────────┴───────────╯

Policy

Policy is required to use the mongdb service gateway to create services, and to view that the persistence_provider option is enabled.

The following policy claims are supported:

on gateway::/apcera/service-gateways::mongodb {
  { permit use }
  { serviceParam persistence_provider }
}

The permit use policy claim on the mongodb service gateway lets you create MongoDB services as described above.

The serviceParam persistence_provider policy claim on the mongodb service gateway lets you view when the persistence provider is enabled.

For example, with both the --persistence_provider option enabled and the serviceParam persistence_provider claim in place, apc service show provides the following information:

apc service show mongodb
╭───────────────────────┬─────────────────────────────────────────╮
│ Service:              │ mongodb                                 │
├───────────────────────┼─────────────────────────────────────────┤
│ FQN:                  │ service::/sandbox/admin::mongodb        │
│ Type:                 │ mongodb                                 │
│ persistence_provider: │ provider::/apcera/providers::apcfs      │
│ Completion Status:    │ created                                 │
│                       │                                         │
│ Created by:           │ admin@apcera.me                         │
│ Created at:           │ 2016-04-05 05:23:41.019475444 +0000 UTC │
╰───────────────────────┴─────────────────────────────────────────╯

In addition, to consume MongoDB services, both the service and the job must have permit bind policy claims. See service binding policy examples.