1. 程式人生 > >centos部署redis主從

centos部署redis主從

start slow level filename 個數字 table pac 從服務器 一個

安裝環境

CentOS 6.5 、CentOS 7.4

主Redis:10.159.44.175

從Redis: 10.159.44.176、10.159.44.177

Redis下載和安裝

在3臺機器上分別安裝redis

#添加yum倉庫

yum install epel-release -y

#安裝redis

yum install redis -y

程序文件說明

安裝完畢後有以下幾個文件位於/usr/bin目錄:

redis-server:Redis服務器的daemon啟動程序

redis-cli:Redis命令行操作工具,也可以用telnet根據其純文本協議來操作

redis-benchmark:Redis性能測試工具,測試Redis在當前系統及配置下的讀寫性能

redis-check-aof:更新日誌檢查

redis-check-dump:用於本地數據庫檢查

iptables策略配置

如果iptable處於啟用狀態,且INPUT鏈默認規則為drop,則需要配置開放端口

firewall-cmd --zone=public --add-port=4100/tcp --permanent

firewall-cmd --zone=public --add-port=4200/tcp --permanent

firewall-cmd --reload #重新加載生效

**以上為端口根據實際情況添加,默認端口為:6370、26379

Redis主從配置

主redis的配置

在主Redis上編輯配置文件

# cat /etc/redis.conf

daemonize yes
pidfile "/var/run/redis/redis.pid"
port 4100
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/var/log/redis/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
maxmemory 10g
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/var/lib/redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
masterauth "LQi#czFe$!"
requirepass "LQi#czFe$!"
# Generated by CONFIG REWRITE
protected-mode no

從redis配置

在2臺從服務器上編輯Redis的配置文件

以上面主redis的配置文件為基準,添加一行

slaveof <主redis服務器IP> <端口>

即完成 從redis的配置。

如:slaveof 10.159.44.175 4100 # 指向10.159.44.175:4100作為master服務器

Sentinel的配置

在3臺服務器上分別編輯redis-sentinel的配置文件

vi /etc/redis-sentinel.conf

port 4200 # 默認使用4200端口
daemonize yes
protected-mode no
logfile "/var/log/redis/sentinel.log"
pidfile "/var/run/redis/sentinel.pid"
dir "/tmp


sentinel monitor mymaster 10.159.44.175 4100 1 # 監視一個名為 mymaster 的主服務器, 這個主服務器的 IP 地址為 127.0.0.1 , 端口號為 4100 , 而將這個主服務器判斷為失效至少需要 2 個 Sentinel 同意 (只要同意 Sentinel 的數量不達標,自動故障遷移就不會執行)。
sentinel down-after-milliseconds mymaster 5000 # 指定了 Sentinel 認為服務器已經斷線所需的毫秒數。
sentinel config-epoch mymaster 10 # 配置紀元,可以理解為配置的版本號,因為Sentinel會主動自動修改redis.conf和自己的redis-sentinel.conf來保持主從同步,此項數值可隨意設置,但當Sentinel做過自動的主從切換以後,數值會加1。

sentinel parallel-syncs mymaster 1 # 在執行故障轉移時, 最多可以有多少個從服務器同時對新的主服務器進行同步, 這個數字越小, 完成故障轉移所需的時間就越長,但越大就意味著越多的從服務器因為復制而不可用。可以通過將這個值設為 1 來保證每次只有一個從服務器處於不能處理命令請求的狀態。

註意:/etc/redis-sentinel.conf 必須要有寫入權限。

常用命令

Redis的啟動,停止,重啟:

systemctl start redis.service

systemctl stop redis.service

systemctl restart redis.service

Redis Sentinel的啟動,停止,重啟:

systemctl start redis-sentinel.service

systemctl stop redis-sentinel.service

systemctl restart redis-sentinel.service

使用redis客戶端連接服務初始化已經確認狀態

redis-cli -p 4100 -h 127.0.0.1 # 使用參數指定:-p 端口 -h 服務器地址

127.0.0.1:6379> auth passwd #使用密碼登錄

OK

127.0.0.1:6379> config set protected-mode "no" #修改redis的保護模式為no,不啟用。

OK

127.0.0.1:6379> info #查看狀態

centos部署redis主從