Linux学习之Redis集群部署

Redis集群部署

准备集群环境

Linux学习之Redis集群部署_第1张图片

创建集群

# 准备集群环境--配置192.168.88.51(host51)
[root@localhost ~]# yum install -y redis
[root@host51 ~]# vim /etc/redis.conf
bind 192.168.88.51
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
[root@host51 ~]# ss -tnlp
State      Recv-Q     Send-Q         Local Address:Port          Peer Address:Port    Process                                                                               
LISTEN     0          128                  0.0.0.0:22                 0.0.0.0:*        users:(("sshd",pid=548,fd=3))                                                        
LISTEN     0          128            192.168.88.51:16379              0.0.0.0:*        users:(("redis-server",pid=1080,fd=8))                                               
LISTEN     0          128            192.168.88.51:6379               0.0.0.0:*        users:(("redis-server",pid=1080,fd=6))    
# 查看集群状态
[root@host51 ~]# redis-cli -h 192.168.88.51
192.168.88.51:6379> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0

# 注:其它5台机器配置同上,只需修改redis.conf中bind的值为本机的ip,配置完成后,启动redis服务

#创建集群
1、在任意一台redis服务器上都可以执行创建集群的命令。
2--cluster-replicas 1 给每个master服务器分配一台slave服务器,每个主至少要分配1台slave服务器,不然无法实现redis服务的高可用。
3、创建集群时,会自动创建主从角色,默认把主机列表中的前3台服务器创建为Master角色的redis服务器,剩下的均配置为slave角色服务器。
4、创建集群时,会自动给master角色的主机分配hash槽 ,通过hash槽实现数据的分布式存储。
[root@host51 ~]# redis-cli --cluster create 192.168.88.51:6379  192.168.88.52:6379  192.168.88.53:6379  192.168.88.54:6379 192.168.88.55:6379 192.168.88.56:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.88.54:6379 to 192.168.88.51:6379
Adding replica 192.168.88.55:6379 to 192.168.88.52:6379
Adding replica 192.168.88.56:6379 to 192.168.88.53:6379
M: 1cbc84ecf3dbde1c3a06d1d7cd8c527da6b7fe32 192.168.88.51:6379
   slots:[0-5460] (5461 slots) master
M: 88e66d4df0867bf76015e22cc480be85ed5eb5de 192.168.88.52:6379
   slots:[5461-10922] (5462 slots) master
M: b8968a8c613a389052a2978803dd7fd6d24ca076 192.168.88.53:6379
   slots:[10923-16383] (5461 slots) master
S: 07740e8a77b462dbbb4ab55cade01b102cb3a5d8 192.168.88.54:6379
   replicates 1cbc84ecf3dbde1c3a06d1d7cd8c527da6b7fe32
S: 8615847c6f9ce5c65f6017efcbc8f6e4072ae9d1 192.168.88.55:6379
   replicates 88e66d4df0867bf76015e22cc480be85ed5eb5de
S: 6f766d3638e6e66710743a88f684bac1d252bbef 192.168.88.56:6379
   replicates b8968a8c613a389052a2978803dd7fd6d24ca076
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
....
>>> Performing Cluster Check (using node 192.168.88.51:6379)
M: 1cbc84ecf3dbde1c3a06d1d7cd8c527da6b7fe32 192.168.88.51:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 07740e8a77b462dbbb4ab55cade01b102cb3a5d8 192.168.88.54:6379
   slots: (0 slots) slave
   replicates 1cbc84ecf3dbde1c3a06d1d7cd8c527da6b7fe32
S: 8615847c6f9ce5c65f6017efcbc8f6e4072ae9d1 192.168.88.55:6379
   slots: (0 slots) slave
   replicates 88e66d4df0867bf76015e22cc480be85ed5eb5de
S: 6f766d3638e6e66710743a88f684bac1d252bbef 192.168.88.56:6379
   slots: (0 slots) slave
   replicates b8968a8c613a389052a2978803dd7fd6d24ca076
M: 88e66d4df0867bf76015e22cc480be85ed5eb5de 192.168.88.52:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: b8968a8c613a389052a2978803dd7fd6d24ca076 192.168.88.53:6379
   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.
