环境准备
CentOS 7.3
redis1 172.18.1.101:7001 master
redis2 172.18.1.102:7002 master
redis3 172.18.1.103:7003 master
redis4 172.18.1.104:7004 slave
redis5 172.18.1.105:7005 slave
redis6 172.18.1.106:7006 slave
停止防火墙(有可能还需要iptables加规则)
systemctl stop firewalld.service
systemctl disable firewalld.service
修改内核参数
vi /etc/sysctl.conf
vm.overcommit_memory = 1
查看效果
sysctl -p
安装必要的软件包
yum -y install gcc automake autoconf libtool make telnet ruby-devel ruby-irb ruby-libs ruby-rdoc ruby rubygems-devel rubygems
gem install redis #忘了这一步会导致使用redis-trib.rb创建集群的时候报错
编译安装Redis
cd ~
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar -zxf redis-3.2.8.tar.gz;cd redis-3.2.8;make
make install PREFIX=/usr/local/redis
mkdir /usr/local/redis/etc;mkdir /usr/local/redis/db;cp redis.conf /usr/local/redis/etc/;cp ./src/redis-trib.rb /usr/local/redis/bin/
修改配置文件,密码为password
vi /usr/local/redis/etc/redis.conf
#bind 127.0.0.1 #这里注释掉,否则会导致其它节点无法与本节点通信
protected-mode yes
port 7001 #不同机器端口不一样,我们用7001-7006 #如果用一样的端口,创建集群会出问题
daemonize yes
pidfile /var/run/redis_7001.pid #不同机器PID文件不一样,我们用7001-7006 #可以用同一个,但是为了统一还是建议改成不同的
logfile "/var/log/redis.log"
dir /usr/local/redis/db/
masterauth password
requirepass password
appendonly yes
cluster-enabled yes
cluster-config-file nodes-7001.conf #不同机器nodes.conf文件不一样,我们用7001-7006 #原因同pidfile
cluster-node-timeout 15000
启动Redis测试能否正常启动,注意看一下端口
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
备份Redis配置文件,未来如果改了其他设置除了问题,可以留作比对
cp -p redis.conf redis.conf.bak
查看日志
tail -100f /var/log/redis.log
修改ruby脚本,否则无法创建集群
vi /usr/local/share/gems/gems/redis-3.3.3/lib/redis/client.rb
修改DEFAULTS里面的password一项,我们这里用的是"password",一定要加引号
创建集群,这里一定要用IP和端口,不能用hostname
/usr/local/redis/bin/redis-trib.rb create --replicas 1 172.18.1.101:7001 172.18.1.102:7002 172.18.1.103:7003 172.18.1.104:7004 172.18.1.105:7005 172.18.1.106:7006
查看哪些Redis实例被定为Master
/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 7001
cluster nodes
拷贝sentinel.conf到etc下
cp /root/redis-3.2.8/sentinel.conf /usr/local/redis/etc
编辑sentinel.conf
vi /usr/local/redis/etc/sentinel.conf
protected-mode no
port 27001 #不同Sentinel端口不一样,我们用27001-27003
sentinel monitor mymaster1 172.18.1.101 7001 2
sentinel monitor mymaster2 172.18.1.102 7002 2
sentinel monitor mymaster3 172.18.1.103 7003 2
sentinel down-after-milliseconds mymaster1 10000
sentinel down-after-milliseconds mymaster2 10000
sentinel down-after-milliseconds mymaster3 10000
sentinel parallel-syncs mymaster1 1
sentinel parallel-syncs mymaster2 1
sentinel parallel-syncs mymaster3 1
sentinel failover-timeout mymaster1 15000
sentinel failover-timeout mymaster2 15000
sentinel failover-timeout mymaster3 15000
daemonize yes
logfile "/var/log/sentinel.log"
备份Sentinel配置文件,未来如果改了其他设置除了问题,可以留作比对
cp -p sentinel.conf sentinel.conf.bak
启动Sentinel
/usr/local/redis/bin/redis-sentinel /usr/local/redis/etc/sentinel.conf
常用命令
ps -ef | grep redis
tail -100f /var/log/redis.log
tail -100f /var/log/sentinel.log
/usr/local/redis/bin/redis-cli -h 172.18.1.101 -p 7001 -a password
kill $(ps -ef | grep redis | grep -v grep | awk '{print $2}')