手动创建redis 主从+sentinel
文章目录
1 主从
1.1 可选项(可使用服务器初始化脚本替代)
# 依赖
yum install gcc gcc-c++ automake autoconf lib tool make tcl zlib-devel -y
# 创建redis用户和redis组
# groupadd --gid 500 redis
# useradd redis --uid 500 --gid 500
# 网络相关内核参数
vi /etc/sysctl.conf
添加以下参数
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_max_tw_buckets = 262144
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_time = 120
# sysctl -p
# 关闭透明大页
1.检查THP(Transparent Huge Pages)是否开启(以下情况为开启)
cat /sys/kernel/mm/transparent_hugepage/defrag
cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
2.禁用THP(立即生效)
echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
3.重启生效
vi /etc/rc.d/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
fi
# chmod +x /etc/rc.d/rc.local
或者在文件中追加:
echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
# 内存及资源限制相关参数
vi /etc/sysctl.conf
添加以下内容
vm.overcommit_memory = 1 # Redis设置该值为1的目的为防止Redis在后台save时失败
vm.swappness = 1
fs.file-max = 1024000
# sysctl -p
# 修改ulimit
$ su redis
$ cat /proc/sys/fs/file-max
96701
# vi /etc/security/limits.conf //添加以下内容
redis soft nofile 288000
redis hard nofile 288000
# vi /etc/security/limits.d/90-nproc.conf //添加以下内容
redis soft nofile 288000
redis hard nofile 288000
1.2 安装
查看配置文件的命令:
cat redis.conf |grep -v “^#”|grep -v “^$”|more
# 挂载磁盘
mkdir /data
mkfs.xfs /dev/sdc
vim /etc/fstab
/dev/sdc /data xfs defaults 0 0
mount -a
/dev/sdc 500G 33M 500G 1% /data
# 创建目录
mkdir /data/redis/{bin,data,log} -p
cd /tmp
tar -xvf redis-5.0.10.centos7.tar.gz
# make (以上版本是定制的不需要make,一般环境是需要的)
# make PREFIX=/data/redis install
mv /tmp/redis-* /data/redis/bin/
# 手动建立配置文件 vim /data/redis/redis_6379.conf
(内容见文末,注意修改设置的内存大小)
# 配置启动脚本
vim /etc/systemd/system/redis_6379.service
[Unit]
Description=Redis Database Service
Wants=network.target
After=network.target
[Service]
Type=forking
PIDFile=/data/redis/data/redis.pid
ExecStart=/data/redis/bin/redis-server /data/redis/redis_6379.conf
User=root
[Install]
WantedBy=multi-user.target
# 环境变量
export PATH=$PATH:/data/redis/bin
source /etc/profile
# 启动
systemctl start redis_6379
# 登录从库,建立主从
./redis-cli -h 10.186.62.11 -p 6380 -a
SLAVEOF 10.186.62.11 6379
2 搭建sentinel
简述:
tar xfz ~/redis-5.0.10.centos7.tar.gz
vim /etc/systemd/system/redis.service
systemctl start redis
redis-sentinel /data/redis/redis_26379.conf
# 搭建sentinel
# 准备目录
mkdir /data/redis/redis-sentinel/{redis26379,redis26380,redis26381}
mkdir /data/redis/redis-sentinel/redis26379/{bin,data,log} -p
mkdir /data/redis/redis-sentinel/redis26380/{bin,data,log} -p
mkdir /data/redis/redis-sentinel/redis26381/{bin,data,log} -p
# 准备命令
cp /tools/redis/redis-6.2.5/src/redis-sentinel /data/redis/redis-sentinel/redis26379/bin/
cp /tools/redis/redis-6.2.5/src/redis-cli /data/redis/redis-sentinel/redis26379/bin/
cp /tools/redis/redis-6.2.5/src/redis-sentinel /data/redis/redis-sentinel/redis26380/bin/
cp /tools/redis/redis-6.2.5/src/redis-cli /data/redis/redis-sentinel/redis26380/bin/
cp /tools/redis/redis-6.2.5/src/redis-sentinel /data/redis/redis-sentinel/redis26381/bin/
cp /tools/redis/redis-6.2.5/src/redis-cli /data/redis/redis-sentinel/redis26381/bin/
# 修改配置文件(其他节点的配置文件对应修改,redis_26379{80,81}.conf)
daemonize yes
protected-mode no
logfile "/data/redis/redis-sentinel/redis26379/log/redis.log"
pidfile "/data/redis/redis-sentinel/redis26379/data/redis-sentinel.pid"
port 26379
# 启动
./bin/redis-sentinel redis_26379.conf
./bin/redis-sentinel redis_26380.conf
./bin/redis-sentinel redis_26381.conf
# 进程信息
root 23727 1 0 11:29 ? 00:00:01 ./bin/redis-sentinel *:26379 [sentinel]
root 24475 1 0 11:38 ? 00:00:00 ./bin/redis-sentinel *:26380 [sentinel]
root 24702 1 0 11:40 ? 00:00:00 ./bin/redis-sentinel *:26381 [sentinel]
----华丽的分割线----以上是哨兵搭建---下面是主从搭建---最后是让哨兵监控主机(参考步骤3,加入sentinel)---
# 搭建1主2从
mkdir /data/redis/redis-sentinel/redis700{1,2,3}
mkdir /data/redis/redis-sentinel/redis700{1,2,3}/{bin,data,log} -p
cp /tools/redis/redis-6.2.5/src/redis-server /data/redis/redis-sentinel/redis7001/bin/
cp /tools/redis/redis-6.2.5/src/redis-cli /data/redis/redis-sentinel/redis7001/bin/
ls /data/redis/redis-sentinel/redis7001/bin/
cp /tools/redis/redis-6.2.5/src/redis-server /data/redis/redis-sentinel/redis7002/bin/
cp /tools/redis/redis-6.2.5/src/redis-cli /data/redis/redis-sentinel/redis7002/bin/
ls /data/redis/redis-sentinel/redis7002/bin/
cp /tools/redis/redis-6.2.5/src/redis-server /data/redis/redis-sentinel/redis7003/bin/
cp /tools/redis/redis-6.2.5/src/redis-cli /data/redis/redis-sentinel/redis7003/bin/
ls /data/redis/redis-sentinel/redis7003/bin/
# 使用安装包中的配置文件,或是复制一份线上的配置文件
cp redis.conf /data/redis/redis-sentinel/redis7001/
cp redis.conf /data/redis/redis-sentinel/redis7002/
cp redis.conf /data/redis/redis-sentinel/redis7003/
# 修改配置文件名称和内容(修改对应目录下的配置文件/data/redis/redis-sentinel/redis700{1,2,3})
protected-mode no # 关闭保护模式,运行使用网络IP访问,yes时 只允许127.0.0.1
port 7001
daemonize yes
#bind 127.0.0.1 -::1
dir "/data/redis/redis-sentinel/redis7001/data"
# 启动
./bin/redis-server redis.conf
root 9406 1 0 10:43 ? 00:00:00 ./bin/redis-server *:7001
root 9503 1 0 10:44 ? 00:00:00 ./bin/redis-server *:7002
root 9629 1 0 10:47 ? 00:00:00 ./bin/redis-server *:7003
# 建立主从(只需要在从库上运行),直接使用redis-cli需要设置环境变量
# 或是创建ln -s /data/redis/bin/redis-cli /usr/local/bin/
redis-cli -h 10.186.62.11 -p 7002
SLAVEOF 10.186.62.11 7001
redis-cli -h 10.186.62.11 -p 7003
SLAVEOF 10.186.62.11 7001
# 在主库上验证
redis-cli -h 10.186.62.11 -p 7001
10.186.62.11:7001> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.186.62.11,port=7002,state=online,offset=56,lag=1
slave1:ip=10.186.62.11,port=7003,state=online,offset=56,lag=1
# 例如上面的1主2从添加到哨兵
redis-cli -p 26379
sentinel monitor redis-test01_sentinel 10.186.62.11 7001 2
sentinel set redis-test01_sentinel down-after-milliseconds 30000
sentinel set redis-test01_sentinel parallel-syncs 1
sentinel set redis-test01_sentinel failover-timeout 180000
sentinel reset redis-test01_sentinel
SENTINEL flushconfig
## 验证
info sentinel
master0:name=redis-test01_sentinel,status=ok,address=10.186.62.11:7001,slaves=2,sentinels=3
3 加入sentinel
每个哨兵节点都要执行
master_name是应用连接的名称(需要应用方提供)
# 手动替换以下内容:
master_name:redis-appname01_sentinel
主库IP 端口 决策数:10.186.62.11 6379 2
密码:****redis密码****
## 手工配置监控和高可用
redis-cli -p 26379 # 一般3个哨兵节点都需要配置
sentinel monitor redis-appname01_sentinel 10.186.62.11 6379 2
sentinel set redis-appname01_sentinel auth-pass '****redis密码****'
sentinel set redis-appname01_sentinel down-after-milliseconds 30000
sentinel set redis-appname01_sentinel parallel-syncs 1
sentinel set redis-appname01_sentinel failover-timeout 180000
# 30000 超时30s判断下线
# 180000 切换超时时间3分钟
## 配置后注意需要刷新配置状态,将重新发现sentinel和redis的slave节点
sentinel reset redis-appname01_sentinel
#强制重写 SENTINEL 配置到磁盘文件中
SENTINEL flushconfig
## 验证
info sentinel # 验证 status=ok sentinels=3
正常:master0:name=redis-appname01_sentinel,status=ok,address=10.186.62.11:6379,slaves=1,sentinels=3
异常:master0:name=redis-appname01_sentinel,status=sdown,address=10.186.62.11:6379,slaves=0,sentinels=1
# status=sdown 表示主观下线了,不正常,需要看日志
`(不执行以下的内容:)`
## 是否能够决策
- 检查当前Sentinel配置是否能够达到故障转移主服务器所需的仲裁
SENTINEL ckquorum <master name>
## 查看某个哨兵组故障超时时间
SENTINEL master redis-redis-appname01_sentinel
4 redis 配置文件
修改目录和配置的路径(默认是/data/redis/{bin,data,log}),存放软件,数据,和日志,redis配置文件在/data/redis目录下(手动创建redis_端口号.conf)
需修改内存设置maxmemory ,和是否持久化设置(appendonly )
并检查参数maxmemory-policy volatile-lru
repl-backlog-size 64mb
密码设置参数
requirepass
masterauth主库建议设置为save ,关闭持久化
# Redis Client
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# Redis Server
## Redis Basic
bind 0.0.0.0
daemonize yes
loglevel notice
dir /data/redis/data
logfile /data/redis/log/redis.log
timeout 3600
maxclients 10000
maxmemory 34359738368
maxmemory-policy volatile-lru
slowlog-log-slower-than 1000
slowlog-max-len 1024
tcp-backlog 2048
tcp-keepalive 60
## Redis Security
requirepass ***密码***
masterauth ***密码***
rename-command FLUSHALL "dba_flushall"
rename-command FLUSHDB "dba_flushdb"
#rename-command CONFIG "dba_config"
## Redis RDB
dbfilename 'dump.rdb'
save
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum no
rdbcompression yes
rdbchecksum no
## Redis AOF
appendonly no # 不持久化
appendfilename 'appendonly.aof'
appendfsync everysec # 默认为everysec,平衡性能与安全性,每秒一次落盘
aof-use-rdb-preamble yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 2G
aof-load-truncated yes
no-appendfsync-on-rewrite yes
## Redis Replication
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
repl-backlog-size 64mb
repl-backlog-ttl 3600
slave-priority 100
min-slaves-to-write 0
min-slaves-max-lag 0
## Redis Cluster
cluster-enabled no
cluster-config-file 'nodes-6379.conf'
cluster-node-timeout 10000
cluster-replica-validity-factor 5
cluster-migration-barrier 1
cluster-require-full-coverage no
cluster-replica-no-failover no
port 6379
supervised systemd
pidfile /data/redis/data/redis.pid
save ""
appendfsync
# appendfsync
AOF写入磁盘的方式。
always:每次写操作都会落盘
everysec:每秒一次落盘
no:redis不做控制,由操作系统来控制落盘,一般为30s。
默认为everysec,平衡性能与安全性。
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 2G
自动重写AOF文件。当aof文件增长到指定数值时,redis会自动隐式调用bgrewriteaof来重写aof文件。
默认百分比为100,大小为64mb,建议设置auto-aof-rewrite-min-size为2G。
slowlog-log-slower-than 1000
记录超过该值的慢查询的信息,单位是微秒。
默认值为10000,建议设置为1000。
slowlog-max-len 1024
记录慢日志的队列的长度。
默认为128,建议设置为1024。
5 sentinel 配置文件
通过命令行写入
cat redis_26379.conf
daemonize yes
protected-mode no
logfile "/data/redis/redis-sentinel/redis26379/log/redis.log"
pidfile "/data/redis/redis-sentinel/redis26379/data/redis-sentinel.pid"
port 26379
dir "/data/redis/redis-sentinel/redis26379/data"
# Generated by CONFIG REWRITE
user default on nopass ~* &* +@all
sentinel myid 0cf2c1aa62f5256c8931f19316d57f1c6efbab11
sentinel current-epoch 0
sentinel monitor redis-test01_sentinel 10.186.62.11 7001 2
sentinel config-epoch redis-test01_sentinel 0
sentinel leader-epoch redis-test01_sentinel 0
sentinel known-replica redis-test01_sentinel 10.186.62.11 7002
sentinel known-replica redis-test01_sentinel 10.186.62.11 7003
sentinel known-sentinel redis-test01_sentinel 10.186.62.57 26381 851f2f5765aa682e45f816eef9a6df153fc2ed6c
sentinel known-sentinel redis-test01_sentinel 10.186.62.57 26380 1938e7fea33b41366740a472279c9b0048add004
6 一些命令
# 连接sentinel
1.
SENTINEL masters
1) 1) "name"
2) "redis-test01_sentinel"
3) "ip"
4) "10.186.62.11"
5) "port"
6) "7001"
7) "runid"
8) "3e4c3cafcb455ae879a5c1eb47fd88fec201fe98"
2.
# SENTINEL slaves <master name>
SENTINEL slaves redis-test01_sentinel
# 1主2从显示
"master-link-status"
32) "ok"
3.检查当前Sentinel配置是否能够达到故障转移主服务器所需的仲裁
SENTINEL ckquorum redis-test01_sentinel
OK 3 usable Sentinels. Quorum and failover authorization can be reached
4.在不询问其他 Sentinel 意见的情况下, 强制开始一次自动故障迁移
SENTINEL failover <master name>
SENTINEL failover redis-test01_sentinel
`可以通过 info sentinel验证切换
# 连接 redis DB
# 获取N条Redis慢日志信息
SLOWLOG GET N
# 获取配置信息
CONFIG GET *
# 修改配置信息
CONFIG SET 参数 value
# 触发RDB持久化
BGSAVE
# 触发AOF文件重写
BGREWRITEAOF
# Redis hotkeys、bigkeys查看
hotkeys命令仅在内存策略“ maxmemory-policy”为“ volatile-lfu/ allkeys-lfu” 可以使用
redis-cli -h 10.186.62.11 -p 7001 --hotkeys
redis-cli -h 10.186.62.11 -p 7001 --bigkeys