# 查看集群信息
第一列:主服务器ip地址
第二列:主服务器ID
第三列:存储变量个数
第四列:hash槽个数 (hash槽的作用在集群存储工程过程里讲)
第五列:从服务器数量
[root@host51 ~]# redis-cli --cluster info 192.168.88.51:6379
192.168.88.51:6379 (1cbc84ec...) -> 0 keys | 5461 slots | 1 slaves.
192.168.88.52:6379 (88e66d4d...) -> 0 keys | 5462 slots | 1 slaves.
192.168.88.53:6379 (b8968a8c...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
# 访问集群
[root@host51 ~]# redis-cli -c -h 192.168.88.51 -p 6379
192.168.88.51:6379> set school nsd2306
-> Redirected to slot [8455] located at 192.168.88.52:6379
OK
192.168.88.52:6379> set teacher laoyang
-> Redirected to slot [12541] located at 192.168.88.53:6379
OK
192.168.88.53:6379> set class NSD
-> Redirected to slot [7755] located at 192.168.88.52:6379
OK
#会发现3个变量被分别存储到集群中的master服务器上,实现了数据的分布式存储。当连接集群中的任意一台redis服务器存储数据时,会调用集群CRC16算法 得出此次存储变量使用的hash 槽,然后连接hash 槽 所在的master服务器存储变量。
#在创建集群时会把默认的16384个槽平均的分配给集群中的3个master服务器。可以通过查看集群信息查看每个master服务器占用hash槽的个数。
[root@host51 ~]# redis-cli  --cluster  info  192.168.88.51:6379
192.168.88.51:6379 (1cbc84ec...) -> 0 keys | 5461 slots | 1 slaves.
192.168.88.52:6379 (88e66d4d...) -> 2 keys | 5462 slots | 1 slaves.
192.168.88.53:6379 (b8968a8c...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
# 集群中slave角色的主机会自动同步master角色主机的数据,实现数据的自动备份,
# 分别连接集群中的3台slave服务器查看变量
[root@host51 ~]# redis-cli -c -h 192.168.88.54 -p 6379
192.168.88.54:6379> keys *
(empty list or set)
192.168.88.54:6379> exit
[root@host51 ~]# redis-cli -c -h 192.168.88.55 -p 6379
192.168.88.55:6379> keys *
1) "class"
2) "school"
192.168.88.55:6379> exit
[root@host51 ~]# redis-cli -c -h 192.168.88.56 -p 6379
192.168.88.56:6379> keys *
1) "teacher"
# 当master角色的服务器宕机时,对应的slave服务器会升级为master 并接手对应的hash槽,实现redis服务的高可用,例如停止host53主机的redis服务,对应的从会升级为master 。(当宕机的master恢复后 自动做当前主的从服务器)
# 停止53的redis服务
[root@host53 ~]# systemctl stop redis
# 查看集群信息
[root@host51 ~]# redis-cli --cluster info 192.168.88.51:6379
Could not connect to Redis at 192.168.88.53:6379: Connection refused
192.168.88.51:6379 (1cbc84ec...) -> 1 keys | 5461 slots | 1 slaves.
192.168.88.56:6379 (6f766d36...) -> 2 keys | 5461 slots | 0 slaves.
192.168.88.52:6379 (88e66d4d...) -> 3 keys | 5462 slots | 1 slaves.
[OK] 6 keys in 3 masters.
0.00 keys per slot on average.
# 53上启动redis服务
[root@host51 ~]# redis-cli --cluster info 192.168.88.51:6379
192.168.88.51:6379 (1cbc84ec...) -> 1 keys | 5461 slots | 1 slaves.
192.168.88.56:6379 (6f766d36...) -> 2 keys | 5461 slots | 1 slaves.
192.168.88.52:6379 (88e66d4d...) -> 3 keys | 5462 slots | 1 slaves.
[OK] 6 keys in 3 masters.
0.00 keys per slot on average.
# 存储数据脚本
[root@redis64 ~]# vim /usr/local/nginx/html/set.php
<?php
$redis_list = ['192.168.88.51:6379','192.168.88.52:6379','192.168.88.53:6379','192.168.88.54:6379','192.168.88.55:6379','192.168.88.56:6379'];
$client = new RedisCluster(NUll,$redis_list);
$client->set("i","tarenaA ");
$client->set("j","tarenaB ");
$client->set("k","tarenaC ");
echo "save ok\n";
?>

[root@host51 ~]# redis-cli -c -h 192.168.88.51 -p 6379
192.168.88.51:6379> keys *
1) "j"
192.168.88.51:6379> exit
[root@host51 ~]# redis-cli -c -h 192.168.88.52 -p 6379
192.168.88.52:6379> keys *
1) "class"
2) "k"
3) "school"
192.168.88.52:6379> exit
[root@host51 ~]# redis-cli -c -h 192.168.88.53 -p 6379
192.168.88.53:6379> keys *
1) "teacher"
2) "i"
# 查看数据脚本
<?php
$redis_list = ['192.168.88.51:6379','192.168.88.52:6379','192.168.88.53:6379','192.168.88.54:6379','192.168.88.55:6379','192.168.88.56:6379'];  //定义redis服务器列表
$client = new RedisCluster(NUll,$redis_list); //定义连接redis服务命令
echo $client->get("i");  //获取变量i 的数据
echo $client->get("j");  //获取变量j 的数据
echo $client->get("k");  //获取变量k 的数据
?>
# 访问脚本,查看数据
[root@redis64 ~]# curl localhost/get_data.php
tarenaA tarenaB tarenaC
# 命令行连接redis集群主机查看数据
[root@host51 ~]# redis-cli -c -h 192.168.88.51 -p 6379
192.168.88.51:6379> keys *
1) "j"
192.168.88.51:6379> exit
[root@host51 ~]# redis-cli -c -h 192.168.88.52 -p 6379
192.168.88.52:6379> keys *
1) "class"
2) "k"
3) "school"
192.168.88.52:6379> exit
[root@host51 ~]# redis-cli -c -h 192.168.88.53 -p 6379
192.168.88.53:6379> keys *
1) "teacher"
2) "i"

你可能感兴趣的:(redis,Linux,linux,学习,redis)