Create three Docker Machines – one for Master and two for Worker nodes.Each machine is configured to be part of a Swarm cluster using --swarm. It also uses the Consul service discovery specified using --swarm-discovery.

Compose file uses host network. This is equivalent to using --net=host on docker run CLI. It allows the container to use the host networking stack. It also limits only a single Couchbase container to run on a single Docker Machine. So this means that our Couchbase cluster can scale based upon the number of Docker Machines – 3 in our case.

The exact command to use this Compose file is:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

docker-compose scale db=2

WARNING:The"db"service specifiesaport on the host.Ifmultiple containers forthisservice are created onasingle host,the port will clash.

Creating andstarting couchbasedockerswarm_db_1...

Creating andstarting couchbasedockerswarm_db_2...

Pulling db(arungupta/couchbase:latest)...

swarm-node-02:Pulling arungupta/couchbase:latest...

swarm-master:Pulling arungupta/couchbase:latest...

swarm-node-01:Pulling arungupta/couchbase:latest...

Pulling db(arungupta/couchbase:latest)...

swarm-node-02:Pulling arungupta/couchbase:latest...:downloaded

Creating andstarting couchbasedockerswarm_db_1...done

Creating andstarting couchbasedockerswarm_db_2...done

There are three nodes in the Docker Swarm cluster. The default scheduler strategy is spread and so the containers will be spread on different hosts.

If you have jq installed then IP address can be conveniently found as:

1

2

3

4

docker-machine inspect swarm-node-01|jq".Driver.IPAddress"

"192.168.99.108"

Couchbase Cluster on Docker Swarm

All Couchbase server nodes are created equal. This allows the Couchbase cluster to truly scale horizontally to meet your growing application demands. Independently running Couchbase nodes can be added to a cluster by invoking the server-add CLI command. This is typically a two step process. The first step is adding one or more nodes. The second step then rebalances the cluster where the data on the existing nodes is rebalanced across the updated cluster.

In our case, a single Couchbase container is running on each Docker Machine. Lets pick IP address of any one Couchbase node, and add IP address of the other node:

4c8c149f4e34arungupta/couchbase"/entrypoint.sh /opt/"About an hour ago Up About an hour swarm-node-02/couchbasedockerswarm_db_1

d3b6a1dbddb5 arungupta/couchbase"/entrypoint.sh /opt/"About an hour ago Up About an hour swarm-node-01/couchbasedockerswarm_db_2

As mentioned earlier, scaling a Couchbase cluster is a two-step process. This is so because typically you’ll add multiple servers and then rebalance the cluster. However, in cases where you only need to add a single Couchbase node and then rebalance, the rebalance command can be used achieve that.