redis群集,实验环境两台服务器,六个节点,每台服务器三个网卡
6个节点三个主,三个副本
主服务器安装redis
[[email protected] ~]# yum install gcc gcc-c++ make -y #安装环境包
[[email protected] ~]# mkdir /abc
[[email protected] ~]# mount.cifs //192.168.100.25/redis /abc/ 挂载
Password for [email protected]//192.168.100.25/redis:
[[email protected] ~]# cd /abc/
[[email protected] abc]# ls
redis-5.0.7.tar.gz
[[email protected] abc]# tar zxvf redis-5.0.7.tar.gz -C /opt/ #解压到opt底下
[[email protected] abc]# cd /opt/redis-5.0.7/
make
[[email protected] redis-5.0.7]# make PREFIX=/usr/local/redis install #安装
[[email protected] redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/ #做软连接
[[email protected] redis-5.0.7]# cd /opt/redis-5.0.7/utils/
[[email protected] 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/bin/redis-server] /usr/local/redis/bin/redis-server
从服务器(一样的操作不重复了)
主服务器配置redis
带“#”的原本配置文件的内容方便写shell脚本
[[email protected] utils]# vim /etc/redis/6379.conf
#70 bind 127.0.0.1
70 #bind 127.0.0.1
#89 protected-mode yes
89 protected-mode no #关闭保护功能
#833 # cluster-enabled yes
833 cluster-enabled yes #开启群集功能
#841 # cluster-config-file nodes-6379.conf
841 cluster-config-file nodes-6379.conf #开启群集功能配置文件
#847 # cluster-node-timeout 15000
847 cluster-node-timeout 15000 #设置群集超时时间为15000秒
#700 appendonly no
700 appendonly yes #开启AOF支持
[[email protected] redis]# /etc/init.d/redis_6379 restart #开起redis
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
[[email protected] utils]# cd /var/lib/redis/
[[email protected] redis]# cd 6379/
[[email protected] 6379]# ls
appendonly.aof dump.rdb nodes-6379.conf
#aof配置文件,rdb快照文件,节点配置文件
从服务器一样的操作
主服务器安装rvm,ruby控制群集
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 #导入密钥
[[email protected] 6379]# curl -sSL https://get.rvm.io | bash -s stable #寻找这个地址安装RVM
[[email protected] 6379]# cd /opt/
[[email protected] opt]# ls
redis-5.0.7 rh
[[email protected] opt]# vim abc.sh #rvm的脚本很长
[[email protected] opt]# ls
abc.sh redis-5.0.7 rh
[[email protected] opt]# chmod +x abc.sh
[[email protected] opt]# ls
abc.sh redis-5.0.7 rh
[[email protected] opt]# source /etc/profile.d/rvm.sh
[[email protected] opt]# rvm list known #查看rvm安装的版本
[[email protected] opt]# rvm install 2.4.1 #安装rvm2.4.1版本
[[email protected] opt]# rvm use 2.4.1 #使用rvm
Using /usr/local/rvm/gems/ruby-2.4.1
[[email protected] opt]# ruby -v #查看ruby的版本
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
[[email protected] opt]# gem install redis #安装redis
主服务器安装三块网卡
192.168.136.229
192.168.136.232
192.168.136.231
从服务器安装三块网卡
192.168.136.185
192.168.136.233
192.168.136.234
两台服务器都重启网路服务
[[email protected] opt]# systemctl restart network
两台服务器关闭防火墙和增强功能
[[email protected] opt]# systemctl stop firewalld.service
[[email protected] opt]# setenforce 0
主服务器,重启redis,创建群集
[[email protected] 6379]# /etc/init.d/redis_6379 restart #重启redis
redis-cli --cluster create 192.168.136.229:6379 192.168.136.232:6379 192.168.136.231:6379 192.168.136.185:6379 192.168.136.233:6379 192.168.136.234:6379 --cluster-replicas 1
Can I set the above configuration? (type ‘yes‘ to accept): yes #出现这行输入yes
三个节点master,三个副本节点,彼此对应关系是随机的,但是是一主带一从。
开始验证
[[email protected] opt]# redis-cli -h 192.168.136.229 -p 6379 #登录一台主服务器229
192.168.136.229:6379> set name zhangsan #创建键值对
OK
192.168.136.229:6379> keys * #查看所有键
1) "name"
192.168.136.229:6379> get name #查看值
"zhangsan"
192.168.136.229:6379> quit
[[email protected] opt]# redis-cli -h 192.168.136.234 -p 6379 #登录一台从服务器234
192.168.136.234:6379> keys *
1) "name"
192.168.136.234:6379> get name #查看数据,查看不了,会提示你数据存放在231服务器中
(error) MOVED 5798 192.168.136.231:6379
[[email protected] opt]# redis-cli -h 192.168.136.231 -p 6379 #登录231服务器,就看到数据了
192.168.136.231:6379> keys *
1) "name"
192.168.136.231:6379> get name
"zhangsan"
用hash方式建立键值对,设置键的删除时间
[[email protected] opt]# redis-cli -h 192.168.136.229 -p 6379
192.168.136.229:6379> hset person age 20 ##用hash方式建立键值对
(integer) 1
192.168.136.229:6379> hset person name lisi
(integer) 1
192.168.136.229:6379> keys *
1) "person"
192.168.136.229:6379> hget person age ##获取键的值
"20"
192.168.136.229.128:6379> expire person 5 ##设置键的删除时间5s
(integer) 1
192.168.136.229:6379> keys *
1) "person"
192.168.136.229:6379> keys *
(empty list or set)
以上就是我们全部的内容了,谢谢收看
原文地址:https://blog.51cto.com/14449524/2459240
时间: 2024-10-09 21:33:21