CentOS7下Redis3.2.3 Cluster配置

配置redis cluster,以两台机器,6个实例为环境。

操作系统 CentOS Linux release 7 64位

安装依赖包:单独下载redis-3.3.1.gem这个包要对应os版本
yum -y install tcl   
yum -y install ruby ruby-devel rubygems rpm-build
gem install redis 

解压安装:

cd /usr/local/
tar zxvf redis-3.2.3.tar.gz
cd redis-3.2.3
make && make install

在172.16.25.220的/usr/local/webapp下创建bin、redis6379、redis6380、redis6381四个目录
mkdir bin
mkdir redis6379
mkdir redis6380
mkdir redis6381

在172.16.25.72的/usr/local/webapp下创建bin、redis6379、redis6380、redis6381四个目录
mkdir bin
mkdir redis6379
mkdir redis6380
mkdir redis6381

分别在两台机器上创建配置文件,并启动:
cd /usr/local/redis-3.2.3/src/
cp mkreleasehdr.sh redis-benchmark redis-sentinel redis-server redis-trib.rb redis-cli /usr/local/webapp/bin
cd /usr/local/webapp/redis6379/
touch redis-6379.conf
cp /usr/local/webapp/redis6379/redis-6379.conf /usr/local/webapp/redis6380/redis-6380.conf
cp /usr/local/webapp/redis6379/redis-6379.conf /usr/local/webapp/redis6381/redis-6381.conf
sed -i 's/6379/6380/g' /usr/local/webapp/redis6380/redis-6380.conf
sed -i 's/6379/6381/g' /usr/local/webapp/redis6381/redis-6381.conf
/usr/local/webapp/bin/redis-server /usr/local/webapp/redis6379/redis-6379.conf
/usr/local/webapp/bin/redis-server /usr/local/webapp/redis6380/redis-6380.conf
/usr/local/webapp/bin/redis-server /usr/local/webapp/redis6381/redis-6381.conf

创建集群(在其中一台服务器上执行即可)
cd /usr/local/webapp/bin/
./redis-trib.rb create --replicas 1 172.16.25.220:6379 172.16.25.72:6379 172.16.25.220:6380 172.16.25.72:6380 172.16.25.220:6381 172.16.25.72:6381

集群状态查看:
./redis-cli -c -h 172.16.25.220 -p 6380
172.16.25.220:6380> cluster nodes
3b64702727c2e047f255ebb6eb111359990fba85 172.16.25.72:6379 master - 0 1478249255421 2 connected 5461-10922
a0ac9b45dd7bfabeb7036278c13efb744794adc8 172.16.25.72:6380 master - 0 1478249256423 7 connected 0-5460
f612b1d6e362c52ac606a627bcc27a95224ce2b3 172.16.25.220:6381 slave 3b64702727c2e047f255ebb6eb111359990fba85 0 1478249254919 5 connected
954200fe3b22806af5afc035cdf0a25899c14ecd 172.16.25.220:6380 myself,slave ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 0 0 3 connected
ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 172.16.25.72:6381 master - 0 1478249254419 8 connected 10923-16383
2b2b3a1885a9a2b64060b20322d3c425b64ca063 172.16.25.220:6379 slave a0ac9b45dd7bfabeb7036278c13efb744794adc8 0 1478249255921 7 connected

给redis cluster集群加上认证功能:
登陆集群后配置密码(我这里测试不生效)
./redis-cli -c -h 172.16.25.72 -p 6379
config set masterauth mypasswd
config set requirepass mypasswd
config rewrite
 
修改配置文件:加入下面两行
requirepass "mypasswd"
masterauth "mypasswd"
 
停redis
./redis-cli -c -h 172.16.25.220 -p 6379 shutdown
./redis-cli -c -h 172.16.25.220 -p 6380 shutdown
./redis-cli -c -h 172.16.25.220 -p 6381 shutdown
./redis-cli -c -h 172.16.25.72 -p 6381 shutdown 
./redis-cli -c -h 172.16.25.72 -p 6380 shutdown
./redis-cli -c -h 172.16.25.72 -p 6379 shutdown
 
修改
修改gem的redis工具下的一个文件,我这里是默认安装,路径如下:/usr/local/share/gems/gems/redis-3.3.1/lib/redis/client.rb
vim /usr/local/share/gems/gems/redis-3.3.1/lib/redis/client.rb
 
启动redis
service redis6379 start
service redis6380 start
service redis6381 start
 
登陆
./redis-cli -c -h 172.16.25.220 -p 6380
172.16.25.220:6380> cluster nodes
NOAUTH Authentication required.
172.16.25.220:6380> auth mypasswd
OK
172.16.25.220:6380> cluster nodes
3b64702727c2e047f255ebb6eb111359990fba85 172.16.25.72:6379 master - 0 1478249211338 2 connected 5461-10922
a0ac9b45dd7bfabeb7036278c13efb744794adc8 172.16.25.72:6380 master - 0 1478249211839 7 connected 0-5460
f612b1d6e362c52ac606a627bcc27a95224ce2b3 172.16.25.220:6381 slave 3b64702727c2e047f255ebb6eb111359990fba85 0 1478249210836 5 connected
954200fe3b22806af5afc035cdf0a25899c14ecd 172.16.25.220:6380 myself,slave ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 0 0 3 connected
ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 172.16.25.72:6381 master - 0 1478249212341 8 connected 10923-16383
2b2b3a1885a9a2b64060b20322d3c425b64ca063 172.16.25.220:6379 slave a0ac9b45dd7bfabeb7036278c13efb744794adc8 0 1478249210334 7 connected
172.16.25.220:6380> 
 
./redis-cli -c -h 172.16.25.220 -p 6380 -a mypasswd
172.16.25.220:6380> cluster nodes
3b64702727c2e047f255ebb6eb111359990fba85 172.16.25.72:6379 master - 0 1478249255421 2 connected 5461-10922
a0ac9b45dd7bfabeb7036278c13efb744794adc8 172.16.25.72:6380 master - 0 1478249256423 7 connected 0-5460
f612b1d6e362c52ac606a627bcc27a95224ce2b3 172.16.25.220:6381 slave 3b64702727c2e047f255ebb6eb111359990fba85 0 1478249254919 5 connected
954200fe3b22806af5afc035cdf0a25899c14ecd 172.16.25.220:6380 myself,slave ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 0 0 3 connected
ca66b71a3e2b7bf75885720b4c7bd3015dd732a1 172.16.25.72:6381 master - 0 1478249254419 8 connected 10923-16383
2b2b3a1885a9a2b64060b20322d3c425b64ca063 172.16.25.220:6379 slave a0ac9b45dd7bfabeb7036278c13efb744794adc8 0 1478249255921 7 connected
172.16.25.220:6380> 
 
停止有密码认证的集群
./redis-cli -c -a "mypasswd" -h 172.16.25.220 -p 6379 shutdown

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ef4cde3ddb7f58ffb79019aeda2fa497.html