Last active
August 7, 2021 20:33
-
-
Save stavalfi/f5c8c5373c1e619dfde53853b178935c to your computer and use it in GitHub Desktop.
setup a cluster of redis masters on your local machine. support single instance or cluster (3+).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// must have: redis-cli, redis-server executables | |
// need npm modules: @stavalfi/create-folder-structure, execa, lodash | |
// tested on node 14. | |
// RUN single instance: `ts-node <file-name>.ts 1` | |
// RUN cluster of masters only: `ts-node <file-name>.ts 3` // can be 3+ | |
// close with ctrl+c. make sure ports are closed after that as well. | |
/////////////////// | |
/////////////////// | |
/////////////////// | |
// NOT production ready!!!! it's for developement only!!!! | |
/////////////////// | |
/////////////////// | |
/////////////////// | |
import { createFolder } from '@stavalfi/create-folder-structure' | |
import execa from 'execa' | |
import _ from 'lodash' | |
import path from 'path' | |
async function main(argv: string[]) { | |
const instances = argv[0] !== undefined ? Number(argv[0]) : 3 | |
if (instances <= 0 || instances === 2) { | |
throw new Error(`count of instances must be 1 or 3+`) | |
} | |
const nodesPorts = _.range(7000, 7000 + instances) | |
const nodesAddresses = nodesPorts.map(port => `127.0.0.1:${port}`).join(' ') | |
console.log('----------------------------------------------------------------------------') | |
console.log('----------------------------------------------------------------------------') | |
console.log('----------------------------------------------------------------------------') | |
console.log(`starting redis-masters: ${nodesAddresses}...`) | |
console.log('----------------------------------------------------------------------------') | |
console.log('----------------------------------------------------------------------------') | |
console.log('----------------------------------------------------------------------------') | |
const [redisClusterDirPath] = await Promise.all([ | |
createFolder({ | |
'redis-cluster-setup': Object.fromEntries( | |
nodesPorts.map(port => [ | |
port, | |
{ | |
'redis.conf': | |
nodesPorts.length > 1 | |
? ` | |
port ${port} | |
cluster-enabled yes | |
cluster-config-file nodes.conf | |
cluster-node-timeout 5000 | |
appendonly no | |
save ""` | |
: ` | |
port ${port} | |
cluster-enabled no | |
appendonly no | |
save ""`, | |
}, | |
]), | |
), | |
}), | |
execa.command(`yarn kill-used-dev-ports`, { | |
cwd: '..', | |
}), | |
]) | |
nodesPorts.map(nodePort => { | |
const folder = path.join(redisClusterDirPath, 'redis-cluster-setup', nodePort.toString()) | |
return execa.command(`redis-server ${path.join(folder, 'redis.conf')}`, { | |
cwd: folder, | |
shell: true, | |
stdout: 'inherit', | |
}) | |
}) | |
if (nodesPorts.length > 1) { | |
await new Promise(resolve => setTimeout(resolve, 3000)) | |
await execa.command(`redis-cli --cluster create ${nodesAddresses} --cluster-replicas 0 --cluster-yes`, { | |
stdout: 'inherit', | |
}) | |
} | |
console.log('----------------------------------------------------------------------------') | |
console.log('----------------------------------------------------------------------------') | |
console.log('----------------------------------------------------------------------------') | |
console.log(`started redis-masters: ${nodesAddresses}...`) | |
console.log('----------------------------------------------------------------------------') | |
console.log('----------------------------------------------------------------------------') | |
console.log('----------------------------------------------------------------------------') | |
} | |
main(process.argv.slice(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment