目录
一.redis群集介绍
主从同步/复制
哨兵模式
Cluster群集
二.redis-cluster群集搭建
实验环境
master的具体配置
创建群集
salve的具体配置
验证群集功能
类别 | IP地址 | 系统 | 软件包 |
master | ens33:192.168.43.101 ens37:192.168.43.247 ens38:192.168.43.248 |
centos7 | redis-5.0.7.tar.gz rvm-1.29.9.tar.gz |
slave | ens33:192.168.43.102 ens36:192.168.43.185 ens37:192.168.43.156 |
centos7 | redis-5.0.7.tar.gz |
1.下载redis的安装包
[root@localhost ~]# yum install vsftpd -y
[root@localhost ~]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
2.通过yum工具,安装编译工具
[root@localhost ~]# yum install gcc gcc-c++ make -y
3.解压redis软件包
tar xzvf redis-5.0.7.tar.gz -C /opt
4.编译且安装
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
5.开启服务,设置各类文件
cd /opt/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] //定义主配置文件
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] //定义日志文件
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] //数据文件
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server //可执行文件路径,需要自行定义
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@localhost redis]# netstat -natp | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 6475/redis-server 1
6.创建软链接,便于服务控制
ln -s /usr/local/redis/bin/* /usr/local/bin/
7.进入redis数据库
[root@localhost init.d]# vim /etc/redis/6379.conf
bind 127.0.0.1 192.168.43.101 //绑定本机地址
[root@localhost init.d]# service redis_6379 restart //重启服务
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost ~]# redis-cli -h 192.168.43.101 -p 6379 //-h指定地址,-p指定端口
192.168.43.101:6379>
redis的配置文件修改
[root@localhost ~]# cd /etc/redis/
[root@localhost redis]# ls
6379.conf
[root@localhost redis]# vim 6379.conf
#bind 127.0.0.1 //注释第70行的监听127地址,表示监听所有地址
protected-mode no //去掉第89行注释,关闭安全保护
port 6379 //去掉第93行注释,开启端口6379
daemonize yes //去掉第137行注释,以独立进程启动
appendonly yes //去掉第700行注释,开启aof持久化功能
cluster-enabled yes //去掉第833行注释,开启群集功能
cluster-config-file node-6379.conf //去掉第841行注释,群集名称文件设置
cluster-node-timeout 15000 //去掉第847行注释,群集超时时间设置
[root@localhost ~]# service redis_6379 restart //重启服务
[root@localhost ~]# ls /var/lib/redis/6379/ //生成下列三个文件说明设置成功
appendonly.aof dump.rdb nodes-6379.conf
[root@localhost ~]#
安装rvm、ruby等控制群集软件包
先安装rvm
[root@master1 6379]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 '//导入key文件,若出现error,重新导入一次即可'
[root@master1 6379]# curl -sSL https://get.rvm.io | bash -s stable '//安装rvm'
注:执行 gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3第一次时会出现超时错误,可继续执行第二次
执行curl -sSL https://get.rvm.io | bash -s stable时可能会出现curl: (7) Failed connect to get.rvm.io:443; 拒绝连接错误
可以关闭虚拟机防火墙,安全性功能,也有也可能时网络比较差,此外也可以把https换成http试试
执行上述命令之后,到指定的网址下载rvm软件包
[root@master1 ]# tar xzvf rvm-1.29.9.tar.gz -C /opt
[root@master1 ]# cd /opt/rvm-1.29.9/
[root@master1 rvm-master]# ./install
[root@master1 rvm-master]# source /etc/profile.d/rvm.sh '//执行环境变量'
[root@master1 rvm-master]# rvm list known '//列出ruby可以安装的版本'
[root@master1 rvm-master]# rvm install 2.4.1 '//安装ruby2.4.1版本,安装版本的事件会比较长'
[root@master1 rvm-master]# ruby -v '//查看当前ruby版本'
[root@master1 rvm-master]# gem install redis '//再次安装redis'
[root@localhost ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.43.101 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::ba30:ef5e:b59f:4f1c prefixlen 64 scopeid 0x20
ether 00:0c:29:ce:5f:24 txqueuelen 1000 (Ethernet)
RX packets 20474 bytes 23306731 (22.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 13349 bytes 1202079 (1.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163 mtu 1500
inet 192.168.43.247 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::5002:420d:b2f3:d518 prefixlen 64 scopeid 0x20
ether 00:0c:29:ce:5f:2e txqueuelen 1000 (Ethernet)
RX packets 25 bytes 4365 (4.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 18 bytes 3497 (3.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens38: flags=4163 mtu 1500
inet 192.168.43.248 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::65f1:4a49:7551:b2ad prefixlen 64 scopeid 0x20
ether 00:0c:29:ce:5f:38 txqueuelen 1000 (Ethernet)
RX packets 17 bytes 3581 (3.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22 bytes 3757 (3.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 68 bytes 5916 (5.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 5916 (5.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:7a:bf:df txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]#
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]#
[root@localhost ~]# redis-cli --cluster create 192.168.43.101:6379 192.168.43.102:6379 192.168.43.185:6379 192.168.43.156:6379 192.168.43.247:6379 192.168.43.248:6379 --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.43.247:6379 to 192.168.43.101:6379
Adding replica 192.168.43.248:6379 to 192.168.43.102:6379
Adding replica 192.168.43.156:6379 to 192.168.43.185:6379
M: fb29c713b2a345948594e2dfc3ba0d1d7e2b892b 192.168.43.101:6379
slots:[0-5460] (5461 slots) master
M: f0bb6db083e065ca614b306e92a4c7e347379746 192.168.43.102:6379
slots:[5461-10922] (5462 slots) master
M: f0bb6db083e065ca614b306e92a4c7e347379746 192.168.43.185:6379
slots:[10923-16383] (5461 slots) master
S: f0bb6db083e065ca614b306e92a4c7e347379746 192.168.43.156:6379
replicates f0bb6db083e065ca614b306e92a4c7e347379746
S: fb29c713b2a345948594e2dfc3ba0d1d7e2b892b 192.168.43.247:6379
replicates fb29c713b2a345948594e2dfc3ba0d1d7e2b892b
S: fb29c713b2a345948594e2dfc3ba0d1d7e2b892b 192.168.43.248:6379
replicates f0bb6db083e065ca614b306e92a4c7e347379746
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.43.101:6379)
M: fb29c713b2a345948594e2dfc3ba0d1d7e2b892b 192.168.43.101:6379
slots:[0-5460] (5461 slots) master
M: f0bb6db083e065ca614b306e92a4c7e347379746 192.168.43.102:6379
slots:[5461-16383] (10923 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost ~]#
'//一主一从的绑定关系是随机的'
1.下载redis的安装包
[root@localhost ~]# yum install vsftpd -y
[root@localhost ~]# wget http://download.redis.io/releases/redis-5.0.7.tar.gz
2.通过yum工具,安装编译工具
[root@localhost ~]# yum install gcc gcc-c++ make -y
3.解压redis软件包
tar xzvf redis-5.0.7.tar.gz -C /opt
4.编译且安装
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
5.开启服务,设置各类文件
cd /opt/redis-5.0.7/utils/
[root@localhost utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] //定义主配置文件
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] //定义日志文件
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] //数据文件
Selected default - /var/lib/redis/6379
Please select the redis executable path [] /usr/local/redis/bin/redis-server //可执行文件路径,需要自行定义
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@localhost redis]# netstat -natp | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 6475/redis-server 1
6.创建软链接,便于服务控制
ln -s /usr/local/redis/bin/* /usr/local/bin/
7.进入redis数据库
[root@localhost init.d]# vim /etc/redis/6379.conf
bind 127.0.0.1 //绑定本机地址
[root@localhost init.d]# service redis_6379 restart //重启服务
Stopping ...
Redis stopped
Starting Redis server...
[root@localhost ~]# redis-cli -h 192.168.43.102 -p 6379 //-h指定地址,-p指定端口
192.168.43.102:6379>
[root@localhost ~]# cd /etc/redis/
[root@localhost redis]# ls
6379.conf
[root@localhost redis]# vim 6379.conf
#bind 127.0.0.1 //注释第70行的监听127地址,表示监听所有地址
protected-mode no //去掉第89行注释,关闭安全保护
port 6379 //去掉第93行注释,开启端口6379
daemonize yes //去掉第137行注释,以独立进程启动
appendonly yes //去掉第700行注释,开启aof持久化功能
cluster-enabled yes //去掉第833行注释,开启群集功能
cluster-config-file node-6379.conf //去掉第841行注释,群集名称文件设置
cluster-node-timeout 15000 //去掉第847行注释,群集超时时间设置
[root@localhost ~]# service redis_6379 restart //重启服务
[root@localhost ~]# ls /var/lib/redis/6379/ //生成下列三个文件说明设置成功
appendonly.aof dump.rdb nodes-6379.conf
[root@localhost ~]#
[root@localhost ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.43.102 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::8bfb:257d:51e7:df8d prefixlen 64 scopeid 0x20
ether 00:0c:29:0b:69:b8 txqueuelen 1000 (Ethernet)
RX packets 104 bytes 14082 (13.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 130 bytes 15325 (14.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens36: flags=4163 mtu 1500
inet 192.168.43.185 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::e290:e267:67cc:d325 prefixlen 64 scopeid 0x20
ether 00:0c:29:0b:69:cc txqueuelen 1000 (Ethernet)
RX packets 23 bytes 4114 (4.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 23 bytes 3790 (3.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163 mtu 1500
inet 192.168.43.156 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::27d8:17c4:73f1:4b4a prefixlen 64 scopeid 0x20
ether 00:0c:29:0b:69:c2 txqueuelen 1000 (Ethernet)
RX packets 15 bytes 2534 (2.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21 bytes 3566 (3.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 68 bytes 5916 (5.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 5916 (5.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:ae:e3:34 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]#
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]#
[root@localhost ~]# redis-cli -h 192.168.43.101 -p 6379 '//登陆到101服务器数据库'
192.168.43.101:6379> keys *
(empty list or set)
192.168.43.101:6379> set user lisi '//创建一个键值'
OK
192.168.43.101:6379> keys * '//查看所有键'
1) "user"
192.168.43.101:6379> get user '//查看user键的值'
"lisi"
192.168.43.101:6379> exit '//退出'
[root@localhost ~]# redis-cli -h 192.168.43.247 -p 6379 '//登陆到247服务器数据库'
192.168.43.247:6379> keys *
1) "user"
192.168.43.247:6379> get user '//查看user键的值'
(error) MOVED 5474 192.168.43.102:6379 '//提示移动到102节点上'
192.168.43.247:6379> exit
[root@localhost ~]# redis-cli -h 192.168.43.102 -p 6379 '//登陆到102服务器的数据库'
192.168.43.102:6379> keys *
1) "user"
192.168.43.102:6379> get user
"lisi"
192.168.43.102:6379> exit