redis-5.0.3集群搭建

首先部署redis-5.0.3,请参考我的另一篇文章

https://www.cnblogs.com/djlsunshine/p/10592174.html

启动redis服务

# redis-server /root/redis-5.0.3/redis.conf

[root@localhost ~]# redis-server /root/redis-5.0.3/redis.conf
2753:C 25 Mar 2019 22:58:01.274 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2753:C 25 Mar 2019 22:58:01.274 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=2753, just started
2753:C 25 Mar 2019 22:58:01.274 # Configuration loaded

查看进程

# ps aux | grep redis

[root@localhost ~]# ps aux | grep redis
root       2754  0.0  0.4 153828  7716 ?        Ssl  22:58   0:00 redis-server 192.168.2.80:6379
root       2759  0.0  0.0 112644   952 pts/1    S+   22:58   0:00 grep --color=auto redis

创建6个节点

# mkdir cluster-test

# cd cluster-test/

# mkdir 7000 7001 7002 7003 7004 7005

[root@localhost redis-5.0.3]# mkdir cluster-test
[root@localhost redis-5.0.3]#
[root@localhost redis-5.0.3]# ls
00-RELEASENOTES  cluster-test  COPYING  dump.rdb  Makefile   README.md   runtest          runtest-sentinel  src    utils
BUGS             CONTRIBUTING  deps     INSTALL   MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests
[root@localhost redis-5.0.3]#
[root@localhost redis-5.0.3]# cd cluster-test/
[root@localhost cluster-test]#
[root@localhost cluster-test]# mkdir 7000 7001 7002 7003 7004 7005
[root@localhost cluster-test]# ls
7000  7001  7002  7003  7004  7005

 分别在这6个节点文件夹下写入redis配置文件

以节点7000为例

port 7000                          //端口7000,7002,7003
daemonize yes                      //redis后台运行
bind 192.168.2.80                  //设置节点的IP
requirepass 123456                 //设置连接节点的密码
appendfilename "appendonly.aof"    //aof模式持久化文件的名字(appendonly默认是no,yes就是打开aof持久化)
pidfile /var/run/redis_7000.pid    //pidfile文件对应7000,7002,7003
cluster-enabled yes                //开启集群
cluster-config-file nodes_7000.conf    //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 5000              //请求超时  设置5秒够了
appendonly yes                       //aof日志开启  有需要就开启,它会每次写操作都记录一条日志

启动各节点

[root@localhost ~]# redis-server redis-5.0.3/cluster-test/7000/redis.conf
3543:C 26 Mar 2019 19:02:32.393 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3543:C 26 Mar 2019 19:02:32.393 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=3543, just started
3543:C 26 Mar 2019 19:02:32.393 # Configuration loaded
[root@localhost ~]# redis-server redis-5.0.3/cluster-test/7001/redis.conf
3548:C 26 Mar 2019 19:02:37.603 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3548:C 26 Mar 2019 19:02:37.603 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=3548, just started
3548:C 26 Mar 2019 19:02:37.603 # Configuration loaded
[root@localhost ~]# redis-server redis-5.0.3/cluster-test/7002/redis.conf
3553:C 26 Mar 2019 19:02:41.394 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3553:C 26 Mar 2019 19:02:41.394 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=3553, just started
3553:C 26 Mar 2019 19:02:41.394 # Configuration loaded
[root@localhost ~]# redis-server redis-5.0.3/cluster-test/7003/redis.conf
3558:C 26 Mar 2019 19:02:44.487 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3558:C 26 Mar 2019 19:02:44.487 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=3558, just started
3558:C 26 Mar 2019 19:02:44.487 # Configuration loaded
[root@localhost ~]# redis-server redis-5.0.3/cluster-test/7004/redis.conf
3574:C 26 Mar 2019 19:09:26.168 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3574:C 26 Mar 2019 19:09:26.168 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=3574, just started
3574:C 26 Mar 2019 19:09:26.168 # Configuration loaded
[root@localhost ~]#
[root@localhost ~]# redis-server redis-5.0.3/cluster-test/7005/redis.conf
3579:C 26 Mar 2019 19:09:31.677 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3579:C 26 Mar 2019 19:09:31.677 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=3579, just started
3579:C 26 Mar 2019 19:09:31.677 # Configuration loaded
[root@localhost ~]#

查看进程信息

[root@localhost ~]# ps aux | grep redis
root       2754  0.2  0.4 153828  7716 ?        Ssl  Mar25   2:29 redis-server 192.168.2.80:6379
root       3544  0.1  0.4 153828  7676 ?        Ssl  19:02   0:00 redis-server *:7000 [cluster]
root       3549  0.1  0.4 153828  7680 ?        Ssl  19:02   0:00 redis-server *:7001 [cluster]
root       3554  0.1  0.4 153828  7676 ?        Ssl  19:02   0:00 redis-server *:7002 [cluster]
root       3559  0.1  0.4 153828  7676 ?        Ssl  19:02   0:00 redis-server *:7003 [cluster]
root       3575  0.2  0.4 153828  7680 ?        Ssl  19:09   0:00 redis-server *:7004 [cluster]
root       3580  0.1  0.4 153828  7680 ?        Ssl  19:09   0:00 redis-server *:7005 [cluster]
root       3585  0.0  0.0 112644   952 pts/1    R+   19:09   0:00 grep --color=auto redis

集群创建

# redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1

# redis-cli -a 123456 --cluster create 192.168.2.80:7000 192.168.2.80:7001 192.168.2.80:7002 192.168.2.80:7003 192.168.2.80:7004 192.168.2.80:7005 --cluster-replicas 1

--cluster-replicas 1(每个创建的主服务器都有一个从服)

[root@localhost ~]# redis-cli -a 123456 --cluster create 192.168.2.80:7000 192.168.2.80:7001 192.168.2.80:7002 192.168.2.80:7003 192.168.2.80:7004 192.168.2.80:7005 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> 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.2.80:7003 to 192.168.2.80:7000
Adding replica 192.168.2.80:7004 to 192.168.2.80:7001
Adding replica 192.168.2.80:7005 to 192.168.2.80:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 69f17d424b855c3e18265d2c807a7b8e1ad9328d 192.168.2.80:7000
   slots:[0-5460] (5461 slots) master
M: d59b01f910c2adb972bd380ec07458e1866a248d 192.168.2.80:7001
   slots:[5461-10922] (5462 slots) master
M: 137f1ccf4be0c6479e76befd6b96a72dd642ce75 192.168.2.80:7002
   slots:[10923-16383] (5461 slots) master
S: 83bf3a15381bd624669b3e7f964b1ce26a87e619 192.168.2.80:7003
   replicates 137f1ccf4be0c6479e76befd6b96a72dd642ce75
S: df8c31bc9c417571aa38745349607a366309d208 192.168.2.80:7004
   replicates 69f17d424b855c3e18265d2c807a7b8e1ad9328d
S: 70193c889ce1ea1a4fbd811c101af3208691264f 192.168.2.80:7005
   replicates d59b01f910c2adb972bd380ec07458e1866a248d
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.2.80:7000)
M: 69f17d424b855c3e18265d2c807a7b8e1ad9328d 192.168.2.80:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: d59b01f910c2adb972bd380ec07458e1866a248d 192.168.2.80:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 70193c889ce1ea1a4fbd811c101af3208691264f 192.168.2.80:7005
   slots: (0 slots) slave
   replicates d59b01f910c2adb972bd380ec07458e1866a248d
M: 137f1ccf4be0c6479e76befd6b96a72dd642ce75 192.168.2.80:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: df8c31bc9c417571aa38745349607a366309d208 192.168.2.80:7004
   slots: (0 slots) slave
   replicates 69f17d424b855c3e18265d2c807a7b8e1ad9328d
S: 83bf3a15381bd624669b3e7f964b1ce26a87e619 192.168.2.80:7003
   slots: (0 slots) slave
   replicates 137f1ccf4be0c6479e76befd6b96a72dd642ce75
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看redis监听端口

# netstat -tnlp | grep redis

[root@localhost ~]# netstat -tnlp | grep redis
tcp        0      0 0.0.0.0:17000           0.0.0.0:*               LISTEN      3544/redis-server *
tcp        0      0 0.0.0.0:17001           0.0.0.0:*               LISTEN      3549/redis-server *
tcp        0      0 0.0.0.0:17002           0.0.0.0:*               LISTEN      3554/redis-server *
tcp        0      0 0.0.0.0:17003           0.0.0.0:*               LISTEN      3559/redis-server *
tcp        0      0 192.168.2.80:6379       0.0.0.0:*               LISTEN      2754/redis-server 1
tcp        0      0 0.0.0.0:17004           0.0.0.0:*               LISTEN      3575/redis-server *
tcp        0      0 0.0.0.0:17005           0.0.0.0:*               LISTEN      3580/redis-server *
tcp        0      0 0.0.0.0:7000            0.0.0.0:*               LISTEN      3544/redis-server *
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      3549/redis-server *
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      3554/redis-server *
tcp        0      0 0.0.0.0:7003            0.0.0.0:*               LISTEN      3559/redis-server *
tcp        0      0 0.0.0.0:7004            0.0.0.0:*               LISTEN      3575/redis-server *
tcp        0      0 0.0.0.0:7005            0.0.0.0:*               LISTEN      3580/redis-server *
tcp6       0      0 :::17000                :::*                    LISTEN      3544/redis-server *
tcp6       0      0 :::17001                :::*                    LISTEN      3549/redis-server *
tcp6       0      0 :::17002                :::*                    LISTEN      3554/redis-server *
tcp6       0      0 :::17003                :::*                    LISTEN      3559/redis-server *
tcp6       0      0 :::17004                :::*                    LISTEN      3575/redis-server *
tcp6       0      0 :::17005                :::*                    LISTEN      3580/redis-server *
tcp6       0      0 :::7000                 :::*                    LISTEN      3544/redis-server *
tcp6       0      0 :::7001                 :::*                    LISTEN      3549/redis-server *
tcp6       0      0 :::7002                 :::*                    LISTEN      3554/redis-server *
tcp6       0      0 :::7003                 :::*                    LISTEN      3559/redis-server *
tcp6       0      0 :::7004                 :::*                    LISTEN      3575/redis-server *
tcp6       0      0 :::7005                 :::*                    LISTEN      3580/redis-server *

 查看集群节点的常用参数

# redis-cli --cluster help

[root@localhost ~]# redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

[root@localhost ~]#

官方文档地址:

https://redis.io/topics/cluster-tutorial

参考文章:

https://www.cnblogs.com/yuanermen/p/5717885.html

原文地址:https://www.cnblogs.com/djlsunshine/p/10598822.html