勿以浮沙筑高台
Linux搭建集群
搭建环境
-
创建集群文件夹
mkdir -p /usr/local/redis-cluster mkdir -p /usr/local/redis-cluster
-
copy源conf文件到cluster目录下
[root@work1 redis-cluster]# cp ~/redis-6.0.7/redis.conf ./
-
修改配置文件内容
92 行 port 6380 #端口号 247 行 pidfile /var/run/redis_6380.pid #pidfile文件 260 行 logfile "/usr/local/redis/logs/6380.log" #配置文件地址 342 行 dbfilename dump_6380.rdb #rdb文件 appendonly yes #开启AOF cluster-enabled yes #开启集群 cluster-config-file nodes-6379.conf 集群配置文件读取 # 后端访问开启 bind 0.0.0.0 protected-mode no daemonize yes
-
将配置文件copy到六个文件夹中。
[root@work1 redis-cluster]# echo 8001 8002 8003 8004 8005 8006 | xargs -n 1 cp -v /usr/local/redis-cluster/redis.conf "/usr/local/redis-cluster/redis.conf" -> "8001/redis.conf" "/usr/local/redis-cluster/redis.conf" -> "8002/redis.conf" "/usr/local/redis-cluster/redis.conf" -> "8003/redis.conf" "/usr/local/redis-cluster/redis.conf" -> "8004/redis.conf" "/usr/local/redis-cluster/redis.conf" -> "8005/redis.conf" "/usr/local/redis-cluster/redis.conf" -> "8006/redis.conf"
-
批量替换修改6个文件夹的配置文件。
格式: sed -i “s/查找字段/替换字段/g”
grep 查找字 段 -rl 路径
sed -i “s/oldstring/newstring/g”grep oldstring -rl yourdir
[root@work1 redis-cluster]# sed -i "s/6379/8001/g" `grep 6379 -rl /usr/local/redis-cluster/8001/redis.conf` [root@work1 redis-cluster]# sed -i "s/6379/8002/g" `grep 6379 -rl /usr/local/redis-cluster/8002/redis.conf` [root@work1 redis-cluster]# sed -i "s/6379/8003/g" `grep 6379 -rl /usr/local/redis-cluster/8003/redis.conf` [root@work1 redis-cluster]# sed -i "s/6379/8004/g" `grep 6379 -rl /usr/local/redis-cluster/8004/redis.conf` [root@work1 redis-cluster]# sed -i "s/6379/8005/g" `grep 6379 -rl /usr/local/redis-cluster/8005/redis.conf` [root@work1 redis-cluster]# sed -i "s/6379/8006/g" `grep 6379 -rl /usr/local/redis-cluster/8006/redis.conf`
-
指定配置文件启动
/usr/local/redis/bin/redis-server /usr/local/redis-cluster/8001/redis.conf
[root@work1 redis-cluster]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8001/redis.conf 1540:C 22 Jun 2021 14:49:32.130 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1540:C 22 Jun 2021 14:49:32.130 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=1540, just started 1540:C 22 Jun 2021 14:49:32.130 # Configuration loaded [root@work1 redis-cluster]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8002/redis.conf 1616:C 22 Jun 2021 14:51:25.885 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1616:C 22 Jun 2021 14:51:25.885 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=1616, just started 1616:C 22 Jun 2021 14:51:25.885 # Configuration loaded [root@work1 redis-cluster]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8003/redis.conf 1621:C 22 Jun 2021 14:51:29.075 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1621:C 22 Jun 2021 14:51:29.075 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=1621, just started 1621:C 22 Jun 2021 14:51:29.075 # Configuration loaded [root@work1 redis-cluster]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8005/redis.conf 1626:C 22 Jun 2021 14:51:31.523 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1626:C 22 Jun 2021 14:51:31.523 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=1626, just started 1626:C 22 Jun 2021 14:51:31.523 # Configuration loaded [root@work1 redis-cluster]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8004/redis.conf 1664:C 22 Jun 2021 15:02:18.445 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1664:C 22 Jun 2021 15:02:18.445 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=1664, just started 1664:C 22 Jun 2021 15:02:18.445 # Configuration loaded [root@work1 redis-cluster]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8006/redis.conf 1631:C 22 Jun 2021 14:51:34.599 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1631:C 22 Jun 2021 14:51:34.599 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=1631, just started 1631:C 22 Jun 2021 14:51:34.599 # Configuration loaded
-
ps -ef | grep redis
输入指令查看执行结果成功
配置主从
- 输入命令自动配置主从,cluster-replicas 1,这里的1代表集群后面几个从,这里设置位1个从,代表2,2一组
/usr/local/redis/bin/redis-cli --cluster create --cluster-replicas 1 192.168.31.109:8001 192.168.31.109:8002 192.168.31.109:8003 192.168.31.109:8004 192.168.31.109:8005 192.168.31.109:8006
- 配置成功,8004挂到了8002,8005挂到了8003下
验证
- 配置集群模式,每次登录都要设置集群模式
/usr/local/redis6/src/redis-cli -c -h 192.168.31.109 -p 8001 #每次登录都要设置集群模式
-
验证集群 节点信息 cluster info 查看集群信息 cluster nodes 查看节点信息
192.168.31.109:8002> 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:8783 cluster_stats_messages_pong_sent:8154 cluster_stats_messages_meet_sent:1 cluster_stats_messages_sent:16938 cluster_stats_messages_ping_received:8154 cluster_stats_messages_pong_received:8784 cluster_stats_messages_received:16938 192.168.31.109:8002> 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:8791 cluster_stats_messages_pong_sent:8161 cluster_stats_messages_meet_sent:1 cluster_stats_messages_sent:16953 cluster_stats_messages_ping_received:8161 cluster_stats_messages_pong_received:8792 cluster_stats_messages_received:16953
-
数据库验证,
[root@work1 bin]# ./redis-cli -c -h 192.168.31.109 -p 8001 #登录要指定集群模式 192.168.31.109:8001> set name 123 #写入一个节点 -> Redirected to slot [5798] located at 192.168.31.109:8002 #发生跳转到8002 OK 192.168.31.109:8006> get name -> Redirected to slot [5798] located at 192.168.31.109:8002 #发生跳转到8002 "123"
Redis水平扩展容
-
新建8007和8008文件夹,并按之前的方法修改配置文件
[root@work1 redis-cluster]# cp redis.conf ./8007 [root@work1 redis-cluster]# cp redis.conf ./8008
-
启动2台服务器
[root@work1 bin]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8008/redis.conf 2052:C 22 Jun 2021 18:19:00.340 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 2052:C 22 Jun 2021 18:19:00.340 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=2052, just started 2052:C 22 Jun 2021 18:19:00.340 # Configuration loaded [root@work1 bin]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/8007/redis.conf 2032:C 22 Jun 2021 18:18:26.940 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 2032:C 22 Jun 2021 18:18:26.940 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=2032, just started 2032:C 22 Jun 2021 18:18:26.940 # Configuration loaded
-
新增节点8007,记住后面是8001
/usr/local/redis/bin/redis-cli --cluster add-node 192.168.31.109:8007 192.168.31.109:8001
-
重新分配Hash槽,需要对节点进行编写,需要选择分配的Hash槽和接收节点,接收节点信息就算前面的节点信息,最后输入done完成
[root@work1 bin]# /usr/local/redis/bin/redis-cli --cluster reshard 192.168.31.109:8001 #重新分配Hash槽 >>> Performing Cluster Check (using node 192.168.31.109:8001) M: 787af44d9ca8338ac2bc312430b83ac72628ac3a 192.168.31.109:8001 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 541ecb4566102ee61b4550db37e726aeca94e737 192.168.31.109:8005 slots: (0 slots) slave replicates cb2002b7bf11991e467e9096b362dfd6daab60fd M: ae715962269cf2d9122d956253d180e90dcc8293 192.168.31.109:8002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: c5d2affde575df33b4a72f04b29e951d1e245609 192.168.31.109:8006 slots: (0 slots) slave replicates 787af44d9ca8338ac2bc312430b83ac72628ac3a M: cb2002b7bf11991e467e9096b362dfd6daab60fd 192.168.31.109:8003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: 6081324f55e7e933709669a65beba5ad3c793f6c 192.168.31.109:8007 slots: (0 slots) master S: c8d1034709353d4218560aa24cb4e21a31a009c4 192.168.31.109:8004 slots: (0 slots) slave replicates ae715962269cf2d9122d956253d180e90dcc8293 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)? 999 What is the receiving node ID? cb2002b7bf11991e467e9096b362dfd6daab60fd 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: done *** No source nodes given, operation aborted.
-
将从节点8008添加进去
/usr/local/redis/bin/redis-cli --cluster add-node 192.168.31.109:8008 192.168.31.109:8001
-
查看节点
cluster neodes
发现这里的8008是Master,8007也是Master
-
登录8008,设置跟随8007。后面的是Node ID
[root@work1 bin]# /usr/local/redis/bin/redis-cli -c -p 8008 127.0.0.1:8008> cluster replicate 6081324f55e7e933709669a65beba5ad3c793f6c OK
-
查看Nodes状态
成功!!!,8008是Salve