redis伪集群搭建

来源:互联网 发布:语音随机聊天软件 编辑:程序博客网 时间:2024/06/05 19:51

搭建环境是vmware虚拟机+ubuntu-14.04,以redis伪集群的方式搭建搭建,一共实现了6台机器集群的搭建,三个master节点和三个slave节点。

<pre name="code" class="cpp">#首先安装redis的集群管理需要ruby和zlibsudo apt-get install zlib ruby#以及安装和redis有关的ruby packagesudo gem install redis#下载安装包,编译安装redis-3.0.0mkdir rediswget http://download.redis.io/releases/redis-3.0.0.tar.gz  tar -zxvf redis-3.0.0.tar.gzcd redis-3.0.0make & make test &make install

编译安装以后,运行redis-server如果打印出以下的结果就表示安装成功了。

<pre name="code" class="cpp">61702:C 27 Oct 11:11:12.324 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf61702:M 27 Oct 11:11:12.325 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.61702:M 27 Oct 11:11:12.325 # Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.61702:M 27 Oct 11:11:12.326 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.                _._                                                             _.-``__ ''-._                                                   _.-``    `.  `_.  ''-._           Redis 3.0.0 (00000000/0) 64 bit  .-`` .-```.  ```\/    _.,_ ''-._                                    (    '      ,       .-`  | `,    )     Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379 |    `-._   `._    /     _.-'    |     PID: 61702  `-._    `-._  `-./  _.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |           http://redis.io          `-._    `-._`-.__.-'_.-'    _.-'                                    |`-._`-._    `-.__.-'    _.-'_.-'|                                   |    `-._`-._        _.-'_.-'    |                                    `-._    `-._`-.__.-'_.-'    _.-'                                         `-._    `-.__.-'    _.-'                                                 `-._        _.-'                                                         `-.__.-'                                               61702:M 27 Oct 11:11:12.332 # Server started, Redis version 3.0.061702:M 27 Oct 11:11:12.332 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.61702:M 27 Oct 11:11:12.332 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.61702:M 27 Oct 11:11:12.333 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.61702:M 27 Oct 11:11:12.333 * The server is now ready to accept connections on port 6379

然后我们关闭掉已经打开的redis-server程序,开始伪集群的搭建。

首先在redis-3.0.0目录下创建cluster目录

<pre name="code" class="cpp">ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ mkdir clusterubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ cd cluster#创建node-7000.conf node-7001.conf node-7002.conf node-7003.conf  node-7004.conf  node-7005.conf#这些分别是7000,7001,7002,7003,7004,7005六个节点的redis-server配置文件#配置文件内容如下ubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7000.conf pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7000.pidlogfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7000.log"dir /home/ubuntu/redis-3.0.0/cluster/data/node-7000port 7000daemonize yescluster-enabled yescluster-config-file node-7000.confcluster-node-timeout 5000appendonly yesubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7001.conf pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7001.pidlogfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7001.log"dir /home/ubuntu/redis-3.0.0/cluster/data/node-7001port 7001daemonize yescluster-enabled yescluster-config-file node-7001.confcluster-node-timeout 5000appendonly yesubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7002.conf pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7002.pidlogfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7002.log"dir /home/ubuntu/redis-3.0.0/cluster/data/node-7002port 7002daemonize yescluster-enabled yescluster-config-file node-7002.confcluster-node-timeout 5000appendonly yesubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7003.conf pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7003.pidlogfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7003.log"dir /home/ubuntu/redis-3.0.0/cluster/data/node-7003port 7003daemonize yescluster-enabled yescluster-config-file node-7003.confcluster-node-timeout 5000appendonly yesubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7004.conf pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7004.pidlogfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7004.log"dir /home/ubuntu/redis-3.0.0/cluster/data/node-7004port 7004daemonize yescluster-enabled yescluster-config-file node-7004.confcluster-node-timeout 5000appendonly yesubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ cat node-7005.conf pidfile /home/ubuntu/redis-3.0.0/cluster/pid/node7005.pidlogfile "/home/ubuntu/redis-3.0.0/cluster/logs/node-7005.log"dir /home/ubuntu/redis-3.0.0/cluster/data/node-7005port 7005daemonize yescluster-enabled yescluster-config-file node-7005.confcluster-node-timeout 5000appendonly yes

