baton is a TypeScript-based application that enhances fault tolerance in Docker Swarm environments. It continuously monitors the health of Swarm nodes and automatically migrates services to a backup node if the primary node becomes unavailable.
This tool leverages the Docker API and custom placement constraints to ensure high availability and smooth failover of containerized applications.
- Node Health Monitoring: Detects node failures or changes in availability.
- Service Migration: Dynamically reassigns services to a secondary node during failures.
- Docker API Integration: Uses the Docker socket for real-time interaction with Swarm.
- Customizable Node Preferences: Easily configure primary and secondary nodes with labels.
- TypeScript Powered: Clean, maintainable, and strongly-typed codebase.
- Primary Node Monitoring: The monitor detects if the primary node (labeled
type=primary) is unhealthy or unavailable. - Service Inspection: Queries the Docker API to identify services deployed on the primary node.
- Service Migration: Updates the service’s placement constraints to move it to the backup node (labeled
type=secondary).
docker build -t baton:latest .Create a docker-compose.yml file:
services:
baton:
image: baton:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
replicas: 1
environment:
- PRIMARY_LABEL=primary
- SECONDARY_LABEL=secondaryDeploy the stack:
docker stack deploy -c docker-compose.yml batonAssign labels to your Swarm nodes for primary and secondary roles.
- Label the Primary Node:
docker node update --label-add type=primary <primary-node-id>
- Label the Secondary Node:
docker node update --label-add type=secondary <secondary-node-id>
- Deploy a test service:
docker service create --name test-service --replicas 1 --constraint 'node.labels.type == primary' nginx - Simulate a failure on the primary node:
docker node update --availability drain <primary-node-id>
- Observe the monitor logs and ensure the service is reassigned to the secondary node:
docker service logs monitor-stack_baton
PRIMARY_LABEL: The label identifying the primary node. Default:primary.SECONDARY_LABEL: The label identifying the secondary node. Default:secondary.
npm run buildThe output will be in the dist folder.
- Add support for multi-node placement strategies.
- Improve failover logic with configurable priorities.
- Include a web dashboard for real-time monitoring.
- Add unit and integration tests.
We welcome contributions! Please follow these steps:
- Fork the repository.
- Create a new branch:
git checkout -b feature-name. - Commit your changes:
git commit -m 'Add feature-name'. - Push to the branch:
git push origin feature-name. - Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Dockerode - Docker SDK for Node.js.
- TypeScript - Typed JavaScript at scale.