集群规划:
三台服务器各部署一个主节点、一个从节点,同一台服务器上不是直接主从关系。
服务器
角色
ip:端口
node1
master1/slave2
192.168.146.199:6379/192.168.146.199:6380
node2
master2/slave3
192.168.146.200:6379/192.168.146.200:6380
node3
master3/slave1
192.168.146.201:6379/192.168.146.201:6380
命令行开头为#表示在所有主机上操作,命令行开后为具体主机名则表示在该主机上操作。
1、三台服务器基础设置
设置主机名:
# hostnamectl --static set-hostname node1/node2/node3
配置映射:
# vi /etc/hosts
192.168.146.199 node1
192.168.146.200 node2
192.168.146.201 node3
时区调整,时间校准:
# date -R
# timedatectl set-timezone Asia/Shanghai
# yum -y install ntp
# ntpdate ntp1.aliyun.com
安装wget:
$ yum install -y wget
关闭selinux:
# vi /etc/sysconfig/selinux
SELINUX=disabled
重启服务器并验证:
# getenforce
Disabled
设置防火墙
# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
# firewall-cmd --zone=public --add-port=6380/tcp --permanent
success
# firewall-cmd --zone=public --add-port=16379/tcp --permanent
success
# firewall-cmd --zone=public --add-port=16380/tcp --permanent
success
# firewall-cmd --permanent --zone=public --list-ports
6379/tcp 6380/tcp 16379/tcp 16380/tcp
# firewall-cmd --reload
success
2、gcc版本问题避免
# yum -y install gcc tcl
# yum -y install centos-release-scl
# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
查看gcc版本:
# gcc -v
scl只是临时启用,退出shell后会恢复原系统gcc版本:
# scl enable devtoolset-9 bash
如下命令表示永久启用:
# echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
查看gcc版本:
# gcc -v
3、redis安装
# cd /opt
# wget http://download.redis.io/releases/redis-6.0.8.tar.gz
# tar zxvf redis-6.0.8.tar.gz
# cd redis-6.0.8
# make MALLOC=libc
# make test
将执行文件弄到环境变量所在目录:
# make install
临时启动测试:
# redis-server
# ps -ef | grep redis
root 17830 8648 0 23:14 pts/0 00:00:00 redis-server *:6379
# redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
创建日志文件存放目录:
# mkdir -p /var/log/redis/
创建工作目录:
# mkdir -p /var/lib/redis/6379
# mkdir -p /var/lib/redis/6380
4、修改redis配置文件
节点1上修改配置文件:
[root@node1 ~]# cd /opt/redis-6.0.8
[root@node1 redis-6.0.8]# mkdir cluster
[root@node1 redis-6.0.8]# cp -i redis.conf cluster/redis-6379.conf
[root@node1 redis-6.0.8]# vi cluster/redis-6379.conf
port 6379
daemonize yes
dir /var/lib/redis/6379
logfile "/var/log/redis/redis-6379.log"
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
cluster-require-full-coverage no
appendonly yes
#port 6379 #端口
#daemonize yes #守护进程
#dir /var/lib/redis/6379 #数据存储目录
#logfile "/var/log/redis/redis-6379.log" #日志存储目录
#cluster-enabled yes #开启集群
#cluster-config-file nodes-6379.conf
#cluster-node-timeout 15000
#cluster-require-full-coverage no
#appendonly yes #持久化
将节点1服务器配置文件拷贝到节点2、节点3:
[root@node1 redis-6.0.8]# scp -r /opt/redis-6.0.8/cluster/ root@192.168.146.200:/opt/redis-6.0.8/
[root@node1 redis-6.0.8]# scp -r /opt/redis-6.0.8/cluster/ root@192.168.146.201:/opt/redis-6.0.8/
修改节点服务器监听IP地址:
[root@node1 opt]# cd /opt/redis-6.0.8
[root@node1 opt]# sed -i 's/127.0.0.1/192.168.146.199/g' cluster/redis-6379.conf
[root@node2 opt]# cd /opt/redis-6.0.8
[root@node2 opt]# sed -i 's/127.0.0.1/192.168.146.200/g' cluster/redis-6379.conf
[root@node3 opt]# cd /opt/redis-6.0.8
[root@node3 opt]# sed -i 's/127.0.0.1/192.168.146.201/g' cluster/redis-6379.conf
根据7379生成6380配置文件:
# sed 's/6379/6380/g' cluster/redis-6379.conf > cluster/redis-6380.conf
5、制作redis启动配置文件
[root@node1 cluster]# cd /opt/redis-6.0.8
制作启动脚本:
[root@node1 redis-6.0.8]# vi cluster_start.sh
redis-server cluster/redis-6379.conf
redis-server cluster/redis-6380.conf
[root@node1 redis-6.0.8]# chmod +x cluster_start.sh
制作关闭脚本:
[root@node1 redis-6.0.8]# vi cluster_shutdown.sh
pgrep redis-server | xargs -exec kill -9
[root@node1 redis-6.0.8]# chmod +x cluster_shutdown.sh
将节点1服务器启动关闭脚本拷贝到节点2、节点3:
[root@node1 redis-6.0.8]# scp -r /opt/redis-6.0.8/cluster_*.sh root@192.168.146.200:/opt/redis-6.0.8/
[root@node1 redis-6.0.8]# scp -r /opt/redis-6.0.8/cluster_*.sh root@192.168.146.201:/opt/redis-6.0.8/
启动redis:
# ./cluster_start.sh
# ps -ef|grep redis
root 26731 1 0 01:23 ? 00:00:00 redis-server 192.168.146.199:6379 [cluster]
root 26736 1 0 01:23 ? 00:00:00 redis-server 192.168.146.199:6380 [cluster]
root 26741 8648 0 01:23 pts/0 00:00:00 grep --color=auto redis
关闭redis:
# ./cluster_shutdown.sh
6、创建集群
创建集群,--cluster-replicas 1代表3主3从,前3个代表3个master,后3个代表3个slave。通过该方式创建的带有从节点的机器不能够自己手动指定主节点
[root@node1 redis-6.0.8]# redis-cli --cluster create 192.168.146.199:6379 192.168.146.200:6379 192.168.146.201:6379 192.168.146.199:6380 192.168.146.200:6380 192.168.146.201:6380 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.146.200:6380 to 192.168.146.199:6379
Adding replica 192.168.146.201:6380 to 192.168.146.200:6379
Adding replica 192.168.146.199:6380 to 192.168.146.201:6379
M: 7a0af6d896222c60b582c8ecb16d1369a5ef33b4 192.168.146.199:6379
slots:[0-5460] (5461 slots) master
M: 825e26272e82f4900f2b99bb05cc8efa87f2c238 192.168.146.200:6379
slots:[5461-10922] (5462 slots) master
M: e52b4a755ab35381931ba89cf0399ac2657c0d93 192.168.146.201:6379
slots:[10923-16383] (5461 slots) master
S: caed0e5b336cb31d6d6b29874170075a21e6923b 192.168.146.199:6380
replicates e52b4a755ab35381931ba89cf0399ac2657c0d93
S: c155299d628e705565915cec624faf67946556a5 192.168.146.200:6380
replicates 7a0af6d896222c60b582c8ecb16d1369a5ef33b4
S: d9bca2b5cabb426aa2e296e6696e60e35ef4bebe 192.168.146.201:6380
replicates 825e26272e82f4900f2b99bb05cc8efa87f2c238
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 cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.146.199:6379)
M: 7a0af6d896222c60b582c8ecb16d1369a5ef33b4 192.168.146.199:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: c155299d628e705565915cec624faf67946556a5 192.168.146.200:6380
slots: (0 slots) slave
replicates 7a0af6d896222c60b582c8ecb16d1369a5ef33b4
M: 825e26272e82f4900f2b99bb05cc8efa87f2c238 192.168.146.200:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: d9bca2b5cabb426aa2e296e6696e60e35ef4bebe 192.168.146.201:6380
slots: (0 slots) slave
replicates 825e26272e82f4900f2b99bb05cc8efa87f2c238
M: e52b4a755ab35381931ba89cf0399ac2657c0d93 192.168.146.201:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: caed0e5b336cb31d6d6b29874170075a21e6923b 192.168.146.199:6380
slots: (0 slots) slave
replicates e52b4a755ab35381931ba89cf0399ac2657c0d93
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
备注:注意看M和S,对照下集群角色表
7、查看集群信息
查看集群状态:
[root@node1 redis-6.0.8]# redis-cli -c -h 192.168.146.199 -p 6379 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:482
cluster_stats_messages_pong_sent:488
cluster_stats_messages_sent:970
cluster_stats_messages_ping_received:483
cluster_stats_messages_pong_received:482
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:970
查看集群节点信息:
[root@node1 redis-6.0.8]# redis-cli -c -h 192.168.146.199 -p 6379 cluster nodes
7a0af6d896222c60b582c8ecb16d1369a5ef33b4 192.168.146.199:6379@16379 myself,master - 0 1600191841000 1 connected 0-5460
c155299d628e705565915cec624faf67946556a5 192.168.146.200:6380@16380 slave 7a0af6d896222c60b582c8ecb16d1369a5ef33b4 0 1600191840159 1 connected
825e26272e82f4900f2b99bb05cc8efa87f2c238 192.168.146.200:6379@16379 master - 0 1600191842184 2 connected 5461-10922
d9bca2b5cabb426aa2e296e6696e60e35ef4bebe 192.168.146.201:6380@16380 slave 825e26272e82f4900f2b99bb05cc8efa87f2c238 0 1600191843194 2 connected
e52b4a755ab35381931ba89cf0399ac2657c0d93 192.168.146.201:6379@16379 master - 0 1600191842000 3 connected 10923-16383
caed0e5b336cb31d6d6b29874170075a21e6923b 192.168.146.199:6380@16380 slave e52b4a755ab35381931ba89cf0399ac2657c0d93 0 1600191844207 3 connected
8、测试用例
[root@node1 redis-6.0.8]# redis-cli -c -h 192.168.146.199 -p 6379
192.168.146.199:6379> set name node1
-> Redirected to slot [5798] located at 192.168.146.200:6379
OK
[root@node2 redis-6.0.8]# redis-cli -c -h 192.168.146.200 -p 6379
192.168.146.200:6379> get name
"node1"
root@node3 redis-6.0.8]# redis-cli -c -h 192.168.146.201 -p 6379
192.168.146.201:6379> get name
-> Redirected to slot [5798] located at 192.168.146.200:6379
"node1"
192.168.146.201:6379> del name
-> Redirected to slot [5798] located at 192.168.146.200:6379
(integer) 1
192.168.146.201:6379> get name
-> Redirected to slot [5798] located at 192.168.146.200:6379
(nil)
9、目录说明
# cd /var/lib/redis/6379
# ll
total 8
-rw-r--r-- 1 root root 0 Sep 16 01:21 appendonly.aof
-rw-r--r-- 1 root root 175 Sep 16 01:35 dump.rdb
-rw-r--r-- 1 root root 817 Sep 16 01:36 nodes-6379.conf
# cd /var/lib/redis/6380
# ll
total 12
-rw-r--r-- 1 root root 149 Sep 16 01:52 appendonly.aof
-rw-r--r-- 1 root root 191 Sep 16 01:52 dump.rdb
-rw-r--r-- 1 root root 817 Sep 16 01:36 nodes-6380.conf