經過redis-trib工具建立redis-cluster 集羣 yum -y install redis-trib #建立目錄並添加環境變量 mkdir -p /usr/local/redis/bin/ mkdir -p /data/redis/ echo 'export PATH=$PATH:/usr/local/redis/bin' >> /etc/profile source /etc/profile #生成啓動腳本 cat > /data/redis/start.sh <<EOF #!/bin/sh user=redis BINDIR=/usr/local/redis BASEDIR=/data/redis ConfFile=\$BASEDIR/redis.conf ThisUser=`whoami` ISInUser=0 if [ \$ThisUser == \$user ]; then ISInUser=1 fi if [ \$ISInUser -eq 1 ]; then \$BINDIR/bin/redis-server \$ConfFile else su \$user -c "\$BINDIR/bin/redis-server \$ConfFile" fi EOF #生成配置文件 cat > /app/redis/redis.conf<<EOF port 6379 #端口 cluster-enabled yes #開啓集羣功能 cluster-config-file nodes-6379.conf #集羣配置文件 cluster-node-timeout 5000 #集羣節點超時時間,單位毫秒 cluster-slave-validity-factor 0 #主節點失聯後是否當即升爲主節點 appendonly yes #開啓AOF模式 appendfsync everysec #fsync模式,每秒寫入一次aof maxmemory 8gb #最大內存 maxmemory-policy volatile-lru #內存不足時清除策略 bind 0.0.0.0 #綁定IP dir /data/redis/ #持久化文件路徑 appendfilename "appendonly.aof" #保存數據的aof名稱 daemonize yes #後臺運行 pidfile redis.pid #pid文件 EOF
#全部節點 node=(10.20.242.2 10.20.242.3 10.20.242.4 10.20.242.5 10.20.242.6 10.20.242.7) #下載redis wget http://download.redis.io/releases/redis-3.2.11.tar.gz tar zxvf redis-3.2.11.tar.gz cd redis-3.2.11 #編譯 make cd src for i in ${node[@]};do scp redis-cli redis-server redis-check-aof redis-check-aof redis-sentinel redis-benchmark $i:/usr/local/redis/bin/ ;done
#建立redis集羣,3主3從 redis-trib create --replicas 1 10.20.242.2:6379 10.20.242.3:6379 10.20.242.4:6379 10.20.242.5:6379 10.20.242.6:6379 10.20.242.7:6379