yum -y install expect gcc tar -xf /root/soft/redis/redis-4.0.8.tar.gz cd /root/soft/redis/redis-4.0.8/ make && make install
./utils/install_server.sh #初始化
一路回车
/etc/init.d/redis_6379 stop sed -i "70c bind 192.168.4.51" /etc/redis/6379.conf sed -i "93c port 6351" /etc/redis/6379.conf sed -i "815c cluster-enabled yes" /etc/redis/6379.conf #启用集群 sed -i "823c cluster-config-file nodes-6351.conf" /etc/redis/6379.conf sed -i "829c cluster-node-timeout 5000" /etc/redis/6379.conf sed -i "43s/\$REDISPORT/6351 -h 192.168.4.51/" /etc/init.d/redis_6379 #修改脚本能正常使用停止/etc/init.d/redis_6379 startss -nutlp | grep redis tcp LISTEN 0 128 192.168.4.56:6356 *:* users:(("redis-server",pid=2341,fd=6)) tcp LISTEN 0 128 192.168.4.56:16356(集群通信端口默认+10000) *:* users:(("redis-server",pid=2341,fd=8)) [root@host56 ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> CLUSTER info
cluster_state:fail 192.168.4.51:6351> CLUSTER nodes
3.1 部署管理主机(管理集群)[192.168.4.51] 部署ruby脚本运行环境
1.装包[真机]# scp -r redis-cluster/ 192.168.4.51:/root
[root@host51 ~]# cd /root/redis-cluster
redis-3.2.1.gem ruby-devel-2.0.0.648-30.el7.x86_64.rpm [root@host51 redis-cluster]# gem install redis-3.2.1.gem #gem安装工具,gem第三方插件
[root@host51 redis-cluster]# yum -y install ruby rubygems ruby-devel-2.0.0.648-30.el7.x86_64.rpm
2.创建管理集群命令(使用ruby语言编写)[root@host51 ~]# cd /root/redis-4.0.8/src
[root@host51 src]# echo $PATH
[root@host51 src]# mkdir /root/bin
[root@host51 src]# cp -r redis-trib.rb /root/bin/redis-trib #设搜索路径
3.查看命令选项]# redis-trib help
4.建集群(默认)必须选举三台作主库
[root@host51 src]# redis-trib create --replicas 1(这里指自动每个主库配几个从库) \
> 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 \ #做集群的host:port...
> 192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356
Using 3 masters: 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 Adding replica 192.168.4.55:6355 to 192.168.4.51:6351 Adding replica 192.168.4.56:6356 to 192.168.4.52:6352 Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
......
Can I set the above configuration? (type 'yes' to accept): yes #是否接收上述配置
......[OK] All 16384 slots covered.
查看集群信息(任意一台主机访问本机的redis服务查看即可)192.168.4.51:6351> CLUSTER info 查看集群信息
cluster_state:ok 192.168.4.51:6351> CLUSTER nodes 查看集群节点信息
1.服务没启,/etc/init.d/redis_6379 start 启的时候报pid文件的话就删掉
rm -rf /var/run/redis_6379.pid
/etc/init.d/redis_6379 start
服务还没启就把配置文件删了 rm -rf /etc/redis/6379.conf 再从别的集群拷过来再修改
2.没有清空数据库目录
[ERR] Node 192.168.4.56:6356 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
/etc/init.d/redis_6379 stop
rm -rf /var/lib/redis/6379/* /etc/init.d/redis_6379 start
ss -nutlp | grep redis tcp LISTEN 0 128 192.168.4.56:6356 *:* users:(("redis-server",pid=2341,fd=6)) tcp LISTEN 0 128 192.168.4.56:16356 *:* users:(("redis-server",pid=2341,fd=8))
3.终极解决办法:还原(前提配置环境没问题)
/etc/init.d/redis_6379 stoprm -rf /var/lib/redis/6379/*
/etc/init.d/redis_6379 start
ss -nutlp | grep redis
查看集群信息
[root@host51 src]# redis-trib info 192.168.4.56:6356 #查看统计信息
[root@host51 src]# redis-trib check 192.168.4.52:6352 #查看详细信息
[192.168.4.50]# redis-cli -c -h 192.168.4.55 -p 6355
192.168.4.55:6355> set x 101 -> Redirected to slot [16287] located at 192.168.4.53:6353 OK 192.168.4.53:6353> set y 102 OK 192.168.4.53:6353> set z 102 -> Redirected to slot [8157] located at 192.168.4.52:6352 OK 192.168.4.52:6352> exit[root@host50 ~]# redis-cli -c(连接集群) -h 192.168.4.52 -p 6352 192.168.4.52:6352> ping #测试是否可以使用 PONG 192.168.4.52:6352> keys * #查看所有变量 1) "z" 192.168.4.52:6352> get x #取x的变量 -> Redirected to slot [16287] located at 192.168.4.53:6353 "101" 192.168.4.53:6353> get y "102" 192.168.4.53:6353> get z -> Redirected to slot [8157] located at 192.168.4.52:6352 "102"
测试集群功能
集群中主服务宕机后,对应的从服务器会自动升为主库
随便停一个服务,观察集群状态,之后再启服务
redis-cli -h 192.168.4.51 -p 6351
宕机的主服务器启后,自动配置为 当前主键服务器的从库,并自动同步宕机期间的数据
redis集群有0~16383编号(slot,hash槽)
主库才有槽,从库自动同步(没有槽)
槽编号的范围决定了每次某个数据存在哪个库
存储数据 --> 变量名用crc16算法 做hash运算 得出来一个数字,用这个数和16384取余
取数据 ---> 变量名做运算 取余
存多少变量由内存存储空间决定
1)扩本机物理内存
2)添加新服务器
redis-trib 脚本的选项
add-node #添加master主机
check #检测集群
reshard #重新分片
add-node --slave #添加slave主机
del-node #删除主机
添加master 角色 redis 服务器[192.168.4.57]
1)装包,
2)初始化
3)启用集群配置
4)重启服务
清空数据库目录
/etc/init.d/redis_6379 stop
rm -rf /var/lib/redis/6379/*
/etc/init.d/redis_6379 start
1.添加master主机[root@host51 src]# redis-trib add-node 192.168.4.57:6357(新主机ip:端口) 192.168.4.51:6351(随便写集群的一个)
[root@host51 src]# redis-trib check 192.168.4.57:6357 #查看主机ID M: 3f39a7bed09c112eab183fa83e3b80bc3014c8a6 192.168.4.57:6357 slots:0-1364,5461-6826,10923-12287 (4096 slots) master .......
[root@host51 src]# redis-trib reshard 192.168.4.51:6351 How many slots do you want to move (from 1 to 16384)? 4096 #指定移出hash槽个数 Wha t is the receiving node ID? 3f39a7bed09c112eab183fa83e3b80bc3014c8a6 #指定接收hash槽主机ID Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1:all #指定移出hash槽主机IDyes
2.1 [192.168.4.58]装包、初始化、运行redis服务 且启用集群配置
/etc/init.d/redis_6379 stop
rm -rf /var/lib/redis/6379/*
/etc/init.d/redis_6379 start
2.2 在管理主机上添加slave角色服务器 (slave不需hash槽)
[root@host51 src]# redis-trib add-node --slave 192.168.4.58:6358 192.168.4.52:6352(集群里随便一个) 不指定主节点会随机添加到最少从库的主库上 [root@host51 src]# redis-trib check 192.168.4.58:58 #查看状态
[root@host51 src]# redis-trib check 192.168.4.58:6358 (查看58的id)
[root@host51 src]# redis-trib del-node 192.168.4.51:6351(集群中任意一台) f8a8fe2d77810e4b8dc85955ad1e1665d7f8bc22 (58的ID) [root@host51 src]# redis-trib check 192.168.4.51:6351 #查看是否删除
2.移出master角色redis服务器[192.168.4.57]在管理主机51做如下操作
2.1重新分片释放占用的hash槽
[root@host51 src]# redis-trib check 192.168.4.57:6357 [root@host51 src]# redis-trib reshard 192.168.4.51:6351
指定移出hash槽个数 4096
指定接收 hash槽主机ID *******
指定移出hash槽主机ID ******
done
yes
2.2移除master主机 [root@host51 src]# redis-trib del-node 192.168.4.51:6351 3f39a7bed09c112eab183fa83e3b80bc3014c8a6(57的ID)
保证redis服务是正常启的[192.168.4.58]
重置集群信息
1)rm -rf /var/lib/redis/6379/*
2)redis-cli -h 192.168.4.58 -p 6358
192.168.4.58:6358> CLUSTER reset #重置集群信息
192.168.4.58:6358> FLUSHALL #删除所有数据 192.168.4.58:6358> save #马上存盘
管理主机上
添加主机
[root@host51 src]# redis-trib add-node 192.168.4.58:6358 192.168.4.51:6351 [root@host51 src]# redis-trib check 192.168.4.58:6358 [root@host51 src]# redis-trib info 192.168.4.58:6358
4.4把集群主机 恢复为独立的redis数据库服务器 [非必须操作,下个实验不需要集群]
]# /etc/init.d/redis_6379 stop ]# rm -rf /var/lib/redis/6379/* ]# vim /etc/redis/6379.conf #cluster-enabled yes #cluster-config-file nodes-xxxx.conf #cluster-node-timeout 5000 :wq ]# /etc/init.d/redis_6379 start