1.redies版本,redis-5.0.7,(注意:5.0版本之后将不再需要ruby)准备三台centos 7的机器:如下图机器的分配情况(每台机器关闭防火墙):
主从方式:三主三备
IP master节点 salve节点
192.168.0.220 wintop_redies1:7001 wintop_redies1:7002
192.168.0.221 wintop_redies2:7001 wintop_redies2:7002
192.168.0.222 wintop_redies3:7001 wintop_redies3:7002
2.如果centos中没有gcc环境,则需要先安装gcc。如果有就直接看下一步。
[root@localhost tools]# yum install -y gcc
3.解压redis包到当前目录下
[root@localhost tools]# tar -zxvf redis-5.0.7.tar.gz
4.进入刚解压出来的redis目录,开始编译安装
[root@localhost tools]# cd redis-5.0.5/ [root@localhost redis-5.0.5]# make && make PREFIX=/usr/local/redis install
5.看是否编译安装成功,执行 如下命令,如果输出0 表示redis安装成功了
[root@localhost redis-5.0.5]# echo $?
0
6.配置环境变量,在/etc/profile文件最后添加如下内容:
[root@localhost tools]# vim /etc/profile
###### redis env ######
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin
7.分别创建两个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/
8.建7001的配置文件,并添加如下内容:
vim /usr/local/redis/redis_cluster/7001/conf/redis.conf
# 绑定服务器域名或IP地址
bind 192.168.0.200
# 设置端口,区分集群中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
9.创建7002的配置文件,并添加如下内容:
vim /usr/local/redis/redis_cluster/7002/conf/redis.conf
# 绑定服务器域名或IP地址
bind 192.168.0.200
# 设置端口,区分集群中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
以上流程以220为例,其他节点相同
10.在保证上面220,221,222都配置完成后,开始启动220,221,222各节点,所有节点执行启动命令
redis-server /usr/local/redis/redis_cluster/7001/conf/redis.conf
redis-server /usr/local/redis/redis_cluster/7002/conf/redis.conf
11.查看各机器上的各节点是否都启动成功
[root@wintop-redies3 ~]# ps -ef | grep redis
root 2681 1 0 14:46 ? 00:00:00 redis-server 192.168.0.222:7001 [cluster]
root 2687 1 0 14:47 ? 00:00:00 redis-server 192.168.0.222:7002 [cluster]
root 2713 2695 0 14:49 pts/1 00:00:00 grep --color=auto redis
12.使用 reids-cli 创建Redis集群 (注:这里必须使用IP)
redis-cli --cluster create 192.168.0.220:7001 192.168.0.221:7001 192.168.0.222:7001 192.168.0.220:7002 192.168.0.221:7002 192.168.0.222:7002 --cluster-replicas 1
13.查看我们刚创建集群的状态,如下命令:(在3台集群中的任意一台机器查看任意节点都一样,会带出所有的节点信息)
[root@wintop-redies1 ~]# redis-cli --cluster check 192.168.0.220:7001
192.168.0.220:7001 (41cff485...) -> 1 keys | 5461 slots | 1 slaves.
192.168.0.222:7001 (feee6a96...) -> 1 keys | 5461 slots | 1 slaves.
192.168.0.221:7001 (6f76ed7d...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 2 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.0.220:7001)
M: 41cff4851ba6855e61fb3535dd53d329026587de 192.168.0.220:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: feee6a9623c65d41e0a3cfec2549ab8102c4d3e3 192.168.0.222:7001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: eef0f0520546060ffb9c4b276b1aacdc8e68e964 192.168.0.221:7002
slots: (0 slots) slave
replicates 41cff4851ba6855e61fb3535dd53d329026587de
M: 6f76ed7df33775a9cb7c3ce12fd28a527f942ec1 192.168.0.221:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 03e1579a63f773fe8ff0df25d09cea8d2449f1b6 192.168.0.220:7002
slots: (0 slots) slave
replicates feee6a9623c65d41e0a3cfec2549ab8102c4d3e3
S: a4737f4c75d6c70aef37ce0a5f163afd7502eb67 192.168.0.222:7002
slots: (0 slots) slave
replicates 6f76ed7df33775a9cb7c3ce12fd28a527f942ec1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
14.测试集群是否正常:在集群中的任意一台测试都可以,如我们可以在220上连接221上的7002节点并添加一个数据
[root@wintop-redies1 ~]# redis-cli -c -h 192.168.0.220 -p 7002
192.168.0.220:7002> set key001 xty_test
-> Redirected to slot [12657] located at 192.168.0.222:7001
OK
192.168.0.222:7001> get key001
"xty_test"
192.168.0.222:7001>
15.再去221上连接7001节点和222上连接7001或者7002看是否可以查询到数据
[root@wintop-redies2 ~]# redis-cli -c -h 192.168.0.221 -p 7001
192.168.0.221:7001> get key001
-> Redirected to slot [12657] located at 192.168.0.222:7001
"xty_test"
[root@wintop-redies3 ~]# redis-cli -c -h 192.168.0.222 -p 7002
192.168.0.222:7002> get key001
-> Redirected to slot [12657] located at 192.168.0.222:7001
"xty_test"
16.如果需要配置开机自启动,添加如下两行:(每台机器都要配置),保存退出即可。以后开机就会自动启动集群
[root@localhost redis-5.0.7]# 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
17.如果上面设置了,还是不能开机自启动,则有可能是/etc/rc.d/rc.local没有执行权限,需要给一个执行权限,如下:
[root@localhost redis-5.0.7]# chmod +x /etc/rc.d/rc.local