服务器上redis集群搭建

2017-12-05 19:32:53来源:CSDN作者:zhaoyahui_666人点击

分享

我是用一台机器6个节点,创建出3 master、3 salve。

redis 采用 redis-3.2.4 版本。

安装过程

1、创建安装目录

mkdir /home/user/redis/download
2、下载并解压
mkdir /home/user/redis/downloadwget http://download.redis.io/releases/redis-3.2.4.tar.gztar -zxvf redis-3.2.4.tar.gz
3、编译安装

cd redis-3.2.4make && make install
4、创建redis节点

mkdir /home/user/redis/redis-clustercd /home/user/redis/redis-clustermkdir 7001 7002 7003 7004 7005 7006cd /home/user/redis/downloadcp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7001cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7002
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7003
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7004
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7005
cp redis-3.2.4.tar.gz /home/user/redis/redis-cluster/7006
配置各个节点

cd /home/user/redis/redis-cluster/7001tar -zxvf redis-3.2.4.tar.gzcd redis-3.2.4vim redis.conf
port  7001                                        //端口7000,7002,7003        bind 本机ip                                       //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群daemonize    yes                               //redis后台运行pidfile  /var/run/redis_7001.pid          //pidfile文件对应7000,7001,7002cluster-enabled  yes                           //开启集群  把注释#去掉cluster-config-file  nodes_7001.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志
重复此步骤,配置其他节点

5、启动各节点

cd /home/user/redis/redis-cluster/7001/redis-server redis.conf
6、检查 redis 启动情况

[root@iZ2zeheg4tqt4xca0kdo1kZ /]# ps -ef | grep redisroot     10878     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7001 [cluster]root     10903     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7002 [cluster]root     10923     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7004 [cluster]root     10930     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7003 [cluster]root     10938     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7005 [cluster]root     10951     1  0 Dec04 ?        00:00:12 redis-server 127.0.0.1:7006 [cluster]root     17848 17695  0 11:26 pts/1    00:00:00 redis-cli -h 127.0.0.1 -c -p 7002root     17862  1883  0 11:49 pts/0    00:00:00 grep redis
[root@iZ2zeheg4tqt4xca0kdo1kZ /]# netstat -tnlp | grep redistcp        0      0 127.0.0.1:17001             0.0.0.0:*                   LISTEN      10878/redis-server  tcp        0      0 127.0.0.1:17002             0.0.0.0:*                   LISTEN      10903/redis-server  tcp        0      0 127.0.0.1:17003             0.0.0.0:*                   LISTEN      10930/redis-server  tcp        0      0 127.0.0.1:17004             0.0.0.0:*                   LISTEN      10923/redis-server  tcp        0      0 127.0.0.1:17005             0.0.0.0:*                   LISTEN      10938/redis-server  tcp        0      0 127.0.0.1:17006             0.0.0.0:*                   LISTEN      10951/redis-server  tcp        0      0 127.0.0.1:7001              0.0.0.0:*                   LISTEN      10878/redis-server  tcp        0      0 127.0.0.1:7002              0.0.0.0:*                   LISTEN      10903/redis-server  tcp        0      0 127.0.0.1:7003              0.0.0.0:*                   LISTEN      10930/redis-server  tcp        0      0 127.0.0.1:7004              0.0.0.0:*                   LISTEN      10923/redis-server  tcp        0      0 127.0.0.1:7005              0.0.0.0:*                   LISTEN      10938/redis-server  tcp        0      0 127.0.0.1:7006              0.0.0.0:*                   LISTEN      10951/redis-server
7、创建集群

创建集群需要适用ruby环境提供的redis接口,所以首先安装ruby环境和接口。这个可以参考http://blog.csdn.net/zhaoyahui_666/article/details/78716511。

安装好环境后,执行创建集群操作。

Redis 官方提供了 redis-trib.rb 这个工具,就在解压目录的 src 目录中,第三步中已将它复制到 /usr/local/bin 目录中,可以直接在命令行中使用了。使用下面这个命令即可完成安装。

[root@iZ2zeheg4tqt4xca0kdo1kZ bin]# redis-trib.rb  create  --replicas  1 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 127.0.0.1:7006>>> Creating cluster>>> Performing hash slots allocation on 6 nodes...Using 3 masters:127.0.0.1:7001127.0.0.1:7002127.0.0.1:7003Adding replica 127.0.0.1:7004 to 127.0.0.1:7001Adding replica 127.0.0.1:7005 to 127.0.0.1:7002Adding replica 127.0.0.1:7006 to 127.0.0.1:7003M: 241c84a7dead6708296fda6929c6408193c00880 127.0.0.1:7001   slots:0-5460 (5461 slots) masterM: 68052f0fe49468d376eaada2280ea1a0a6b48065 127.0.0.1:7002   slots:5461-10922 (5462 slots) masterM: 48f57f10f1cb5e5b3601546630f759869146f904 127.0.0.1:7003   slots:10923-16383 (5461 slots) masterS: 3b7df0c92cf16d346ae90ebea41692ae0283228e 127.0.0.1:7004   replicates 241c84a7dead6708296fda6929c6408193c00880S: 2cd82cb7fc136382ff165a36c713d3644b83c21f 127.0.0.1:7005   replicates 68052f0fe49468d376eaada2280ea1a0a6b48065S: 060829dc91dab9b850fe994aff0eea8a9de06b9f 127.0.0.1:7006   replicates 48f57f10f1cb5e5b3601546630f759869146f904Can I set the above configuration? (type 'yes' to accept):
输入yes

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 clusterWaiting for the cluster to join....>>> Performing Cluster Check (using node 127.0.0.1:7001)M: 241c84a7dead6708296fda6929c6408193c00880 127.0.0.1:7001   slots:0-5460 (5461 slots) masterM: 68052f0fe49468d376eaada2280ea1a0a6b48065 127.0.0.1:7002   slots:5461-10922 (5462 slots) masterM: 48f57f10f1cb5e5b3601546630f759869146f904 127.0.0.1:7003   slots:10923-16383 (5461 slots) masterM: 3b7df0c92cf16d346ae90ebea41692ae0283228e 127.0.0.1:7004   slots: (0 slots) master   replicates 241c84a7dead6708296fda6929c6408193c00880M: 2cd82cb7fc136382ff165a36c713d3644b83c21f 127.0.0.1:7005   slots: (0 slots) master   replicates 68052f0fe49468d376eaada2280ea1a0a6b48065M: 060829dc91dab9b850fe994aff0eea8a9de06b9f 127.0.0.1:7006   slots: (0 slots) master   replicates 48f57f10f1cb5e5b3601546630f759869146f904[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
8、验证

登录7001客户端存储值,登录7002客户端取值

[root@iZ2zeheg4tqt4xca0kdo1kZ redis-3.2.1]# redis-cli -h 127.0.0.1 -c -p 7001127.0.0.1:7001> set hello worldOK127.0.0.1:7001> get hello"world"

[root@iZ2zeheg4tqt4xca0kdo1kZ redis-3.2.1]# redis-cli -h 127.0.0.1 -c -p 7002127.0.0.1:7002> get hello-> Redirected to slot [866] located at 127.0.0.1:7001"world"127.0.0.1:7001> keys *1) "hello"

redis集群搭建结束



最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台