本文共 5363 字,大约阅读时间需要 17 分钟。
目录
一、需求
二、安装redis
1. 安装gcc
2. 建redis用户
3. 解压并编译
4. 限制redis用户登录
三、建立配置文件模板
1. master模板
2. slave模板
3. 哨兵模板
四、编写创建集群的shell脚本
1. master
2. slave
五、其它准备
1. 配置SSH登录无密码验证
2. 禁用requiretty
3. 将create_redis_cluster.sh改为可执行
三台128G内存的物理机,目标为在其上构建多套一主两从的redis集群。要求根据业务需要,能够快速创建新集群。三台服务器IP如下:
103.244.233.166(master)
103.244.233.167(slave)
103.244.233.168(slave)
以下操作步骤均在三台机器上执行。
yum install gcc
useradd redis
su - redis
tar -zxvf redis-3.2.3.tar.gz
cd redis-3.2.3/src/ && make
编辑/etc/passwd文件,将redis用户的登录shell改为/sbin/nologin:
redis:x:8002:8002::/home/redis:/sbin/nologin
在103.244.233.166上创建/home/redis/redis.conf.templet文件,内容如下:
rename-command flushAll ""
daemonize yes
port 20001
dir "/home/redis/redisdata1"
pidfile "/home/redis/redisdata1/redis.pid"
logfile "/home/redis/redisdata1/redis.log"
dbfilename "dump.rdb"
save 900 1
appendonly no
appendfilename "appendonly.aof"
appendfsync always
maxmemory 8gb
maxmemory-policy volatile-lru
maxmemory-samples 3
slowlog-log-slower-than 10000
repl-backlog-size 64mb
timeout 0
repl-timeout 240
requirepass "123456"
masterauth "123456"
protected-mode no
在103.244.233.167、103.244.233.168上创建/home/redis/redis.conf.templet文件,内容如下:
rename-command flushAll ""
daemonize yes
port 20001
dir "/home/redis/redisdata1"
pidfile "/home/redis/redisdata1/redis.pid"
logfile "/home/redis/redisdata1/redis.log"
dbfilename "dump.rdb"
save 900 1
appendonly no
appendfilename "appendonly.aof"
appendfsync always
maxmemory 8gb
maxmemory-policy volatile-lru
maxmemory-samples 3
slowlog-log-slower-than 10000
repl-backlog-size 64mb
timeout 0
repl-timeout 240
requirepass "123456"
masterauth "123456"
protected-mode no
slaveof 103.244.233.166 20001
slave的模板文件比master的模板文件只是多了最后一行。
在全部三台机器上创建/home/redis/redis.conf.templet文件,内容如下:
port 20002
protected-mode no
sentinel monitor redis1 103.244.233.166 20001 2
dir "/home/redis/redisdata1"
sentinel auth-pass redis1 123456
sentinel down-after-milliseconds redis1 5000
sentinel failover-timeout redis1 10000
说明:
在103.244.233.166上创建/root/create_redis_cluster.sh文件,内容如下:
maxdir=`ls -l /home/redis | grep "redisdata" | awk '{print $9}' | sort -k1.10n | tail -n1`
maxnum=`expr ${maxdir:9} + 1`
datadir='/home/redis/redisdata'${maxnum}
clustername='redis'${maxnum}
redis_port=$((20000+maxnum*2-1))
sentinel_port=$((20000+maxnum*2))
mkdir ${datadir}
cp /home/redis/redis.conf.templet ${datadir}/redis.conf
cp /home/redis/sentinel.conf.templet ${datadir}/sentinel.conf
sed -i "s/20001/$redis_port/g" ${datadir}/redis.conf
sed -i "s/redisdata1/redisdata$maxnum/g" ${datadir}/redis.conf
sed -i "s/20002/$sentinel_port/g" ${datadir}/sentinel.conf
sed -i "s/20001/$redis_port/g" ${datadir}/sentinel.conf
sed -i "s/redis1/$clustername/g" ${datadir}/sentinel.conf
sed -i "s/redisdata1/redisdata$maxnum/g" ${datadir}/sentinel.conf
chown -R redis:redis ${datadir}
chmod 775 ${datadir}
chmod 664 ${datadir}/*
sudo -u redis /home/redis/redis-3.2.3/src/redis-server ${datadir}/redis.conf
sudo -u redis /home/redis/redis-3.2.3/src/redis-sentinel ${datadir}/sentinel.conf > ${datadir}/sentinel.log 2>&1 &
ssh root@103.244.233.167 /root/create_redis_cluster.sh
ssh root@103.244.233.168 /root/create_redis_cluster.sh
info="集群名称:${clustername} |哨兵:103.244.233.166 ${sentinel_port}, 103.244.233.167 ${sentinel_port}, 103.244.233.168 ${sentinel_port} |口令:123456"
echo ${info} | sed 's/|/\n /g'
在103.244.233.167、103.244.233.168上创建/root/create_redis_cluster.sh文件,内容如下:
maxdir=`ls -l /home/redis | grep "redisdata" | awk '{print $9}' | sort -k1.10n | tail -n1`
maxnum=`expr ${maxdir:9} + 1`
datadir='/home/redis/redisdata'${maxnum}
clustername='redis'${maxnum}
redis_port=$((20000+maxnum*2-1))
sentinel_port=$((20000+maxnum*2))
mkdir ${datadir}
cp /home/redis/redis.conf.templet ${datadir}/redis.conf
cp /home/redis/sentinel.conf.templet ${datadir}/sentinel.conf
sed -i "s/20001/$redis_port/g" ${datadir}/redis.conf
sed -i "s/redisdata1/redisdata$maxnum/g" ${datadir}/redis.conf
sed -i "s/20002/$sentinel_port/g" ${datadir}/sentinel.conf
sed -i "s/20001/$redis_port/g" ${datadir}/sentinel.conf
sed -i "s/redis1/$clustername/g" ${datadir}/sentinel.conf
sed -i "s/redisdata1/redisdata$maxnum/g" ${datadir}/sentinel.conf
chown -R redis:redis ${datadir}
chmod 775 ${datadir}
chmod 664 ${datadir}/*
sudo -u redis /home/redis/redis-3.2.3/src/redis-server ${datadir}/redis.conf
sudo -u redis /home/redis/redis-3.2.3/src/redis-sentinel ${datadir}/sentinel.conf > ${datadir}/sentinel.log 2>&1 &
在103.244.233.166(master)上用root用户执行:
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@103.244.233.167
ssh-copy-id -i /root/.ssh/id_rsa.pub root@103.244.233.168
编辑/etc/sudoers文件,注释以下行,三台都执行:
# Defaults requiretty
chmod 755 /root/create_redis_cluster.sh
三台都执行。
至此所有配置已经完成。每次执行103.244.233.166上的/root/create_redis_cluster.sh文件,就会自动创建一个新的redis集群,一键式秒建。
转载地址:http://dixqa.baihongyu.com/