redis集群安装

Posted 杨小羊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis集群安装相关的知识,希望对你有一定的参考价值。

Redis 集群是一个提供在多个Redis间节点间共享数据的程序集.redis3.0以前,只支持主从同步的,如果主的挂了,写入就成问题了。3.0出来后就可以很好帮我们解决这个问题。
目前redis 3.0还不稳定,如果要用在生产环境中,要慎重。

一,redis服务器说明

192.168.120.45 7000  
192.168.120.45 7001  
192.168.120.45 7002  
  
192.168.120.46 7003  
192.168.120.46 7004  

192.168.120.46 7005

  
要让集群正常运作至少需要三个主节点,不过在刚开始试用集群功能时, 强烈建议使用六个节点: 其中三个为主节点, 而其余三个则是各个主节点的从节点。所以用二台机器,开6个redis进程,模拟6台机器。

 
二,安装redis
 1.将压缩包复制到/usr/local/目录下
 2.解压缩并安装redis
   [/usr/local/] #tar -xvzf redis-3.0.5.tar.gz   
   [/usr/local/] #cd redis-3.0.5
   [/usr/local/redis-3.0.5]#make
   [/usr/local/redis-3.0.5]#make install
   [/usr/local/redis-3.0.5]#cd src
   [/usr/local/redis-3.0.5]#cp redis-trib.rb /usr/local/bin
 
3.创建redis集群配置文件
    #mkdir /etc/redis
    #mkdi /var/log/redis
(2,3需要在两台服务上执行)
 
4.配置redis
   [/usr/local/redis-3.0.5]#vim redis.conf
修改内容:
    port 7000
    pidfile /var/run/redis-7000.pid  
    dbfilename dump-7000.rdb  
    appendfilename "appendonly-7000.aof"  
    cluster-config-file nodes-7000.conf  
    cluster-enabledyes  
    cluster-node-timeout 5000  
    appendonly yes 
 
5.copy配置文件,并修改端口
    # cp redis.conf /etc/redis/redis-7000.conf  
    # cp redis.conf /etc/redis/redis-7001.conf  
    # cp redis.conf /etc/redis/redis-7002.conf  
  
    # scp redis.conf 192.168.120.46:/etc/redis/redis-7003.conf  
    # scp redis.conf 192.168.120.46:/etc/redis/redis-7004.conf  
    # scp redis.conf 192.168.120.46:/etc/redis/redis-7005.conf  
   //将redis-7001.conf文件中的所有7000替换为7001
    # sed -i "s/7000/7001/g" /etc/redis/redis-7001.conf  
    # sed -i "s/7000/7002/g" /etc/redis/redis-7002.conf  
  
    # sed -i "s/7000/7003/g" /etc/redis/redis-7003.conf  
    # sed -i "s/7000/7004/g" /etc/redis/redis-7004.conf  
    # sed -i "s/7000/7005/g" /etc/redis/redis-7005.conf 
 
三,启动redis并查看redis
    #./redis-server /etc/redis/redis-7000.conf > /var/log/redis/redis-7000.log 2>&1 &  
    # ./redis-server /etc/redis/redis-7001.conf > /var/log/redis/redis-7001.log 2>&1 &  
    # ./redis-server /etc/redis/redis-7002.conf > /var/log/redis/redis-7002.log 2>&1 &  
  
    # ./redis-server /etc/redis/redis-7003.conf > /var/log/redis/redis-7003.log 2>&1 &  
    # ./redis-server /etc/redis/redis-7004.conf > /var/log/redis/redis-7004.log 2>&1 &  
    # ./redis-server /etc/redis/redis-7005.conf > /var/log/redis/redis-7005.log 2>&1 &  
  
    # netstat -tpnl |grep redis 
tcp        0      0 192.168.120.45:17000        0.0.0.0:*                   LISTEN      15658/./redis-serve 
tcp        0      0 192.168.120.45:17001        0.0.0.0:*                   LISTEN      15652/./redis-serve 
tcp        0      0 192.168.120.45:17002        0.0.0.0:*                   LISTEN      15644/./redis-serve 
tcp        0      0 192.168.120.45:7000         0.0.0.0:*                   LISTEN      15658/./redis-serve 
tcp        0      0 192.168.120.45:7001         0.0.0.0:*                   LISTEN      15652/./redis-serve 
tcp        0      0 192.168.120.45:7002         0.0.0.0:*                   LISTEN      15644/./redis-serve 
所有节点都启动成功,并不代表,他们就是集群了。
四,创建集群并查看
    

1,创建redis集群

# ./redis-trib.rb create --replicas 1 192.168.120.45:7000 192.168.120.45:7001 192.168.120.45:7002 192.168.120.46:7003 192.168.120.46:7004 192.168.120.46:7005

命令的意义如下:
给定 redis-trib.rb 程序的命令是 create , 这表示我们希望创建一个新的集群。
选项 --replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
之后跟着的其他参数则是实例的地址列表, 我们希望程序使用这些地址所指示的实例来创建新集群。
简单来说, 以上命令的意思就是让 redis-trib 程序创建一个包含三个主节点和三个从节点的集群。

