Redis 主从切换+Predixy.md
部署环境
主机名 | ip |
---|---|
redis-zhu | 192.168.244.132 |
redis-cong | 192.168.244.128 |
1.配置互信
两台主机都做
通过ssh-keygen 生成公钥私钥对,拷贝到兄弟节点;
ssh-keygen
ssh-copy ip
2.Redis 包安装及账号设置
安装Redis 包如低版本,可以直接官网下载rpm 包进行安装,会直接生成Redis 账号及组;
如较新版本需要下载源码tar.gz,解压后自行编译,同时需要手动添加Redis
安装依赖
yum -y install gcc gcc-c++ tcl
账号及组;
groupadd redis
useradd -s /sbin/nologin -g redis redis
Redis 下载到/opt下:
wget http://download.redis.io/releases/redis-5.0.8.tar.gz
tar -xzf redis-5.0.8.tar.gz
cd redis-5.0.8
make & make install
复制配置文件到/etc
cp /opt/redis-5.0.8/redix.conf /etc
cp /opt/redis-5.0.8/sentinel.conf /etc
系统 及 Redis参数配置
系统 :
vi /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
echo 4096 > /proc/sys/net/core/somaxconn
vi /etc/sysctl.conf
vm.overcommit_memory = 1
net.core.somaxconn = 4096
sysctl -p
ulimit -SHn 100000
vi /etc/security/limits.conf
* soft nofile 100000
* hard nofile 100000
* soft nproc 64000
* hard nproc 64000
Redis: 参数配置
vim /etc/redix.conf
requirepass "wxIMtMRWw7104MSLitiowI4QCknkzFMV" #主上配置
masterauth "wxIMtMRWw7104MSLitiowI4QCknkzFMV" #从上配置
bind 0.0.0.0
port 6380
maxmemory 3gb
dir "/redis/data"
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error no
tcp-backlog 511
timeout 0
tcp-keepalive 300
pidfile "/var/run/redis.pid"
logfile "/var/log/redis/redis.log"
sentinel 配置:
vim /etc/sentinle.conf #主配置
port 26380
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/opt/redis/data/sentinel.log"
dir "/tmp"
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.244.132 6380 1
sentinel config-epoch mymaster 5
sentinel leader-epoch mymaster 5
protected-mode no
sentinel current-epoch 5
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
vim /etc/sentinle.conf #从配置
port 26380
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/opt/redis5/data/sentinel.log"
dir "/tmp"
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.244.132 6380 1
sentinel config-epoch mymaster 5
sentinel leader-epoch mymaster 5
protected-mode no
sentinel current-epoch 5
4.predixy 安装
同Redis ,可以直接下载编译后执行文件或是下载源码编译;配置文件拷贝到固定目录;
安装依赖
yum install libstdc++-static -y
下载predixy
git clone https://github.com/joyieldInc/predixy.git
cd predixy
编译既可以
make
cp 执行文件到bin目录
cp src/predixy /usr/local/bin/
cp配置文件到/etc/predixy(目录需要创建)
cp predixy/conf/* /etc/predixy/
5. 配置Predixy
vim /etc/predixy/auth.conf
Authority {
Auth {
Mode write
}
Auth {
Mode admin
}
}
vim /etc/predixy/predixy.conf
Name PredixyExample
WorkerThreads 1
ClientTimeout 300
LogVerbSample 0
LogDebugSample 0
LogInfoSample 10000
LogNoticeSample 1
LogWarnSample 1
LogErrorSample 1
Include auth.conf
Include sentinel.conf
Include latency.conf
vim /etc/predixy/sentinel.conf
SentinelServerPool {
Pssword wxIMtMRWw7104MSLitiowI4QCknkzFMV
Databases 16
Hash crc16
HashTag {}
Distribution modula
MasterReadPriority 60
StaticSlaveReadPriority 50
DynamicSlaveReadPriority 50
RefreshInterval 1
ServerTimeout 1
ServerFailureLimit 1
ServerRetryTimeout 1
KeepAlive 120
Sentinels {
+ 192.168.244.132:26380
+ 192.168.244.128:26380
}
Group mymaster {
+ 192.168.244.132:6380
+ 192.168.244.128:6380
}
}
6.keepalived 安装
可以直接下载rpm,并进行安装;
yum -y install keepalived
7.配置Keepalived
vim /etc/keepalived/keepalived.conf #主上配置
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id redis-zhu
vrrp_mcast_group4 224.0.100.100
}
vrrp_script check_redis_proxy {
script "/usr/bin/check_redis_proxy.sh"
interval 3
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 6
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 571f97b2
}
track_script {
check_redis_proxy
}
virtual_ipaddress {
192.168.244.144/24
}
}
vim /etc/keepalived/keepalived.conf #从上配置
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id redis-zhu
vrrp_mcast_group4 224.0.100.100
}
vrrp_script check_redis_proxy {
script "/usr/bin/check_redis_proxy.sh"
interval 3
weight 2
}
vrrp_instance VI_1 {
state BADKUP
interface ens33
virtual_router_id 6
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 571f97b2
}
track_script {
check_redis_proxy
}
virtual_ipaddress {
192.168.244.144/24
}
}
8. Keepalived 脚本
vim /usr/bin/check_redis_proxy.sh
#!/bin/bash
PREDIXY_PID=`ps -ef |grep predixy|grep -v 'grep'|wc -l`
ps -ef |grep predixy|grep -v 'grep'|wc -l
if [ "${PREDIXY_PID}" -ge 1 ];then
echo "predixy is Ok"
else
systemctl start predixy
sleep 3;
PREDIXY_PID2=`ps -ef |grep predixy|grep -v 'grep'|wc -l`
if [ "${PREDIXY_PID2}" -eq 0 ];then
systemctl stop keepalived
fi
fi
9. Service 文件供参考
cat /usr/lib/systemd/system/redis.service
[Unit]
Description=Redis
After=syslog.target nework.target
[Service]
ExecStart=/usr/bin/redis-server /etc/redis.conf
ExecStop=/usr/bin/pkill redis-server
TimeoutStopSec=0
Restart=on-failure
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# Virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# stack size
LimitSTACK=1048576
# locked memory
LimitMEMLOCK=infinity
# total threads(user+kernel)
TasksMax=infinity
TasksAccounting=false
[Install]
WantedBy=multi-user.target
cat /usr/lib/systemd/system/redis-sentinel.service
[Unit]
Description=Redis-sentinel
After=syslog.target nework.target
[Service]
Type=forking
ExecStart=/usr/bin/redis-sentinel /etc/sentinel.conf
ExecStop=/usr/bin/pkill redis-sentinel
TimeoutStopSec=0
Restart=on-failure
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
# file sizes
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# stack size
LimitSTACK=1048576
# lcoked memory
LimitMEMLOCK=infinity
# total threads
TasksMax=infinity
TasksAccounting=false
[Install]
WantedBy=multi-user.target
cat /usr/lib/systemd/system/predixy.service
[Unit]
Description=Predixy
After=syslog.target nework.target
[Service]
Type=simple
ExecStart=/usr/local/bin/predixy /etc/predixy/predixy.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
PrivateTmp=true
TimeoutStopSec=0
Restart=on-failure
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectory=0755
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# stack size
LimitSTACK=1048576
# locked memory
LimitMEMLOCK=infinity
# total threads
TasksMax=infinity
TasksAccounting=false
[Install]
WantedBy=multi-user.target
10. 启动服务
依次启动并确认正常后,启动下一个Redis, Redis-Sentinel, Predixy, Keepalived;
最后确认主从状态,代理状态,vip;
systemctl start keepalived
systemctl daemon-reload
systemctl enable redis.service predixy.service sentinel.service keepalived.service
systemctl start redis.service predixy.service sentinel.service
注意以上两台主机都安装
Predixy 参考文档:https://github.com/joyieldInc/predixy/blob/master/doc/config_CN.md