Setup

For this example we are using one manager named manager1 and two workers: worker1 and worker2.

For instructions how to create the manager and workers, see the Docker Swarm Mode documentation.

Running N|Solid Storage and Console

We can run storage and console on Docker Swarm by using a docker-compose file as shown below.

Note: remember to set your license key to the environment variable in the storage section.

nsolid.yml

version: '3'

services:
  storage:
    image: nodesource/nsolid-storage:alpine
    environment:
      - NSOLID_STORAGE_LICENSE_KEY=xxxx-xxxx-xxxx-xxxx-xxxx
    ports:
      - 9001:9001
      - 9002:9002
      - 9003:9003
      - 4000:4000
    networks:
      - nsolid
    deploy:
      restart_policy:
        condition: on-failure

  console:
    image: nodesource/nsolid-console:alpine
    environment:
      - NSOLID_APPNAME=nsolid-console
      - NSOLID_CONSOLE_STORAGE_URL=https://storage:4000
      - NSOLID_COMMAND=storage:9001
      - NSOLID_DATA=storage:9002
      - NSOLID_BULK=storage:9003
    ports:
      - 6753:6753
    networks:
      - nsolid
    deploy:
      restart_policy:
        condition: on-failure
networks:
  nsolid:

To add the services to the Swarm you can execute the following command in your Docker Swarm manager manager1:

$ docker stack deploy --compose-file=nsolid.yml nsolid

To confirm they are running properly you can execute:

$ docker stack services nsolid
ID                  NAME                MODE                REPLICAS            IMAGE                              PORTS
a6gtvcue1yst        nsolid_console      replicated          1/1                 nodesource/nsolid-console:alpine   *:6753->6753/tcp
a8gd9euont4u        nsolid_storage      replicated          1/1                 nodesource/nsolid-storage:alpine   *:4000->4000/tcp,*:9001->9001/tcp,*:9002->9002/tcp,*:9003->9003/tcp

Note that we created a network called nsolid, this network is crucial to give the agents access to storage.

$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
6cde5e49ea1e        bridge              bridge              local
59c28812f92c        docker_gwbridge     bridge              local
7aba3e08d84b        host                host                local
ylkf08cpp5m7        ingress             overlay             swarm
e7529a54b183        none                null                local
677m1gikmmyq        nsolid_nsolid       overlay             swarm

Recommendation: Do not scale storage or console, we don't currently support multiple instances running at the same time.

Production Considerations:

You can specify resource limits and reservations on your Docker Compose file by setting the following under the storage section:

      resources:
        limits:
          cpus: '4'
          memory: 15G
        reservations:
          cpus: '2'
          memory: 4G

Also we recommend to set a persistent volume for storage at /var/lib/nsolid/storage This will make sure InfluxDB data and any artifacts are saved on disk.

Running a N|Solid Agent application

For this example we created a basic dockerized Node.js http server application using the NodeSource nsolid:alpine image:

server.js

var http = require('http');
var os = require('os');
var hostname = os.hostname();

var port = process.env.PORT || 3000;

function handleRequest (request, response) {
  response.end('[' + hostname + '] Serving requests from myapp. Request URL:' + request.url);
}

var server = http.createServer(handleRequest);

server.listen(port, function () {
  console.log('Server listening on port', port);
});

Dockerfile

FROM nodesource/nsolid:alpine

WORKDIR /usr/src/app

ADD server.js /usr/src/app/server.js

EXPOSE 3000
ENTRYPOINT ["nsolid", "server.js"]

Next we need to build the image into the Docker Swarm manager manager1

$ docker build -t . my-nsolid-app

Then we need to create a new service on Docker Swarm with our new application:

$ docker service create -e NSOLID_COMMAND=storage:9001 --network nsolid_nsolid -p 3000:3000 --restart-condition on-failure --name my-nsolid-app my-nsolid-app

Note that we are setting the NSOLID_COMMAND env variable to connect to storage:9001, it can be reached only if we specify the same --network we defined for our previous deployed N|Solid stack.

Also we are setting a --restart-condition so the agent is going to restart on a failure.

After running and confirming that your new N|Solid application is shown in the console you can scale it and see the processes appear in the cluster view.

$ docker service scale my-nsolid-app=5