# 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 docker worker), run docker swarm init on the docker manager node.

  • Create a user-defined overlay network on the docker manager node for typesense service communication. Run the command below on the docker manager node.

    Take a note of the subnet value. The same subnet value will be used in docker-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 the token or retrieve the token using the command docker 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 docker manager node.

  • Create a new nodes file on each docker worker node part of swarm.

  • Connect to each Docker worker node and create the data folder.

  • Create docker-stack.yml file on the docker manager node. This file will be used to deploy Typesense service across all the docker worker nodes.

    # Content for docker-stack.yml file

    IMPORTANT

    In the Docker swarm setup --peering-subnet flag should be the same subnet defined in the default or user-defined overlay network. --peering-subnet was introduced in typesense/typesense:0.23.0 (opens new window). For more information on Overlay 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 the manager node. docker stack ps command will display the typesense service distribution across all the docker worker nodes.

  • Verify if typesense service is clustered successfully by connecting to any docker worker node.

    NOTE: For a successful docker swarm setup, you can determine whether a container node is a leader or follower by the value of the state field in the /debug end-point response. The state value for at least one of the container node should be 1 as documented in Multi-node deployment (opens new window)

Last Updated: 10/20/2022, 10:47:22 PM