Magent緩存代理介紹
成都創(chuàng)新互聯(lián)公司致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營(yíng)銷,包括做網(wǎng)站、成都網(wǎng)站制作、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營(yíng)銷策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營(yíng)銷等。成都創(chuàng)新互聯(lián)公司為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,成都創(chuàng)新互聯(lián)公司核心團(tuán)隊(duì)十年專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。因?yàn)镸emcached服務(wù)器與服務(wù)器之間沒有任何通訊,所以當(dāng)任何服務(wù)器節(jié)點(diǎn)出現(xiàn)故障時(shí),會(huì)出現(xiàn)單點(diǎn)故障。通過Magent緩存代理,防止單點(diǎn)現(xiàn)象。通過客戶端連接到緩存代理服務(wù)器,緩存代理服務(wù)器可以連接多臺(tái)Memcached機(jī)器并同時(shí)將每臺(tái)Memcached機(jī)器進(jìn)行數(shù)據(jù)同步。如果其中一臺(tái)緩存服務(wù)器down機(jī),系統(tǒng)依然可以繼續(xù)工作,如果其中一臺(tái)Memcached機(jī)器down掉,數(shù)據(jù)不會(huì)丟失并且可以保證數(shù)據(jù)的完整性。
原理演示圖:
實(shí)驗(yàn)環(huán)境介紹:
三臺(tái)服務(wù)器:
master/slave/client
實(shí)驗(yàn)步驟
在master和slave上同時(shí)安裝libevent、memcached
master&&slave:
systemctl stop firewalld.service
setenforce 0
tar zxvf memcached-1.5.6.tar.gz -C /opt
tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
yum install gcc gcc-c++ -y
cd /opt/libevent-2.1.8-stable/
./configure --prefix=/usr/local
make && make install
cp /usr/local/lib/libevent-2.1.so.6 /usr/lib64/
cd /opt/memcached-1.5.6/
./configure --with-libevent=/usr/local
make && make install
在master上安裝配置magent
master:
mkdir /opt/magent
tar zxvf magent-0.5.tar.gz -C /opt
vim /opt/magent/ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
vim /opt/magent/Makefile
LIBS = -levent -lm 此處為英文單詞‘l’,不是1
make
將magent命令復(fù)制到slave服務(wù)器上
注:標(biāo)記地方為需要更改的位置
yum install openssl-clients -y
cp magent /usr/bin
scp magent root@192.168.218.157:/usr/bin
yum install keepalived -y
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id MAGENT_HA
}
vrrp_script magent {
script "/opt/shell/magent.sh" 腳本位置
interval 2 檢測(cè)腳本為2秒
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { 調(diào)用vrrp_script magent
magent
}
virtual_ipaddress {
192.168.218.16
}
}
從服務(wù)器上安裝配置keepalived
slave:
yum install openssl-clients -y
yum install keepalived -y
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bk
master:
cd /etc/keepalived/
scp keepalived.conf root@192.168.218.157:/etc/keepalived/keepalived.conf
slave:
vim /etc/keepalived/keepalived.conf
global_defs {
router_id MAGENT_HB
vrrp_instance VI_1 {
state BACKUP
virtual_router_id 52
priority 90
master:
mkdir /opt/shell
vim /opt/shell/magent.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.218.16 -p 12000 -s 192.168.218.130:11211 -b 192.168.218.157:11211 else pkill -9 magent fichmod +x /opt/shell/magent.sh
systemctl start keepalived.servic
netstat -ntap | grep 12000 #此時(shí)檢查應(yīng)有12000端口號(hào)
slave:
mkdir /opt/shell
vim /opt/shell/magent.sh
#!/bin/bash K=`ip addr | grep 192.168.218.16 | grep -v grep | wc -l` if [ $K -gt 0 ];then magent -u root -n 51200 -l 192.168.218.16 -p 12000 -s 192.168.218.130:11211 -b 192.168.218.157:11211 else pkill -9 magent fi
#magent -u指定用戶、-n指定連接數(shù)量、-l指定漂移地址、-p指定端口、-s指定主服務(wù)器地址、-b指定從服務(wù)器地址
chmod +x /opt/shell/magent.sh
systemctl start keepalived.servic
netstat -ntap | grep 12000 #此時(shí)檢查應(yīng)有12000端口號(hào)
master:
memcached -m 512k -u root -d -l 192.168.218.130 -p 11211
netstat -ntap | grep 11211
slave:
memcached -m 512k -u root -d -l 192.168.218.157 -p 11211
netstat -ntap | grep 11211
測(cè)試:
master:
yum install telnet -y
telnet 192.168.218.130 11211
slave:
yum install telnet -y
telnet 192.168.218.157 11211
client:
yum install telnet -y
telnet 192.168.218.16 12000
Trying 192.168.218.16...
Connected to 192.168.218.16.
Escape character is '^]'.
add username 0 0 7 #寫入一段數(shù)據(jù)
1231231
STORED
slave:
telnet 192.168.218.157 11211
Trying 192.168.218.157...
Connected to 192.168.218.157.
Escape character is '^]'.
get username
VALUE username 0 7
1231231 #在主服務(wù)器和從服務(wù)器上可以看到同步過來的數(shù)據(jù)
END
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。