CentOS 7搭建Redis5.0.5集群(三台机器,3主3备)
作者:互联网
CentOS 7搭建Redis5.0.5集群(三台机器,3主3备)
准备工作
准备三台centos 7的机器:如下图机器的分配情况(每台机器关闭防火墙):
IP master节点 salve节点
192.168.0.166 redis-166:7001 redis-166:7002
192.168.0.167 redis-167:7001 redis-167:7002
192.168.0.168 redis-168:7001 redis-168:7002
设置三台机器的/etc/hosts,每台都要添加如下三行,保存退出
[root@localhost tools]# vim /etc/hosts
192.168.0.166 redis-166
192.168.0.167 redis-167
192.168.0.168 redis-168
1
2
3
4
官网下载redis-5.0.5版本: http://download.redis.io/releases/redis-5.0.5.tar.gz
把下载的包上传到上面3台centos中
开始安装
我们以192.168.0.166机器安装为例,其他两台只是配置文件的IP不一样
如果centos中没有gcc环境,则需要先安装gcc。如果有就直接看下一步。
[root@localhost tools]# yum install -y gcc
1
解压redis包到当前目录下
[root@localhost tools]# tar -zxvf redis-5.0.5.tar.gz
1
进入刚解压出来的redis目录,开始编译安装
[root@localhost tools]# cd redis-5.0.5/
[root@localhost redis-5.0.5]# make && make PREFIX=/usr/local/redis install
1
2
查看是否编译安装成功,执行 如下命令,如果输出0 表示redis安装成功了
[root@localhost redis-5.0.5]# echo $?
0
1
2
配置环境变量,在/etc/profile文件最后添加如下内容:
[root@localhost tools]# vim /etc/profile
###### redis env ######
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin
1
2
3
4
5
其他167和168两台机器,安装redis跟上面166机器完全一样
集群配置
我们还是以192.168.0.166机器安装为例,其他两台只是配置文件的IP不一样
分别创建两个7001和7002的配置文件目录conf,日志目录logs,数据存储目录data,如下命令:
mkdir -p /usr/local/redis/redis_cluster/7001/conf/
mkdir -p /usr/local/redis/redis_cluster/7001/logs/
mkdir -p /usr/local/redis/redis_cluster/7001/data/
mkdir -p /usr/local/redis/redis_cluster/7002/conf/
mkdir -p /usr/local/redis/redis_cluster/7002/logs/
mkdir -p /usr/local/redis/redis_cluster/7002/data/
1
2
3
4
5
6
7
创建7001的配置文件,并添加如下内容:
vim /usr/local/redis/redis_cluster/7001/conf/redis.conf
# 绑定服务器域名或IP地址
bind redis-166
# 设置端口,区分集群中Redis的实例
port 7001
# 后台运行
daemonize yes
# pid进程文件名,以端口号命名
pidfile /var/run/redis-7001.pid
# 日志文件名称,以端口号为目录来区分
logfile /usr/local/redis/redis_cluster/7001/logs/redis.log
# 数据文件存放地址,以端口号为目录名来区分
dir /usr/local/redis/redis_cluster/7001/data
# 启用集群
cluster-enabled yes
# 配置每个节点的配置文件,同样以端口号为名称
cluster-config-file nodes_7001.conf
# 配置集群节点的超时时间
cluster-node-timeout 15000
# 启动AOF增量持久化策略
appendonly yes
# 发生改变,则记录日志
appendfsync always
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
创建7002的配置文件,并添加如下内容:
vim /usr/local/redis/redis_cluster/7002/conf/redis.conf
# 绑定服务器域名或IP地址
bind redis-166
# 设置端口,区分集群中Redis的实例
port 7002
# 后台运行
daemonize yes
# pid进程文件名,以端口号命名
pidfile /var/run/redis-7002.pid
# 日志文件名称,以端口号为目录来区分
logfile /usr/local/redis/redis_cluster/7002/logs/redis.log
# 数据文件存放地址,以端口号为目录名来区分
dir /usr/local/redis/redis_cluster/7002/data
# 启用集群
cluster-enabled yes
# 配置每个节点的配置文件,同样以端口号为名称
cluster-config-file nodes_7002.conf
# 配置集群节点的超时时间
cluster-node-timeout 15000
# 启动AOF增量持久化策略
appendonly yes
# 发生改变,则记录日志
appendfsync always
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
其他167和168两台机器,集群配置跟上面166机器配置只是域名不同,其他设置都是一样的
启动集群
在保证上面166,167,168都配置完成后,开始启动166,167,168各节点
redis-server /usr/local/redis/redis_cluster/7001/conf/redis.conf
redis-server /usr/local/redis/redis_cluster/7002/conf/redis.conf
1
2
查看各机器上的各节点是否都启动成功
[root@localhost redis-5.0.5]# ps -ef | grep redis
root 6460 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-166:7001 [cluster]
root 6471 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-166:7002 [cluster]
1
2
3
4
[root@localhost redis-5.0.5]# ps -ef | grep redis
root 6532 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-167:7001 [cluster]
root 6548 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-167:7002 [cluster]
1
2
3
4
[root@localhost redis-5.0.5]# ps -ef | grep redis
root 6486 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-168:7001 [cluster]
root 6498 1 0 14:14 ? 00:00:10 /usr/local/redis/bin/redis-server redis-168:7002 [cluster]
1
2
3
4
使用 reids-cli 创建Redis集群 (注:这里必须使用IP,如果使用像redis-166这样的域名,则会报错,具体原因我也不清楚)
redis-cli --cluster create 192.168.0.166:7001 192.168.0.167:7001 192.168.0.168:7001 192.168.0.166:7002 192.168.0.167:7002 192.168.0.168:7002 --cluster-replicas 1
1
查看我们刚创建集群的状态,如下命令:(在3台集群中的任意一台机器查看任意节点都一样,会带出所有的节点信息)
[root@localhost redis-5.0.5]# redis-cli --cluster check 192.168.0.167:7001
192.168.0.167:7001 (b909c05c...) -> 0 keys | 5462 slots | 1 slaves.
192.168.0.166:7001 (e136a43b...) -> 0 keys | 5461 slots | 1 slaves.
192.168.0.168:7001 (4bf0b7df...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.0.167:7001)
M: b909c05ca4c89695fee7b4799050312ed20c989e 192.168.0.167:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: e136a43b8dbfd7612f48fe2c17e33203d5329eed 192.168.0.166:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 4bf0b7df6ce34ad2d3ee87ab500b200f3ae64cee 192.168.0.168:7001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 84ada651baf494cbcdbfe26232b0061146260a3d 192.168.0.167:7002
slots: (0 slots) slave
replicates e136a43b8dbfd7612f48fe2c17e33203d5329eed
S: 4b9a181365ba49dde3def4d9d562c5a4d4ef657e 192.168.0.166:7002
slots: (0 slots) slave
replicates 4bf0b7df6ce34ad2d3ee87ab500b200f3ae64cee
S: 02c3fc75bc6e6d4e070563f2b42a41f2616f66a5 192.168.0.168:7002
slots: (0 slots) slave
replicates b909c05ca4c89695fee7b4799050312ed20c989e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
测试集群是否正常:
在集群中的任意一台测试都可以,如我们可以在166上连接167上的7002节点并添加一个数据
[root@localhost redis-5.0.5]# redis-cli -c -h 192.168.0.167 -p 7002
192.168.0.167:7002> set key001 helloRredis
-> Redirected to slot [274] located at 192.168.0.166:7001
OK
192.168.0.166:7001> get key001
"helloRredis"
192.168.0.166:7001>
1
2
3
4
5
6
7
8
再去167上连接7001节点和168上连接7001或者7002看是否可以查询到数据
[root@localhost redis-5.0.5]# redis-cli -c -h 192.168.0.167 -p 7001
192.168.0.167:7001> get key001
-> Redirected to slot [274] located at 192.168.0.166:7001
"helloRredis"
192.168.0.166:7001>
1
2
3
4
5
6
[root@localhost redis-5.0.5]# redis-cli -c -h 192.168.0.168 -p 7002
192.168.0.168:7002> get key001
-> Redirected to slot [274] located at 192.168.0.166:7001
"helloRredis"
192.168.0.166:7001>
1
2
3
4
5
6
出现上面的结果,说明我们搭建的集群运作正常
如果需要配置开机自启动,添加如下两行:(每台机器都要配置),保存退出即可。以后开机就会自动启动集群
[root@localhost redis-5.0.5]# vim /etc/rc.local
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7001/conf/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7002/conf/redis.conf
1
2
3
4
如果上面设置了,还是不能开机自启动,则有可能是/etc/rc.d/rc.local没有执行权限,需要给一个执行权限,如下:
[root@localhost redis-5.0.5]# chmod +x /etc/rc.d/rc.local
1
到此集群全部搭建完成。
————————————————
版权声明:本文为CSDN博主「默默的跳跳蛙」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/molaobusi/article/details/100802602
|
标签:CentOS,三台,redis,192.168,cluster,Redis5.0,7001,7002,local 来源: https://www.cnblogs.com/iancloud/p/15019922.html