# 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
managerand Node2, Node3, Node4 acting as dockerworker), rundocker swarm initon the dockermanagernode.Create a user-defined
overlaynetwork on the dockermanagernode for typesense service communication. Run the command below on the dockermanagernode.Take a note of the
subnetvalue. The samesubnetvalue will be used indocker-stack.yml --peering-subnetflag below.Add Docker instance running on rest of the Docker nodes to the existing Docker Swarm as
worker. Remember to change thetokenor retrieve thetokenusing the commanddocker swarm join-token workerand repeat the command below on all the docker nodes.Verify status and roles of
docker swarmnodes by running command below on the dockermanagernode.Create a new
nodesfile on each dockerworkernode part of swarm.Connect to each Docker
workernode and create thedatafolder.Create
docker-stack.ymlfile on the dockermanagernode. This file will be used to deploy Typesense service across all the dockerworkernodes.# Content for
docker-stack.ymlfileIMPORTANT
In the
Docker swarmsetup--peering-subnetflag should be the samesubnetdefined in the default or user-definedoverlaynetwork.--peering-subnetwas introduced intypesense/typesense:0.23.0(opens new window). For more information onOverlaynetworks, 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: trueDeploy the stack from the docker
managernode by running the command below.List the distribution of services and tasks across docker
workernodes from themanagernode.docker stack pscommand will display the typesense service distribution across all the dockerworkernodes.Verify if typesense service is
clusteredsuccessfully by connecting to any dockerworkernode.NOTE: For a successful docker swarm setup, you can determine whether a container node is a
leaderorfollowerby the value of thestatefield in the/debugend-point response. The state value for at least one of the container node should be1as documented in Multi-node deployment (opens new window)