Redis缓存加速群集

旧城冷巷雨未停 提交于 2020-12-31 10:32:35

Redis缓存加速群集

                                Redis缓存加速
-----master的具体配置--------
安装redis
1.下载redis的安装包
[root@localhost ~]# mount.cfs //192.168.1.150/qq-Download /mnt
[root@localhost ~]# cd /mnt
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.100.140 -p 6379    //-h指定地址,-p指定端口
192.168.100.140: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@localhost 6379]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3    '//导入key文件,若出现error,重新导入一次即可'
[root@localhost 6379]# curl -sSL https://get.rvm.io | bash -s stable    '//安装rvm'

执行上述命令之后,到指定的网址下载rvm软件包

----继续安装ruby
[root@localhost ]# tar xzvf rvm-1.29.9.tar.gz -C /opt
[root@localhost ]# cd /opt/rvm-1.29.9/
[root@localhost rvm-master]# ./install
[root@localhost rvm-master]# source /etc/profile.d/rvm.sh   '//执行环境变量'
[root@localhost rvm-master]# rvm list known '//列出ruby可以安装的版本'
[root@localhost rvm-master]# rvm install 2.4.1  '//安装ruby2.4.1版本,安装版本的事件会比较长'
[root@localhost rvm-master]# rvm use 2.4.1
[root@localhost rvm-master]# ruby -v '//查看当前ruby版本'
[root@localhost rvm-master]# gem install redis '//再次安装redis'

----在主服务器上添加两张网卡(NAT模式)
[root@localhost 6379]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.140  netmask 255.255.255.0  broadcast 192.168.100.255
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.128  netmask 255.255.255.0  broadcast 192.168.100.255
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.130  netmask 255.255.255.0  broadcast 192.168.100.255

[root@localhost ~]# 
----关闭防火墙,SElinux功能
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# 

----创建群集

[root@localhost 6379]# redis-cli --cluster create 192.168.100.140:6379 192.168.100.128:6379 192.168.100.130:6379 192.168.100.150:6379 192.168.100.129:6379 192.168.100.131: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.100.129:6379 to 192.168.100.140:6379                 '//一主一从的绑定关系是随机的'
Adding replica 192.168.100.131:6379 to 192.168.100.128:6379
Adding replica 192.168.100.150:6379 to 192.168.100.130:6379
M: ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb 192.168.100.140:6379
   slots:[0-5460],[10923-16383] (5461 slots) master
M: ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb 192.168.100.128:6379
   slots:[0-16383] (5462 slots) master
M: ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb 192.168.100.130:6379
   slots:[0-5460],[10923-16383] (5461 slots) master
S: 637bebedda821add8127dc8355d2722cf61b73b1 192.168.100.150:6379
   replicates ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb
S: 637bebedda821add8127dc8355d2722cf61b73b1 192.168.100.129:6379
   replicates ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb
S: 637bebedda821add8127dc8355d2722cf61b73b1 192.168.100.131:6379
   replicates ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb
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.100.140:6379)
M: ef8534eb9a9a9029220c08a8b4fc4a5b3fb591bb 192.168.100.140:6379
   slots:[0-5460],[10923-16383] (10922 slots) master
M: 637bebedda821add8127dc8355d2722cf61b73b1 192.168.100.150:6379
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost 6379]# 

>生产环境rvm和ruby只在一个节点安装,搭建群集
>其他节点只安装Redis开放数据库端口即可

-------------------------------salve的具体配置------------------------------------
安装redis
1.下载redis的安装包
[root@localhost ~]# mount.cfs //192.168.1.150/qq-Download /mnt
[root@localhost ~]# cd /mnt
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.100.150 -p 6379    //-h指定地址,-p指定端口
192.168.100.150:6379> 

8.启用群集
#修改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       "'//生成了三个文件,appendonly.aof是AOF持久化文件,dump.rdb是RDB快照文件,nodes-6379.conf是节点首次启动生成的配置文件'"
[root@localhost ~]# 

----添加两张网卡,NAT模式
[root@slave ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.150  netmask 255.255.255.0  broadcast 192.168.100.255

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.129  netmask 255.255.255.0  broadcast 192.168.100.255

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.131  netmask 255.255.255.0  broadcast 192.168.100.255

[root@localhost ~]# 

#关闭防火墙,SElinux
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# 

-----验证群集功能
#验证主从数据同步                   "-c 指定登陆群集"
"创建键值只能在master"
[root@localhost ~]# redis-cli -c -h 192.168.100.140 '//登陆到140服务器数据库'
192.168.100.140:6379> keys *
(empty list or set)
192.168.100.140:6379> set user lisi '//创建一个键值'
OK
192.168.100.140:6379> keys *        '//查看所有键'
1) "user"
192.168.100.140:6379> get user      '//查看user键的值'
"lisi"
192.168.100.140:6379> exit          '//退出'
[root@localhost ~]# redis-cli -c -h 192.168.43.247  '//登陆到150服务器数据库'
192.168.100.150:6379> keys *
1) "user"
192.168.100.150:6379> get user              '查看user键的值'
(error) MOVED 5474 192.168.100.129:6379     '//提示移动到129节点上'
192.168.100.150:6379> exit
[root@localhost ~]# redis-cli -c 192.168.100.129        '//登陆到129服务器的数据库'
192.168.100.129:6379> keys *
1) "user"
192.168.100.129:6379> get user
"lisi"
192.168.100.129:6379> exit
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!