redis两主两从

  1. 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
  2. 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
    1. 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。

接下来我们准备两台机器,ip分别是
192.168.124.148
192.168.124.82

安装所需要的环境和工具
mkdir  -p  /data/server
cd /data/server
yum -y install wget vim tcl gcc make
下载redis安装包

wget http://download.redis.io/releases/redis-3.2.10.tar.gz
tar -zxvf redis-3.2.10.tar.gz
解压之后把之前安装的包移动到/usr/local/src目录下
cd redis-3.2.10
编译源文件
make
cd src
make install
测试一下:make test
(如果 /usr/local/bin/ 文件夹内没有 redis-server 那几个文件,就从 /data/server/redis-3.2.10/src/ 中拷贝过去,命令:cp redis-server redis-cli redis-sentinel redis-benchmark redis-check-aof redis-check-rdb /usr/local/bin/)

创建多实例的文件夹,用来存放不同实例的配置文件

cd /data/server

mkdir cluster

cd cluster

mkdir 6379 6380 我们这里创建两个文件夹6379是主6380是从

```拷贝一下配置文件 148这台机器是老大哥不需要哨兵所以就不拷贝了

cp /data/server/redis-3.2.10/redis.conf /data/server/cluster/6379
cp /data/server/redis-3.2.10/redis.conf /data/server/cluster/6380
cp /data/server/redis-3.2.10/sentinel.conf /data/server/cluster/6380


+++++++++++++++++++++++++++++++++++++以上配置另外一台机器同步
配置192.168.124.148机器
cd /data/server/cluster/6379
![](https://s1.51cto.com/images/blog/201908/14/a24649461adf06ec1d0a972a2bb02d44.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

按照如图所配置 (个人把注释和空行都去掉了)

![](https://s1.51cto.com/images/blog/201908/14/731e75c1d61e4120e982a81fc5a6e3ff.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

cd /data/server/server/cluster/6380
![](https://s1.51cto.com/images/blog/201908/14/b2c9ebd0cde555037e67f0f937a8793c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

按照如图所配置 (个人把注释和空行都去掉了)
![](https://s1.51cto.com/images/blog/201908/14/c0377362876d5a71bf39d432d303bf9f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

配置一下哨兵

![](https://s1.51cto.com/images/blog/201908/14/9af26c844c006ab9a2b51e7a45c1600e.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

3个哨兵的配置文件都是一样的  (老大哥可以没有哨兵)
![](https://s1.51cto.com/images/blog/201908/14/82241e03c89d2d73109c65613d530871.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

配置192.168.124.82机器
cd /data/server/cluster/6379

![](https://s1.51cto.com/images/blog/201908/14/a24649461adf06ec1d0a972a2bb02d44.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

按照如图所配置 (个人把注释和空行都去掉了)(备主配置)
![](https://s1.51cto.com/images/blog/201908/14/c8d54eeb1631f7acedd9412c66ada133.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

配置一下哨兵
cd /data/server/server/cluster/6379
![](https://s1.51cto.com/images/blog/201908/14/c87073ded66fa7d0a5f921113b78e53a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

![](https://s1.51cto.com/images/blog/201908/14/82241e03c89d2d73109c65613d530871.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

cd /data/server/cluster/6380
![](https://s1.51cto.com/images/blog/201908/14/b2c9ebd0cde555037e67f0f937a8793c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

按照如图所配置 (个人把注释和空行都去掉了)

![](https://s1.51cto.com/images/blog/201908/14/c0377362876d5a71bf39d432d303bf9f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

配置哨兵
cd /data/server/server/cluster/6379

![](https://s1.51cto.com/images/blog/201908/14/c87073ded66fa7d0a5f921113b78e53a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

++++++++++++++++++++

接下来我们开启redis和哨兵

192.168.124.148
redis-server /data/server/cluster/6379/redis.conf
redis-server /data/server/cluster/6380/redis.conf
redis-sentinel /data/server/cluster/6380/sentinel.conf

![](https://s1.51cto.com/images/blog/201908/14/0fe45d8178ee6ee8523304708945a228.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

192.168.124.82
redis-server /data/server/cluster/6379/redis.conf
redis-server /data/server/cluster/6380/redis.conf
redis-sentinel /data/server/cluster/6379/sentinel.conf
redis-sentinel /data/server/cluster/6380/sentinel.conf

![](https://s1.51cto.com/images/blog/201908/14/a1069739b53f682b477f5f904a4df346.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

关闭防火墙
systemctl stop firewalld
setenforce 0

接下来我们做下测试
两台机器看下服务都启动了没
ps aux | grep redis
再看下连接上了没
![](https://s1.51cto.com/images/blog/201908/14/c09eb69b5e642e0ae4f9eb4f8cd38fc3.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
先把redis写到环境变量里
![](https://s1.51cto.com/images/blog/201908/14/77fb2621a87f2591aa0aea346a885457.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

source /etc/profile  重读配置文件

redis-cli -h 192.168.124.148 -p 6379
![](https://s1.51cto.com/images/blog/201908/14/366a6a839aaff4c638f2d1c40736fd3e.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

我们杀掉老大
![](https://s1.51cto.com/images/blog/201908/14/c150fc1ce9467923152c4a465c1a75c5.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

查看另外一台备主
![](https://s1.51cto.com/images/blog/201908/14/f3f5930dadbaf5b45ba38bd211649fba.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

发现备主上位了

现在开老大
把备主杀掉会发现老大又上位了
所以主只会在这两切换
这就是两主两从了

猜你喜欢

转载自blog.51cto.com/14354119/2429391