前言
Memcached雖然能夠通過分佈式緩存,實現其中memcached宕掉不會丟失全部緩存數據,但部分數據還是會有缺失,本文主要利用magent代理來實現memcached主從備份來保證緩存數據完整性
實驗環境
IP地址部署
主機名 | IP地址 |
---|---|
master | 192.168.7.128 |
backup | 192.168.7.129 |
客戶端 | 192.168.7.134 |
實驗步驟
1、主服務器配置
(1)安裝環境包
[root@master memcached]# yum install gcc gcc-c++ make -y
(2)安裝事件庫
[root@master memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
[root@master memcached]# cd /opt/libevent-2.1.8-stable/
[root@master libevent-2.1.8-stable]# ./configure --prefix=/usr
[root@master libevent-2.1.8-stable]# make && make install
(3)安裝memcached
[root@master memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt
[root@master memcached]# cd /opt/memcached-1.5.6/
[root@master memcached-1.5.6]# ./configure --with-libevent=/usr
[root@master memcached-1.5.6]# make &&make install
(4)安裝magent
[root@master memcached]# mkdir /opt/magent
[root@master memcached]# cd /opt/magent/
[root@master magent]# vim ketama.h
#更改開頭的兩行
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
添加#endif,將刪除最後一行的#endif刪掉
[root@master magent]# vim Makefile
LIBS = -levent -lm
[root@master magent]# make
[root@master magent]# cp magent /usr/bin/
#將/opt/magent/magent文件複製到從服務器上
[root@master magent]# scp magent [email protected]:/usr/bin
(5)安裝keepalived
[root@master ~]# yum install keepalived -y
更改keepalived主配置文件
[root@master ~]# vim /etc/keepalived/keepalived.conf
在第二行添加
vrrp_script magent {
script "/opt/shell/magent.sh" //此腳本手動添加
interval 2
}
global_defs {
···
router_id MAGENT_HA //從服務器改爲MAGET_HB
}
vrrp_instance VI_1 {
state MASTER //從服務器改爲BACKUP
interface ens33
virtual_router_id 51 //從服務器ID不同
priority 100 //從服務器優先級比主服務要低
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#添加
track_script {
magent
}
virtual_ipaddress {
192.168.7.188 //定義漂移地址
}
}
#剩下的全部刪除
[root@master ~]# mkdir /opt/shell
[root@master ~]# cd /opt/shell/
[root@master shell]# vim manage.sh
#!/bin/bash
K=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.7.188 -p 12000 -s 192.168.7.128:11211 -b 192.168.7.129:11211
else
pkill -9 magent
fi
[root@master shell]# chmod +x manage.sh
[root@master shell]# systemctl start keepalived
2、從服務器與主服務器安裝一致
3、客戶端測試
[root@client ~]# telnet 192.168.7.188 12000
Trying 192.168.7.188...
Connected to 192.168.7.188.
Escape character is '^]'.
add user 0 0 4
test
STORED
get user
VALUE user 0 4
test
END
4、關閉主服務器,可正常連接
[root@client ~]# telnet 192.168.7.188 12000
Trying 192.168.7.188...
Connected to 192.168.7.188.
Escape character is '^]'.
get user
VALUE user 0 4
test
END
add bao 0 0 1
2
STORED
get bao
VALUE bao 0 1
2
END