redis+sentinel集群部署


一、環境介紹 CentOS7

192.168.1.201 主+Sentinel      redis端口:6379 Sentinel端口:26379
192.168.1.202 從+Sentinel      redis端口:6379 Sentinel端口:26379
192.168.1.203 從+Sentinel      redis端口:6379 Sentinel端口:26379

二、安裝redis服務

1、安裝依賴(3台)

$ yum -y install gcc*

2、下載redis安裝(3台)

$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz

3、解壓編譯(3台)

$ tar xf redis-5.0.5.tar.gz -C /usr/local/
$ cd /usr/local/redis-5.0.5
$ make

4、創建redis 數據目錄 日志目錄(3台)

$ mkdir -p /opt/redis/{data,conf,redis-log,sentinel-log}

5、移動配置文件(3台)

$ cd /usr/local/redis-5.0.5/
$ cat redis.conf | grep -v ^#|grep -v ^$ >/opt/redis/conf/redis.conf

6、修改配置文件redis.conf(主)

#修改配置
bind 0.0.0.0
dir "/opt/redis/data"
daemonize yes
logfile "/opt/redis/redis-log/redis.log"
#增加配置
requirepass 123456789
masterauth 123456789

修改配置文件redis.conf(從)

#修改配置
bind 0.0.0.0
dir "/opt/redis/data"
daemonize yes
logfile "/opt/redis/redis-log/redis.log"
#增加配置
requirepass 123456789
masterauth 123456789
slaveof 192.168.1.201 6379
#修改配置
bind 0.0.0.0
dir "/opt/redis/data"
daemonize yes
logfile "/opt/redis/redis-log/redis.log"
#增加配置
requirepass 123456789
masterauth 123456789
slaveof 192.168.1.201 6379

7、把啟動文件放在/usr/local/bin方便啟動(3台)

$ cp /usr/local/redis-5.0.5/src/redis-server  /usr/local/bin/
$ cp /usr/local/redis-5.0.5/src/redis-cli  /usr/local/bin/

8、啟動(3台)

$ redis-server  /opt/redis/conf/redis.conf

9、設置開機自動啟動(3台)

$ vim /etc/systemd/system/redis-server.service

[Unit]
Description=redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/bin/redis-server /opt/redis/conf/redis.conf
ExecStop=/usr/local/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown
PrivateTmp=true

[Install]
WantedBy=multi-user.target

$ systemctl daemon-reload
$ systemctl enable redis-server.service

10、測試

# 查看集群是否正常
$ redis-cli  -h 192.168.1.201 -p 6379 -a 123456789 info Replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.202,port=6379,state=online,offset=906,lag=0
slave1:ip=192.168.1.203,port=6379,state=online,offset=906,lag=0
master_replid:4ac62473efb8c3767bbd2cd0e1754c4e8d2980d1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:906
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:906
192.168.1.201:6379> AUTH 123.456.789
OK
192.168.1.201:6379> set k1 v1
OK
192.168.1.201:6379> exit
$ redis-cli  -h 192.168.1.202 -p 6379 -a 123456789
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.202:6379> AUTH 123456789
OK
192.168.1.202:6379> get k1
"v1"
192.168.1.202:6379> exit
$ redis-cli  -h 192.168.1.203 -p 6379 -a 123456789
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.203:6379> AUTH 123.456.789
OK
192.168.1.203:6379> get k1
"v1"

三、安裝哨兵 Sentinel

1、復制配置文件 拷貝哨兵啟動文件到/usr/local/bin(3台)

$ cd /usr/local/redis-5.0.5/
$ cat sentinel.conf | grep -v ^#|grep -v ^$ >/opt/redis/conf/sentinel.conf
$ cd src
$ cp redis-sentinel /usr/local/bin

2、修改配置文件/opt/redis/conf/sentinel.conf(3台一致)

port 26379
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile "/opt/redis/sentinel-log/sentinel.log"
dir /tmp
sentinel monitor mymaster 192.168.1.201 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel auth-pass mymaster 123456789

3、啟動哨兵

$ redis-sentinel  /opt/redis/conf/sentinel.conf

4、設置開機自動啟動

$ vim /etc/systemd/system/redis-sentinel.service

[Unit]
Description=The redis-sentinel Process Manager
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/var/run/redis-sentinel.pid
ExecStart=/usr/local/bin/redis-sentinel /opt/redis/conf/sentinel.conf
ExecStop=/usr/local/bin/redis-cli -h 127.0.0.1  -p 26379
PrivateTmp=true

[Install]
WantedBy=multi-user.target

$ systemctl daemon-reload
$ systemctl enable redis-sentinel.service

5、測試

$ redis-cli  -h 192.168.1.201 -p 6379 -a 123456789 info Replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.202,port=6379,state=online,offset=30335,lag=1
slave1:ip=192.168.1.203,port=6379,state=online,offset=30335,lag=1
master_replid:db7b7b2d05b7bc2e770f5597e31bf9b7274b6add
master_replid2:d8095aad044052ca59d4ecfc54a89911eb17e966
master_repl_offset:30617
second_repl_offset:11834
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:30617

kill掉201的redis服務,可以看到主已經順利切換到202了

$ redis-cli  -h 192.168.1.202 -p 6379 -a 123456789 info Replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.203,port=6379,state=online,offset=0,lag=0
master_replid:68c6b9e0b0ca4f51a887adb82ecd6e1b36a2276e
master_replid2:db7b7b2d05b7bc2e770f5597e31bf9b7274b6add
master_repl_offset:42413
second_repl_offset:41827
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1448
repl_backlog_histlen:40966


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM