1. 程式人生 > >centos7.4搭建Memcached(2)

centos7.4搭建Memcached(2)

memcached magent keepalived

單臺memcached服務的安裝和測試很簡單,考慮到老生常談的單點故障問題,可以使用
magent和keepalived加以解決
magent:把一臺memcached服務端的數據實時備份到另一臺,互備
keepalived:一臺服務器down,通過VIP調度到另一臺up的服務器,實現高可用


本例:
主memcached服務器1 192.168.80.80
從memcached服務器2 192.168.80.81
主從都要安裝:memcached-1.5.6.tar.gz libevent-2.1.8-stable.tar.gz magent-0.5.tar.gz
測試機 192.168.80.82


主memcached服務器1 192.168.80.80 配置:

1.首先安裝libevent:
tar xf libevent-2.1.8-stable.tar.gz -C /opt/
cd /opt/libevent-2.1.8-stable
./configure --prefix=/usr/
make && make install

2.然後安裝memcached:
tar xf memcached-1.5.6.tar.gz -C /opt/
cd /opt/memcached-1.5.6
./configure \
--with-libevent=/usr
make && make install

3.最後安裝magent:
mkdir /opt/magent
tar xf magent-0.5.tar.gz -C /opt/magent

cd /opt/magent
vi ketama.h //在文件開頭處新增

#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
……

vi Makefile
LIBS = -levent -lm //第一行末尾加-lm (不是數字1)

make //修改完成後可以編譯了

ls //會產生magent可執行程序

cp magent /usr/bin/ //將編譯好的magent程序復制到PATH路徑中

ln -s /usr/lib/libevent-1.4.so.2.1.2 /usr/lib64/libevent-1.4.so.2 //不可缺少

yum install openssh-clients

scp magent [email protected]:/usr/bin/ //把產生的magent文件直接復制到從服務器。

4.安裝keepalived

yum install keepalived -y

vi /etc/keepalived/keepalived.conf //原來的全部刪除,添加以下

! Configuration File for keepalived

global_defs {
router_id MAGENT_HA //主調度器名
}
vrrp_script magent {
script "/opt/shell/magent.sh" //主magent腳本
interval 2
}
vrrp_instance VI_1 {
state MASTER //主狀態
interface ens33
virtual_router_id 51
priority 99 //主優先級
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
magent //跟蹤magent腳本
}
virtual_ipaddress {
192.168.80.188 //虛擬ip VIP
}
}

scp keepalived.conf [email protected]:/etc/keepalived/keepalived.conf ./ //把主服務器的配置文件復制到從,並相應修改

5.制作主跟蹤腳本
mkdir /opt/shell
cd /opt/shell

vi 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.80.188 -p 12000 -s 192.168.80.81:11211 -b 192.168.80.82:11211
else
pkill -9 magent
fi

-n 51200 //定義用戶最大連接數
-l 192.168.80.188 //指定虛擬IP
-p 12000 //指定端口號
-s //指定主緩存服務器
-b //指定從緩存服務器

chmod +x magent.sh

service keepalived start
netstat -anpt | grep 12000 //確認magent運行
cat /var/log/messages //驗證主從

……
Transition to MASTER STATE
……

ip addr //確認漂移地址生效

6.memcached -m 512k -u root -d -l 192.168.80.80 -p 11211 //啟動主
netstat -anptu | grep 11211


從memcached服務器2 192.168.80.81 配置:

1.首先安裝libevent:
tar xf libevent-2.1.8-stable.tar.gz -C /opt/
cd /opt/libevent-2.1.8-stable
./configure --prefix=/usr/
make && make install

2.然後安裝memcached:
tar xf memcached-1.5.6.tar.gz -C /opt/
cd /opt/memcached-1.5.6
./configure \
--with-libevent=/usr
make && make install

ln -s /usr/lib/libevent-1.4.so.2.1.2 /usr/lib64/libevent-1.4.so.2 //不可缺少

3.先安裝keepalived
yum install keepalived -y
後修改主傳過來的配置文件
vi /etc/keepalived/keepalived.conf
router_id MAGENT_HB //修改調度器名

state BACKUP //修改狀態

priority 90 //修改優先級

4.制作從跟蹤腳本
mkdir /opt/shell
cd /opt/shell

vi magent.sh

#!/bin/bash
#!/bin/bash
K=ip addr | grep 192.168.80.188 | grep -v grep | wc -l
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.80:11211 -b 192.168.80.81:11211
else
pkill -9 magent
fi

chmod +x magent.sh

service keepalived start
cat /var/log/messages //驗證主從

……
Entering BACKUP STATE
……

5.
memcached -m 512k -u root -d -l 192.168.80.81 -p 11211 //啟動從
netstat -anptu | grep 11211


下面進行測試:

1.在客戶端80.82上,首先測試magent是否備份:
telnet 192.168.80.188 12000 //連接VIP漂移地址,新建數據

set key 33 0 4
aaaa
STORED
quit

telnet 192.168.80.188 12000 //連接VIP漂移地址,查看數據
get key
VALUE key 33 4
aaaa
END

telnet 192.168.80.81 11211 //連接到主memcached上,查看數據,應該會有新建的數據
get key
VALUE key 33 4
aaaa
END

telnet 192.168.80.83 11211 //連接到從memcached上,查看數據,應該會有新建的數據
get key
VALUE key 33 4
aaaa
END

2.測試keepalived
主80.81 service keepalived stop,通過ip addr查看80.188應該漂移到從80.82上
且telnet 192.168.80.188 12000 //連接VIP漂移地址,還能查看數據,說明業務切換到從上,實現了memecached業務不中斷

centos7.4搭建Memcached(2)