CentOS7快速搭建redis-5.0.7集群

  sre

集群

Redis Cluster 即 Redis 集群,是 Redis 官方在 3.0 版本推出的一套分布式存储方案。完全去中心化,由多个节点组成,所有节点彼此互联。Redis 客户端可以直接连接任何一节点获取集群中的键值对,不需要中间代理,如果该节点不存在用户所指定的键值,其内部会自动把客户端重定向到键值所在的节点。

Redis 集群是一个网状结构,每个节点都通过 TCP 连接跟其他每个节点连接。在一个有 N 个节点的集群中,每个节点都有 N-1 个流出的 TCP 连接,和 N-1 个流入的连接,这些 TCP 连接会永久保持。

主从规划

192.168.139.131:30001 192.168.139.131:30004
192.168.139.131:30002 192.168.139.131:30005
192.168.139.131:30003 192.168.139.131:30006

编译

yum -y install gcc
cd /root/
wget http://download.redis.io/releases/redis-5.0.7.tar.gz
tar -xzvf redis-5.0.7.tar.gz
cd redis-5.0.7
make -j 8
make install

实例准备

准备目录

mkdir -p /data/public/redis_cluster
cd /data/public/redis_cluster
mkdir -p 30001 30002 30003 30004 30005 30006

通用配置 集群主从认证aR0xn4mL9W_ik_VJ 集群对外密码NrDDW_iA8ycEFtd9

sed -i "s#bind 127.0.0.1#bind 0.0.0.0#g" /root/redis-5.0.7/redis.conf
sed -i "s#daemonize no#daemonize yes#g" /root/redis-5.0.7/redis.conf
sed -i "s#appendonly no#appendonly yes#g" /root/redis-5.0.7/redis.conf
sed -i "s#protected-mode yes#protected-mode no#g" /root/redis-5.0.7/redis.conf
echo 'cluster-node-timeout 5000' >> /root/redis-5.0.7/redis.conf
echo 'cluster-enabled yes' >> /root/redis-5.0.7/redis.conf
echo 'masterauth "aR0xn4mL9W_ik_VJ"' >> /root/redis-5.0.7/redis.conf
echo 'requirepass "NrDDW_iA8ycEFtd9"' >> /root/redis-5.0.7/redis.conf
/bin/cp -arf /root/redis-5.0.7/redis.conf /data/public/redis_cluster/30001/redis.conf
/bin/cp -arf /root/redis-5.0.7/redis.conf /data/public/redis_cluster/30002/redis.conf
/bin/cp -arf /root/redis-5.0.7/redis.conf /data/public/redis_cluster/30003/redis.conf
/bin/cp -arf /root/redis-5.0.7/redis.conf /data/public/redis_cluster/30004/redis.conf
/bin/cp -arf /root/redis-5.0.7/redis.conf /data/public/redis_cluster/30005/redis.conf
/bin/cp -arf /root/redis-5.0.7/redis.conf /data/public/redis_cluster/30006/redis.conf

各实例配置

sed -i "s#port 6379#port 30001#g" /data/public/redis_cluster/30001/redis.conf
sed -i "s#pidfile /var/run/redis_6379.pid#pidfile /data/public/redis_cluster/30001/redis_30001.pid#g" /data/public/redis_cluster/30001/redis.conf
sed -i 's#logfile ""#logfile "/data/public/redis_cluster/30001/redis_30001.log"#g' /data/public/redis_cluster/30001/redis.conf
echo 'dir /data/public/redis_cluster/30001' >> /data/public/redis_cluster/30001/redis.conf
echo 'cluster-config-file nodes-30001.conf' >> /data/public/redis_cluster/30001/redis.conf

sed -i "s#port 6379#port 30002#g" /data/public/redis_cluster/30002/redis.conf
sed -i "s#pidfile /var/run/redis_6379.pid#pidfile /data/public/redis_cluster/30002/redis_30002.pid#g" /data/public/redis_cluster/30002/redis.conf
sed -i 's#logfile ""#logfile "/data/public/redis_cluster/30002/redis_30002.log"#g' /data/public/redis_cluster/30002/redis.conf
echo 'dir /data/public/redis_cluster/30002' >> /data/public/redis_cluster/30002/redis.conf
echo 'cluster-config-file nodes-30002.conf' >> /data/public/redis_cluster/30002/redis.conf

sed -i "s#port 6379#port 30003#g" /data/public/redis_cluster/30003/redis.conf
sed -i "s#pidfile /var/run/redis_6379.pid#pidfile /data/public/redis_cluster/30003/redis_30003.pid#g" /data/public/redis_cluster/30003/redis.conf
sed -i 's#logfile ""#logfile "/data/public/redis_cluster/30003/redis_30003.log"#g' /data/public/redis_cluster/30003/redis.conf
echo 'dir /data/public/redis_cluster/30003' >> /data/public/redis_cluster/30003/redis.conf
echo 'cluster-config-file nodes-30003.conf' >> /data/public/redis_cluster/30003/redis.conf

sed -i "s#port 6379#port 30004#g" /data/public/redis_cluster/30004/redis.conf
sed -i "s#pidfile /var/run/redis_6379.pid#pidfile /data/public/redis_cluster/30004/redis_30004.pid#g" /data/public/redis_cluster/30004/redis.conf
sed -i 's#logfile ""#logfile "/data/public/redis_cluster/30004/redis_30004.log"#g' /data/public/redis_cluster/30004/redis.conf
echo 'dir /data/public/redis_cluster/30004' >> /data/public/redis_cluster/30004/redis.conf
echo 'cluster-config-file nodes-30004.conf' >> /data/public/redis_cluster/30004/redis.conf

sed -i "s#port 6379#port 30005#g" /data/public/redis_cluster/30005/redis.conf
sed -i "s#pidfile /var/run/redis_6379.pid#pidfile /data/public/redis_cluster/30005/redis_30005.pid#g" /data/public/redis_cluster/30005/redis.conf
sed -i 's#logfile ""#logfile "/data/public/redis_cluster/30005/redis_30005.log"#g' /data/public/redis_cluster/30005/redis.conf
echo 'dir /data/public/redis_cluster/30005' >> /data/public/redis_cluster/30005/redis.conf
echo 'cluster-config-file nodes-30005.conf' >> /data/public/redis_cluster/30005/redis.conf

sed -i "s#port 6379#port 30006#g" /data/public/redis_cluster/30006/redis.conf
sed -i "s#pidfile /var/run/redis_6379.pid#pidfile /data/public/redis_cluster/30006/redis_30006.pid#g" /data/public/redis_cluster/30006/redis.conf
sed -i 's#logfile ""#logfile "/data/public/redis_cluster/30006/redis_30006.log"#g' /data/public/redis_cluster/30006/redis.conf
echo 'dir /data/public/redis_cluster/30006' >> /data/public/redis_cluster/30006/redis.conf
echo 'cluster-config-file nodes-30006.conf' >> /data/public/redis_cluster/30006/redis.conf

重启各实例restart-redis-cluster.sh

#!/bin/bash
pkill redis-server
sleep 0.5 && ps -ef |grep redis-server
/usr/local/bin/redis-server /data/public/redis_cluster/30001/redis.conf
sleep 0.5 && ps -ef |grep redis-server
/usr/local/bin/redis-server /data/public/redis_cluster/30002/redis.conf
sleep 0.5 && ps -ef |grep redis-server
/usr/local/bin/redis-server /data/public/redis_cluster/30003/redis.conf
sleep 0.5 && ps -ef |grep redis-server
/usr/local/bin/redis-server /data/public/redis_cluster/30004/redis.conf
sleep 0.5 && ps -ef |grep redis-server
/usr/local/bin/redis-server /data/public/redis_cluster/30005/redis.conf
sleep 0.5 && ps -ef |grep redis-server
/usr/local/bin/redis-server /data/public/redis_cluster/30006/redis.conf
sleep 0.5 && ps -ef |grep redis-server

启动正常:

[root@localhost 30001]# ps -ef |grep redis
root 18430 1 0 23:28 ? 00:00:00 redis-server 0.0.0.0:30001 [cluster]
root 18435 1 0 23:28 ? 00:00:00 redis-server 0.0.0.0:30002 [cluster]
root 18437 1 0 23:28 ? 00:00:00 redis-server 0.0.0.0:30003 [cluster]
root 18442 1 0 23:28 ? 00:00:00 redis-server 0.0.0.0:30004 [cluster]
root 18450 1 0 23:28 ? 00:00:00 redis-server 0.0.0.0:30005 [cluster]
root 18455 1 0 23:28 ? 00:00:00 redis-server 0.0.0.0:30006 [cluster]
root 18460 6879 0 23:28 pts/0 00:00:00 grep –color=auto redis

组集群

redis-cli --cluster create 192.168.139.131:30001 192.168.139.131:30002 192.168.139.131:30003 192.168.139.131:30004 192.168.139.131:30005 192.168.139.131:30006 --cluster-replicas 1 -a NrDDW_iA8ycEFtd9

redis-cli --cluster create只能是redis 5.0用,老版本需要用trib
主节点在前,从节点在后。
其中–cluster-replicas参数用来指定一个主节点带有的从节点个数,如上–cluster-replicas 1即表示 1 个主节点有 1 个从节点。
一主一从配置,六个节点就是 三主三从
命令执行成功会有类似如下输出:
捕获.PNG

客户端登录

[root@localhost 30001]# redis-cli -c -p 30001 -h 192.168.139.131 -a NrDDW_iA8ycEFtd9 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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:1
cluster_stats_messages_ping_sent:1494
cluster_stats_messages_pong_sent:1513
cluster_stats_messages_sent:3007
cluster_stats_messages_ping_received:1508
cluster_stats_messages_pong_received:1494
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:3007
[root@localhost ~]# redis-cli -c -p 30001 -h 192.168.139.131 -a NrDDW_iA8ycEFtd9 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
737d62e72a879173a2732bb8a8aa8ffe6bb59ed6 192.168.139.131:30006@40006 slave 7cf2aa93223f98eca26062e5374826bc51e3c5a3 0 1608181634858 6 connected
ad40c09b967826a780882e930c8761f49a96c5a9 192.168.139.131:30002@40002 master - 0 1608181635880 2 connected 5461-10922
5b674bdeb98db89efecaaba8cebbdd4ba2349731 192.168.139.131:30001@40001 myself,master - 0 1608181636000 1 connected 0-5460
611a6fc69fc92a1fb21a02e542431b5d760f16dd 192.168.139.131:30004@40004 slave 5b674bdeb98db89efecaaba8cebbdd4ba2349731 0 1608181636599 4 connected
7cf2aa93223f98eca26062e5374826bc51e3c5a3 192.168.139.131:30003@40003 master - 0 1608181635574 3 connected 10923-16383
bef6d434a9604ee333351f1587a325a185bd772f 192.168.139.131:30005@40005 slave ad40c09b967826a780882e930c8761f49a96c5a9 0 1608181635000 5 connected

LEAVE A COMMENT

Captcha Code