# Deploy a Typesense cluster to Docker Swarm
To Deploy a Typesense cluster on multiple hosts which run in Docker Swarm (opens new window) mode, follow these steps:
Initialize Docker Swarm. For example, in a 4 node Docker Swarm setup (Node1 acting as docker
manager
and Node2, Node3, Node4 acting as dockerworker
), rundocker swarm init
on the dockermanager
node.Create a user-defined
overlay
network on the dockermanager
node for typesense service communication. Run the command below on the dockermanager
node.Take a note of the
subnet
value. The samesubnet
value will be used indocker-stack.yml --peering-subnet
flag below.Add Docker instance running on rest of the Docker nodes to the existing Docker Swarm as
worker
. Remember to change thetoken
or retrieve thetoken
using the commanddocker swarm join-token worker
and repeat the command below on all the docker nodes.Verify status and roles of
docker swarm
nodes by running command below on the dockermanager
node.Create a new
nodes
file on each dockerworker
node part of swarm.Connect to each Docker
worker
node and create thedata
folder.Create
docker-stack.yml
file on the dockermanager
node. This file will be used to deploy Typesense service across all the dockerworker
nodes.# Content for
docker-stack.yml
fileIMPORTANT
In the
Docker swarm
setup--peering-subnet
flag should be the samesubnet
defined in the default or user-definedoverlay
network.--peering-subnet
was introduced intypesense/typesense:0.23.0
(opens new window). For more information onOverlay
networks, read the official Docker documentation here (opens new window).version: "3.8" services: typesense-1: image: typesense/typesense:0.23.0 hostname: typesense-1 volumes: - /var/lib/typesense-data-1/:/data - ./nodes:/nodes ports: - 7108:7108 - 7107:7107 command: ["--peering-subnet","10.11.0.10/16","--data-dir", "/data","--api-key", "xyz","--nodes","/nodes","--peering-port","7107","--api-port","7108","--enable-cors"] deploy: replicas: 1 labels: feature.description: “typesense-1” restart_policy: condition: any placement: constraints: [node.hostname == node2] networks: - ts_net typesense-2: image: typesense/typesense:0.23.0 hostname: typesense-2 volumes: - /var/lib/typesense-data-2/:/data - ./nodes:/nodes ports: - 8108:8108 - 8107:8107 command: ["--peering-subnet","10.11.0.10/16","--data-dir", "/data","--api-key", "xyz","--nodes","/nodes","--peering-port","8107","--api-port","8108","--enable-cors"] deploy: replicas: 1 labels: feature.description: “typesense-2” restart_policy: condition: any placement: constraints: [node.hostname == node3] networks: - ts_net typesense-3: image: typesense/typesense:0.23.0 hostname: typesense-3 volumes: - /var/lib/typesense-data-3/:/data - ./nodes:/nodes ports: - 9108:9108 - 9107:9107 command: ["--peering-subnet","10.11.0.10/16","--data-dir", "/data","--api-key", "xyz","--nodes","/nodes","--peering-port","9107","--api-port","9108","--enable-cors"] deploy: replicas: 1 labels: feature.description: “typesense-3” restart_policy: condition: any placement: constraints: [node.hostname == node4] networks: - ts_net networks: ts_net: external: true
Deploy the stack from the docker
manager
node by running the command below.List the distribution of services and tasks across docker
worker
nodes from themanager
node.docker stack ps
command will display the typesense service distribution across all the dockerworker
nodes.Verify if typesense service is
clustered
successfully by connecting to any dockerworker
node.NOTE: For a successful docker swarm setup, you can determine whether a container node is a
leader
orfollower
by the value of thestate
field in the/debug
end-point response. The state value for at least one of the container node should be1
as documented in Multi-node deployment (opens new window)