linux下安裝配置redis主從+哨兵

環境:

redhat 7.4、redis-5.0.5、2個節點(節點一:1主+1從+1哨兵,節點二:2從+1哨兵)

1、配置啓動第一個節點(1主+1從)

[root@csy3 redis-5.0.5]# yum install gcc gcc-c++ -y
......
[root@csy3 tools]# pwd
/software/tools
[root@csy3 tools]# tar -zxvf redis-5.0.5.tar.gz
[root@csy3 tools]# ls
redis-5.0.5  redis-5.0.5.tar.gz
[root@csy3 tools]# cd redis-5.0.5/
[root@csy3 redis-5.0.5]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  INSTALL  Makefile  MANIFESTO  README.md  redis.conf  runtest  runtest-cluster  runtest-moduleapi  runtest-sentinel  sentinel.conf  src  tests  utils
[root@csy3 redis-5.0.5]# make
cd src && make all
make[1]: Entering directory `/software/tools/redis-5.0.5/src'
    CC Makefile.dep
make[1]: Leaving directory `/software/tools/redis-5.0.5/src'
make[1]: Entering directory `/software/tools/redis-5.0.5/src'
    CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
 #include <jemalloc/jemalloc.h>
                               
compilation terminated.
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/software/tools/redis-5.0.5/src'
make: *** [all] Error 2
[root@csy3 redis-5.0.5]# cat README.md  | grep -5 jemalloc  #針對上面的報錯在README裏面有說明
dependencies tree is modified in any other way, make sure to use the following
command in order to really clean everything and rebuild from scratch:

    make distclean

This will clean: jemalloc, lua, hiredis, linenoise.

Also if you force certain build options like 32bit target, no C compiler
optimizations (for debugging purposes), and other similar build time options,
those options are cached indefinitely until you issue a `make distclean`
command.
--
Allocator
---------

Selecting a non-default memory allocator when building Redis is done by setting
the `MALLOC` environment variable. Redis is compiled and linked against libc
malloc by default, with the exception of jemalloc being the default on Linux
systems. This default was picked because jemalloc has proven to have fewer
fragmentation problems than libc malloc.

To force compiling against libc malloc, use:

    % make MALLOC=libc

To compile against jemalloc on Mac OS X systems, use:

    % make MALLOC=jemalloc

Verbose build
-------------
......
[root@csy3 redis-5.0.5]# make MALLOC=libc   #如上說明,直接用README裏面的
......
Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/software/tools/redis-5.0.5/src'
......
[root@csy3 redis-5.0.5]# cd src/
[root@csy3 src]# ll redis-*   #可以看到生成的redis-server這些直接用的命令
-rwxr-xr-x. 1 root root  354096 Aug 19 14:18 redis-benchmark
-rw-rw-r--. 1 root root   29605 May 16 00:07 redis-benchmark.c
-rw-r--r--. 1 root root  109136 Aug 19 14:18 redis-benchmark.o
-rwxr-xr-x. 1 root root 4046880 Aug 19 14:18 redis-check-aof
-rw-rw-r--. 1 root root    7175 May 16 00:07 redis-check-aof.c
-rw-r--r--. 1 root root   28888 Aug 19 14:18 redis-check-aof.o
-rwxr-xr-x. 1 root root 4046880 Aug 19 14:18 redis-check-rdb
-rw-rw-r--. 1 root root   13545 May 16 00:07 redis-check-rdb.c
-rw-r--r--. 1 root root   66064 Aug 19 14:18 redis-check-rdb.o
-rwxr-xr-x. 1 root root  798904 Aug 19 14:18 redis-cli
-rw-rw-r--. 1 root root  266751 May 16 00:07 redis-cli.c
-rw-r--r--. 1 root root  928640 Aug 19 14:18 redis-cli.o
-rwxr-xr-x. 1 root root 4046880 Aug 19 14:18 redis-sentinel
-rwxr-xr-x. 1 root root 4046880 Aug 19 14:18 redis-server
-rwxrwxr-x. 1 root root    3600 May 16 00:07 redis-trib.rb
[root@csy3 src]# mkdir -p /software/redis/
[root@csy3 src]# cd /software/redis/
[root@csy3 redis]# mkdir 8000 8001 8002
[root@csy3 redis]# \cp /software/tools/redis-5.0.5/redis.conf 8001/master.conf
[root@csy3 redis]# \cp /software/tools/redis-5.0.5/redis.conf 8002/slave.conf
[root@csy3 redis]# \cp /software/tools/redis-5.0.5/sentinel.conf 8000/sentinel.conf
[root@csy3 redis]# \cp /software/tools/redis-5.0.5/src/redis-server .
[root@csy3 redis]# \cp /software/tools/redis-5.0.5/src/redis-cli .
[root@csy3 redis]# vi 8001/master.conf
......
#bind 127.0.0.1   #註釋掉
protected-mode no
port 8001      #主節點端口根據自己需要修改
daemonize yes   #後臺運行
logfile "/software/redis/8001/8001.log"
dir "/software/redis/8001"
[root@csy3 redis]# vi 8002/slave.conf
......
#bind 127.0.0.1   #註釋掉
protected-mode no
port 8002    #修改從節點端口
daemonize yes    #後臺運行
logfile "/software/redis/8002/8002.log"
dir "/software/redis/8002"
slaveof 10.104.189.13 8001   #從節點加上主節點的地址
[root@csy3 redis]# ./redis-server 8001/master.conf
[root@csy3 redis]# ./redis-server 8002/slave.conf
[root@csy3 redis]# ps -ef | grep redis
root     27386     1  0 15:22 ?        00:00:00 ./redis-server 127.0.0.1:8001
root     27475     1  0 15:23 ?        00:00:00 ./redis-server 127.0.0.1:8002
root     27485 21868  0 15:23 pts/0    00:00:00 grep --color=auto redis
[root@csy3 redis]# netstat -tlnp | grep redis
tcp        0      0 127.0.0.1:8001          0.0.0.0:*               LISTEN      27386/./redis-serve
tcp        0      0 127.0.0.1:8002          0.0.0.0:*               LISTEN      27475/./redis-serve
[root@csy3 redis]# ./redis-cli -p 8001  #連主節點測試下
127.0.0.1:8001> set test 123
OK
127.0.0.1:8001> get test
"123"
127.0.0.1:8001> incr counter
(integer) 1
127.0.0.1:8001> incr counter
(integer) 2
127.0.0.1:8001> incr counter
(integer) 3
127.0.0.1:8001>

2、配置啓動第二個節點(2從)

先按上面方法在另一臺機器裝好redis
[root@csy4 redis-5.0.5]# mkdir -p /software/redis
[root@csy4 redis-5.0.5]# cd /software/redis/
[root@csy4 redis]# mkdir 8000 8003 8004
[root@csy4 redis]# \cp /software/tools/redis-5.0.5/src/redis-server .
[root@csy4 redis]# \cp /software/tools/redis-5.0.5/src/redis-cli .
[root@csy4 redis]# \cp /software/tools/redis-5.0.5/redis.conf 8003/slave.conf
[root@csy4 redis]# \cp /software/tools/redis-5.0.5/redis.conf 8004/slave.conf
[root@csy4 redis]# \cp /software/tools/redis-5.0.5/sentinel.conf 8000
[root@csy4 redis]# ls
8000  8003  8004  redis-cli  redis-server
[root@csy4 redis]# vi 8003/slave.conf
......
#bind 127.0.0.1   #註釋掉
protected-mode no
port 8003   #修改從節點端口
daemonize yes    #後臺運行
logfile "/software/redis/8003/8003.log"
dir "/software/redis/8003"
slaveof 10.104.189.13 8001   #從節點加上主節點的地址
[root@csy4 redis]# vi 8004/slave.conf
......
#bind 127.0.0.1   #註釋掉
protected-mode no
port 8004    #修改從節點端口
daemonize yes    #後臺運行
logfile "/software/redis/8004/8004.log"
dir "/software/redis/8004"
slaveof 10.104.189.13 8001   #從節點加上主節點的地址
[root@csy4 redis]# ./redis-server 8003/slave.conf
32653:C 19 Aug 2019 15:51:06.676 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32653:C 19 Aug 2019 15:51:06.676 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=32653, just started
32653:C 19 Aug 2019 15:51:06.676 # Configuration loaded
[root@csy4 redis]# ./redis-server 8004/slave.conf
32684:C 19 Aug 2019 15:51:40.889 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32684:C 19 Aug 2019 15:51:40.889 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=32684, just started
32684:C 19 Aug 2019 15:51:40.889 # Configuration loaded
[root@csy4 redis]# netstat -tlnp | grep redis
tcp        0      0 127.0.0.1:8003          0.0.0.0:*               LISTEN      32654/./redis-serve
tcp        0      0 127.0.0.1:8004          0.0.0.0:*               LISTEN      32685/./redis-serve
[root@csy4 redis]# ps -ef | grep redis
root     32654     1  0 15:51 ?        00:00:00 ./redis-server 127.0.0.1:8003
root     32685     1  0 15:51 ?        00:00:00 ./redis-server 127.0.0.1:8004
root     32692 31395  0 15:52 pts/1    00:00:00 grep --color=auto redis
[root@csy4 redis]# ./redis-cli -p 8003   #登錄從節點測試下,可以讀出主節點的key
127.0.0.1:8003> get test
"123"
127.0.0.1:8003> quit

3、分別在2個節點配置啓動哨兵

[root@csy3 redis]# ./redis-cli -p 8001
127.0.0.1:8001> info Replication   #登主節點,可以看到有3個從節點連上了
# Replication
role:master
connected_slaves:3
slave0:ip=10.104.189.13,port=8002,state=online,offset=601,lag=0
slave1:ip=10.104.189.14,port=8003,state=online,offset=601,lag=1
slave2:ip=10.104.189.14,port=8004,state=online,offset=601,lag=1
master_replid:d21ef0c08d4d2fc9bc01447c91dd7ae14bb09539
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:601
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:601
127.0.0.1:8001>quit
[root@csy3 redis]# vi 8000/sentinel.conf
......
port 8000
daemonize yes
logfile "/software/redis/8000/8000.log"
dir "/software/redis/8000"
sentinel monitor mymaster 10.104.189.13 8001 2  #主機名、IP、端口、投票選取次數
sentinel parallel-syncs mymaster 3   #從節點數量
[root@csy3 redis]# ./redis-server 8000/sentinel.conf --sentinel
[root@csy3 redis]# tail -f 8000/8000.log   #從日誌可以看出
2366:X 19 Aug 2019 17:18:32.921 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=2366, just started
2366:X 19 Aug 2019 17:18:32.921 # Configuration loaded
2367:X 19 Aug 2019 17:18:32.925 * Increased maximum number of open files to 10032 (it was originally set to 1024).
2367:X 19 Aug 2019 17:18:32.931 * Running mode=sentinel, port=8000.
2367:X 19 Aug 2019 17:18:32.932 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2367:X 19 Aug 2019 17:18:32.934 # Sentinel ID is 7cef65c8638770aef68826b903ffb8768400a9c3
2367:X 19 Aug 2019 17:18:32.934 # +monitor master mymaster 10.104.189.13 8001 quorum 2
2367:X 19 Aug 2019 17:18:32.937 * +slave slave 10.104.189.13:8002 10.104.189.13 8002 @ mymaster 10.104.189.13 8001
2367:X 19 Aug 2019 17:18:32.939 * +slave slave 10.104.189.14:8003 10.104.189.14 8003 @ mymaster 10.104.189.13 8001
2367:X 19 Aug 2019 17:18:32.941 * +slave slave 10.104.189.14:8004 10.104.189.14 8004 @ mymaster 10.104.189.13 8001
^C
[root@csy3 redis]# ps -ef | grep redis
root       694     1  0 16:54 ?        00:00:02 ./redis-server *:8001
root       709     1  0 16:54 ?        00:00:01 ./redis-server *:8002
root      2367     1  0 17:18 ?        00:00:00 ./redis-server *:8000 [sentinel]
root      2406 21868  0 17:19 pts/0    00:00:00 grep --color=auto redis
[root@csy3 redis]# vi 8000/sentinel.conf  #再看下配置文件,啓動後自動更新了一些配置

#下面到第二個節點去配置啓動哨兵,配置文件同上
[root@csy4 redis]# vi 8000/sentinel.conf
[root@csy4 redis]# ps -ef | grep redis
root      1214     1  0 16:58 ?        00:00:02 ./redis-server *:8003
root      1227     1  0 16:58 ?        00:00:02 ./redis-server *:8004
root      1633 31395  0 17:26 pts/1    00:00:00 grep --color=auto redis
[root@csy4 redis]# ./redis-server 8000/sentinel.conf --sentinel
[root@csy4 redis]# tail -f 8000/
8000.log       sentinel.conf
[root@csy4 redis]# tail -f 8000/8000.log
1646:X 19 Aug 2019 17:26:45.880 # Configuration loaded
1647:X 19 Aug 2019 17:26:45.885 * Increased maximum number of open files to 10032 (it was originally set to 1024).
1647:X 19 Aug 2019 17:26:45.890 * Running mode=sentinel, port=8000.
1647:X 19 Aug 2019 17:26:45.890 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1647:X 19 Aug 2019 17:26:45.893 # Sentinel ID is 6d585a456becfa9d5c75da8f2e2a2eca7e786f18
1647:X 19 Aug 2019 17:26:45.893 # +monitor master mymaster 10.104.189.13 8001 quorum 2
1647:X 19 Aug 2019 17:26:45.895 * +slave slave 10.104.189.13:8002 10.104.189.13 8002 @ mymaster 10.104.189.13 8001
1647:X 19 Aug 2019 17:26:45.897 * +slave slave 10.104.189.14:8003 10.104.189.14 8003 @ mymaster 10.104.189.13 8001
1647:X 19 Aug 2019 17:26:45.899 * +slave slave 10.104.189.14:8004 10.104.189.14 8004 @ mymaster 10.104.189.13 8001
1647:X 19 Aug 2019 17:26:46.961 * +sentinel sentinel 7cef65c8638770aef68826b903ffb8768400a9c3 10.104.189.13 8000 @ mymaster 10.104.189.13 8001

上面已經配置完2個節點共1主+3從+2哨兵了,配置文件根據自己的實際情況設置。

重啓過程是先所有主從起來後再起哨兵,爲了方便建議做個重啓shell腳本。

安裝配置之前要把防火牆關掉,安裝啓動完主從節點後測試下在每個從節點能否讀出主節點設置的key。

可以加上keepalived高可用和zabbix之類的監控報警。

如果連接要加上密碼,要設置主節點(master.conf)“masterauth 密碼”和從節點(slave.conf)“requirepass 密碼”。

完成後測試下,把主節點kill掉,監控哨兵日誌,觀察下會不會故障遷移到另一個從節點。(哨兵日誌主要有下面幾種情況)

................................................................................................................................................................................................................

Sentinel模式下的幾個事件

  • +reset-master :主服務器已被重置。
  • +slave :一個新的從服務器已經被 Sentinel 識別並關聯。
  • +failover-state-reconf-slaves :故障轉移狀態切換到了 reconf-slaves 狀態。
  • +failover-detected :另一個 Sentinel 開始了一次故障轉移操作,或者一個從服務器轉換成了主服務器。
  • +slave-reconf-sent :領頭(leader)的 Sentinel 向實例發送了 [SLAVEOF](/commands/slaveof.html) 命令,爲實例設置新的主服務器。
  • +slave-reconf-inprog :實例正在將自己設置爲指定主服務器的從服務器,但相應的同步過程仍未完成。
  • +slave-reconf-done :從服務器已經成功完成對新主服務器的同步。
  • -dup-sentinel :對給定主服務器進行監視的一個或多個 Sentinel 已經因爲重複出現而被移除 —— 當 Sentinel 實例重啓的時候,就會出現這種情況。
  • +sentinel :一個監視給定主服務器的新 Sentinel 已經被識別並添加。
  • +sdown :給定的實例現在處於主觀下線狀態。
  • -sdown :給定的實例已經不再處於主觀下線狀態。
  • +odown :給定的實例現在處於客觀下線狀態。
  • -odown :給定的實例已經不再處於客觀下線狀態。
  • +new-epoch :當前的紀元(epoch)已經被更新。
  • +try-failover :一個新的故障遷移操作正在執行中,等待被大多數 Sentinel 選中(waiting to be elected by the majority)。
  • +elected-leader :贏得指定紀元的選舉,可以進行故障遷移操作了。
  • +failover-state-select-slave :故障轉移操作現在處於 select-slave 狀態 —— Sentinel 正在尋找可以升級爲主服務器的從服務器。
  • no-good-slave :Sentinel 操作未能找到適合進行升級的從服務器。Sentinel 會在一段時間之後再次嘗試尋找合適的從服務器來進行升級,又或者直接放棄執行故障轉移操作。
  • selected-slave :Sentinel 順利找到適合進行升級的從服務器。
  • failover-state-send-slaveof-noone :Sentinel 正在將指定的從服務器升級爲主服務器,等待升級功能完成。
  • failover-end-for-timeout :故障轉移因爲超時而中止,不過最終所有從服務器都會開始複製新的主服務器(slaves will eventually be configured to replicate with the new master anyway)。
  • failover-end :故障轉移操作順利完成。所有從服務器都開始複製新的主服務器了。
  • +switch-master :配置變更,主服務器的 IP 和地址已經改變。 這是絕大多數外部用戶都關心的信息。
  • +tilt :進入 tilt 模式。
  • -tilt :退出 tilt 模式。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章