實驗:多實例節點 10.220.5.65(三個實例)redis
高可用節點:10.220.5.67vim
建立目錄,將三個實例文件都放在這個目錄中spa
[root@65 ~]# yum install redis -y
[root@65~]# mkdir /redis/{6379,6380,6381} -pv
查看目錄日誌
[root@65 ~]# ls /redis 6379 6380 6381 <<<三個實例所對應的端口號,爲了方便理解這裏每一個文件名和對應的端口號一致
將/etc/redis.conf文件複製到每個目錄下code
[root@65DRslave 6381]# cp /etc/redis.conf /redis/6379 [root@65DRslave 6381]# cp /etc/redis.conf /redis/63780 [root@65DRslave 6381]# cp /etc/redis.conf /redis/63781
修改每個配置文件server
[root@65DRslave 6381]# cp /etc/redis.conf /redis/6379
80 protected-mode yes 84 port 6379 128 daemonize yes <<<啓用後臺運行模式 150 pidfile /redis/6379/redis_6379.pid 163 logfile /redis/6379/redis.log <<<日誌文件 200 # save "" 201 202 save 900 1 203 save 300 10 204 save 60 10000 247 dir /redis/6379
同理剩下兩個配置文件和上邊相似,只需該對應端口便可blog
啓動實例ip
[root@65 ~]# redis-server /redis/6379/redis.conf [root@65 ~]# redis-server /redis/6380/redis.conf [root@65~]# redis-server /redis/6381/redis.conf
[root@65DRslave ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 12 128 *:6379 *:* LISTEN 0 128 10.220.5.65:6380 *:* LISTEN 0 128 10.220.5.65:6381 *:*
打開多個終端,分別登錄實例get
[root@65 ~]# redis-cli -h 10.220.5.65 -p 6379
10.220.5.65:6379> [root@65 ~]# redis-cli -h 10.220.5.65 -p 6380
10.220.5.65:6380> [root@65 ~]# redis-cli -h 10.220.5.65 -p 6381
10.220.5.65:6381>
主從複製,讓6381做爲主,6379 和6380做爲從
10.220.5.65:6380> slaveof 10.220.5.65 6381
OK
10.220.5.65:6379> slaveof 10.220.5.65 6381 OK
在主端建立數據,在從節點查看是否被複制了過來
10.220.5.65:6381> set age 29 OK <<<主端
10.220.5.65:6379> get age "29" <<<從端,說明已經複製
10.220.5.65:6380> get age "29" <<<從端,說明已經複製
主從複製已經作好,下面作redis的高可用it
redis的高可用是用redis-sentinel,默認在redis中,須要修改配置文件
[root@67 ~]# vim /etc/redis-sentinel.conf
17 protected-mode no 98 sentinel monitor mymaster 10.220.5.65 6381 1 131 sentinel config-epoch mymaster 1
啓動redis-sentinel
[root@67 ~]# systemctl start redis-sentinel <<<端口號爲 26379
[root@67 ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:26379 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 :::26379 :::* LISTEN 0 128 :::22 :::*
登陸redistribute-sentinel,並查看主從節點信息
[root@67 ~]# redis-cli -h 10.220.5.67 -p 26379 10.220.5.67:26379>sentinel masters <<<主節點信息
1) 1) "name"
2) "mymaster"
3) "ip"
4) "10.220.5.65"
5) "port"
6) "6381"
.......
10.220.5.67:26379>sentinel slaves master <<<從節點信息
1) 1) "name"
2) "10.220.5.65:6380"
3) "ip"
4) "10.220.5.65"
5) "port"
6) "6380"
....
2) 1) "name"
2) "10.220.5.65:6379"
3) "ip"
4) "10.220.5.65"
5) "port"
6) "6379"
....
將主節點停掉,再看效果
[root@65DRslave ~]# ps aux |grep redis
redis 1169 0.3 0.7 142952 7320 ? Ssl 16:07 0:11 /usr/bin/redis-server 10.220.5.65:6379
root 1255 0.0 0.0 107988 612 pts/2 T 16:12 0:00 tail -f /redis/6381/redis.log
root 1288 0.3 0.7 142952 7504 ? Ssl 16:18 0:10 redis-server 10.220.5.65:6381
root 1298 0.0 0.5 22128 5212 pts/1 S+ 16:19 0:00 redis-cli -h 10.220.5.65 -p 6379
root 1338 0.3 0.5 142952 5440 ? Ssl 17:04 0:00 redis-server 10.220.5.65:6380
root 1344 0.0 0.5 22124 5128 pts/0 S+ 17:04 0:00 redis-cli -h 10.220.5.65 -p 6380
root 1346 0.0 0.0 112704 956 pts/2 R+ 17:04 0:00 grep --color=auto redis
[root@65DRslave ~]# kill -9 1288
在redis-sentinel端再次查看
10.220.5.67:26379> sentinel masters 1) 1) "name" 2) "mymaster" 3) "ip" 4) "10.220.5.65" 5) "port" 6) "6379" <<<這次主節點變成了 6379 ,這就是redis的高可用
查看從節點信息
10.220.5.67:26379> sentinel slaves mymaster 1) 1) "name" 2) "10.220.5.65:6381" 3) "ip" 4) "10.220.5.65" 5) "port" 6) "6381"
10) "s_down,slave,disconnected" <<<6381停掉後變成了從,狀態是未鏈接 2) 1) "name" 2) "10.220.5.65:6380" 3) "ip" 4) "10.220.5.65" 5) "port" 6) "6380"