環境準備
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 slavenode
中止防火牆(有可能還須要iptables加規則)
systemctl stop firewalld.service
systemctl disable firewalld.service redis
修改內核參數
vi /etc/sysctl.conf
vm.overcommit_memory = 1ruby
查看效果
sysctl -papp
安裝必要的軟件包
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/ui
修改配置文件,密碼爲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 15000server
啓動Redis測試可否正常啓動,注意看一下端口
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.confip
備份Redis配置文件,將來若是改了其餘設置除了問題,能夠留做比對
cp -p redis.conf redis.conf.bakget
查看日誌
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 redistail -100f /var/log/redis.logtail -100f /var/log/sentinel.log/usr/local/redis/bin/redis-cli -h 172.18.1.101 -p 7001 -a passwordkill $(ps -ef | grep redis | grep -v grep | awk '{print $2}')