docker swarm快速部署redis分布式集群( 二 )

wait-for-it.shmkdir /root/redis-swarm/startvi wait-for-it.shvi redis-start.sh#!/usr/bin/env bash#Use this script to test if a given TCP host/port are availablecmdname=$(basename $0)echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }usage(){cat << USAGE >&2Usage:$cmdname host:port [-s] [-t timeout] [-- command args]-h HOST | --host=HOSTHost or IP under test-p PORT | --port=PORTTCP port under testAlternatively, you specify the host and port as host:port-s | --strictOnly execute subcommand if the test succeeds-q | --quietDon't output any status messages-t TIMEOUT | --timeout=TIMEOUTTimeout in seconds, zero for no timeout-- COMMAND ARGSExecute command with args after the test finishesUSAGEexit 1}wait_for(){if [[ $TIMEOUT -gt 0 ]]; thenechoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"elseechoerr "$cmdname: waiting for $HOST:$PORT without a timeout"fistart_ts=$(date +%s)while :do(echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1result=$?if [[ $result -eq 0 ]]; thenend_ts=$(date +%s)echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"breakfisleep 1donereturn $result}wait_for_wrapper(){# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692if [[ $QUIET -eq 1 ]]; thentimeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &elsetimeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &fiPID=$!trap "kill -INT -$PID" INTwait $PIDRESULT=$?if [[ $RESULT -ne 0 ]]; thenechoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"fireturn $RESULT}# process argumentswhile [[ $# -gt 0 ]]docase "$1" in*:* )hostport=(${1//:/ })HOST=${hostport[0]}PORT=${hostport[1]}shift 1;;--child)CHILD=1shift 1;;-q | --quiet)QUIET=1shift 1;;-s | --strict)STRICT=1shift 1;;-h)HOST="$2"if [[ $HOST == "" ]]; then break; fishift 2;;--host=*)HOST="${1#*=}"shift 1;;-p)PORT="$2"if [[ $PORT == "" ]]; then break; fishift 2;;--port=*)PORT="${1#*=}"shift 1;;-t)TIMEOUT="$2"if [[ $TIMEOUT == "" ]]; then break; fishift 2;;--timeout=*)TIMEOUT="${1#*=}"shift 1;;--)shiftCLI="$@"break;;--help)usage;;*)echoerr "Unknown argument: $1"usage;;esacdoneif [[ "$HOST" == "" || "$PORT" == "" ]]; thenechoerr "Error: you need to provide a host and port to test."usagefiTIMEOUT=${TIMEOUT:-15}STRICT=${STRICT:-0}CHILD=${CHILD:-0}QUIET=${QUIET:-0}if [[ $CHILD -gt 0 ]]; thenwait_forRESULT=$?exit $RESULTelseif [[ $TIMEOUT -gt 0 ]]; thenwait_for_wrapperRESULT=$?elsewait_forRESULT=$?fifiif [[ $CLI != "" ]]; thenif [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; thenechoerr "$cmdname: strict mode, refusing to execute subprocess"exit $RESULTfiexec $CLIelseexit $RESULTfiredis-start.shgetent hosts xxx查看主机中/etc/hosts域名映射的IP
cd /redis-start/bash wait-for-it.sh redis-node1:6379 --timeout=0bash wait-for-it.sh redis-node2:6379 --timeout=0bash wait-for-it.sh redis-node3:6379 --timeout=0bash wait-for-it.sh redis-node4:6379 --timeout=0bash wait-for-it.sh redis-node5:6379 --timeout=0bash wait-for-it.sh redis-node6:6379 --timeout=0echo 'redis-cluster begin'echo 'yes' | redis-cli --cluster create --cluster-replicas 1 \`getent hosts redis-node1 | awk '{ print $1 ":6379" }'` \`getent hosts redis-node2 | awk '{ print $1 ":6379" }'` \`getent hosts redis-node3 | awk '{ print $1 ":6379" }'` \`getent hosts redis-node4 | awk '{ print $1 ":6379" }'` \`getent hosts redis-node5 | awk '{ print $1 ":6379" }'` \`getent hosts redis-node6 | awk '{ print $1 ":6379" }'`echo 'redis-cluster end'启动目录结构├── docker-compose.yml└── start├── redis-start.sh└── wait-for-it.shswarm管理节点执行
cd /root/redis-swarmdocker stack deploy -c docker-compose.yml redis_cluster查看redis-start服务日志,如下即为启动成功
redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: waiting for redis-node1:6379 without a timeoutredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: redis-node1:6379 is available after 18 secondsredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: waiting for redis-node2:6379 without a timeoutredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: redis-node2:6379 is available after 13 secondsredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: waiting for redis-node3:6379 without a timeoutredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: redis-node3:6379 is available after 0 secondsredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: waiting for redis-node4:6379 without a timeoutredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: redis-node4:6379 is available after 0 secondsredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: waiting for redis-node5:6379 without a timeoutredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: redis-node5:6379 is available after 0 secondsredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: waiting for redis-node6:6379 without a timeoutredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| wait-for-it.sh: redis-node6:6379 is available after 0 secondsredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| redis-cluster beginredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| >>> Performing hash slots allocation on 12 nodes...redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Master[0] -> Slots 0 - 2730redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Master[1] -> Slots 2731 - 5460redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Master[2] -> Slots 5461 - 8191redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Master[3] -> Slots 8192 - 10922redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Master[4] -> Slots 10923 - 13652redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Master[5] -> Slots 13653 - 16383redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Adding replica 10.0.5.6:6379 to 10.0.5.17:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Adding replica 10.0.5.9:6379 to 10.0.5.16:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Adding replica 10.0.5.8:6379 to 10.0.5.18:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Adding replica 10.0.5.12:6379 to 10.0.5.19:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Adding replica 10.0.5.11:6379 to 10.0.5.3:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Adding replica 10.0.5.5:6379 to 10.0.5.2:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| M: 6ce90be6daabc0c700471d03deb3c6bd88c9f0e1 10.0.5.17:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots:[0-2730] (2731 slots) masterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| M: 6ce90be6daabc0c700471d03deb3c6bd88c9f0e1 10.0.5.16:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots:[2731-5460] (2730 slots) masterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| M: ea9b45ec64c08c17283239f8b8e5405b2d182428 10.0.5.18:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots:[5461-8191] (2731 slots) masterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| M: ea9b45ec64c08c17283239f8b8e5405b2d182428 10.0.5.19:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots:[8192-10922] (2731 slots) masterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| M: 935c177308232de05b5483776478020de51bc578 10.0.5.3:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots:[10923-13652] (2730 slots) masterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| M: 935c177308232de05b5483776478020de51bc578 10.0.5.2:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots:[13653-16383] (2731 slots) masterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| S: 1c99e42bcfb28a9fe72952d4e4cc5cd88aded0f9 10.0.5.5:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|replicates 935c177308232de05b5483776478020de51bc578redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| S: 1c99e42bcfb28a9fe72952d4e4cc5cd88aded0f9 10.0.5.6:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|replicates 6ce90be6daabc0c700471d03deb3c6bd88c9f0e1redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| S: 73cf232f232e83126f058cc01458df11146d8537 10.0.5.9:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|replicates 6ce90be6daabc0c700471d03deb3c6bd88c9f0e1redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| S: 73cf232f232e83126f058cc01458df11146d8537 10.0.5.8:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|replicates ea9b45ec64c08c17283239f8b8e5405b2d182428redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| S: ca3c50899d6deb04e296c542cd485791fb3e8922 10.0.5.12:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|replicates ea9b45ec64c08c17283239f8b8e5405b2d182428redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| S: ca3c50899d6deb04e296c542cd485791fb3e8922 10.0.5.11:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|replicates 935c177308232de05b5483776478020de51bc578redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Can I set the above configuration? (type 'yes' to accept): >>> Nodes configuration updatedredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| >>> Assign a different config epoch to each noderedis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| >>> Sending CLUSTER MEET messages to join the clusterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| Waiting for the cluster to joinredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| .redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| >>> Performing Cluster Check (using node 10.0.5.17:6379)redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| M: 6ce90be6daabc0c700471d03deb3c6bd88c9f0e1 10.0.5.17:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots:[0-5460] (5461 slots) masterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|1 additional replica(s)redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| S: ca3c50899d6deb04e296c542cd485791fb3e8922 10.0.5.12:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots: (0 slots) slaveredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|replicates 935c177308232de05b5483776478020de51bc578redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| M: ea9b45ec64c08c17283239f8b8e5405b2d182428 10.0.5.19:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots:[5461-10922] (5462 slots) masterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|1 additional replica(s)redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| M: 935c177308232de05b5483776478020de51bc578 10.0.5.3:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots:[10923-16383] (5461 slots) masterredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|1 additional replica(s)redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| S: 1c99e42bcfb28a9fe72952d4e4cc5cd88aded0f9 10.0.5.6:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots: (0 slots) slaveredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|replicates 6ce90be6daabc0c700471d03deb3c6bd88c9f0e1redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| S: 73cf232f232e83126f058cc01458df11146d8537 10.0.5.9:6379redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|slots: (0 slots) slaveredis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3|replicates ea9b45ec64c08c17283239f8b8e5405b2d182428redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| [OK] All nodes agree about slots configuration.redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| >>> Check for open slots...redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| >>> Check slots coverage...redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| [OK] All 16384 slots covered.redis-swarm_redis-start.1.6xawjqf5shfw@hyx-test3| redis-cluster end

推荐阅读