必会Redis单节点、Sentinel和Cluster操作实战
扫描二维码
随时随地手机看文章
建议当作操作手册收藏。
安装单实例 Redis
编译Redis
1.下载Redis源码包wget https://download.redis.io/releases/redis-5.0.13.tar.gz
2.解压文件tar -zxvf redis-5.0.13.tar.gz
3.cd redis-5.0.13
4.make
编译1.如果遇到以下报错,需要安装依赖此时编译完成,文件目录下已经生成可执行文件。yum install gcc
2.如果遇到以下报错
分配器allocator, 如果有MALLOC 这个 环境变量, 会有用这个环境变量的 去建立Redis。
而且libc 并不是默认的 分配器, 默认的是 jemalloc, 因为 jemalloc 被证明 有更少的 fragmentation problems 比libc。但是如果你又没有jemalloc 而只有 libc 当然 make 出错。所以加这么一个参数,运行如下命令:make MALLOC=libc
安装Redis服务
1.安装编译后的文件make install PREFIX=/usr/local/redis5
2.编辑环境变量vi /etc/profile
3.追加如下信息export REDIS_HOME=/usr/local/redis5
export PATH=$PATH:$REDIS_HOME/bin
4.更新配置source /etc/profile
echo $PATH检查环境变量是否正确5.
cd utils/
6.安装服务./install_server.sh
然后出现如下输出[root@node1 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/redis5/bin/redis-server] 设置可执行文件目录
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/redis5/bin/redis-server
Cli Executable : /usr/local/redis5/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379 复制配置文件
Installing service... 安装服务
Successfully added to chkconfig! 设置开机启动
Successfully added to runlevels 345!
Starting Redis server... 启动服务
Installation successful!
7.
service redis_6379 status
检查是否安装成功ps -ef|grep redis
检查正在运行的服务配置修改
1.修改 配置信息vi /etc/redis/6379.conf
(这一步可选)daemonize no # 前台方式运行 为了方便观察
#logfile /var/log/redis_6379.log #注释掉,前台输出 为了方便观察
appendonly yes # aof同步开启
2.启动 redis-server /etc/redis/6379.conf
3.启动客户端 redis-cli -p 6379
主备安装
1.使用install_server.sh再安装两个Redis实例,使用端口号在同一机器区分 这里分别使用6379,6380,6381,并且分别启动2.登陆客户端 redis-cli -p 63813.输入命令 help SLAVEOF,6380端口实例操作同理。redis-cli -p 6381 127.0.0.1:6381>help SLAVEOF
SLAVEOF host port
summary: Make the server a replica of another instance, or promote it as master. Deprecated starting with Redis 5. Use REPLICAOF instead.
since: 1.0.0
group: server
127.0.0.1:6381> REPLICAOF 127.0.0.1 6379
OK
127.0.0.1:6381>
4.设置追随6379为主 REPLICAOF 127.0.0.1 6379
6381变化:6379变化:25692:S 03 Sep 2021 20:19:53.182 * REPLICAOF 127.0.0.1:6379 enabled (user request from 'id=3 addr=127.0.0.1:42336 fd=7 name= age=113 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=44 qbuf-free=32724 obl=0 oll=0 omem=0 events=r cmd=replicaof')
25692:S 03 Sep 2021 20:19:54.146 * Connecting to MASTER 127.0.0.1:6379
25692:S 03 Sep 2021 20:19:54.146 * MASTER <-> REPLICA sync started
25692:S 03 Sep 2021 20:19:54.146 * Non blocking connect for SYNC fired the event.
25692:S 03 Sep 2021 20:19:54.147 * Master replied to PING, replication can continue...
25692:S 03 Sep 2021 20:19:54.147 * Trying a partial resynchronization (request 839fc4e0817f7c98e9fa8204c53aedf661c71bf1:1).
25692:S 03 Sep 2021 20:19:54.148 * Full resync from master: 4cff4bffe3e33adcca1864de53064480eb00c200:0
25692:S 03 Sep 2021 20:19:54.148 * Discarding previously cached master state.
25692:S 03 Sep 2021 20:19:54.251 * MASTER <-> REPLICA sync: receiving 176 bytes from master
25692:S 03 Sep 2021 20:19:54.254 * MASTER <-> REPLICA sync: Flushing old data # 会先删除本节点老数据
25692:S 03 Sep 2021 20:19:54.254 * MASTER <-> REPLICA sync: Loading DB in memory
25692:S 03 Sep 2021 20:19:54.254 * MASTER <-> REPLICA sync: Finished with success
效果如下图:如果一个节点首次追随主节点,会执行RDB同步 如果从节点挂掉,启动直接追随先前的主节点,只做增量同步 但是如果节点挂掉,启动直接追随先前的主节点,同步方式为AOF,则会做RDB全量同步。。可能是因为AOF没有记录之前追随过谁取消追随主节点:
REPLICAOF no one
哨兵模式 Sentinel
配置 sentinel
1.从redis源码目录复制官网提供的配置文件cp /root/soft/redis-5.0.13/sentinel.conf /etc/redis/sentinel_26379.conf
2.修改配置文件port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
3.启动sentinel实例redis-server /etc/redis/sentinel_26379.conf --sentinel
以此为例启动三台 4.设置 sentinel 成功验证
1.此时杀死master 6379实例2.选举6380为新的主节点3.此时重启6379节点redis-server /etc/redis/6379.conf
其中一个sentinel输出如下11035:X 04 Sep 2021 01:41:29.685 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
11035:X 04 Sep 2021 01:41:39.675 * convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
原有的主节点已经切换为备用节点
Redis Cluster
配置集群
1.查看Redis cluster相关命令 redis-cli --cluster help[root@node1 ~]# redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from
--cluster-to
--cluster-slots
--cluster-yes
--cluster-timeout
--cluster-pipeline
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout
--cluster-simulate
--cluster-pipeline
--cluster-threshold
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from
--cluster-copy
--cluster-replace
2.分别启动每个Redis实例Redis 本身在Utils目录下提供了单机部署 cluster demo 的util脚本,可以快速一键部署。cd /root/soft/redis-5.0.13/utils/create-clustervi create-cluster3.redis-cli --cluster create 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 127.0.0.1:30006 --cluster-replicas 1# Settings
PORT=30000
TIMEOUT=2000
NODES=6 # 节点数
REPLICAS=1 # 备用节点数 此配置为3主3从
./create-cluster start 启动实例./create-cluster create
4.创建集群完成,客户端链接校验redis-cli -c -p 30001 如果发生了自动重定向,则表示RedisCluster使用正常
检查Redis cluster状态
1.redis-cli --cluster check 127.0.0.1:30001reshrad Redis槽位
1.redis-cli --cluster reshard 127.0.0.1:300012.然后按照提示操作,首先选择要移动的槽位数量,然后选择接受节点3.再选择需要从哪些节点移出槽位4.最后输入done,然后yes确定5.检查移动后的结果 redis-cli --cluster info 127.0.0.1:30001