Centos7 Redis5.0.5 三主三从集群安装和环境配置
1.下载Redis
打开redis官网https://redis.io/下载最新版http://download.redis.io/releases/redis-5.0.5.tar.gz
cd /usr/local/00temp
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
2.编译安装Redis
需要先安装gcc编译器
yum -y install gcc
解压Redis包然后编译安装
tar -zxvf /usr/local/00temp/redis-5.0.5.tar.gz -C /usr/local
cd /usr/local/redis-5.0.5
#安装在当前目录
make && make install
#或者安装到指定目录/usr/local/jervalj/redis
make && make PREFIX=/usr/local/jervalj/redis install
二. 创建节点
1. 在/usr/local下创建了一个redis-cluster文件夹用于存放redis节点的配置文件(redis.conf)
mkdir /usr/local/redis-cluster
cd /usr/local/redis-cluster
mkdir 9000 9001
cp /usr/local/redis-5.0.5/redis.conf /usr/local/redis-cluster/9000
#也可以将其放到/usr/local/jervalj/redis/bin下面统一管理
cp /usr/local/redis-5.0.5/redis.conf /usr/local/jervalj/redis/bin
也可以直接将
2.修改配置文件, 9000和9001文件夹下各一个配置文件 都需要修改
bind 0.0.0.0//开启网络,保证其他网络可以访问该机子
port 9000//每个节点的端口号
daemonize yes//守护线程 ,后台运行redis
pidfile /var/run/redis_9000.pid
appendonly yes//aof日志每一个操作都记录模式
cluster-enabled yes//开启集群模式
cluster-config-file nodes_9000.conf//集群节点的配置
cluster-node-timeout 5000//节点请求超时时间
复制上面9000下改好的文件
cp /usr/local/redis-cluster/9000/redis.conf /usr/local/redis-cluster/9001
9001参照9000的配置只需要把对应端口的地方改了
bind 0.0.0.0//开启网络,保证其他网络可以访问该机子
port 9001//每个节点的端口号
daemonize yes//守护线程 ,后台运行redis
pidfile /var/run/redis_9001.pid
appendonly yes//aof日志每一个操作都记录模式
cluster-enabled yes//开启集群模式
cluster-config-file nodes_9001.conf//集群节点的配置
cluster-node-timeout 5000//节点请求超时时间
protected-mode no//关闭保护模式
requirepass xxx //设置redis访问密码
masterauth xxx //设置集群节点间访问密码,跟上面一致
三. 分发该配置到另外两台机器
scp -r /usr/local/redis-cluster 192.168.31.112:/usr/local
#然后输入root密码
scp -r /usr/local/redis-cluster 192.168.31.113:/usr/local
#然后输入root密码
四. 启动每台机器上的两个节点
cd /usr/local/redis-5.0.5/src
redis-server /usr/local/redis-cluster/9000/redis.conf
redis-server /usr/local/redis-cluster/9001/redis.conf
同样的操作去另外两条机器启动剩下的四个节点。
验证启动是否成功
ps -ef | grep redis
查看redis版本:
#查看redis-server的版本
redis-server --version
#查看redis-cli的版本
redis-cli --version
五. 创建集群
redis5.0集群创建方式改为了C编写的redis-cli创建,不用再安装麻烦的ruby了。
#创建集群,--cluster-replicas 1指定从库数量1
cd /usr/local/redis-5.0.5/src
redis-cli --cluster create 192.168.31.111:9000 192.168.31.112:9000 192.168.31.113:9000 192.168.31.111:9001 192.168.31.112:9001 192.168.31.113:9001 --cluster-replicas 1
#创建顺序三个主,三个从,前面三个是主后面三个是从
如果配置了密码则加上密码信息:
cd /usr/local/redis-5.0.5/src
redis-cli -a xxx --cluster create 192.168.31.111:9000 192.168.31.112:9000 192.168.31.113:9000 192.168.31.111:9001 192.168.31.112:9001 192.168.31.113:9001 --cluster-replicas 1
启动后,可看到成功信息,如下:
[root@localhost src]# redis-cli --cluster create 192.168.31.111:9000 192.168.31.112:9000 192.168.31.113:9000 192.168.31.111:9001 192.168.31.112:9001 192.168.31.113:9001 --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.31.112:9001 to 192.168.31.111:9000
Adding replica 192.168.31.113:9001 to 192.168.31.112:9000
Adding replica 192.168.31.111:9001 to 192.168.31.113:9000
M: d84caba18d4913fdef35bad78b049a42453b9876 192.168.31.111:9000
slots:[0-5460] (5461 slots) master
M: 14cc319ba6b60eccb4412059f6676fc2a34cbd41 192.168.31.112:9000
slots:[5461-10922] (5462 slots) master
M: 0c97db8729b4fa8ff139e4bc163a9d533ccddd70 192.168.31.113:9000
slots:[10923-16383] (5461 slots) master
S: 180e7d0f39fb84827c8c084b74276d3d5bed8c00 192.168.31.111:9001
replicates 0c97db8729b4fa8ff139e4bc163a9d533ccddd70
S: 98d02ee6890df6fcfd9c44b4e3ae361d62c990be 192.168.31.112:9001
replicates d84caba18d4913fdef35bad78b049a42453b9876
S: 7ab7412f40f4223890266f6dfddf3ec7ec2e7515 192.168.31.113:9001
replicates 14cc319ba6b60eccb4412059f6676fc2a34cbd41
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.31.111:9000)
M: d84caba18d4913fdef35bad78b049a42453b9876 192.168.31.111:9000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 7ab7412f40f4223890266f6dfddf3ec7ec2e7515 192.168.31.113:9001
slots: (0 slots) slave
replicates 14cc319ba6b60eccb4412059f6676fc2a34cbd41
S: 98d02ee6890df6fcfd9c44b4e3ae361d62c990be 192.168.31.112:9001
slots: (0 slots) slave
replicates d84caba18d4913fdef35bad78b049a42453b9876
M: 0c97db8729b4fa8ff139e4bc163a9d533ccddd70 192.168.31.113:9000
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 180e7d0f39fb84827c8c084b74276d3d5bed8c00 192.168.31.111:9001
slots: (0 slots) slave
replicates 0c97db8729b4fa8ff139e4bc163a9d533ccddd70
M: 14cc319ba6b60eccb4412059f6676fc2a34cbd41 192.168.31.112:9000
slots:[5461-10922] (5462 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.
[root@localhost src]#
六、进入redis客户端
cd /usr/local/redis/redis-5.0.5/src
./redis-cli
七、退出客户端
exit
八、退出redis服务:
pkill redis-server
kill 进程号
src/redis-cli shutdown
九,验证集群:
连接任意一个客户端即可:
/usr/local/redis-5.0.5/src/redis-cli -a xxx -c -h 192.168.31.111 -p 9001
#提示:-a访问服务端密码,-c表示集群模式,指定ip地址和端口号
注意这里可能会redirected进入到其它server。
进行验证: cluster info(查看集群信息)、cluster nodes(查看节点列表)
192.168.31.111:9001> 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:287
cluster_stats_messages_pong_sent:277
cluster_stats_messages_sent:564
cluster_stats_messages_ping_received:277
cluster_stats_messages_pong_received:286
cluster_stats_messages_received:563
192.168.31.111:9001> cluster nodes
14cc319ba6b60eccb4412059f6676fc2a34cbd41 192.168.31.112:9000@19000 master - 0 1564303047602 2 connected 5461-10922
180e7d0f39fb84827c8c084b74276d3d5bed8c00 192.168.31.111:9001@19001 myself,slave 0c97db8729b4fa8ff139e4bc163a9d533ccddd70 0 1564303046000 4 connected
98d02ee6890df6fcfd9c44b4e3ae361d62c990be 192.168.31.112:9001@19001 slave d84caba18d4913fdef35bad78b049a42453b9876 0 1564303048005 5 connected
d84caba18d4913fdef35bad78b049a42453b9876 192.168.31.111:9000@19000 master - 0 1564303046000 1 connected 0-5460
7ab7412f40f4223890266f6dfddf3ec7ec2e7515 192.168.31.113:9001@19001 slave 14cc319ba6b60eccb4412059f6676fc2a34cbd41 0 1564303047600 6 connected
0c97db8729b4fa8ff139e4bc163a9d533ccddd70 192.168.31.113:9000@19000 master - 0 1564303047000 3 connected 10923-16383
进行数据操作验证
192.168.31.111:9001> set mykey aaa
-> Redirected to slot [14687] located at 192.168.31.113:9000
OK
192.168.31.113:9000> get mykey
"aaa"
192.168.31.113:9000>
关闭集群则需要逐个进行关闭,使用命令:
/usr/local/redis-5.0.5/src/redis-cli -a xxx -c -h 192.168.31.111 -p 9001 shutdown