美文网首页
Redis搭建

Redis搭建

作者: 就是这么任性_ac9c | 来源:发表于2019-05-06 09:50 被阅读0次

 

部署redis集群环境

1)配置6台redis服务器

2)准备环境

如下修改每台配置文件参数

IP 规划

• redis 服务器 ip 地址及端口规划

– redisA 192.168.4.51   6351

– redisB 192.168.4.52   6352

– redisC 192.168.4.53   6353

– redisD 192.168.4.54   6354

– redisE 192.168.4.55   6355

– redisF 192.168.4.56  6356

#vim /etc/redis/6379.conf

bind IP 地址   //只写物理接口 IP 地址

daemonize yes  //守护进程方式运行

port xxxx      //端口号不要使用默认的6379

cluster-enabled yes    //开启集群

cluster-config-file nodes.conf  //集群的配置文件不要使用默认的名称

cluster-node-timeout 5000    //请求超时 5 秒   

3)修改/etc/init.d/redis.6379启动脚本 并启动redis 

[root@host51 ~]# ss -nuplt | grep redis

tcp    LISTEN     0     128    192.168.4.51:6351                  *:*                  users:(("redis-server",pid=27627,fd=6))

tcp    LISTEN     0     128    192.168.4.51:16351                 *:*                  users:(("redis-server",pid=27627,fd=8))

[root@host51 ~]# #####每台都会有两个端口 一个集群时集群通信

[root@host53 ~]# ls /var/lib/redis/6379/

dump.rdb         nodes-6353.conf ##储存集群信息文件

4)创建集群

[root@host51 src]# pwd

/root/redis-4.0.8/src/redis-trib.rb 创建集群脚本

把ruby脚本拷贝到系统命令路径下

[root@host51 src]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@host51 src]# cp redis-trib.rb /usr/local/sbin/

[root@host51 src]# ls /usr/local/sbin/redis-trib.rb

/usr/local/sbin/redis-trib.rb

安装ruby脚本运行环境包

[root@host51 redis-cluster]# yum -y install ruby rubygems

rpm -ivh --nodeps ruby-devel-2.0.0.648-30.el7.x86_64.rpm

--nodeps 忽略依赖关系安装

[root@host51 redis-cluster]# ls

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

[root@host51 redis-cluster]# redis-trib.rb help

Usage: redis-trib

  create          host1:port1 ... hostN:portN

                  --replicas

  check           host:port

  info            host:port

  fix             host:port

                  --timeout

  reshard         host:port

                  --from

                  --to

                  --slots

创建:

[root@host51 ~]# redis-trib.rb create --replicas 1 \

> 192.168.4.51:6351 \

> 192.168.4.52:6352 \

> 192.168.4.53:6353 \

> 192.168.4.54:6354 \

> 192.168.4.55:6355 \

> 192.168.4.56:6356

--replicas 1 表示自动为每一个master节点,分配一个slave 节点

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.   ##成功 16384个hash槽(slots)

[root@host51 ~]#

M: dcdaea3e76b649b9793b4ba25c0480d09e3071b4 192.168.4.51:6351

   slots:0-5460 (5461 slots) master

M: ddbfc55da0d4e464fa6a9a295db3cc68b8c5491e 192.168.4.52:6352

   slots:5461-10922 (5462 slots)master

M: be99e13aad8f225410c258eff4713d8e2e334b02 192.168.4.53:6353

   slots:10923-16383 (5461 slots)master

key/CRC16的值对16384取余后的值在那个范围内就存那台redis服务器

查看集群信息

[root@host51 ~]# cat /var/lib/redis/6379/nodes-6351.conf

be99e13aad8f225410c258eff4713d8e2e334b02 192.168.4.53:6353@16353 master - 01530608195025 3 connected 10923-16383

1203e9088538d89286f2e197b1204615d193f842 192.168.4.55:6355@16355 slavedcdaea3e76b649b9793b4ba25c0480d09e3071b4 0 1530608195000 5 connected

dcdaea3e76b649b9793b4ba25c0480d09e3071b4 192.168.4.51:6351@16351myself,master - 0 1530608195000 1 connected 0-5460

78c10670f14c0697c2372c070c41f7b513fb81c2 192.168.4.56:6356@16356 slaveddbfc55da0d4e464fa6a9a295db3cc68b8c5491e 0 1530608196028 6 connected

ddbfc55da0d4e464fa6a9a295db3cc68b8c5491e 192.168.4.52:6352@16352 master - 01530608197033 2 connected 5461-10922

