redis集群判断某一节点是否宕机采用投票机制,这里我们就需要创建奇数个节点。先创建三台虚拟机(129,130,131),6个redis(4.x),三个节点即三主三从。
创建redis目录:
mkdir -p /home/soft/redis
下载redis:
cd /home/soft/redis
wget http://download.redis.io/releases/redis-4.0.14.tar.gz
解压安装:
tar -zxvf redis-4.0.14.tar.gz
cd redis-4.0.14
make && make install
如果报错,安装相应的依赖。删除报错的安装残存重新安装:
make distclean
make && make install
操作集群命令(操作其中一台机器即可):
cd src
cp redis-trib.rb /usr/local/bin/
操作配置文件:
mkdir /home/soft/redis/conf
cp redis.conf /home/soft/redis/conf/redis7001.conf
修改配置文件:
vim /home/soft/redis/conf/redis7001.conf
port 7001 #端口7001,7002,7003
bind 192.168.248.129 #本机IP
daemonize yes #后台运行
masterauth key #主redis密码(主redis可以主调)
requirepass key #本redis密码
pidfile /var/run/redis_7001.pid #pidfile文件对应7001,7002,7003
cluster-enabled yes #开启集群 把注释#去掉
cluster-config-file nodes_7001.conf #集群的配置
cluster-node-timeout 15000 #请求超时
复制一份,并修改:
cp redis7001.conf redis7011.conf
vim redis7011.conf
:%s/7001/7011/g
:wq
启动redis:
redis-server /home/soft/redis/conf/redis7001.conf
redis-server /home/soft/redis/conf/redis7011.conf
查看状态:
ps -ef | grep redis
正常:
其他两台机器同样操作。
redis-trib.rb为ruby文件需要ruby环境,安装ruby:
mkdir -p /home/soft/ruby
cd /home/soft/ruby
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
解压安装:
./configure –-prefix=/usr/local/ruby
make && make install
配置ruby的环境变量:
vim /etc/profile
export PATH=/usr/local/ruby/bin:$PATH #最后一行添加保存
source /etc/profile
安装redis依赖:
gem install redis
如果报错:(参照:https://blog.csdn.net/feinifi/article/details/78251486)
//集成zlib库到ruby环境
cd /home/soft/ruby/ruby-2.5.5/ext/zlib
ruby extconf.rb
vim Makefile
:%s/$(top_srcdir)/..\/../g
make && make install
//集成openssl库到ruby环境
cd /home/soft/ruby/ruby-2.5.5/ext/openssl
ruby extconf.rb
vim Makefile
:%s/$(top_srcdir)/..\/../g
make && make install
因为加密码了:
vim /usr/local/ruby/lib/ruby/gems/2.5.0/gems/redis-4.1.2/lib/redis/client.rb
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "key",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}
开放端口:
firewall-cmd --permanent --zone=public --add-port=7001/tcp
firewall-cmd --permanent --zone=public --add-port=7011/tcp
开放总线端口:
firewall-cmd --permanent --zone=public --add-port=17001/tcp
firewall-cmd --permanent --zone=public --add-port=17011/tcp
systemctl restart firewalld.service
创建集群:
redis-trib.rb create --replicas 1 192.168.248.129:7001 192.168.248.130:7002 192.168.248.131:7003 192.168.248.129:7011 192.168.248.130:7012 192.168.248.131:7013
查看集群状态:
redis-trib.rb info 192.168.248.129:7001
完成。