架构三主三从,每台服务器有主从两个,分别为6379 6380.
服务器分别为10.63.50.211.10.63.50.212.10.63.50.213
演练增加删除节点10.63.50.221
#升级基础软件
yum update -y glibc nss
#安装rvm
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
#导入rvm变量
find / -name rvm.sh
source /etc/profile.d/rvm.sh
rvm requirements
rvm install ruby 2.3.1
#安装redis
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
tar zxvf redis-5.0.4.tar.gz
cd redis-5.0.4
make PREFIX=/usr/local/webserver/redis install
cp -a ./* /usr/local/webserver/redis/
cp utils/redis_init_script /etc/init.d/redis1
cp utils/redis_init_script /etc/init.d/redis2
#修改对应的端口redis1:6379,redis2:6380;替换程序路径为安装路径
vi /etc/init.d/redis1
vi /etc/init.d/redis2
chmod a+x /etc/init.d/redis1
chmod a+x /etc/init.d/redis2
#创建相关目录
mkdir /etc/{redis,redis-cluster}
mkdir -p /var/log/redis/
mkdir -p /var/redis/{6379,6380}
#长传内容到
vi /etc/redis/6379.conf
port 6379
cluster-enabled yes
cluster-config-file /etc/redis-cluster/node-6379.conf
cluster-node-timeout 15000
daemonize yes
pidfile /var/run/redis_6379.pid
dir /var/redis/6379
logfile /var/log/redis/6379.log
appendonly yes
protected-mode no
#长传内容到
vi /etc/redis/6380.conf
port 6380
cluster-enabled yes
cluster-config-file /etc/redis-cluster/node-6380.conf
cluster-node-timeout 15000
daemonize yes
pidfile /var/run/redis_6380.pid
dir /var/redis/6380
logfile /var/log/redis/6380.log
appendonly yes
protected-mode no
#启动服务
/etc/init.d/redis1 start
/etc/init.d/redis2 start
添加开机启动
chkconfig redis1 on
chkconfig redis2 on
ps -ef |grep redis
#安装ruby-redis
gem install redis
#创建集群
ln -s /usr/local/webserver/redis/src/redis-trib.rb /bin/redis-trib.rb
/usr/local/webserver/redis/bin/redis-cli --cluster create 10.63.50.211:6379 10.63.50.211:6380 10.63.50.212:6379 10.63.50.212:6380 10.63.50.213:6379 10.63.50.213:6380 --cluster-replicas 1
#查看结果
/usr/local/webserver/redis/bin/redis-cli -h 10.63.50.211 -c
10.63.50.211:6379> cluster nodes
e27162b370ad16615ef06316015e0f0fa7cb70d1 10.63.50.211:6380@16380 slave ff5659bd033d422d4505bc1e777cc43c830fef34 0 1553242764000 5 connected
475fb3fa766efd1c544b16f52b96e491bf0a5834 10.63.50.211:6379@16379 myself,master - 0 1553242764000 1 connected 0-5460
d9627863dd9f584a0408aae669c0352c71e536aa 10.63.50.212:6380@16380 slave 475fb3fa766efd1c544b16f52b96e491bf0a5834 0 1553242765978 4 connected
ff5659bd033d422d4505bc1e777cc43c830fef34 10.63.50.213:6379@16379 master - 0 1553242765000 5 connected 10923-16383
c81a6aa66564109d8b8be2a8ae5bcf223cde2374 10.63.50.212:6379@16379 master - 0 1553242764000 3 connected 5461-10922
1ac58a60a1dcb9e43be0e8e4b937cf1ff4cb6ffe 10.63.50.213:6380@16380 slave c81a6aa66564109d8b8be2a8ae5bcf223cde2374 0 1553242764976 6 connected
10.63.50.211:6379> exit
#添加主节点
/usr/local/webserver/redis/bin/redis-cli --cluster add-node 10.63.50.221:6379 10.63.50.211:6379
/usr/local/webserver/redis/bin/redis-cli --cluster add-node 10.63.50.221:6380 10.63.50.211:6379
#调整分片位置
/usr/local/webserver/redis/bin/redis-cli --cluster reshard 10.63.50.211:6379
第一步选择迁入的节点的nodeid
第二步输入需要迁入的总slot数
第三步 输入all,或者输入需要调整的节点的nodeid
/usr/local/webserver/redis/bin/redis-cli -c
127.0.0.1:6379> cluster nodes
1ac58a60a1dcb9e43be0e8e4b937cf1ff4cb6ffe 10.63.50.213:6380@16380 slave c81a6aa66564109d8b8be2a8ae5bcf223cde2374 0 1553243302905 6 connected
7057c747ff2207c69434901d567823688c6d7526 10.63.50.221:6379@16379 master - 0 1553243303000 8 connected 0-1332 5461-6794 10923-12255
c81a6aa66564109d8b8be2a8ae5bcf223cde2374 10.63.50.212:6379@16379 master - 0 1553243301000 3 connected 6795-10922
475fb3fa766efd1c544b16f52b96e491bf0a5834 10.63.50.211:6379@16379 master - 0 1553243301000 1 connected 1333-5460
189e67a5781f6ce559c0ce2af1d4874a9ec26af7 10.63.50.221:6380@16380 master - 0 1553243299899 7 connected
e27162b370ad16615ef06316015e0f0fa7cb70d1 10.63.50.211:6380@16380 slave ff5659bd033d422d4505bc1e777cc43c830fef34 0 1553243300000 5 connected
d9627863dd9f584a0408aae669c0352c71e536aa 10.63.50.212:6380@16380 slave 475fb3fa766efd1c544b16f52b96e491bf0a5834 0 1553243303908 4 connected
ff5659bd033d422d4505bc1e777cc43c830fef34 10.63.50.213:6379@16379 myself,master - 0 1553243302000 5 connected 12256-16383
#删除节点
/usr/local/webserver/redis/bin/redis-cli --cluster del-node 10.63.50.221:6380 189e67a5781f6ce559c0ce2af1d4874a9ec26af7
>>> Removing node 189e67a5781f6ce559c0ce2af1d4874a9ec26af7 from cluster 10.63.50.221:6380
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
#添加从节点
如果有历史数据 需要先清空
/usr/local/webserver/redis/bin/redis-cli --cluster add-node 10.63.50.221:6380 10.63.50.211:6379 --cluster-slave --cluster-master-id 475fb3fa766efd1c544b16f52b96e491bf0a5834
>>> Adding node 10.63.50.221:6380 to cluster 10.63.50.211:6379
>>> Performing Cluster Check (using node 10.63.50.211:6379)
M: 475fb3fa766efd1c544b16f52b96e491bf0a5834 10.63.50.211:6379
slots:[1333-5460] (4128 slots) master
1 additional replica(s)
S: e27162b370ad16615ef06316015e0f0fa7cb70d1 10.63.50.211:6380
slots: (0 slots) slave
replicates ff5659bd033d422d4505bc1e777cc43c830fef34
S: d9627863dd9f584a0408aae669c0352c71e536aa 10.63.50.212:6380
slots: (0 slots) slave
replicates 475fb3fa766efd1c544b16f52b96e491bf0a5834
M: 7057c747ff2207c69434901d567823688c6d7526 10.63.50.221:6379
slots:[0-1332],[5461-6794],[10923-12255] (4000 slots) master
M: ff5659bd033d422d4505bc1e777cc43c830fef34 10.63.50.213:6379
slots:[12256-16383] (4128 slots) master
1 additional replica(s)
M: c81a6aa66564109d8b8be2a8ae5bcf223cde2374 10.63.50.212:6379
slots:[6795-10922] (4128 slots) master
1 additional replica(s)
S: 1ac58a60a1dcb9e43be0e8e4b937cf1ff4cb6ffe 10.63.50.213:6380
slots: (0 slots) slave
replicates c81a6aa66564109d8b8be2a8ae5bcf223cde2374
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.63.50.221:6380 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 10.63.50.211:6379.
[OK] New node added correctly.
#修改从节点的master
[root@vps09 redis]# /usr/local/webserver/redis/bin/redis-cli -h 10.63.50.212 -p 6380 -c
/usr/local/webserver/redis/bin/redis-cli -h 10.3.0.9 -p 7000 -c
10.63.50.212:6380> cluster replicate 7057c747ff2207c69434901d567823688c6d7526
OK
10.63.50.212:6380>
#宕机多个节点强制指定failover
通过fail节点ID获取从节点的IP及端口,进入从节点 cluster failover TAKEOVER。
执行完之后该从节点 变为主节点
10.63.50.212:6380> cluster nodes
7057c747ff2207c69434901d567823688c6d7526 10.63.50.221:6379@16379 master,fail - 1554445446408 1554445440000 8 disconnected
ff5659bd033d422d4505bc1e777cc43c830fef34 10.63.50.213:6379@16379 slave e27162b370ad16615ef06316015e0f0fa7cb70d1 0 1554446437000 11 connected
e27162b370ad16615ef06316015e0f0fa7cb70d1 10.63.50.211:6380@16380 master - 0 1554446438000 11 connected 12256-16383
c81a6aa66564109d8b8be2a8ae5bcf223cde2374 10.63.50.212:6379@16379 slave 1ac58a60a1dcb9e43be0e8e4b937cf1ff4cb6ffe 0 1554446438000 13 connected
d9627863dd9f584a0408aae669c0352c71e536aa 10.63.50.212:6380@16380 myself,master - 0 1554446435000 14 connected 0-1332 5461-6794 10923-12255
1ac58a60a1dcb9e43be0e8e4b937cf1ff4cb6ffe 10.63.50.213:6380@16380 master - 0 1554446438000 13 connected 6795-10922
39b556be2695e68503b687771c51615cf9ba164d 10.63.50.221:6380@16380 master,fail - 1554445441395 1554445437000 12 disconnected 1333-5460
475fb3fa766efd1c544b16f52b96e491bf0a5834 10.63.50.211:6379@16379 slave 39b556be2695e68503b687771c51615cf9ba164d 0 1554446439686 12 connected
10.63.50.212:6380> exit
[root@vps09 ~]# /usr/local/webserver/redis/bin/redis-cli -h 10.63.50.211 -p 6379
10.63.50.211:6379> cluster failover TAKEOVER
127.0.0.1:6379> cluster nodes
ff5659bd033d422d4505bc1e777cc43c830fef34 10.63.50.213:6379@16379 myself,slave e27162b370ad16615ef06316015e0f0fa7cb70d1 0 1554446514000 5 connected
7057c747ff2207c69434901d567823688c6d7526 10.63.50.221:6379@16379 master,fail - 1554445442799 1554445438000 8 disconnected
475fb3fa766efd1c544b16f52b96e491bf0a5834 10.63.50.211:6379@16379 master - 0 1554446510000 15 connected 1333-5460
d9627863dd9f584a0408aae669c0352c71e536aa 10.63.50.212:6380@16380 master - 0 1554446513649 14 connected 0-1332 5461-6794 10923-12255
e27162b370ad16615ef06316015e0f0fa7cb70d1 10.63.50.211:6380@16380 master - 0 1554446515000 11 connected 12256-16383
c81a6aa66564109d8b8be2a8ae5bcf223cde2374 10.63.50.212:6379@16379 slave 1ac58a60a1dcb9e43be0e8e4b937cf1ff4cb6ffe 0 1554446515154 13 connected
39b556be2695e68503b687771c51615cf9ba164d 10.63.50.221:6380@16380 master,fail - 1554445441796 1554445439791 12 disconnected
1ac58a60a1dcb9e43be0e8e4b937cf1ff4cb6ffe 10.63.50.213:6380@16380 master - 0 1554446515655 13 connected 6795-10922
2、PHP-nginx配置文件在/usr/local/webserver/下
#升级基础软件
3、yum update -y glibc nss
4、#安装rvm
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
5、#导入rvm变量
find / -name rvm.sh
source /etc/profile.d/rvm.sh
rvm requirements
rvm install ruby 2.3.1
6、创建集群配置文件:7000.conf
[root@91_SWT_OL_Web3 redis]# cat 7000.conf
port 7000
cluster-enabled yes
cluster-config-file /usr/local/webserver/redis/redis-cluster/node-7000.conf
cluster-node-timeout 15000
daemonize yes
pidfile /var/run/redis_7000.pid
dir /var/redis/7000
logfile /var/log/redis/7000.log
appendonly yes
protected-mode no
8、创建集群配置文件:7001.conf
[root@91_SWT_OL_Web3 redis]# cat 7000.conf
port 7000
cluster-enabled yes
cluster-config-file /usr/local/webserver/redis/redis-cluster/node-7000.conf
cluster-node-timeout 15000
daemonize yes
pidfile /var/run/redis_7000.pid
dir /var/redis/7000
logfile /var/log/redis/7000.log
appendonly yes
protected-mode no
9、[root@91_SWT_OL_Web3 redis-cluster]# cp utils/redis_init_script /usr/local/webserver/redis/bin/reids-7000 拷贝开机文件 记得修改redis路径
[root@91_SWT_OL_Web3 redis-cluster]# cp utils/redis_init_script /usr/local/webserver/redis/bin/reids-7001 拷贝开机文件 记得修改redis路径
10、[root@91_SWT_OL_Web3 redis-cluster]# cp /usr/local/webserver/redis/bin/reids-7000 /etc/init.d/ 启动服务
[root@91_SWT_OL_Web3 redis-cluster]# cp /usr/local/webserver/redis/bin/reids-7001/etc/init.d/ 启动服务
[root@91_SWT_OL_Web3 redis-cluster]# chkconfig redis-7000 on 添加为开机自启
[root@91_SWT_OL_Web3 redis-cluster]# chkconfig redis-7001 on 添加为开机自启
11、[root@91_SWT_OL_Web3 redis-cluster]# /usr/local/webserver/redis/bin/redis-cli --cluster create 10.7.0.13:7000 10.7.0.13:7001 10.7.0.12:7000 10.7.0.12:7001 10.7.0.11:7000 10.7.0.11:7001 --cluster --replicas 1 创建节点
检查节点是否正常创建:
12、[root@91_SWT_OL_Web3 redis-cluster]# /usr/local/webserver/redis/bin/redis-cli -h 10.7.0.13 -p 7000 -c
10.7.0.13:7000> cluster nodes //检查集群
eedd54a0c8e0dda66f4ed2b8e6958d8296ce08fe 10.7.0.13:7001@17001 slave 67867932cf94cc00c78fb5acc874c346ac9aa05a 0 1570851600395 5 connected
21baaad5bf805b16bea56fba3f7b9e1f4fd91163 10.7.0.13:7000@17000 myself,master - 0 1570851600000 1 connected 0-5460
c7dbe7f5a6e0a472452a40349b09aef5f507410e 10.7.0.12:7001@17001 slave 21baaad5bf805b16bea56fba3f7b9e1f4fd91163 0 1570851602399 4 connected
67867932cf94cc00c78fb5acc874c346ac9aa05a 10.7.0.11:7000@17000 master - 0 1570851599000 5 connected 10923-16383
fb222b7abdbce0f698ae05ce8cfc124f652f1cb5 10.7.0.11:7001@17001 slave d8c31b9bc08848c5119e60d2642fde642cd35a84 0 1570851597390 6 connected
d8c31b9bc08848c5119e60d2642fde642cd35a84 10.7.0.12:7000@17000 master - 0 1570851601398 3 connected 5461-10922
10.7.0.13:7000>
13、根据redis集群部署文档配置启动redis。推送和mmc的redis为单点,修改域名,代码路径后启动nginx,修改php-fpm.conf后启动php-fpm,
https://blog.csdn.net/wzy0623/article/details/89405708
https://www.jianshu.com/p/853ee422b6d1
转移槽位到192.168.112.36:6004节点
./redis-trib.rb reshard --from bdd63e1f522d78eb1bb2574b2461a7302e14944a --to 1d204c88a14a76dc30abb05025135f7e850f2a5d --slots 5461 --yes 192.168.112.36:6004
./redis-trib.rb reshard --from be5b41880afac9c41b09e0d4e3be1ce1eb00959a --to 1d204c88a14a76dc30abb05025135f7e850f2a5d --slots 5462 --yes 192.168.112.36:6004
迁移卡槽全部分配到master 然后重新分曹
/usr/local/webserver/redis/bin/redis-cli --cluster reshard 149.28.16.223:6379 --cluster-from 12d78642c80b8d69bc7dd6a23d4b3eddd26517f4 --cluster-to 4bd7fa3e9ab70c12e89a1b6b8851a6059a91a2ab --cluster-slots 1000 --cluster-yes