redis单机、主从、哨兵、集群部署

卸载机器上自带的ruby rubygems

yum remove ruby rubygems -y

解决ruby语言的环境依赖库

yum install gcc gcc-c++ gdbm-devel readline-devel openssl-devel wget -y

下载程序源码包,解压并安装

wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
tar xf ruby-2.5.5.tar.gz
cd ruby-2.5.5
./configure --prefix=/usr/local/ruby --enable-shared make && make install

下载redis源码包解压并安装

wget http://download.redis.io/releases/redis-5.0.9.tar.gz
tar -xvf redis-5.0.9.tar.gz
cd redis-5.0.9
make PREFIX=/usr/local/redis install

创建目录

cd /usr/local/redis && mkdir cluster && mkdir replication && mkdir sentinel
cluster/replication/sentinel目录下均执行:
mkdir data && mkdir log && mkdir redis_conf && cp /usr/local/src/redis-5.0.9/redis.conf cd /usr/local/redis/replication/redis_conf

三台主机下的配置文件分别做修改:

mv /usr/local/redis/replication/redis_conf/redis.conf /usr/local/redis/replication/redis_conf/redis_6379.conf
mv /usr/local/redis/replication/redis_conf/redis.conf /usr/local/redis/replication/redis_conf/redis_6380.conf
mv /usr/local/redis/replication/redis_conf/redis.conf /usr/local/redis/replication/redis_conf/redis_6381.conf

redis单机部署

redis主从部署

节点信息

节点名称 节点IP 服务端口
master 192.168.28.135 6379
slave1 192.168.28.136 6380
slave2 192.168.28.137 6381

防火墙添加端口的准入准出

firewall-cmd --add-port=6379/tcp
firewall-cmd --add-port=6380/tcp
firewall-cmd --add-port=6381/tcp

修改master配置

vim /usr/local/redis/replication/redis_conf/redis_6379.conf

port 6379
daemonize yes
pidfile /usr/local/redis/replication/redis_6379.pid
logfile "/usr/local/redis/replication/log/redis_6379.log"
dbfilename 6379_dump.rdb
dir /usr/local/redis/replication/data/6379
requirepass 123456
bind 192.168.28.135

修改slave1配置

vim /usr/local/redis/replication/redis_conf/redis_6380.conf

port 6380
daemonize yes
pidfile /usr/local/redis/replication/redis_6380.pid
logfile "/usr/local/redis/replication/log/redis_6380.log"
dbfilename 6380_dump.rdb
dir /usr/local/redis/replication/data/6380
replicaof 192.168.28.135 6379
replica-read-only yes
masterauth 123456
bind 192.168.28.136

修改slave2配置

vim /usr/local/redis/replication/redis_conf/redis_6381.conf

port 6381
daemonize yes
pidfile /usr/local/redis/replication/redis_6381.pid
logfile "/usr/local/redis/replication/log/redis_6381.log"
dbfilename 6381_dump.rdb
dir /usr/local/redis/replication/data/6381
replicaof 192.168.28.135 6379
replica-read-only yes
masterauth 123456
bind 192.168.28.137

启动master服务&&登陆master服务并查看主从连接信息

/usr/local/redis/bin/redis-server /usr/local/redis/replication/redis_conf/redis_6379.conf
/usr/local/redis/bin/redis-cli -h 192.168.28.135 -p 6379 -a 123456
info replication

启动slave1服务&&登陆slave1服务并查看主从连接信息

/usr/local/redis/bin/redis-server /usr/local/redis/replication/redis_conf/redis_6380.conf

/usr/local/redis/bin/redis-cli -h 192.168.28.136 -p 6380 -a 123456
info replication

启动slave2服务&&登陆slave2服务并查看主从连接信息

/usr/local/redis/bin/redis-server /usr/local/redis/replication/redis_conf/redis_6381.conf
/usr/local/redis/bin/redis-cli -h 192.168.28.137 -p 6381 -a 123456
info replication

redis哨兵部署

先完成主从的配置,见上述步骤

节点信息

节点名称 节点IP 服务端口
sentinel1 192.168.28.135 26379
sentinel2 192.168.28.136 26379
sentinel3 192.168.28.137 26379

防火墙添加端口的准入准出

firewall-cmd --add-port=26379/tcp

修改哨兵配置,三者保持一致

port 26379
daemonize yes
pidfile "/usr/local/redis/sentinel/redis_26379.pid"
logfile "/usr/local/redis/sentinel/log/sentinel_26379.log"
dir "/usr/local/redis/sentinel/data/26379"
sentinel monitor mymaster 192.168.28.135 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster 123456

启动三个哨兵

/usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel/sentinel_conf/sentinel_26379.conf

查看135是否为master

/usr/local/redis/bin/redis-cli -h 192.168.28.135 -p 6379 -a 123456

info replication

关闭135的redis服务

ps -ef|grep redis
kill -9 ${pid}

重启135服务

/usr/local/redis/bin/redis-server /usr/local/redis/replication/redis_conf/redis_6379.conf

登录136和137查看当前的master是谁

/usr/local/redis/bin/redis-cli -h 192.168.28.136 -p 6380 -a 123456
info replication
/usr/local/redis/bin/redis-cli -h 192.168.28.135 -p 6381 -a 123456
info replication
当前的master已经变为137,135已经变为了master
[root@localhost sentinel]# /usr/local/redis/bin/redis-cli -h 192.168.28.137 -p 6381 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.28.137:6381> info replication
role:master
connected_slaves:1
slave0:ip=192.168.28.135,port=6379,state=online,offset=248671,lag=1
master_replid:a63539a8e42c4489ee765750057614eaf705d4f4
master_replid2:3b3cfaf2132d154891a48fe16089aa5f08acb170
master_repl_offset:248814
second_repl_offset:46650
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:248814
192.168.28.137:6381>

redis集群部署

见文章[https://www.jianshu.com/p/28d39f25adea]

你可能感兴趣的:(redis单机、主从、哨兵、集群部署)