ec6a17092cef51cae2e3db1f3d15e5d95e9a05bb 192.168.4.54:6354@16354 slavebe99e13aad8f225410c258eff4713d8e2e334b02 0 1530608195000 4 connected

vars currentEpoch 6 lastVoteEpoch 0

使用ruby查看集群信息

[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352

192.168.4.52:6352> cluster nodes

be99e13aad8f225410c258eff4713d8e2e334b02 192.168.4.53:6353@16353 master - 01530609880570 3 connected 10923-16383

1203e9088538d89286f2e197b1204615d193f842 192.168.4.55:6355@16355 slavedcdaea3e76b649b9793b4ba25c0480d09e3071b4 0 1530609881574 5 connected

78c10670f14c0697c2372c070c41f7b513fb81c2 192.168.4.56:6356@16356 slaveddbfc55da0d4e464fa6a9a295db3cc68b8c5491e 0 1530609879566 6 connected

ec6a17092cef51cae2e3db1f3d15e5d95e9a05bb 192.168.4.54:6354@16354 slavebe99e13aad8f225410c258eff4713d8e2e334b02 0 1530609881574 4 connected

dcdaea3e76b649b9793b4ba25c0480d09e3071b4 192.168.4.51:6351@16351 master - 01530609881072 1 connected 0-5460

ddbfc55da0d4e464fa6a9a295db3cc68b8c5491e 192.168.4.52:6352@16352myself,master - 0 1530609881000 2 connected 5461-10922

192.168.4.52:6352> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:2

cluster_stats_messages_ping_sent:3343

cluster_stats_messages_pong_sent:2988

cluster_stats_messages_meet_sent:4

cluster_stats_messages_sent:6335

cluster_stats_messages_ping_received:2986

cluster_stats_messages_pong_received:3347

cluster_stats_messages_meet_received:2

cluster_stats_messages_received:6335

验证redis集群是否正常:

[root@host53 ~]# ss -nuplt | grep redis

tcp    LISTEN     0     128    192.168.4.53:6353                  *:*                  users:(("redis-server",pid=984,fd=6))

tcp    LISTEN     0     128    192.168.4.53:16353                 *:*                   users:(("redis-server",pid=984,fd=8))

[root@host53 ~]# redis-cli -h 192.168.4.51 -p 6351

192.168.4.51:6351> cluster info   #查看本机信息

192.168.4.51:6351> cluster nodes  #查看集群信息

#################################################################

测试集群

1 在客户端访问任意一台master角色的redis服务器,查询数据或存储数据

redis-cli -c -h IP地址 -p端口           ##-c是访问集群

[root@host50 ~]# redis-cli -c -h 192.168.4.51 -p 6351

192.168.4.51:6351> keys *

(empty list or set)

192.168.4.51:6351> set name bob

-> Redirected to slot [5798] located at 192.168.4.52:6352

OK    #####hash槽5798 存在4.52上 去4.52查看

[root@host52 ~]# redis-cli -h 192.168.4.52 -p 6352

192.168.4.52:6352> keys *

1) "name"

192.168.4.52:6352> get name

"bob"

4.56时52的从库 去4.56查看

[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6356

192.168.4.56:6356> keys *

1) "name"

192.168.4.56:6356> get name

(error) MOVED 5798 192.168.4.52:6352#### 56从库有,但从库默认不能查看值,需设置

192.168.4.52:6352> set age 19

-> Redirected to slot [741] located at 192.168.4.51:6351

192.168.4.52:6352> get age 因为age存在51上所以52取值报错,去51上get

(error) MOVED 741 192.168.4.51:6351

192.168.4.51:6351> set age 19

OK

192.168.4.51:6351> get age

"19"

管理集群:redis-trib.rb

redis-trib.rb 脚本

• 语法格式

– Redis-trib.rb

选项 参数

• 选项

– add-node 添加新节点

– check 对节点主机做检查

– reshard 对节点主机重新分片

– add-node --slave 添加从节点主机###默认为主库 加--slave是指定为从库

– del-node 删除节点主机

当前的主库挂掉,他对应的从库自动升级为主库

[root@host51 ~]# redis-trib.rb help

 check           host:port

[root@host51 ~]# redis-trib.rb check 192.168.4.51:6351

主  从

51  55

52  56

53  54

现在down掉52 结果时56升级为主从库只有两个 还是54/55 这时56主库没从库,如果56在down掉整个集群就挂掉了(可以一诸多从,解决)

这是52再起,会变成56的从库

[root@host51 ~]# redis-trib.rb check 192.168.4.53:6353

