redis sentinel哨兵集群-centos7.6

 

【centos7.6】

  • 环境搭建:【】

192.168.16.3   master 

192.168.16.7   slave

192.168.16.8   slave2

 

[root@localhost ~]# hostnamectl set-hostname master;bash

[root@localhost ~]# hostnamectl set-hostname slave;bash

[root@localhost ~]# hostnamectl set-hostname slave2;bash

[root@master ~]# ls

anaconda-ks.cfg  redis-4.0.9.tar.gz

[root@master ~]#tar zxvf redis-4.0.9.tar.gz

[root@master ~]# cd redis-4.0.9

[root@master redis-4.0.9]# make

[root@master redis-4.0.9]#  echo $?

0

[root@master redis-4.0.9]# cd

[root@master ~]# mkdir -p /usr/local/redis

[root@master ~]# cp /root/redis-4.0.9/src/redis-server /usr/local/redis/

[root@master ~]# cp /root/redis-4.0.9/src/redis-cli /usr/local/redis/

[root@master ~]# cp /root/redis-4.0.9/redis.conf /usr/local/redis/

[root@master ~]#  ls /usr/local/redis/

redis-cli  redis.conf  redis-server

【此处ip为本机ip】【ip出错会导致redis启动失败】

[root@master ~]# sed -i '/^bind 127.0.0.1$/s/127.0.0.1/192.168.16.3/g' /usr/local/redis/redis.conf

[root@master ~]# sed -i '/protected-mode/s/yes/no/g' /usr/local/redis/redis.conf

[root@master ~]# sed -i '/daemonize/s/no/yes/g' /usr/local/redis/redis.conf

[root@master ~]# sed -i '/requirepass/s/foobared/123123/g' /usr/local/redis/redis.conf

[root@master ~]# sed -i '/requirepass 123123/s/^#//g' /usr/local/redis/redis.conf

[root@master ~]# cat /usr/local/redis/redis.conf

  69 bind 192.168.16.3

  88 protected-mode no

  136 daemonize yes

  500 requirepass 123123

  • 所有节点配置文件

[root@master ~]# ln -s /usr/local/redis/redis-cli /usr/local/bin/redis

[root@master ~]# cat <>/etc/init.d/redis

> #!/bin/sh

> # chkconfig: 2345 80 90

> # description: Start and Stop redis

> #PATH=/usr/local/bin:/sbin:/usr/bin:/bin

> REDISPORT=6379

> EXEC=/usr/local/redis/redis-server

> REDIS_CLI=/usr/local/redis/redis-cli

> PIDFILE=/var/run/redis_6379.pid

> CONF="/usr/local/redis/redis.conf"

> AUTH="123123"

> LISTEN_IP=\$(netstat -utpln |grep redis-server |awk '{print \$4}'|awk -F':' '{print \$1}')

>

> case "\$1" in

>     start)

>         if [ -f \$PIDFILE ]

>         then

>                 echo "\$PIDFILE exists, process is already running or crashed"

>         else

>                 echo "Starting Redis server..."

>                 \$EXEC \$CONF

>         fi

>         if [ "\$?"="0" ]

>         then

>               echo "Redis is running..."

>         fi

>         ;;

>     stop)

>         if [ ! -f \$PIDFILE ]

>         then

>                 echo "\$PIDFILE does not exist, process is not running"

>         else

>                 PID=\$(cat \$PIDFILE)

>                 echo "Stopping ..."

>                 \$REDIS_CLI -h \$LISTEN_IP -p \$REDISPORT -a \$AUTH SHUTDOWN

>                 while [ -x \${PIDFILE} ]

>                do

>                     echo "Waiting for Redis to shutdown ..."

> sleep 1

>                 done

>                 echo "Redis stopped"

>         fi

>         ;;

>    restart|force-reload)

>         \${0} stop

>         \${0} start

>         ;;

>   *)

>     echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2

>         exit 1

> esac

> END

[root@master ~]# chmod 755 /etc/init.d/redis

[root@master ~]# chkconfig --add redis

[root@master ~]# /etc/init.d/redis start

/var/run/redis_6379.pid exists, process is already running or crashed

Redis is running...

[root@master ~]# netstat -utpln |grep redis

tcp        0      0 192.168.16.3:6379       0.0.0.0:*               LISTEN      10627/redis-server 

[root@master ~]#  redis -h 192.168.16.3 -a 123123 -p 6379

192.168.16.3:6379> exit

  • master的配置文件

[root@master ~]# sed -i '450s/^\(.\).\{22\}/min-slaves-to-write 2/g' /usr/local/redis/redis.conf

[root@master ~]#  sed -n '451s/^\(.\).\{22\}/min-slaves-max-lag 10/g' /usr/local/redis/redis.conf

[root@master ~]# cat /usr/local/redis/redis.conf

450 min-slaves-to-write 2

 451 # min-slaves-max-lag 10

[root@master ~]# /etc/init.d/redis restart

Stopping ...

Redis stopped

Starting Redis server...

10730:C 25 May 17:29:23.834 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

10730:C 25 May 17:29:23.834 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=10730, just started

10730:C 25 May 17:29:23.834 # Configuration loaded

Redis is running...

 

  • slave配置文件

[root@slave ~]# sed -i '281s/^\(.\).\{32\}/slaveof 192.168.16.3 6379/g' /usr/local/redis/redis.conf

[root@slave ~]#  sed -i '288s/^\(.\).\{29\}/masterauth 123123/g' /usr/local/redis/redis.conf

[root@slave ~]# /etc/init.d/redis restart

/var/run/redis_6379.pid does not exist, process is not running

Starting Redis server...

21547:C 26 May 09:16:50.961 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

21547:C 26 May 09:16:50.962 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=21547, just started

21547:C 26 May 09:16:50.962 # Configuration loaded

Redis is running...

[root@slave ~]# cat /usr/local/redis/redis.conf

281 slaveof 192.168.16.3 6379

289 masterauth 123123

434 slave-priority 100【俩个从服务器的优先级是一样的,不用修改】

    appendonly yes【这个实验里没有这一项,如果不用此脚本,用默认redis.conf的配置文件就得加这一项】

  • slave查看服务

[root@slave ~]# netstat -anpt|grep redis

tcp        0      0 192.168.16.7:6379       0.0.0.0:*               LISTEN      21791/redis-server 

tcp        0      0 192.168.16.7:42394      192.168.16.3:6379

 

  • slave2查看服务

[root@slave2 ~]# netstat -anpt|grep redis

tcp        0      0 192.168.16.8:6379       0.0.0.0:*               LISTEN      14606/redis-server 

tcp        0      0 192.168.16.8:37579      192.168.16.3:6379       ESTABLISHED 14606/redis-server

 

  • master查看服务

[root@master redis]# netstat -anpt|grep redis

tcp        0      0 192.168.16.3:6379       0.0.0.0:*               LISTEN      21339/redis-server 

tcp        0      0 192.168.16.3:6379       192.168.16.7:42394      ESTABLISHED 21339/redis-server 

tcp        0      0 192.168.16.3:6379       192.168.16.8:37579      ESTABLISHED 21339/redis-server

 

  • 验证主从节点主从同步

[root@master ~]# redis -h 192.168.16.3 -a 123123 -p 6379

192.168.16.3:6379> set name xiaoer

OK

192.168.16.3:6379> get xiaoer

(nil)

192.168.16.3:6379> get name

"xiaoer"

192.168.16.3:6379> quit

[root@slave ~]# redis -h 192.168.16.7 -a 123123 -p 6379【slave只能读。不能写】

192.168.16.7:6379> set name hao

(error) READONLY You can't write against a read only slave.

192.168.16.7:6379> get name

"xiaoer"

192.168.16.7:6379> quit

[root@slave2 ~]# redis -h 192.168.16.8 -a 123123 -p 6379

192.168.16.8:6379> set name lili

(error) READONLY You can't write against a read only slave.

192.168.16.8:6379> get name

"xiaoer"

192.168.16.8:6379> quit

 

  • 只能有一个名字,创建新的就得会被覆盖

[root@slave2 ~]# redis -h 192.168.16.3 -a 123123 -p 6379

192.168.16.3:6379> get name

"ff"

192.168.16.3:6379> set name gg

OK

192.168.16.3:6379> get name

"gg"

192.168.16.3:6379> quit

 

  • master节点上启动redis sentinel

[root@master ~]# vi /usr/local/redis/sentinel.conf

21 port 26379

22 dir  "/var/redis/data"

69 sentinel monitor mymaster 192.168.16.3 6379 2

98 sentinel down-after-milliseconds mymaster 30000

71 sentinel auth-pass mymaster 123123

72 sentinel config-epoch mymaster 1

131 sentinel failover-timeout mymaster 180000

 [root@master ~]# /usr/local/redis/redis-sentinel /usr/local/redis/sentinel.conf

21654:X 26 May 13:27:26.891 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

21654:X 26 May 13:27:26.891 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=21654, just started

21654:X 26 May 13:27:26.891 # Configuration loaded

21654:X 26 May 13:27:26.892 * Increased maximum number of open files to 10032 (it was originally set to 1024).

              _._                                                 

           _.-``__ ''-._                                            

              _.-``    `.  `_.  ''-._             Redis 4.0.9 (00000000/0) 64 bit

  .-`` .-```.  ```\/    _.,_ ''-._                                  

 (    '      ,       .-`  | `,    )     Running in sentinel mode

 |`-._`-...-` __...-.``-._|'` _.-'|             Port: 26379

 |    `-._   `._    /     _.-'    |     PID: 21654

  `-._    `-._  `-./  _.-'    _.-'                                  

 |`-._`-._    `-.__.-'    _.-'_.-'|                                  

 |    `-._`-._        _.-'_.-'    |           http://redis.io       

  `-._    `-._`-.__.-'_.-'    _.-'                                  

 |`-._`-._    `-.__.-'    _.-'_.-'|                                 

 |    `-._`-._        _.-'_.-'    |                                 

  `-._    `-._`-.__.-'_.-'    _.-'                                  

      `-._    `-.__.-'    _.-'                                      

          `-._        _.-'                                          

              `-.__.-'                                              

 

21654:X 26 May 13:27:26.897 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

21654:X 26 May 13:27:26.905 # Sentinel ID is be16e39fa02fc5d4377f78132987fc0e07095275

21654:X 26 May 13:27:26.905 # +monitor master mymaster 192.168.16.3 6379 quorum 2

21654:X 26 May 13:27:26.910 * +slave slave 192.168.16.7:6379 192.168.16.7 6379 @ mymaster 192.168.16.3 6379

21654:X 26 May 13:27:26.913 * +slave slave 192.168.16.8:6379 192.168.16.8 6379 @ mymaster 192.168.16.3 6379

【以下关闭主redis才会出现的数据】

22121:X 26 May 16:23:22.283 # +sdown master mymaster 192.168.16.3 6379

22121:X 26 May 16:23:22.283 # +odown master mymaster 192.168.16.3 6379 #quorum 1/1

22121:X 26 May 16:23:22.283 # +new-epoch 2

22121:X 26 May 16:23:22.283 # +try-failover master mymaster 192.168.16.3 6379

22121:X 26 May 16:23:22.672 # +vote-for-leader be16e39fa02fc5d4377f78132987fc0e07095275 2

22121:X 26 May 16:23:22.672 # +elected-leader master mymaster 192.168.16.3 6379

22121:X 26 May 16:23:22.672 # +failover-state-select-slave master mymaster 192.168.16.3 6379

22121:X 26 May 16:23:22.731 # +selected-slave slave 192.168.16.7:6379 192.168.16.7 6379 @ mymaster 192.168.16.3 6379

22121:X 26 May 16:23:22.731 * +failover-state-send-slaveof-noone slave 192.168.16.7:6379 192.168.16.7 6379 @ mymaster 192.168.16.3 6379

22121:X 26 May 16:23:22.845 * +failover-state-wait-promotion slave 192.168.16.7:6379 192.168.16.7 6379 @ mymaster 192.168.16.3 6379

22121:X 26 May 16:23:23.740 # +promoted-slave slave 192.168.16.7:6379 192.168.16.7 6379 @ mymaster 192.168.16.3 6379

22121:X 26 May 16:23:23.741 # +failover-state-reconf-slaves master mymaster 192.168.16.3 6379

22121:X 26 May 16:23:23.788 * +slave-reconf-sent slave 192.168.16.8:6379 192.168.16.8 6379 @ mymaster 192.168.16.3 6379

22121:X 26 May 16:23:24.767 * +slave-reconf-inprog slave 192.168.16.8:6379 192.168.16.8 6379 @ mymaster 192.168.16.3 6379

22121:X 26 May 16:23:24.767 * +slave-reconf-done slave 192.168.16.8:6379 192.168.16.8 6379 @ mymaster 192.168.16.3 6379

22121:X 26 May 16:23:24.818 # +failover-end master mymaster 192.168.16.3 6379

22121:X 26 May 16:23:24.819 # +switch-master mymaster 192.168.16.3 6379 192.168.16.7 6379

22121:X 26 May 16:23:24.820 * +slave slave 192.168.16.8:6379 192.168.16.8 6379 @ mymaster 192.168.16.7 6379

22121:X 26 May 16:23:24.820 * +slave slave 192.168.16.3:6379 192.168.16.3 6379 @ mymaster 192.168.16.7 6379

22121:X 26 May 16:23:54.842 # +sdown slave 192.168.16.3:6379 192.168.16.3 6379 @ mymaster 192.168.16.7 6379

【恢复master后出现的数据】

22121:X 26 May 16:52:35.226 # -sdown slave 192.168.16.3:6379 192.168.16.3 6379 @ mymaster 192.168.16.7 6379

22121:X 26 May 16:52:45.169 * +convert-to-slave slave 192.168.16.3:6379 192.168.16.3 6379 @ mymaster 192.168.16.7 6379

  • 开启另一通道

[root@master ~]# netstat -anpt|grep redis-sen

tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      21654/redis-sentine

tcp        0      0 192.168.16.3:36434      192.168.16.8:6379       ESTABLISHED 21654/redis-sentine

tcp        0      0 192.168.16.3:59404      192.168.16.3:6379       ESTABLISHED 21654/redis-sentine

tcp        0      0 192.168.16.3:59402      192.168.16.3:6379       ESTABLISHED 21654/redis-sentine

tcp        0      0 192.168.16.3:50386      192.168.16.7:6379       ESTABLISHED 21654/redis-sentine

tcp        0      0 192.168.16.3:50388      192.168.16.7:6379       ESTABLISHED 21654/redis-sentine

tcp        0      0 192.168.16.3:36432      192.168.16.8:6379       ESTABLISHED 21654/redis-sentine

tcp6       0      0 :::26379                :::*                    LISTEN      21654/redis-sentine

 

  • 测试redis sentinel 关闭master节点后,测试集群切换

[root@master ~]# /etc/init.d/redis stop

Stopping ...

Redis stopped

 

  • 查看redis-sentine

[root@slave2 ~]# netstat -anpt|grep redis

tcp        0      0 192.168.16.8:6379       0.0.0.0:*               LISTEN      14981/redis-server 

tcp        0      0 192.168.16.8:37782      192.168.16.7:6379       ESTABLISHED 14981/redis-server 

tcp        0      0 192.168.16.8:6379       192.168.16.3:37910      ESTABLISHED 14981/redis-server 

tcp        0      0 192.168.16.8:6379       192.168.16.3:37908      ESTABLISHED 14981/redis-server

[root@slave ~]# netstat -anpt|grep redis

tcp        0      0 192.168.16.7:6379       0.0.0.0:*               LISTEN      22193/redis-server 

tcp        0      0 192.168.16.7:6379       192.168.16.3:51868      ESTABLISHED 22193/redis-server 

tcp        0      0 192.168.16.7:6379       192.168.16.3:51866      ESTABLISHED 22193/redis-server 

tcp        0      0 192.168.16.7:6379       192.168.16.8:37782      ESTABLISHED 22193/redis-server

[root@master ~]# netstat -anpt|grep redis

tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      22121/redis-sentine

tcp        0      0 192.168.16.3:51866      192.168.16.7:6379       ESTABLISHED 22121/redis-sentine

tcp        0      0 192.168.16.3:37910      192.168.16.8:6379       ESTABLISHED 22121/redis-sentine

tcp        0      0 192.168.16.3:51868      192.168.16.7:6379       ESTABLISHED 22121/redis-sentine

tcp        0      0 192.168.16.3:37908      192.168.16.8:6379       ESTABLISHED 22121/redis-sentine

tcp6       0      0 :::26379                :::*                    LISTEN      22121/redis-sentine

 

  • 验证新的master节点与salve节点之间的主从同步

[root@slave ~]# redis -h 192.168.16.7 -a 123123 -p 6379

192.168.16.7:6379> set en 11

OK

192.168.16.7:6379> keys *

1) "en"

2) "name"

192.168.16.7:6379> get en

"11"

192.168.16.7:6379> quit

[root@slave ~]# redis -h 192.168.16.7 -a 123123 -p 6379

192.168.16.7:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.16.8,port=6379,state=online,offset=91550,lag=1

master_replid:274be87b037e852182432cc8c8dc728f0158e6c7

master_replid2:bf2e29f6b916fb5987131f8b66fbfef565fea778

master_repl_offset:91550

second_repl_offset:1581

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:91550

192.168.16.7:6379> exit

[root@slave2 ~]# redis -h 192.168.16.8 -a 123123 -p 6379

192.168.16.8:6379> keys *

1) "name"

2) "en"

192.168.16.8:6379> get en

"11"

192.168.16.8:6379> info replication

# Replication

role:slave

master_host:192.168.16.7

master_port:6379

master_link_status:up

master_last_io_seconds_ago:0

master_sync_in_progress:0

slave_repl_offset:107009

slave_priority:99

slave_read_only:1

connected_slaves:0

master_replid:274be87b037e852182432cc8c8dc728f0158e6c7

master_replid2:bf2e29f6b916fb5987131f8b66fbfef565fea778

master_repl_offset:107009

second_repl_offset:1581

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:107009

 

  • 恢复master节点,验证集群状态

[root@master ~]# netstat -utpln|grep redis

tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      22121/redis-sentine

tcp6       0      0 :::26379                :::*                    LISTEN      22121/redis-sentine

[root@master ~]# /etc/init.d/redis start

Starting Redis server...

22198:C 26 May 16:52:34.560 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

22198:C 26 May 16:52:34.561 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=22198, just started

22198:C 26 May 16:52:34.561 # Configuration loaded

Redis is running...

[root@master ~]# netstat -utpln|grep redis

tcp        0      0 192.168.16.3:6379       0.0.0.0:*               LISTEN      22199/redis-server 

tcp        0      0 0.0.0.0:26379           0.0.0.0:*               LISTEN      22121/redis-sentine

tcp6       0      0 :::26379                :::*                    LISTEN      22121/redis-sentine

[root@master ~]# redis -h 192.168.16.3 -a 123123 -p 6379

192.168.16.3:6379> info replication

# Replication

role:slave

master_host:192.168.16.7

master_port:6379

master_link_status:down

master_last_io_seconds_ago:-1

master_sync_in_progress:0

slave_repl_offset:1

master_link_down_since_seconds:1558861022

slave_priority:100

slave_read_only:1

connected_slaves:0

min_slaves_good_slaves:0

master_replid:a8b793d6a63482e3e7e6f2b85d21f3032fd570cc

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:0

second_repl_offset:-1

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

192.168.16.3:6379> quit

  • 补充
  • 288 # masterauth

      sed -i '288s/^\(.\).\{29\}/masterauth 123123/g' /usr/local/redis/redis.conf

  • 281 # slaveof

sed -i '281s/^\(.\).\{32\}/slaveof 192.168.100.101 6379/g' /usr/local/redis/redis.conf

  • 报错没有写权限

[root@master ~]# redis -h 192.168.16.3

192.168.16.3:6379> auth 123123

OK

192.168.16.3:6379> set name nou

(error) NOREPLICAS Not enough good slaves to write.

原因:从服务器redis没有启动。主服务器配置文件里设置的从redis没有启动主的redis就没有写入权限

  • port :指定启动服务的端口号;
  • slaveof :配置当前服务成为某redis服务的slave,后面分别是master的ip和端口号
  • name :redis主服务名称,可以自行命名,但是在一个sentinel网络中,一个redis主服务只能有一个名称

主从不会一个会同步,一个不会,查看这从配置文件里这俩masterauth "yunjisuan"        appendonly yes

 

 

你可能感兴趣的:(数据库篇,redis,sentinel)