最后在cluster目录下创建log data文件夹,以及node-7000.log   node-7001.log   node-7002.log   node-7003.log   #node-7004.log   node-7005.log6log文件,最后cluster目录下的文件如下:

<pre name="code" class="cpp">ubuntu@ubuntu-virtual-machine:~/redis-3.0.0/cluster$ lsdata  node-7000.conf  node-7001.conf  node-7002.conf  node-7003.conf  node-7004.conf  node-7005.conflogs  node-7000.log   node-7001.log   node-7002.log   node-7003.log   node-7004.log   node-7005.log

最后,配置文件都ok了,开始启动6单机”redis-server服务,启动命令如下:

<pre name="code" class="cpp">ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7000.confubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7001.confubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7002.confubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7003.confubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7004.confubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ redis-server cluster/node-7005.conf

把每一台机器加入集群中

<pre name="code" class="cpp">ubuntu@ubuntu-virtual-machine:~/redis-3.0.0$ ./src/redis-trib.rb create --replicas 1 192.168.39.153:7000 192.168.39.153:7001 192.168.39.153:7002 192.168.39.153:7003 192.168.39.153:7004 192.168.39.153:7005

打印出以下的结果,就代表集群启动成功了。

<pre name="code" class="cpp">3:7003 192.168.39.153:7004 192.168.39.153:7005>>> Creating clusterConnecting to node 192.168.39.153:7000: OKConnecting to node 192.168.39.153:7001: OKConnecting to node 192.168.39.153:7002: OKConnecting to node 192.168.39.153:7003: OKConnecting to node 192.168.39.153:7004: OKConnecting to node 192.168.39.153:7005: OK>>> Performing hash slots allocation on 6 nodes...Using 3 masters:192.168.39.153:7000192.168.39.153:7001192.168.39.153:7002Adding replica 192.168.39.153:7003 to 192.168.39.153:7000Adding replica 192.168.39.153:7004 to 192.168.39.153:7001Adding replica 192.168.39.153:7005 to 192.168.39.153:7002M: 57d0ccdc7dc98ab786c961e0979b8a8f135f60cd 192.168.39.153:7000   slots:0-5460 (5461 slots) masterM: adfbf48901b1e7e7e7e2fe0a1b87c86f57530ebd 192.168.39.153:7001   slots:5461-10922 (5462 slots) masterM: 314cbd10bbc4c1339fb737575af41f04408c2087 192.168.39.153:7002   slots:10923-16383 (5461 slots) masterS: fb2d1830a4b429fcb80da7edbbdacc50ed439d63 192.168.39.153:7003   replicates 57d0ccdc7dc98ab786c961e0979b8a8f135f60cdS: 1384a16ccb208828d9634f49695556ae11ec57a7 192.168.39.153:7004   replicates adfbf48901b1e7e7e7e2fe0a1b87c86f57530ebdS: 9d9350c96c50fe88621575047a6a07250a287b51 192.168.39.153:7005   replicates 314cbd10bbc4c1339fb737575af41f04408c2087Can I set the above configuration? (type 'yes' to accept): yes#这里输入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 192.168.39.153:7000)M: 57d0ccdc7dc98ab786c961e0979b8a8f135f60cd 192.168.39.153:7000   slots:0-5460 (5461 slots) masterM: adfbf48901b1e7e7e7e2fe0a1b87c86f57530ebd 192.168.39.153:7001   slots:5461-10922 (5462 slots) masterM: 314cbd10bbc4c1339fb737575af41f04408c2087 192.168.39.153:7002   slots:10923-16383 (5461 slots) masterM: fb2d1830a4b429fcb80da7edbbdacc50ed439d63 192.168.39.153:7003   slots: (0 slots) master   replicates 57d0ccdc7dc98ab786c961e0979b8a8f135f60cdM: 1384a16ccb208828d9634f49695556ae11ec57a7 192.168.39.153:7004   slots: (0 slots) master   replicates adfbf48901b1e7e7e7e2fe0a1b87c86f57530ebdM: 9d9350c96c50fe88621575047a6a07250a287b51 192.168.39.153:7005   slots: (0 slots) master   replicates 314cbd10bbc4c1339fb737575af41f04408c2087[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

至此,一个集群就搭建起来了,从上边的信息我们也可以看出来,192.168.39.153:7000192.168.39.153:7001192.168.39.153:7002master节点,另外三个是slave节点。


本文参考了http://lib.csdn.net/article/redis/35915

1 0
原创粉丝点击