Redis主从、哨兵、Redis集群部署

2021/03/12

Redis主从实现热备和读写分离、哨兵实现主从切换高可用、Redis集群实现高可用和分布式负载、横向扩容

参考资料

一、redis主从部署

redis安装-所有节点执行

准备相关依赖
$ yum install -y gcc gcc-c++ unzip telnet

1、下载解压
cd /opt
wget https://download.redis.io/releases/redis-5.0.3.tar.gz
tar -zxf redis-5.0.3.tar.gz
cd redis-5.0.3
# 编译
make
# 安装
cd src/
make PREFIX=/data/redis install
# 调整配置文件目录
mkdir  /data/redis/conf
cp /opt/redis-5.0.3/redis.conf /data/redis/conf/
cp /opt/redis-5.0.3/sentinel.conf /data/redis/conf/

2、修改配置文件/data/redis/conf/redis.conf
bind 0.0.0.0 
daemonize yes
protected-mode no
logfile "/data/redis/redis.log"
dir "/data/redis"
masterauth "redispwd"
requirepass "redispwd"

## 以下是从节点增加配置项:
# Slave只能从Master上读数据
replica-read-only yes   # 老版本对应配置项slave-read-only yes   
# 设置master的ip地址和端口
replicaof master_ip 6379  # 老版本对应配置项slaveof master_ip 6379  

3、启动redis
/data/redis/bin/redis-server /data/redis/conf/redis.conf

4、查看redis状态
/data/redis/bin/redis-cli -p 6379
127.0.0.1:6379>auth redispwd
127.0.0.1:6379>info replication

5.如果防火墙必须开启,需要放通6379端口
firewall-cmd --state         ## 查看防火墙状态
firewall-cmd --list-ports    ## 查看已经开放的端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent     ## 开启6379端口
firewall-cmd --reload  ## 重启防火墙 (重新开启端口后一定要重启防火墙,再查看开放的端口) 

二、Redis哨兵部署

redis安装-主从节点都执行

准备相关依赖
$ yum install -y gcc gcc-c++

1、下载解压
cd /opt/
wget https://download.redis.io/releases/redis-5.0.3.tar.gz
tar -zxf redis-5.0.3.tar.gz
cd redis-5.0.3
# 编译
make
# 安装
cd src/
make PREFIX=/data/redis install
# 调整配置文件目录
mkdir  /data/redis/conf
cp /opt/redis-5.0.3/redis.conf /data/redis/conf/
cp /opt/redis-5.0.3/sentinel.conf /data/redis/conf/

2、修改配置文件/data/redis/conf/redis.conf
bind 0.0.0.0 
daemonize yes
protected-mode no
logfile "/data/redis/redis.log"
dir "/data/redis"
masterauth "pwd"
requirepass "pwd" 
replicaof $master_ip 6379  

3、启动redis
/data/redis/bin/redis-server /data/redis/conf/redis.conf

4、查看redis状态
redis-cli -p 6379
127.0.0.1:6379>auth xxxxx
127.0.0.1:6379>info replication

sentinel服务部署-所有节点执行

1、修改配置文件/data/redis/conf/sentinel.conf
protected-mode no
port 26379
bind 0.0.0.0
daemonize yes
logfile "/data/redis/sentinel.log"
dir "/data/redis"
maxclients 4064
protected-mode no
supervised systemd
sentinel myid 1ddc2e3c478738f6da424b8c6f341cf24a665630
sentinel deny-scripts-reconfig yes
sentinel monitor $master_name master_ip 6379 2
sentinel down-after-milliseconds $master_name 3000
sentinel auth-pass $master_name pwd
sentinel config-epoch $master_name 0
sentinel leader-epoch $master_name 0
sentinel known-replica $master_name slave_ip 6379  # 副本配置slave_ip
sentinel known-sentinel $master_name slave_ip 26379 65f1c110aa0bf80f74529cab048867c134776f02  # master配slave,slave配master
sentinel current-epoch 0

2、启动sentinel
/data/redis/bin/redis-sentinel /data/redis/conf/sentinel.conf

3、查看sentinel状态
redis-cli -p 26379
127.0.0.1:6379>info sentinel

三、Redis-cluster 5.x集群-环境部署

### 配置内核参数
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
echo "net.core.somaxconn=1024" >> /etc/sysctl.conf
sysctl -p

### 编译安装
redis集群搭建
home_dir=/data/redis

### 所有节点操作
1、安装相关依赖
$ yum install -y gcc gcc-c++

2、下载解压
cd /data/packages
wget https://download.redis.io/releases/redis-5.0.3.tar.gz
tar -zxf redis-5.0.3.tar.gz
cd redis-5.0.3
# 编译
make
# 安装
cd src/
make PREFIX=/data/redis install

### 分别在各节点机器操作
1、创建redis 集群节点
mkdir -p /data/logs/redis
cd /data/redis
mkdir -p cluster/{7000,7001,7002}---节点1
mkdir -p cluster/{7003,7004,7005}---节点2
# 调整配置文件目录 
cp /opt/redis-5.0.3/redis.conf /data/redis/cluster/700x/ 

2、修改配置/data/redis/cluster/700x/redis.conf
port 7000                                          //端口7000,7002,7003       
bind 0.0.0.0                                        //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群
daemonize yes                                       //redis后台运行
pidfile /var/run/redis_7000.pid                     //pidfile文件对应7000,7001,7002
dbfilename 7000.rdb                                 //rdb文件对应7000,7001,7002
dir /data/redis/cluster/7000/                       //修改集群节点对应文件夹7000,7001,7002
cluster-enabled yes                                 //开启集群  把注释#去掉
cluster-config-file nodes_7000.conf                 //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 15000                          //请求超时  默认15秒,可自行设置
appendonly yes                                      //aof日志开启  有需要就开启,它会每次写操作都记录一条日
logfile "/data/logs/redis/redis_7000.log"               //日志存放路径

3、启动所有节点
/data/redis/bin/redis-server /data/redis/cluster/7000/redis.conf
/data/redis/bin/redis-server /data/redis/cluster/7001/redis.conf
/data/redis/bin/redis-server /data/redis/cluster/7002/redis.conf
 
/data/redis/bin/redis-server /data/redis/cluster/7003/redis.conf
/data/redis/bin/redis-server /data/redis/cluster/7004/redis.conf
/data/redis/bin/redis-server /data/redis/cluster/7005/redis.conf

### 创建集群
/data/redis/bin/redis-cli --cluster create IP1:7000 IP1:7001 IP1:7002 IP2:7003 IP2:7004 IP2:7005 --cluster-replicas 1

出现Can I set the above configuration?(type ‘yes’ to accept):输入yes,等待集群创建完成

问题记录

Post Directory

扫码关注公众号:暂无公众号
发送 290992
即可立即永久解锁本站全部文章