CentOS 7下安装 redis 3.0.6并配置集群的过程详解
安装依赖
[root@centos7-1~]#yum-yinstallgccopenssl-devellibyaml-devellibffi-develreadline-develzlib-develgdbm-develncurses-develgcc-c++automakeautoconf
安装redis
[root@centos7-1~]#wgethttp://download.redis.io/releases/redis-3.0.6.tar.gz [root@centos7-1~]#tarxvfredis-3.0.6.tar.gz [root@centos7-1~]#cdredis-3.0.6/ [root@centos7-1redis-3.0.6]#makeMALLOC=libc [root@centos7-1redis-3.0.6]#makeinstall
启动server
通过命令redis-server来启动redisserver。通过下面的输出信息可以看出这次启动并没有指定配置文件,可以使用命令redis-server/path/to/redis.conf来指定具体的配置文件启动。
[root@centos7-1redis-3.0.6]#redis-server 4435:C25Jan11:40:48.816#Warning:noconfigfilespecified,usingthedefaultconfig.Inordertospecifyaconfigfileuseredis-server/path/to/redis.conf 4435:M25Jan11:40:48.817*Increasedmaximumnumberofopenfilesto10032(itwasoriginallysetto1024). _._ _.-``__''-._ _.-```.`_.''-._Redis3.0.6(00000000/0)64bit .-``.-```.```\/_.,_''-._ (',.-`|`,)Runninginstandalonemode |`-._`-...-`__...-.``-._|'`_.-'|Port:6379 |`-._`._/_.-'|PID:4435 `-._`-._`-./_.-'_.-' |`-._`-._`-.__.-'_.-'_.-'| |`-._`-.__.-'_.-'|http://redis.io `-._`-._`-.__.-'_.-'_.-' |`-._`-._`-.__.-'_.-'_.-'| |`-._`-.__.-'_.-'| `-._`-._`-.__.-'_.-'_.-' `-._`-.__.-'_.-' `-.__.-' `-.__.-' 4435:M25Jan11:40:48.817#WARNING:TheTCPbacklogsettingof511cannotbeenforcedbecause/proc/sys/net/core/somaxconnissettothelowervalueof128. 4435:M25Jan11:40:48.817#Serverstarted,Redisversion3.0.6 4435:M25Jan11:40:48.817#WARNINGovercommit_memoryissetto0!Backgroundsavemayfailunderlowmemorycondition.Tofixthisissueadd'vm.overcommit_memory=1'to/etc/sysctl.confandthenrebootorrunthecommand'sysctlvm.overcommit_memory=1'forthistotakeeffect. 4435:M25Jan11:40:48.817#WARNINGyouhaveTransparentHugePages(THP)supportenabledinyourkernel.ThiswillcreatelatencyandmemoryusageissueswithRedis.Tofixthisissuerunthecommand'echonever>/sys/kernel/mm/transparent_hugepage/enabled'asroot,andaddittoyour/etc/rc.localinordertoretainthesettingafterareboot.RedismustberestartedafterTHPisdisabled. 4435:M25Jan11:40:48.817*DBloadedfromdisk:0.000seconds 4435:M25Jan11:40:48.817*Theserverisnowreadytoacceptconnectionsonport6379
到此,单节点的Redisserver已经安装完毕,以下进入配置和安装集群的过程。
创建集群中的6个节点
集群配置文件
集群相关的配置文件主要修改redis.conf中的一下几个地方,因为都部署在一个服务器上,每个节点的端口不同。
[root@centos7-1redis-3.0.6]$viredis.conf #修改以下地方 port7000 cluster-enabledyes cluster-config-filenodes-7000.conf cluster-node-timeout5000 appendonlyyes
每个节点的配置文件
创建6个文件夹,分别表示一个redis节点,文件夹使用redis节点的端口号命名,文件里存放每个节点的配置文件。
[root@centos7-1redis-3.0.6]#mkdir/usr/local/redis-cluster [root@centos7-1redis-3.0.6]#cd/usr/local/redis-cluster [root@centos7-1redis-cluster]#mkdir700070017002700370047005
拷贝redis.conf拷贝到6个文件夹中
[root@centos7-1redis-cluster]#cp~/redis-3.0.6/redis.conf7000 [root@centos7-1redis-cluster]#cp~/redis-3.0.6/redis.conf7001 [root@centos7-1redis-cluster]#cp~/redis-3.0.6/redis.conf7002 [root@centos7-1redis-cluster]#cp~/redis-3.0.6/redis.conf7003 [root@centos7-1redis-cluster]#cp~/redis-3.0.6/redis.conf7004 [root@centos7-1redis-cluster]#cp~/redis-3.0.6/redis.conf7005
并修改每个配置中的内容为各自节点的端口,注意port和cluster-config-file的值必须唯一。
分别启动每个节点
[root@centos7-1~]#redis-server/usr/local/redis-cluster/7000/redis.conf [root@centos7-1~]#redis-server/usr/local/redis-cluster/7001/redis.conf [root@centos7-1~]#redis-server/usr/local/redis-cluster/7002/redis.conf [root@centos7-1~]#redis-server/usr/local/redis-cluster/7003/redis.conf [root@centos7-1~]#redis-server/usr/local/redis-cluster/7004/redis.conf [root@centos7-1~]#redis-server/usr/local/redis-cluster/7005/redis.conf
启动完成后查看进程
[root@centos7-1~]#ps-ef|grepredis root47042177012:12pts/000:00:07redis-server*:7000[cluster] root47074599012:12pts/100:00:07redis-server*:7001[cluster] root47104638012:12pts/200:00:07redis-server*:7002[cluster] root47524717012:12pts/300:00:07redis-server*:7003[cluster] root47884759012:12pts/400:00:07redis-server*:7004[cluster] root48244795012:13pts/500:00:07redis-server*:7005[cluster] root90188984014:08pts/600:00:00grep--color=autoredis
建立Redis集群
上面的步骤分别创建了6个节点,并已经启动,这里要把这6个节点加入到一个集群里面。redis已经为我们提供了集群操作的脚本redis-trib.rb,操作起来很简单,继续。
安装ruby
由于集群操作需要用到ruby脚本redis-trib.rb,所以要安装ruby和rubygems
[root@centos7-1~]#yum-yinstallrubyrubygems [root@centos7-1~]#geminstallredis--version3.0.6 Fetching:redis-3.0.6.gem(100%) Successfullyinstalledredis-3.0.6 Parsingdocumentationforredis-3.0.6 Installingridocumentationforredis-3.0.6 1geminstalled
redis-trib.rb是一个ruby脚本工具,用来建立和管理redis集群,因为使用比较频繁,所以拷贝脚本redis-trib.rb到/usr/local/bin/
[root@centos7-1redis-3.0.6]$cpsrc/redis-trib.rb/usr/local/bin/
创建集群
[root@centos7-1~]#redis-trib.rbcreate--replicas1127.0.0.1:7000127.0.0.1:7001127.0.0.1:7002127.0.0.1:7003127.0.0.1:7004127.0.0.1:7005 >>>Creatingcluster >>>Performinghashslotsallocationon6nodes... Using3masters: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 Addingreplica127.0.0.1:7003to127.0.0.1:7000 Addingreplica127.0.0.1:7004to127.0.0.1:7001 Addingreplica127.0.0.1:7005to127.0.0.1:7002 M:d900d3667196121f95bdbd68ca2317926a6950bc127.0.0.1:7000 slots:0-5460(5461slots)master M:b662853288cc89a3b78f70120d57c0a34f6a91d5127.0.0.1:7001 slots:5461-10922(5462slots)master M:6dc5ee101c2432ec4c1d934d54a6a3565116bbff127.0.0.1:7002 slots:10923-16383(5461slots)master S:d18e50ac6aff2f612fac43da74a79288aa574c87127.0.0.1:7003 replicatesd900d3667196121f95bdbd68ca2317926a6950bc S:85f6455e14d03ebaefeca3dbe3c78c92f2f76ead127.0.0.1:7004 replicatesb662853288cc89a3b78f70120d57c0a34f6a91d5 S:89d792acda7f2c99c95b0d3947572647cf9edc67127.0.0.1:7005 replicates6dc5ee101c2432ec4c1d934d54a6a3565116bbff CanIsettheaboveconfiguration?(type'yes'toaccept):yes >>>Nodesconfigurationupdated >>>Assignadifferentconfigepochtoeachnode >>>SendingCLUSTERMEETmessagestojointhecluster Waitingfortheclustertojoin.. >>>PerformingClusterCheck(usingnode127.0.0.1:7000) M:d900d3667196121f95bdbd68ca2317926a6950bc127.0.0.1:7000 slots:0-5460(5461slots)master M:b662853288cc89a3b78f70120d57c0a34f6a91d5127.0.0.1:7001 slots:5461-10922(5462slots)master M:6dc5ee101c2432ec4c1d934d54a6a3565116bbff127.0.0.1:7002 slots:10923-16383(5461slots)master M:d18e50ac6aff2f612fac43da74a79288aa574c87127.0.0.1:7003 slots:(0slots)master replicatesd900d3667196121f95bdbd68ca2317926a6950bc M:85f6455e14d03ebaefeca3dbe3c78c92f2f76ead127.0.0.1:7004 slots:(0slots)master replicatesb662853288cc89a3b78f70120d57c0a34f6a91d5 M:89d792acda7f2c99c95b0d3947572647cf9edc67127.0.0.1:7005 slots:(0slots)master replicates6dc5ee101c2432ec4c1d934d54a6a3565116bbff [OK]Allnodesagreeaboutslotsconfiguration. >>>Checkforopenslots... >>>Checkslotscoverage... [OK]All16384slotscovered.
以上create命令创建了集群,选项–replicas1表示每个Master都需要一个Slave。所以结果是集群中创建了3个Master节点和3个Slave节点。
上面信息中M表示Master节点,S表示Slave节点。
由此可以看出三组Master和Slave关系如下:
M:d900d3667196121f95bdbd68ca2317926a6950bc->S:d18e50ac6aff2f612fac43da74a79288aa574c87 M:6dc5ee101c2432ec4c1d934d54a6a3565116bbff->S:89d792acda7f2c99c95b0d3947572647cf9edc67 M:b662853288cc89a3b78f70120d57c0a34f6a91d5->S:85f6455e14d03ebaefeca3dbe3c78c92f2f76ead
检测集群的状态
[root@centos7-1~]#redis-trib.rbcheck127.0.0.1:7000 >>>PerformingClusterCheck(usingnode127.0.0.1:7000) M:d900d3667196121f95bdbd68ca2317926a6950bc127.0.0.1:7000 slots:0-5460(5461slots)master 1additionalreplica(s) M:6dc5ee101c2432ec4c1d934d54a6a3565116bbff127.0.0.1:7002 slots:10923-16383(5461slots)master 1additionalreplica(s) S:89d792acda7f2c99c95b0d3947572647cf9edc67127.0.0.1:7005 slots:(0slots)slave replicates6dc5ee101c2432ec4c1d934d54a6a3565116bbff S:d18e50ac6aff2f612fac43da74a79288aa574c87127.0.0.1:7003 slots:(0slots)slave replicatesd900d3667196121f95bdbd68ca2317926a6950bc S:85f6455e14d03ebaefeca3dbe3c78c92f2f76ead127.0.0.1:7004 slots:(0slots)slave replicatesb662853288cc89a3b78f70120d57c0a34f6a91d5 M:b662853288cc89a3b78f70120d57c0a34f6a91d5127.0.0.1:7001 slots:5461-10922(5462slots)master 1additionalreplica(s) [OK]Allnodesagreeaboutslotsconfiguration. >>>Checkforopenslots... >>>Checkslotscoverage... [OK]All16384slotscovered.
测试集群
利用redis-cli-c-p端口号连接集群
[root@centos7-1~]#redis-cli-c-p7000 127.0.0.1:7000>gethello (nil) 127.0.0.1:7000>sethello"helloworld" OK 127.0.0.1:7000>gethello "helloworld" 127.0.0.1:7000>getname ->Redirectedtoslot[5798]locatedat127.0.0.1:7001 (nil) 127.0.0.1:7001>setname"redisserver" OK 127.0.0.1:7001>getname "redisserver"
总结
上就是关于在CentOS7系统下安装redis3.0.6并配置集群的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。