实验环境
redhat6.5
iptables和selinux关闭
Server2:172.25.35.52
Server3:172.25.35.53
Server4:172.25.35.54
高可用
Server2、Server3、Server4同步骤:
[root@server2 ~]# tar zxf redis-4.0.8.tar.gz
[root@server2 ~]# cd redis-4.0.8
[root@server2 redis-4.0.8]# yum install gcc -y
[root@server2 redis-4.0.8]# make
[root@server2 redis-4.0.8]# make install
[root@server2 redis-4.0.8]# which redis-cli
/usr/local/bin/redis-cli
[root@server2 redis-4.0.8]# cd utils/
[root@server2 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
Server2:
[root@server2 utils]# vim /etc/redis/6379.conf
70 bind 0.0.0.0
[root@server2 utils]# /etc/init.d/redis_6379 stop
[root@server2 utils]# /etc/init.d/redis_6379 start
[root@server2 utils]# netstat -antlp
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 8648/redis-server 0
[root@server2 ~]# redis-cli
127.0.0.1:6379> set name wxh
OK
127.0.0.1:6379> get name
"wxh"
127.0.0.1:6379> set user1 123
OK
127.0.0.1:6379> get user1
"123"
Server3、4同步骤:
[root@server3 utils]# cd /etc/redis/
[root@server3 redis]# vim 6379.conf
70 bind 0.0.0.0
283 slaveof 172.25.35.52 6379
[root@server3 utils]# /etc/init.d/redis_6379 stop
[root@server3 utils]# /etc/init.d/redis_6379 start
[root@server3 redis]# redis-cli
127.0.0.1:6379> get name
"wxh"
127.0.0.1:6379> get user1
"123"
Server2:
127.0.0.1:6379> set name www
OK
127.0.0.1:6379> get name
"www"
Server3:
127.0.0.1:6379> get name
"www"
做主从切换的时候只能有一个与主发起同步
Server2:
[root@server2 redis-4.0.8]# cp sentinel.conf /etc/redis/
[root@server2 redis-4.0.8]# vim /etc/redis/sentinel.conf
[root@server2 redis-4.0.8]# scp /etc/redis/sentinel.conf server3:/etc/redis/
[root@server2 redis-4.0.8]# scp /etc/redis/sentinel.conf server4:/etc/redis/
[root@server2 redis-4.0.8]# cd /etc/redis/
[root@server2 redis]# redis-server /etc/redis/sentinel.conf --sentinel
8838:X 11 Aug 10:10:57.667 * +slave slave 172.25.35.53:6379 172.25.35.53 6379 @ mymaster 172.25.35.52 6379
8838:X 11 Aug 10:10:57.691 * +slave slave 172.25.35.54:6379 172.25.35.54 6379 @ mymaster 172.25.35.52 6379
Server3:
[root@server3 utils]# cd /etc/redis
[root@server3 redis]# redis-server /etc/redis/sentinel.conf --sentinel
8013:X 11 Aug 10:11:09.619 * +slave slave 172.25.35.53:6379 172.25.35.53 6379 @ mymaster 172.25.35.52 6379
8013:X 11 Aug 10:11:09.643 * +slave slave 172.25.35.54:6379 172.25.35.54 6379 @ mymaster 172.25.35.52 6379
8013:X 11 Aug 10:11:10.180 * +sentinel sentinel 99562ba8a25c2a027ada948e4c9989b3b6acb7b1 172.25.35.52 26379 @ mymaster 172.25.35.52 6379
Server4:
[root@server4 utils]# cd /etc/redis/
[root@server4 redis]# redis-server /etc/redis/sentinel.conf --sentinel
6583:X 11 Aug 10:11:16.034 * +slave slave 172.25.35.53:6379 172.25.35.53 6379 @ mymaster 172.25.35.52 6379
6583:X 11 Aug 10:11:16.075 * +slave slave 172.25.35.54:6379 172.25.35.54 6379 @ mymaster 172.25.35.52 6379
6583:X 11 Aug 10:11:16.496 * +sentinel sentinel 99562ba8a25c2a027ada948e4c9989b3b6acb7b1 172.25.35.52 26379 @ mymaster 172.25.35.52 6379
6583:X 11 Aug 10:11:17.680 * +sentinel sentinel d8a4ffdee4b5bad722e1c4371da56f25449b20dc 172.25.35.53 26379 @ mymaster 172.25.35.52 6379
Server2:
[root@server2 ~]# redis-cli //将server2 shutdown之后会换一个master
127.0.0.1:6379> shutdown
not connected>
Server3:
监控显示:
8013:X 11 Aug 10:12:46.486 * +slave slave 172.25.35.53:6379 172.25.35.53 6379 @ mymaster 172.25.35.54 6379
8013:X 11 Aug 10:12:46.486 * +slave slave 172.25.35.52:6379 172.25.35.52 6379 @ mymaster 172.25.35.54 6379
8013:X 11 Aug 10:13:16.550 # +sdown slave 172.25.35.52:6379 172.25.35.52 6379 @ mymaster 172.25.35.54 6379
[root@server3 redis]# vim /etc/redis/sentinel.conf
//查看文件中自动添加server4为master
Server2:
[root@server2 ~]# /etc/init.d/redis_6379 start //打开server2之后server2变成了slave
Server3:
[root@server3 redis]# redis-server /etc/redis/sentinel.conf --sentinel
8018:X 11 Aug 10:15:56.217 * +convert-to-slave slave 172.25.35.52:6379 172.25.35.52 6379 @ mymaster 172.25.35.54 6379
Server2:
[root@server2 redis]# vim /etc/redis/6379.conf
//最后一行添加server4为master
Server4:
[root@server4 redis]# vim /etc/redis/6379.conf //之前添加的master没有了
Redis集群搭建
过半的master挂了就不能使用
Server2:
[root@server2 ~]# cd /usr/local/
[root@server2 local]# cd cluster/
[root@server2 cluster]# mkdir 7001
[root@server2 cluster]# mkdir 7002
[root@server2 cluster]# mkdir 7003
[root@server2 cluster]# mkdir 7004
[root@server2 cluster]# mkdir 7005
[root@server2 cluster]# mkdir 7006
[root@server2 cluster]# cd 7001/
[root@server2 7001]# vim redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
[root@server2 7001]#cd
[root@server2 ~]# cd redis-4.0.8/src/
[root@server2 src]# cp redis-trib.rb /usr/local/bin/
[root@server2 src]# cd
[root@server2 ~]# yum install rubygems-1.3.7-5.el6.noarch.rpm ruby-2.2.3-1.el6.x86_64.rpm libyaml-0.1.3-4.el6_6.x86_64.rpm -y
[root@server2 ~]# gem install --local redis-4.0.1.gem
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 1 seconds
1 gem installed
[root@server2 ~]# gem list --local
*** LOCAL GEMS ***
bigdecimal (1.2.6)
io-console (0.4.3)
json (1.8.1)
minitest (5.4.3)
power_assert (0.2.2)
psych (2.0.8)
rake (10.4.2)
rdoc (4.2.0)
redis (4.0.1)
test-unit (3.0.8)
[root@server2 ~]# /etc/init.d/redis_6379 stop //关闭redis
[root@server2 ~]# chkconfig redis_6379 off
[root@server2 ~]# cd /usr/local/cluster
[root@server2 cluster]# cd 7001/
[root@server2 7001]# vim redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonzie yes
pdifile /usr/local/cluster/7001/redis.pid
logfile /usr/local/cluster/7001/redis.log
[root@server2 7001]# redis-server redis.conf //开启服务
[root@server2 7001]# cat redis.log //日志有三个报错
1267:M 12 Aug 02:05:50.034 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1267:M 12 Aug 02:05:50.034 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1267:M 12 Aug 02:05:50.034 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
[root@server2 7001]# cat /proc/sys/net/core/somaxconn
128
[root@server2 7001]# echo 511 > /proc/sys/net/core/somaxconn
[root@server2 7001]# sysctl -w vm.overcommit_memory=1
vm.overcommit_memory = 1
[root@server2 7001]# vim /etc/sysctl.conf
vm.overcommit_memory = 1
[root@server2 7001]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@server2 7001]# cp redis.conf ../7002/
[root@server2 7001]# cp redis.conf ../7003/
[root@server2 7001]# cp redis.conf ../7004/
[root@server2 7001]# cp redis.conf ../7005/
[root@server2 7001]# cp redis.conf ../7006/
[root@server2 7001]# cd ../7002
[root@server2 7002]# vim redis.conf //修改7001为7002
[root@server2 7002]# redis-server redis.conf
[root@server2 7002]# cat redis.log //查看日志没有报错
[root@server2 7002]# cd ../7003
[root@server2 7003]# vim redis.conf //修改7001为7003
[root@server2 7003]# redis-server redis.conf
[root@server2 7003]# cd ../7004
[root@server2 7004]# vim redis.conf //修改7001为7004
[root@server2 7004]# redis-server redis.conf
[root@server2 7004]# cd ../7005
[root@server2 7005]# vim redis.conf //修改7001为7005
[root@server2 7005]# redis-server redis.conf
[root@server2 7005]# cd ../7006
[root@server2 7006]# vim redis.conf //修改7001为7006
[root@server2 7006]# redis-server redis.conf
[root@server2 7006]# ps ax //查看进程是否打开
1267 ? Ssl 0:00 redis-server *:7001 [cluster]
1282 ? Ssl 0:00 redis-server *:7002 [cluster]
1289 ? Ssl 0:00 redis-server *:7003 [cluster]
1295 ? Ssl 0:00 redis-server *:7004 [cluster]
1301 ? Ssl 0:00 redis-server *:7005 [cluster]
1307 ? Ssl 0:00 redis-server *:7006 [cluster]
[root@server2 7006]# redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
Can I set the above configuration? (type 'yes' to accept): yes
[root@server2 7001]# redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: f267d8cdc8ef173c19accca4ef206d296e190f90 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 03cb031b06d76fb5cc67310660af019f600aaae7 127.0.0.1:7006
slots: (0 slots) slave
replicates 9bf1a17591b04ba8e005ca760f352b772c2a6bbb
M: 9bf1a17591b04ba8e005ca760f352b772c2a6bbb 127.0.0.1:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 476ef9b047e9977e58c22206ec402593de819d2f 127.0.0.1:7004
slots: (0 slots) slave
replicates 6e2005f6345b25bed4b0f10cb8c4bd73b6a90891
S: 8ff695f6de35d5988ba13289b1730ed236a0834b 127.0.0.1:7005
slots: (0 slots) slave
replicates f267d8cdc8ef173c19accca4ef206d296e190f90
M: 6e2005f6345b25bed4b0f10cb8c4bd73b6a90891 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
//可以查看到7001是7005的master,7002是7006的master,7003是7004的master
[root@server2 7001]# redis-cli -c -p 7001 //进入7001
127.0.0.1:7001> set name haha
-> Redirected to slot [5798] located at 127.0.0.1:7002 //数据写入7002
OK
127.0.0.1:7002> get name
"haha"
127.0.0.1:7002>
[root@server2 7001]# redis-cli -c -p 7004
127.0.0.1:7004> get name
-> Redirected to slot [5798] located at 127.0.0.1:7002 //查看name会跳转到7002
"haha"
127.0.0.1:7002>
[root@server2 7001]# redis-cli -c -p 7005
127.0.0.1:7005> get name
-> Redirected to slot [5798] located at 127.0.0.1:7002 //查看name会跳转到7002
"haha"
127.0.0.1:7002>
[root@server2 7001]# ps ax //查看7002的进程id
1282 ? Ssl 0:01 redis-server *:7002 [cluster]
[root@server2 7001]# kill 1282 //杀死7002
[root@server2 7001]# redis-cli -c -p 7002 //不能进入
Could not connect to Redis at 127.0.0.1:7002: Connection refused
Could not connect to Redis at 127.0.0.1:7002: Connection refused
not connected> set name haha [EX seconds] [PX milliseconds] [NX|XX]
[root@server2 7001]# redis-cli -c -p 7005
127.0.0.1:7005> get name
-> Redirected to slot [5798] located at 127.0.0.1:7006 //查看name会跳转到7002的slave 7006
"haha"
127.0.0.1:7006>
[root@server2 7001]# cd ../7002
[root@server2 7002]# redis-server redis.conf //开启
[root@server2 7002]# redis-trib.rb check 127.0.0.1:7001 //查看发现7006变成了7002的master
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: f267d8cdc8ef173c19accca4ef206d296e190f90 127.0.0.1:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 03cb031b06d76fb5cc67310660af019f600aaae7 127.0.0.1:7006
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 9bf1a17591b04ba8e005ca760f352b772c2a6bbb 127.0.0.1:7002
slots: (0 slots) slave
replicates 03cb031b06d76fb5cc67310660af019f600aaae7
S: 476ef9b047e9977e58c22206ec402593de819d2f 127.0.0.1:7004
slots: (0 slots) slave
replicates 6e2005f6345b25bed4b0f10cb8c4bd73b6a90891
S: 8ff695f6de35d5988ba13289b1730ed236a0834b 127.0.0.1:7005
slots: (0 slots) slave
replicates f267d8cdc8ef173c19accca4ef206d296e190f90
M: 6e2005f6345b25bed4b0f10cb8c4bd73b6a90891 127.0.0.1:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@server2 7002]# ps ax
1267 ? Ssl 0:01 redis-server *:7001 [cluster]
1289 ? Ssl 0:01 redis-server *:7003 [cluster]
1295 ? Ssl 0:00 redis-server *:7004 [cluster]
1301 ? Ssl 0:00 redis-server *:7005 [cluster]
1307 ? Ssl 0:01 redis-server *:7006 [cluster]
1378 ? Ssl 0:00 redis-server *:7002 [cluster]
[root@server2 7002]# kill 1307 //杀掉7006
[root@server2 7002]# kill 1378 //杀掉7002
[root@server2 7002]# redis-cli -c -p 7001
127.0.0.1:7001> get name //查看name时就会报错
(error) CLUSTERDOWN The cluster is down
127.0.0.1:7001>
[root@server2 7002]# redis-server redis.conf //重新开启7002
[root@server2 7002]# cd ../7006
[root@server2 7006]# redis-server redis.conf //重新开启7006
[root@server2 7006]# redis-cli -c -p 7001
127.0.0.1:7001> get name //又可以查看到
-> Redirected to slot [5798] located at 127.0.0.1:7006
"haha"
127.0.0.1:7006>