>>> Performing Cluster Check (using node 192.168.4.53:6353)

M: be99e13aad8f225410c258eff4713d8e2e334b02 192.168.4.53:6353

   slots:10923-16383 (5461 slots)master

   1 additional replica(s)

S: ddbfc55da0d4e464fa6a9a295db3cc68b8c5491e 192.168.4.52:6352

   slots: (0 slots) slave

   replicates78c10670f14c0697c2372c070c41f7b513fb81c2#####(56的ip)

M: 78c10670f14c0697c2372c070c41f7b513fb81c2 192.168.4.56:6356

   slots:5461-10922 (5462 slots)master

   1 additional replica(s)

M: dcdaea3e76b649b9793b4ba25c0480d09e3071b4 192.168.4.51:6351

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: 1203e9088538d89286f2e197b1204615d193f842 192.168.4.55:6355

   slots: (0 slots) slave

   replicatesdcdaea3e76b649b9793b4ba25c0480d09e3071b4

S: ec6a17092cef51cae2e3db1f3d15e5d95e9a05bb 192.168.4.54:6354

   slots: (0 slots) slave

   replicatesbe99e13aad8f225410c258eff4713d8e2e334b02

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

添加(准备50 57两台redis服务器)

添加master角色主机 需从新分片

添加slave

将新主机添加到集群中

– # ./redis-trib.rb add-node 新主机Ip:端口192.168.4.51:6351

[root@host51 ~]# redis-trib.rb add-node 192.168.4.50:6350 192.168.4.51:6351

手动对集群进行重新分片

– src]# ./redis-trib.rb reshard 192.168.4.51:6351

[root@host51 ~]# redis-trib.rb reshard 192.168.4.51:6351

How many slots do you want to move (from 1 to 16384)? 4096

####4台主库,16384/4=4096(从当前集群拿出多少个hash slots)

What is the receiving node ID? 93b0dd6ce08c2f43c805aee6b3ca7d408f418756

新主库id,这个id可以[root@host50

~]# cat /var/lib/redis/6379/nodes-6350.conf 看也可以

[root@host51 ~]# redis-trib.rb check 192.168.4.51:6351查看

Source node #1:all

Do you want to proceed with the proposed reshard plan (yes/no)? yes

再查看4.51

[root@host51 ~]# redis-trib.rb check 192.168.4.51:6351

M: 93b0dd6ce08c2f43c805aee6b3ca7d408f418756 192.168.4.50:6350

   slots:0-1364,5461-6826,10923-12287(4096 slots) master

   0 additional replica(s) ###(4096个hash槽)

向新主库50上添加slave57

[root@host51 ~]# redis-trib.rb help

add-node        new_host:new_portexisting_host:existing_port

                  --slave

                  --master-id

添加从节点

# ./redis-trib.rb add-node --slave --master-id node-id 值

[root@host51 ~]# redis-trib.rb add-node --slave --master-idbe99e13aad8f225410c258eff4713d8e2e334b02 192.168.4.58:6358 192.168.4.51:6351

从节点ip:端口192.168.4.51:6351

如果不指定主节点的 id 的话,会把新节点 随机添加为 从节点 最少的主的从

[root@host51 ~]# redis-trib.rb add-node --slave 192.168.4.57:6357192.168.4.51:6351

[root@host51 ~]# redis-trib.rb check 192.168.4.51:6351##查看集群信息 查看57是否加入成功

移除slave节点

[root@host51 ~]# redis-trib.rb del-node 192.168.4.58:6358e928fc152ab568f9a0befc9775b9eb81a1c6ea99

移除master节点

1)先移出hash sloat

[root@host51 ~]# redis-trib.rb reshard 192.168.4.51:6351

How many slots do you want to move (from 1 to 16384)?4096

有多少hash sloat要移出

What is the receiving node ID?78c10670f14c0697c2372c070c41f7b513fb81c2

谁接收移出的hash sloat

Source node #1:93b0dd6ce08c2f43c805aee6b3ca7d408f418756

谁移出hash sloat

Source node #2:done  手动输入done

2)移出

[root@host51 ~]# redis-trib.rb del-node 192.168.4.51:6351 93b0dd6ce08c2f43c805aee6b3ca7d408f418756

移出slave或则master后清空rm

-rf /var/lib/redis/6379/*或者在redis登陆界面输入

cluster reset 把恢复成单独的redis服务器

#######################################################

�R�*u�v

相关文章

网友评论

      本文标题:Redis搭建

      本文链接:https://www.haomeiwen.com/subject/fcgloqtx.html