redis集群
做3主3从
先都安装redis,这里是编译安装
[root@redis-master ~]# mkdir -p /data/application ---创建工作目录 [root@redis-master ~]# wget http://download.redis.io/releases/redis-5.0.10.tar.gz ---下载redis [root@redis-master ~]# tar xzf redis-5.0.10.tar.gz -C /data/application/ ---解压 [root@redis-master ~]# cd /data/application/ [root@redis-master application]# mv redis-5.0.10/ redis [root@redis-master application]# cd redis/ [root@redis-master redis]# yum install -y gcc make #安装编译工具 [root@redis-master redis]# make [root@redis-master redis]# cp redis.conf redis.conf.bak [root@redis-master redis]# vim redis.conf ---修改如下 bind 192.168.246.202 #只监听内网IP daemonize yes #开启后台模式将on改为yes timeout 300 #连接超时时间 port 6379 #端口号 dir /data/application/redis/data #本地数据库存放持久化数据的目录该目录-----需要存在 pidfile /var/run/redis_6379.pid #定义pid文件 logfile /var/log/redis.log #定义log文件 创建存放数据的目录 [root@redis-master redis]# mkdir /data/application/redis/data 配置redis为systemctl启动 [root@redis-master redis]# cd /lib/systemd/system [root@redis-master system]# vim redis.service [Unit] Description=Redis After=network.target [Service] ExecStart=/data/application/redis/src/redis-server /data/application/redis/redis.conf --daemonize no ExecStop=/data/application/redis/src/redis-cli -h 127.0.0.1 -p 6379 shutdown [Install] WantedBy=multi-user.target 启动服务: [root@redis-master system]# systemctl daemon-reload [root@redis-master system]# systemctl start redis.service
创建目录节点: [root@master application]# mv redis/ redis-1001 [root@master application]# ls redis-1001 [root@master application]# cp -r redis-1001/ redis-1002/ --第二个节点 [root@master application]# scp -r redis-1001/ 192.168.19.150:/data/application/redis-1003 别的都远程拷贝过去,改一下端口号就可以 在配置文件redis-conf修改:%s/1001/1003/g 启动命令: [root@master application]# redis-1001/src/redis-server redis-1002/redis.conf 可以查看端口号 [root@master application]# ss -tlnp State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 511 0.0.0.0:1002 0.0.0.0:* users:(("redis-server",pid=6101,fd=6)) LISTEN 0 511 0.0.0.0:11002 0.0.0.0:* users:(("redis-server",pid=6101,fd=9)) LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=749,fd=3))
创建集群 [root@master application]# /data/application/redis-1001/src/redis-cli -h 192.168.19.156 -p 1001 --cluster create --cluster-replicas 1 192.168.19.156:1001 192.168.19.156:1002 192.168.19.150:1003 192.168.19.150:1004 192.168.19.157:1005 192.168.19.157:1006 >>> 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.19.150:1004 to 192.168.19.156:1001 Adding replica 192.168.19.157:1006 to 192.168.19.150:1003 Adding replica 192.168.19.156:1002 to 192.168.19.157:1005 M: 6bdd26bd3bc88bfdea8c7ab66403dfc6ecf577e3 192.168.19.156:1001 slots:[0-5460] (5461 slots) master S: e811e31d8383d17b80d15e59140bf1dab5cbdd1f 192.168.19.156:1002 replicates 67c3a67751b2a3549cb1e9a5abd5e641c9affb8f M: 418b07c0d776a3e1c2952e13a6c31a4634047a28 192.168.19.150:1003 slots:[5461-10922] (5462 slots) master S: 7273e12e3210b1b916485c2d421f29a826957bb5 192.168.19.150:1004 replicates 6bdd26bd3bc88bfdea8c7ab66403dfc6ecf577e3 M: 67c3a67751b2a3549cb1e9a5abd5e641c9affb8f 192.168.19.157:1005 slots:[10923-16383] (5461 slots) master S: 1d982b23c5ab8a0aff264003ac2f22a09925610a 192.168.19.157:1006 replicates 418b07c0d776a3e1c2952e13a6c31a4634047a28 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.19.156:1001) M: 6bdd26bd3bc88bfdea8c7ab66403dfc6ecf577e3 192.168.19.156:1001 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 418b07c0d776a3e1c2952e13a6c31a4634047a28 192.168.19.150:1003 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: e811e31d8383d17b80d15e59140bf1dab5cbdd1f 192.168.19.156:1002 slots: (0 slots) slave replicates 67c3a67751b2a3549cb1e9a5abd5e641c9affb8f S: 7273e12e3210b1b916485c2d421f29a826957bb5 192.168.19.150:1004 slots: (0 slots) slave replicates 6bdd26bd3bc88bfdea8c7ab66403dfc6ecf577e3 M: 67c3a67751b2a3549cb1e9a5abd5e641c9affb8f 192.168.19.157:1005 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 1d982b23c5ab8a0aff264003ac2f22a09925610a 192.168.19.157:1006 slots: (0 slots) slave replicates 418b07c0d776a3e1c2952e13a6c31a4634047a28 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
查看集群信息 192.168.19.150:1003> 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:3 cluster_stats_messages_ping_sent:5460 cluster_stats_messages_pong_sent:5446 cluster_stats_messages_meet_sent:1 cluster_stats_messages_sent:10907 cluster_stats_messages_ping_received:5446 cluster_stats_messages_pong_received:5461 cluster_stats_messages_received:10907 192.168.19.150:1003> 192.168.19.150:1003>
查看集群所有节点 192.168.19.150:1003> cluster nodes 6bdd26bd3bc88bfdea8c7ab66403dfc6ecf577e3 192.168.19.156:1001@11001 master - 0 1719491089253 1 connected 0-5460 67c3a67751b2a3549cb1e9a5abd5e641c9affb8f 192.168.19.157:1005@11005 master - 0 1719491089252 5 connected 10923-16383 7273e12e3210b1b916485c2d421f29a826957bb5 192.168.19.150:1004@11004 slave 6bdd26bd3bc88bfdea8c7ab66403dfc6ecf577e3 0 1719491089252 4 connected 1d982b23c5ab8a0aff264003ac2f22a09925610a 192.168.19.157:1006@11006 slave 418b07c0d776a3e1c2952e13a6c31a4634047a28 0 1719491089373 6 connected 418b07c0d776a3e1c2952e13a6c31a4634047a28 192.168.19.150:1003@11003 myself,master - 0 1719491089000 3 connected 5461-10922 e811e31d8383d17b80d15e59140bf1dab5cbdd1f 192.168.19.156:1002@11002 slave 67c3a67751b2a3549cb1e9a5abd5e641c9affb8f 0 1719491089147 5 connected 192.168.19.150:1003> ---myself:从哪个节点登录进来的,当时增加信息的时候分配到了1003这个节点
当其中的一个master宕掉之后,slave会变成新的master,旧的master重启之后会变成slave
添加新的主节点,首先你得有这个端口:1007 [root@redis-1 ~]# /data/application/redis-1001/src/redis-cli -h 192.168.19.156 -p 1001 cluster meet 10.36.192.195 1007
添加新的从节点,先添加节点 [root@redis-1 ~]# /data/application/redis-1001/src/redis-cli -h 192.168.19.156 -p 1001 cluster meet 10.36.192.195 1008 然后在将这个节点指定为从节点: [root@redis-1 ~]# /data/application/redis-1001/src/redis-cli -h 192.168.19.156 -p 1008 cluster replicate 主的id