目录
主从复制:
# 安装redis
yum install redis
# 创建redis配置文件目录
mkdir /usr/local/redis/ms -p
# 复制默认配置文件到新建的配置文件目录
cp /etc/redis.conf /usr/local/redis/ms/redis6380.conf
cp /etc/redis.conf /usr/local/redis/ms/redis6381.conf
cp /etc/redis.conf /usr/local/redis/ms/redis6382.conf
修改redis.conf的配置文件:
# 配置主服务器
vim /usr/local/redis/ms/redis6380.conf
#监听ip
bind 0.0.0.0
#端口号
port 6380
#后台运行
daemonize yes
#进程号
pidfile /var/run/redis_6380.pid
#日志路径
logfile "/var/log/redis/redis6380.log"
#设置持久化文件名
dbfilename dump6380.rdb
# 配置从服务器
vim /usr/local/redis/ms/redis6381.conf
#监听ip
bind 0.0.0.0
#端口号
port 6381
#后台运行
daemonize yes
#进程号
pidfile /var/run/redis_6381.pid
#日志路径
logfile "/var/log/redis/redis6381.log"
#设置持久化文件名
dbfilename dump6381.rdb
# 配置从服务器
vim /usr/local/redis/ms/redis6382.conf
#监听ip
bind 0.0.0.0
#端口号
port 6382
#后台运行
daemonize yes
#进程号
pidfile /var/run/redis_6382.pid
#日志路径
logfile "/var/log/redis/redis6382.log"
#设置持久化文件名
dbfilename dump6382.rdb
修改文件的属主:
chown -R redis:root /var/log/redis
运行服务:
redis-server /usr/local/redis/ms/redis6380.conf
redis-server /usr/local/redis/ms/redis6381.conf
redis-server /usr/local/redis/ms/redis6382.conf
从服务器(slave):
slave01
# 进去redis 端口为6381的服务器 ,设置6380作为主服务器
redis-cli -h 172.25.250.210 -p 6381
slaveof 172.25.250.210 6380
info replication
exit
slave02
# 进去redis 端口为6382的服务器 ,设置6380作为主服务器
redis-cli -h 172.25.250.210 -p 6382
slaveof 172.25.250.210 6380
info replication
exit
哨兵模式:
复制哨兵模式的默认配置文件到新建的配置文件:
#详情查看https://github.com/redis/redis/blob/unstable/sentinel.conf
cp /etc/redis-sentinel.conf /usr/local/redis/ms/sentinel.conf
修改哨兵模式的配置文件:
vim /usr/local/redis/ms/sentinel.conf
#监视主节点
sentinel monitor MyMaster 172.25.250.210 6380 1
#后台运行
daemonize yes
#端口号
port 26379
#日志目录
logfile "/var/log/redis/sentinel.log"
启动服务:
redis-sentinel /usr/local/redis/ms/sentinel.conf
ps -ef | grep redis
集群模式:
# 新建集群的配置文件路径
mkdir -p /usr/local/redis/cluster/{7001..7006}
# 复制配置文件到其中一个集群路径
cp /etc/redis.conf /usr/local/redis/cluster/7001/.
# 修改集群的配置文件
vim /usr/local/redis/cluster/7001/redis.conf
#监听ip
bind 0.0.0.0
#端口号
port 7001
#进程号
pidfile /var/run/redis_7001.pid
#日志路径
logfile "/var/log/redis/redis7001.log"
#进程目录
dir /usr/local/redis/cluster/7001
#后台运行
appendonly yes
#开启集群模式
cluster-enabled yes
#集群配置文件
cluster-config-file nodes-7001.conf
复制全部集群的配置文件:
# 开启cp -f 不用按y
alias cp="cp"
# 从第一个集群服务器复制到全部集群服务器的路径
cp -f /usr/local/redis/cluster/7001/redis.conf /usr/local/redis/cluster/7002/redis.conf
cp -f /usr/local/redis/cluster/7001/redis.conf /usr/local/redis/cluster/7003/redis.conf
cp -f /usr/local/redis/cluster/7001/redis.conf /usr/local/redis/cluster/7004/redis.conf
cp -f /usr/local/redis/cluster/7001/redis.conf /usr/local/redis/cluster/7005/redis.conf
cp -f /usr/local/redis/cluster/7001/redis.conf /usr/local/redis/cluster/7006/redis.conf
# 关闭cp -f 不用按y
alias cp="cp -i"
修改全部集群的配置文件:
sed -i "s/7001/7002/g" /usr/local/redis/cluster/7002/redis.conf
sed -i "s/7001/7003/g" /usr/local/redis/cluster/7003/redis.conf
sed -i "s/7001/7004/g" /usr/local/redis/cluster/7004/redis.conf
sed -i "s/7001/7005/g" /usr/local/redis/cluster/7005/redis.conf
sed -i "s/7001/7006/g" /usr/local/redis/cluster/7006/redis.conf
运行服务:
# 后台运行服务
redis-server /usr/local/redis/cluster/7001/redis.conf &
redis-server /usr/local/redis/cluster/7002/redis.conf &
redis-server /usr/local/redis/cluster/7003/redis.conf &
redis-server /usr/local/redis/cluster/7004/redis.conf &
redis-server /usr/local/redis/cluster/7005/redis.conf &
redis-server /usr/local/redis/cluster/7006/redis.conf &
开启集群模式
redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1
如果redis版本低于5.0,走下面流程:
# 通过yum下载ruby25 ruby版本高于2.4
yum install centos-release-scl-rh
yum install rh-ruby25 -y
# scl enable 直接运行应用程序
scl enable rh-ruby25 bash
# 修改linux环境
vim /etc/profile.d/rh-ruby25.sh
#!/bin/bash
source /opt/rh/rh-ruby25/enable
export X_SCLS="`scl enable rh-ruby25 'echo $X_SCLS'`"
export PATH=$PATH:/opt/rh/rh-ruby25/root/usr/local/bin
# scl enable 直接运行应用程序
scl enable rh-ruby25 bash
# 安装redis-3.2.1.gem
wget https://rubygems.org/downloads/redis-3.2.1.gem
gem install redis-3.2.1.gem
# 通过redis的软件包拿到redis-trib.rb这个命令
wget https://download.redis.io/releases/redis-3.2.12.tar.gz
tar -zxf redis-3.2.12.tar.gz
# 使redis-trib.rb命令生效
cp redis-3.2.12/src/redis-trib.rb /usr/bin/redis-trib.rb
#–replicas参数指定集群中每个主节点配备几个从节点,这里设置为1,即三主三从
redis-trib.rb create --replicas 1 172.25.250.210:7001 172.25.250.210:7002 172.25.250.210:7003 172.25.250.210:7004 172.25.250.210:7005 172.25.250.210:7006
#检查集群状态
redis-trib.rb check 172.25.250.210:7001
#查看集群信息
redis-trib.rb info 172.25.250.210:7001
远程连接redis可视化:
Releases · qishibo/AnotherRedisDesktopManager (github.com)
扩展:
#加入的是主节点:
redis-trib.rb add-node 127.0.0.1:7010 127.0.0.1:7011
#加入的是从节点(可不设置--master-id,此时会随机选择主节点):
redis-trib.rb add-node --slave --master-id f4xxx 127.0.0.1:7020 127.0.0.1:7021
#删除节点
redis-trib.rb del-node host:port node_id
#将外部redis数据导入集群
redis-trib.rb import host:port --from --copy --replace host:port
删除集群:
# 用之前集群的端口创建新的集群报错
[ERR] Node 172.25.250.210:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
# 进入redis服务器
# cluster_known_nodes 显示的是集群的连接数
redis-cli -p 7001
cluster info #查看信息
cluster nodes #查看node
# 得到node,我们就可以forget
cluster forget "{{node_id}}"
# 如果forget完直接退出redis,选择其他端口进去redis,继续去forget
redis-cli -p 7004
cluster nodes
cluster forget "{{node_id}}"
直到这个集群的节点全部关闭
redis6.2+TLS(高可用):
安装redis服务
# 1.安装 redis 编译的 c++ 环境:
yum install gcc-c++ -y
yum install openssl-devel -y
# 2.下载redis6.2.6
yum install wget -y
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
# 3.解压并make编译
tar -xvf redis-6.2.6.tar.gz
cd redis-6.2.6
make BUILD_TLS=yes
# 如果执行编译出错,提示 zmalloc.h:50:10: fatal error: jemalloc/jemalloc.h: No such file or directory 执行 MALLOC=libc
# make BUILD_TLS=yes MALLOC=libc
make install
# 4.拷贝命令到环境变量中
cd src
cp redis-sentinel redis-cli redis-server /usr/bin/
# 5.overcommit_memory设置为1
echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
sysctl -p
创建TLS证书
# 1.创建目录
mkdir -p /etc/redis/{conf,ssl,logs,pid,data}
for i in $(seq 0 9); do mkdir -p /etc/redis/data/638$i; done
# 2.创建证书
/root/redis-6.2.6/utils/gen-test-certs.sh
# 3.拷贝证书都指定位置
cp tests/tls/redis.* /etc/redis/ssl/
cp tests/tls/ca.crt /etc/redis/ssl/
编写配置文件
vim /etc/redis/conf/redis_6380.conf
################################# cluser配置 #################################
masterauth 123456
cluster-enabled yes
cluster-config-file nodes_6380.conf
cluster-node-timeout 15000
################################## 安全认证 ###################################
requirepass 123456
rename-command CONFIG b840fc02d524045429941cc43f59e41cb7be6c52
################################## TLS 配置 ###################################
tls-cert-file /etc/redis/ssl/redis.crt
tls-key-file /etc/redis/ssl/redis.key
tls-ca-cert-file /etc/redis/ssl/ca.crt
tls-dh-params-file /etc/redis/ssl/redis.dh
tls-auth-clients yes
tls-replication yes
#指定tls-replication yes才能将TLS用于到主服务器的对外连接,sentinel也需要同步设置。
tls-cluster yes
################################## 连接配置 ##################################
maxclients 10000
############################# 懒惰的释放 ####################################
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
################################ LUA时间限制 ###############################
lua-time-limit 5000
############################### 慢日志 ################################
slowlog-log-slower-than 10000
slowlog-max-len 128
######################### 高级设置 #########################
activerehashing yes
#缓存空间限制
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 1024mb 256mb 300
client-output-buffer-limit pubsub 32mb 8mb 60
client-query-buffer-limit 1gb
#加快写入rdb 和aof
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
######################### 多线程设置 #########################
## 设置线程数,不超过CPU可用总核数
#io-threads 4
## 设置yes 开启多线程
#io-threads-do-reads yes
复制多线程配置文件
# 复制配置文件
for i in $(seq 1 9); do cp /etc/redis/conf/redis_6380.conf /etc/redis/conf/redis_638$i.conf; done
# 修改配置文件
for i in $(seq 1 9); do sed -i "s/6380/638$i/g" /etc/redis/conf/redis_638$i.conf;done
# 启动服务
redis-server /etc/redis/conf/redis_6380.conf
for i in $(seq 0 9); do redis-server /etc/redis/conf/redis_638$i.conf;done
ps -ef | grep redis
# 关闭服务
redis-cli -p 6380 -a 123456 shutdown
for i in $(seq 0 9); do redis-cli -p 638$i -a 123456 shutdown ;done
创建cluster 集群
因为我们配置文件里面有设置 requirepass
密码验证,所有在创建时需要加上 -a 输入密码
redis-cli --tls --cert /etc/redis/ssl/redis.crt \
--key /etc/redis/ssl/redis.key \
--cacert /etc/redis/ssl/ca.crt -a 123456 \
--cluster create --cluster-replicas 1 \
172.25.250.231:6380 172.25.250.231:6381 172.25.250.231:6382 \
172.25.250.231:6383 172.25.250.231:6384 172.25.250.231:6385 \
172.25.250.231:6386 172.25.250.231:6387 172.25.250.231:6388 \
172.25.250.231:6388 172.25.250.231:6389 \
# 登录
redis-cli --tls --cert /etc/redis/ssl/redis.crt --key /etc/redis/ssl/redis.key --cacert /etc/redis/ssl/ca.crt -c -p 6380
# 密码
auth 123456
# 查看集群信息
cluster info
# 列出集群所有节点
cluster nodes
远程连接redis可视化:
简化redis-cli登录的命令:
# 我们整个部署流程下来发现 使用TLS后,redis-cli 操作变的很复杂,容易忘记怎么使用参数的话,可以在 ~/.bashrc 自做个别名
vim ~/.bashrc
alias redis-cli='redis-cli --tls --cert /etc/redis/ssl/redis.crt --key /etc/redis/ssl/redis.key --cacert /etc/redis/ssl/ca.crt'
# 刷新
source ~/.bashrc
验证:
另一个终端读取: