本次實驗環境準備用一臺服務器模擬3臺redis服務器,1主2從html
安裝Redis,參考前面安裝Redis文章,保證單機使用沒有問題。redis
定位到安裝後的redis目錄:cd /usr/local/redisvim
對單機的redis配置文件拷貝出3份出來服務器
cp redis.conf redis6381.conf工具
cp redis.conf redis6382.confspa
cp redis.conf redis6383.conf3d
清空新拷貝的三份配置文件,命令爲 「> 文件名」code
[root@localhost redis]# > redis6381.conform
[root@localhost redis]# > redis6382.confserver
[root@localhost redis]# > redis6383.conf
再對三份配置文件分別配置內容
6381端口的配置文件
include /usr/local/redis/redis.conf #包含redis.conf文件 daemonize yes port 6381 pidfile /var/run/redis_6381.pid logfile 6381.log dbfilename dump6381.rdb
6382端口的配置文件
include /usr/local/redis/redis.conf #包含redis.conf文件 daemonize yes port 6382 pidfile /var/run/redis_6382.pid logfile 6382.log dbfilename dump6382.rdb slaveof 127.0.0.1 6381 #說明是從屬於6381端口的主機
6383端口的配置文件
include /usr/local/redis/redis.conf #包含redis.conf文件 daemonize yes port 6383 pidfile /var/run/redis_6383.pid logfile 6383.log dbfilename dump6383.rdb slaveof 127.0.0.1 6381 #說明是從屬於6381端口的主機
模擬啓動三臺redis服務器
[root@localhost redis]# ./bin/redis-server redis6381.conf
[root@localhost redis]# ./bin/redis-server redis6382.conf
[root@localhost redis]# ./bin/redis-server redis6383.conf
查看進程
[root@localhost redis]# ps -ef|grep redis
root 90648 1 0 17:17 ? 00:00:00 ./bin/redis-server 127.0.0.1:6381
root 90652 1 0 17:17 ? 00:00:00 ./bin/redis-server 127.0.0.1:6382
root 90657 1 1 17:17 ? 00:00:00 ./bin/redis-server 127.0.0.1:6383
root 90664 4536 0 17:17 pts/2 00:00:00 grep --color=auto redis
說明3臺服務器已經啓動成功,端口分別是指定的6381,6382,6383
分別打開三個窗口,定位到redis目錄:cd /usr/local/redis
經過客戶端工具鏈接不一樣端口的服務器
如鏈接6381端口服務器
[root@localhost redis]# ./bin/redis-cli -p 6381
查看信息,以下:
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6382,state=online,offset=253,lag=1
slave1:ip=127.0.0.1,port=6383,state=online,offset=253,lag=1
master_repl_offset:253
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:252
能夠看到它是主服務器,咱們在前面配置文件中指定它就是主服務器。
由於是主機,它可讀可寫,寫的數據同步到從機。
寫:
127.0.0.1:6381> set key1 v1
OK
127.0.0.1:6381> set key2 v2
OK
讀:
127.0.0.1:6381> get key1
"v1"
127.0.0.1:6381> get key2
"v2"
如鏈接6382端口服務器
[root@localhost redis]# ./bin/redis-cli -p 6382
查看信息,以下:
127.0.0.1:6382> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:883
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
能夠看到,它是從服務器,咱們在前面配置文件中指定它就是從服務器。
由於是從機,它只讀不可寫。數據只能從主機6381同步。
讀:
127.0.0.1:6382> keys *
1) "key1"
2) "key2"
127.0.0.1:6382> get key2
"v2"
寫:
127.0.0.1:6382> set key3 v3
(error) READONLY You can't write against a read only slave.
定位到redis的源文件目錄,將哨兵配置文件sentinel.conf做爲模板,拷貝出3份
[root@localhost local]# cd redis-3.2.9/
[root@localhost redis-3.2.9]# cp sentinel.conf sentinel26381.conf
[root@localhost redis-3.2.9]# cp sentinel.conf sentinel26382.conf
[root@localhost redis-3.2.9]# cp sentinel.conf sentinel26383.conf
分別修改拷貝的配置文件
[root@localhost redis-3.2.9]# vim sentinel26381.conf
將哨兵的工做端口端口26379和監控的redis主機127.0.0.1 6379 2修改成本身定義的值
模板中的兩處要被修改的地方:
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
我這裏修改成該哨兵端口爲26381,監控的redis主機爲127.0.0.1 6381
port 26381
sentinel monitor mymaster 127.0.0.1 6381 2
其餘兩臺哨兵配置文件也按照這樣修改,只是哨兵的端口要不同,監控的redis主機應該同樣。
分別打開3個窗口,定爲到/usr/local/redis-3.2.9/src,找到redis-sentinel,配合上步的配置文件啓動
注意須要3個窗口,由於這裏是前臺啓動,須要看監控信息
[root@localhost src]# ./redis-sentinel ../sentinel26381.conf
[root@localhost src]# ./redis-sentinel ../sentinel26382.conf
[root@localhost src]# ./redis-sentinel ../sentinel26383.conf
等3臺哨兵都啓動成功之後,
分別查看監控信息,26381哨兵的監控信息,以下:
[root@localhost src]# ./redis-sentinel ../sentinel26381.conf
94744:X 01 Feb 18:29:03.171 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26381
| `-._ `._ / _.-' | PID: 94744
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
94744:X 01 Feb 18:29:03.172 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
94744:X 01 Feb 18:29:03.174 # Sentinel ID is 9044885c3cd355b168a93bc68ed4c2b7c78ebca6
94744:X 01 Feb 18:29:03.174 # +monitor master mymaster 127.0.0.1 6381 quorum 2
94744:X 01 Feb 18:29:03.175 * +slave slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6381
94744:X 01 Feb 18:29:03.176 * +slave slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6381
/usr/local/redis-3.2.9/src/usr/local/redis-3.2.9/src94744:X 01 Feb 18:33:35.645 * +sentinel sentinel 7bc01d8db6cf93c5d1f9d0a146c9967c0c4b339b 127.0.0.1 26382 @ mymaster 127.0.0.1 6381
94744:X 01 Feb 18:34:08.801 * +sentinel sentinel 574de03c2a3e5ace4798af0df4945961398efdf1 127.0.0.1 26383 @ mymaster 127.0.0.1 6381
26382哨兵的監控信息,以下:
[root@localhost src]# ./redis-sentinel ../sentinel26382.conf
95009:X 01 Feb 18:33:33.626 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26382
| `-._ `._ / _.-' | PID: 95009
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
95009:X 01 Feb 18:33:33.627 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
95009:X 01 Feb 18:33:33.648 # Sentinel ID is 7bc01d8db6cf93c5d1f9d0a146c9967c0c4b339b
95009:X 01 Feb 18:33:33.648 # +monitor master mymaster 127.0.0.1 6381 quorum 2
95009:X 01 Feb 18:33:33.649 * +slave slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:33:33.651 * +slave slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:33:34.651 * +sentinel sentinel 9044885c3cd355b168a93bc68ed4c2b7c78ebca6 127.0.0.1 26381 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:34:08.801 * +sentinel sentinel 574de03c2a3e5ace4798af0df4945961398efdf1 127.0.0.1 26383 @ mymaster 127.0.0.1 6381
26383哨兵的監控信息,以下:
[root@localhost src]# ./redis-sentinel ../sentinel26383.conf
95097:X 01 Feb 18:34:06.716 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26383
| `-._ `._ / _.-' | PID: 95097
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
95097:X 01 Feb 18:34:06.717 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
95097:X 01 Feb 18:34:06.719 # Sentinel ID is 574de03c2a3e5ace4798af0df4945961398efdf1
95097:X 01 Feb 18:34:06.719 # +monitor master mymaster 127.0.0.1 6381 quorum 2
95097:X 01 Feb 18:34:06.719 * +slave slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6381
95097:X 01 Feb 18:34:06.721 * +slave slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6381
95097:X 01 Feb 18:34:07.784 * +sentinel sentinel 9044885c3cd355b168a93bc68ed4c2b7c78ebca6 127.0.0.1 26381 @ mymaster 127.0.0.1 6381
95097:X 01 Feb 18:34:08.425 * +sentinel sentinel 7bc01d8db6cf93c5d1f9d0a146c9967c0c4b339b 127.0.0.1 26382 @ mymaster 127.0.0.1 6381
主動讓redis的主服務器6381關掉,模擬主機掛機的情況
127.0.0.1:6381> shutdown
觀看哨兵服務器,會發現如下信息
[root@localhost src]# ./redis-sentinel ../sentinel26382.conf
95009:X 01 Feb 18:33:33.626 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.9 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26382
| `-._ `._ / _.-' | PID: 95009
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
95009:X 01 Feb 18:33:33.627 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
95009:X 01 Feb 18:33:33.648 # Sentinel ID is 7bc01d8db6cf93c5d1f9d0a146c9967c0c4b339b
95009:X 01 Feb 18:33:33.648 # +monitor master mymaster 127.0.0.1 6381 quorum 2
95009:X 01 Feb 18:33:33.649 * +slave slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:33:33.651 * +slave slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:33:34.651 * +sentinel sentinel 9044885c3cd355b168a93bc68ed4c2b7c78ebca6 127.0.0.1 26381 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:34:08.801 * +sentinel sentinel 574de03c2a3e5ace4798af0df4945961398efdf1 127.0.0.1 26383 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:42.232 # +sdown master mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:42.316 # +odown master mymaster 127.0.0.1 6381 #quorum 2/2
95009:X 01 Feb 18:59:42.316 # +new-epoch 1
95009:X 01 Feb 18:59:42.316 # +try-failover master mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:42.325 # +vote-for-leader 7bc01d8db6cf93c5d1f9d0a146c9967c0c4b339b 1
95009:X 01 Feb 18:59:42.363 # 9044885c3cd355b168a93bc68ed4c2b7c78ebca6 voted for 7bc01d8db6cf93c5d1f9d0a146c9967c0c4b339b 1
95009:X 01 Feb 18:59:42.363 # 574de03c2a3e5ace4798af0df4945961398efdf1 voted for 7bc01d8db6cf93c5d1f9d0a146c9967c0c4b339b 1
95009:X 01 Feb 18:59:42.379 # +elected-leader master mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:42.379 # +failover-state-select-slave master mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:42.470 # +selected-slave slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:42.470 * +failover-state-send-slaveof-noone slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:42.522 * +failover-state-wait-promotion slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:43.315 # +promoted-slave slave 127.0.0.1:6383 127.0.0.1 6383 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:43.315 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:43.376 * +slave-reconf-sent slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:44.363 * +slave-reconf-inprog slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:44.363 * +slave-reconf-done slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:44.422 # +failover-end master mymaster 127.0.0.1 6381
95009:X 01 Feb 18:59:44.422 # +switch-master mymaster 127.0.0.1 6381 127.0.0.1 6383
95009:X 01 Feb 18:59:44.422 * +slave slave 127.0.0.1:6382 127.0.0.1 6382 @ mymaster 127.0.0.1 6383
95009:X 01 Feb 18:59:44.422 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6383
95009:X 01 Feb 19:00:14.465 # +sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6383
說明集羣的主服務器已經切換到了6383上,即原來的從機6383變爲了主機
咱們去看一下6383的信息
127.0.0.1:6383> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6382,state=online,offset=19848,lag=1
master_repl_offset:19981
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:19980
確實角色爲master,它下面掛載一臺從機6382
它如今升級爲主機,因此能夠讀寫,驗證以下:
127.0.0.1:6383> set key5 v5
OK
127.0.0.1:6383> get key5
"v5"
再去看下一6382從機的信息
127.0.0.1:6382> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6383
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:17440
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
說明它確實從屬的主機爲6383
它仍是隻能讀,數據只能從主機同步
127.0.0.1:6382> set key6 v6
(error) READONLY You can't write against a read only slave.
127.0.0.1:6382> get key5
"v5"
假設原來的6381服務器已經恢復正常了,咱們來啓動它
能夠看到,6381啓動後,自動變爲了從機,主機仍是後來的6383,說明原來的主機恢復後,不會恢復它原來的地位。
由於它已是從機,因此只能讀,不能寫
再看主服務器6383的信息,它已經有兩個從機了,6381是後來恢復後自動掛載上的
哨兵服務器26381上看到對應的自動掛載信息
哨兵服務器26382上看到對應的自動掛載信息
哨兵服務器26383上看到對應的自動掛載信息