2.查看redis集群状态

    #redis-trib.rb check 192.168.120.45:7000

Connecting to node 192.168.120.45:7000: OK
Connecting to node 192.168.120.46:7005: OK
Connecting to node 192.168.120.46:7004: OK
Connecting to node 192.168.120.46:7003: OK
Connecting to node 192.168.120.45:7001: OK
Connecting to node 192.168.120.45:7002: OK
>>> Performing Cluster Check (using node 192.168.120.45:7000)
M: f84ca7b52e70e179e82b56120db72b828a5a60a4 192.168.120.45:7000
   slots:5461-6917,6919-10922 (5461 slots) master
   1 additional replica(s)
S: 87eb060b519c5ca21cad4229b6bab4fa685c134c 192.168.120.46:7005
   slots: (0 slots) slave
   replicates f84ca7b52e70e179e82b56120db72b828a5a60a4
M: c9f204b2f1b82de04a0cac045164ee1d64ab8b54 192.168.120.46:7004
   slots:10923-16383 (5461 slots) master
   0 additional replica(s)
M: c18ab6cf7fd53ccbb7a4c38a385df4db8e22531d 192.168.120.46:7003
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5e502340bea055417e76985c4cc6355a66263a5b 192.168.120.45:7001
   slots: (0 slots) slave
   replicates c18ab6cf7fd53ccbb7a4c38a385df4db8e22531d
M: 13bbd4a90fef751a57247f92d44e3dc4db6b99ab 192.168.120.45:7002
   slots:6918 (1 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
 
到这儿,redis集群就配置成功了
五,测试redis集群
# ./redis-cli -c -p 7001 -h 192.168.120.45   //在45登录  
192.168.120.45:7001> set tank tank1    //设置测试值  
-> Redirected to slot [4407] located at 192.168.120.45:7000  
OK   //直接转向到45 7000端口,数据存到了45 7000,不过有点奇怪  
192.168.120.45:7001> get tank   //可以取到值  
"tank1"  
  
[[email protected] redis]# redis-cli -c -p 7005 -h 192.168.120.46  //46机器,7005端口  
192.168.120.46:7005> get tank  
-> Redirected to slot [4407] located at 192.168.120.46:7004 //直接转向46 7004端口  
"tank1"  
  
[[email protected] redis]# ps aux |grep redis //查看进程  
root      2838  0.0  0.0 105048  3344 pts/2    T    14:18   0:00 vim redis.conf
root      2870  0.0  0.0 105048  3312 pts/2    T    14:25   0:00 vim /etc/redis/redis-7004.conf
root      2977  0.0  0.0  95388  2644 ?        Ssl  14:58   0:01 ./redis-server 192.168.120.46:7003 [cluster]
root      2983  0.0  0.0  95388  2572 ?        Ssl  14:58   0:01 ./redis-server 192.168.120.46:7004 [cluster]
root      2987  0.0  0.0  95388  2628 ?        Ssl  14:58   0:01 ./redis-server 192.168.120.46:7005 [cluster]
root      3289  0.0  0.0  63412   780 pts/2    R+   16:48   0:00 grep redis
  
[[email protected] redis]# kill -9 2983 //关闭46 7004端口,看数据会不会丢失  
  
[[email protected] redis]# redis-trib.rb check 192.168.120.45:7000  //查看集群情况,任何接口都行 
Connecting to node 192.168.120.45:7000: OK
Connecting to node 192.168.120.46:7005: OK
Connecting to node 192.168.120.46:7003: OK
Connecting to node 192.168.120.45:7001: OK
Connecting to node 192.168.120.45:7002: OK
>>> Performing Cluster Check (using node 192.168.120.45:7000)
M: f84ca7b52e70e179e82b56120db72b828a5a60a4 192.168.120.45:7000
   slots:5461-6917,6919-10922 (5461 slots) master
   1 additional replica(s)
S: 87eb060b519c5ca21cad4229b6bab4fa685c134c 192.168.120.46:7005
   slots: (0 slots) slave
   replicates f84ca7b52e70e179e82b56120db72b828a5a60a4
M: c18ab6cf7fd53ccbb7a4c38a385df4db8e22531d 192.168.120.46:7003
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5e502340bea055417e76985c4cc6355a66263a5b 192.168.120.45:7001
   slots: (0 slots) slave
   replicates c18ab6cf7fd53ccbb7a4c38a385df4db8e22531d
M: 13bbd4a90fef751a57247f92d44e3dc4db6b99ab 192.168.120.45:7002
   slots:6918 (1 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.//变成,三主二从了  
  
[[email protected] src]#  ./redis-cli -c -p 7002 -h 192.168.120.45 //45登录 
192.168.120.45:7002> get tank
-> Redirected to slot [4407] located at 192.168.120.46:7003  //数据被转到了7003节点了  
"tank1"   
经测试,集群是可用的。
   

以上是关于redis集群安装的主要内容,如果未能解决你的问题,请参考以下文章

CentOS 7 Redis 集群安装

Linux(ubuntu)安装redis集群,redis集群搭建

Linux安装redis集群

LINUX安装REDIS集群

redis集群安装

redis离线集群安装