Redis——6.0集群安装部署
前言
redis集群化部署主要用于大型缓存架构,一般的小型架构,使用redis主从配置 + sentinel哨兵集群应付系统压力,
使用redis集群可以方便快捷地对集群进行动态扩容,动态的添加、删除节点,reshard、并带有自动故障恢复功能。
一般redis集群使用三主三从,并且尽量保证主服务器与从服务器不在同一台机器上,防止机器故障导致的集群瘫痪,每个主服务器搭配一个从服务器,保证集群的高可用性。
一、集群规划
三台服务器各部署一个主节点、一个从节点,同一台服务器上不是直接主从关系。
服务器 |
角色 |
ip:端口 |
node1 |
master1/slave2 |
192.168.146.199:7000/192.168.146.199:7001 |
node2 |
master2/slave3 |
192.168.146.200:7000/192.168.146.200:7001 |
node3 |
master3/slave1 |
192.168.146.201:7000/192.168.146.201:7001 |
软件版本:
OS:CentOS7.5
Redis:redis-6.0.9
二、服务器设置
设置主机名:
# 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
关闭selinux:
# vi /etc/sysconfig/selinux SELINUX=disabled
重启服务器并验证:
# getenforce
Disabled
关闭防火墙:
# service iptables stop
# chkconfig iptables off
内核设置:
1、 先执行:chmod +x /etc/rc.d/rc.local
2、 将 vm.overcommit_memory = 1 添加到/etc/sysctl.conf中,然后重启服务器或运行命令sysctl vm.overcommit_memory = 1使其立即生效。
3、 确保禁用Linux内核功能透明的大页面,它将以负面的方式极大地影响内存使用和延迟。这可以通过以下命令完成: 先直接执行(临时生效): echo never> /sys/kernel/mm/transparent_hugepage/enabled 再执行以下命令(永久生效): vim /etc/rc.local 追加:echo never>/sys/kernel/mm/transparent_hugepage/enabled
4、 解决问题:【TCP backlog设置值,511没有成功,因为 /proc/sys/net/core/somaxconn这个设置的是更小的128】 先直接执行(临时生效): echo 511 > /proc/sys/net/core/somaxconn 再执行以下命令(永久生效): vim /etc/rc.local 追加:echo 511 > /proc/sys/net/core/somaxconn
5、 重启服务器。
/etc/rc.local参考图:
三、Redis安装
分别在三台服务器进行如下操作。
1、gcc版本问题避免
Redis是c语言开发的。安装redis需要c语言的编译环境。
安装redis6最主要的一点是要用GCC5以上,而CentOS6.9的GCC版本为4.8.x, 所以安装之前必须升级GCC(使用命令gcc --version查看版本)。
# 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
2、下载安装
cd /opt wget http://download.redis.io/releases/redis-6.0.9.tar.gz tar -xvf redis-6.0.9.tar.gz cd redis-6.0.9 make MALLOC=libc make install PREFIX=/usr/local/redis
查看/usr/local/redis/bin,如看见redis具工表示redis已安装成功:
3、Redis配置
建立安裝Redis時会用到的文件夹:
mkdir -p /usr/local/redis/run mkdir -p /usr/local/redis/log mkdir -p /usr/local/redis/data/7000 mkdir -p /usr/local/redis/conf
设置redis配置文件:
cp /opt/redis-6.0.9/redis.conf /usr/local/redis/conf/redis_7000.conf vi /usr/local/redis/conf/redis_7000.conf
打开redis_7000.conf文件,修改以下内容:
bind 192.168.146.199 #添加本机的ip port 7000 #端口 pidfile /usr/local/redis/run/redis_7000.pid #pid存储目录 logfile /usr/local/redis/log/redis_7000.log #日志存储目录 dir /usr/local/redis/data/7000 #数据存储目录,目录要提前创建好 cluster-enabled yes #开启集群 cluster-config-file nodes-7000.conf #集群节点配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通 cluster-node-timeout 15000 #集群节点的超时时间,单位:ms,超时后集群会认为该节点失败 appendonly yes #持久化 daemonize yes #守护进程
4、建立不同端口实例
由于我们的机器有限,我们将采用一台机器多个端口的方式搭建我们的Redis集群。
复制配置文件:
# cp /usr/local/redis/conf/redis_7000.conf /usr/local/redis/conf/redis_7001.conf
# mkdir -p /usr/local/redis/data/7001
# vi /usr/local/redis/conf/redis_7001.conf
打开redis_7001.conf文件,修改以下内容:
bind 192.168.146.199 port 7001 pidfile /usr/local/redis/run/redis_7001.pid logfile /usr/local/redis/log/redis_7001.log dir /usr/local/redis/data/7001 #目录要提前创建好 cluster-config-file nodes-7001.conf
如果要复制多个端口,按上面的步骤重复操作,修改端口号即可。
5、制作启动配置文件
# cd /usr/local/redis/bin
启动脚本:
# vi cluster_start.sh ./redis-server ../conf/redis_7000.conf ./redis-server ../conf/redis_7001.conf # chmod +x cluster_start.sh
关闭脚本:
# vi cluster_shutdown.sh pgrep redis-server | xargs -exec kill -9 # chmod +x cluster_shutdown.sh
6、启动&关闭Redis:
启动redis:
# ./cluster_start.sh # ps -ef|grep redis root 26731 1 0 01:23 ? 00:00:00 redis-server 192.168.146.199:7000 [cluster] root 26736 1 0 01:23 ? 00:00:00 redis-server 192.168.146.199:7001 [cluster] root 26741 8648 0 01:23 pts/0 00:00:00 grep --color=auto redis
关闭redis:
# ./cluster_shutdown.sh
四、Redis集群
建立集群前需先啟動各個节点的redis服務,並在其中一个redis服务器中执行以下指令建立集群。
1、创建集群
在redis3.0和4.0版本中,创建集群还是使用redis-trib.rb方式去创建,但是在5.0之后,可以直接使用redis-cli直接创建集群,本文redis版本为6.0,所以创建集群的方式为redis-cli方式直接创建。
用以下命令创建集群,--cluster-replicas 1 参数表示希望每个主服务器都有一个从服务器,这里则代表3主3从,前3个代表3个master,后3个代表3个slave。
通过该方式创建的带有从节点的机器不能够自己手动指定主节点,redis集群会尽量把主从服务器分配在不同机器上。
# redis-cli --cluster create 192.168.146.199:7000 192.168.146.200:7000 192.168.146.201:7000 192.168.146.199:7001 192.168.146.200:7001 192.168.146.201:7001 --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:7000 Adding replica 192.168.146.201:6380 to 192.168.146.200:7000 Adding replica 192.168.146.199:6380 to 192.168.146.201:7000 M: 7a0af6d896222c60b582c8ecb16d1369a5ef33b4 192.168.146.199:7000 slots:[0-5460] (5461 slots) master M: 825e26272e82f4900f2b99bb05cc8efa87f2c238 192.168.146.200:7000 slots:[5461-10922] (5462 slots) master M: e52b4a755ab35381931ba89cf0399ac2657c0d93 192.168.146.201:7000 slots:[10923-16383] (5461 slots) master S: caed0e5b336cb31d6d6b29874170075a21e6923b 192.168.146.199:7001 replicates e52b4a755ab35381931ba89cf0399ac2657c0d93 S: c155299d628e705565915cec624faf67946556a5 192.168.146.200:7001 replicates 7a0af6d896222c60b582c8ecb16d1369a5ef33b4 S: d9bca2b5cabb426aa2e296e6696e60e35ef4bebe 192.168.146.201:7001 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:7000) M: 7a0af6d896222c60b582c8ecb16d1369a5ef33b4 192.168.146.199:7000 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: c155299d628e705565915cec624faf67946556a5 192.168.146.200:7000 slots: (0 slots) slave replicates 7a0af6d896222c60b582c8ecb16d1369a5ef33b4 M: 825e26272e82f4900f2b99bb05cc8efa87f2c238 192.168.146.200:7000 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: d9bca2b5cabb426aa2e296e6696e60e35ef4bebe 192.168.146.201:7000 slots: (0 slots) slave replicates 825e26272e82f4900f2b99bb05cc8efa87f2c238 M: e52b4a755ab35381931ba89cf0399ac2657c0d93 192.168.146.201:7000 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: caed0e5b336cb31d6d6b29874170075a21e6923b 192.168.146.199:7001 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,对照下集群角色表
2、查看集群状态
# redis-cli -c -h 192.168.146.199 -p 7000 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
3、查看集群节点
# redis-cli -c -h 192.168.146.199 -p 7000 cluster nodes 7a0af6d896222c60b582c8ecb16d1369a5ef33b4 192.168.146.199:7000@17000 myself,master - 0 1600191841000 1 connected 0-5460 c155299d628e705565915cec624faf67946556a5 192.168.146.200:7001@17001 slave 7a0af6d896222c60b582c8ecb16d1369a5ef33b4 0 1600191840159 1 connected 825e26272e82f4900f2b99bb05cc8efa87f2c238 192.168.146.200:7000@17000 master - 0 1600191842184 2 connected 5461-10922 d9bca2b5cabb426aa2e296e6696e60e35ef4bebe 192.168.146.201:7001@17001 slave 825e26272e82f4900f2b99bb05cc8efa87f2c238 0 1600191843194 2 connected e52b4a755ab35381931ba89cf0399ac2657c0d93 192.168.146.201:7000@17000 master - 0 1600191842000 3 connected 10923-16383 caed0e5b336cb31d6d6b29874170075a21e6923b 192.168.146.199:7001@17001 slave e52b4a755ab35381931ba89cf0399ac2657c0d93 0 1600191844207 3 connected
五、测试用例
[root@node1 redis-6.0.9]# redis-cli -c -h 192.168.146.199 -p 7000 192.168.146.199:7000> set name node1 -> Redirected to slot [5798] located at 192.168.146.200:7000 OK [root@node2 redis-6.0.9]# redis-cli -c -h 192.168.146.200 -p 7000 192.168.146.200:7000> get name "node1" root@node3 redis-6.0.9]# redis-cli -c -h 192.168.146.201 -p 7000 192.168.146.201:7000> get name -> Redirected to slot [5798] located at 192.168.146.200:7000 "node1" 192.168.146.201:7000> del name -> Redirected to slot [5798] located at 192.168.146.200:7000 (integer) 1 192.168.146.201:7000> get name -> Redirected to slot [5798] located at 192.168.146.200:7000 (nil)
六、目录结构
注:刚开始每台服务器只有2个节点,后面又追加了4个节点。