This repository demonstrates how to use Traefik as a load balancer for Colyseus game servers. Each Colyseus server instance is dynamically registered with Traefik using Redis as the configuration provider. This allows for easy scaling and management of game server instances.
Key features:
- Dynamic routing with Traefik
- Redis for centralized configuration
- Wildcard DNS for accessing individual server instances
- Server registration and de-registration (e.g., for scaling up and down)
- Node.js
- Redis server (Make sure to have keyspace notifications enabled)
- Traefik
- Wildcard DNS setup (e.g.,
*.yourgamedomain.com
)
-
Install dependencies:
npm install
-
Configure Traefik.
- See traefik-hardcoded.yml sample for hardcoded config.
- See traefik-redis.yml sample for a dynamic config.
-
Start Traefik:
traefik --configFile=traefik-redis.yml
-
Start your Colyseus server(s):
PORT=2567 npm start PORT=2568 npm start PORT=2569 npm start
- When a Colyseus server starts, it registers itself with Traefik by setting specific keys in Redis.
- The keys in Redis follow this pattern:
traefik/http/routers/${subdomain}/rule traefik/http/routers/${subdomain}/service traefik/http/services/${subdomain}/loadbalancer/servers/${subdomain}/url
- Traefik watches for changes in Redis and updates its routing configuration dynamically.
- Each server is accessible via its own subdomain (e.g.,
server1.yourgamedomain.com
,server2.yourgamedomain.com
).
Adjust the following variables in your server code:
publicAddress
: The public domain for accessing the serverport
: The port the Colyseus server is running on
To scale:
- Start new Colyseus server instances
- They will automatically register with Traefik via Redis (See sources)
- Traefik will start routing traffic to the new instances
- Ensure your wildcard DNS is properly set up to point to the Traefik instance.
- For production use, consider adding security measures like HTTPS, authentication for the Traefik dashboard, and proper Redis security.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT