memcached+keepalived+magent高羣集架構——(實戰!)

簡述

 magent是一款開源的代理服務軟件,我們可以通過它來實現緩存數據的同步,當然這裏說的同步不是說memcached之間就能互相通訊了, 而magent可以同時連接多個memcached節點, 通過magent綁定的VIP從客戶端登錄memcached寫入數據,其他節點的memcached數據也會同步。

實驗環境

memcached主 192.168.13.128 (magent 、memcached 、libevent 、keeplived)
memcached從 192.168.13.129 (memcached 、 libevent 、keeplived)
client客戶端 192.168.13.130 (telnet 測試工具)
虛擬ip 192.168.13.100

1,配置memcached主緩存節點和從緩存節點(兩者配置相同,從中不需要安裝magent)

[root@master ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt/
Password for root@//192.168.100.3/LNMP-C7:  
[root@master ~]# cd /mnt/memcached/
[root@master memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt/
[root@master memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/  ##事件庫
[root@master memcached]# mkdir /opt/magent
[root@master memcached]# tar zxvf magent-0.5.tar.gz -C /opt/magent/
[root@master memcached]# yum install gcc gcc-c++ make -y
[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]# cd ../memcached-1.5.6/
[root@master memcached-1.5.6]# ./configure \
> --with-libevent=/usr
[root@master magent]# systemctl stop firewalld.service 
[root@master magent]# setenforce 0

2,配置主服務器,安裝magent代理

[root@master memcached-1.5.6]# cd /opt/magent/
[root@master magent]# vim ketama.h  ##修改magent配置文件
##首行修改添加
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif //此項如果有就不需要添加
[root@master magent]# vim Makefile  ##編輯Makefile配置文件
##查找此項後面添加-lm
LIBS = -levent -lm
[root@master magent]# make ##編譯後產生一個magent可執行程序
[root@master magent]# yum install openssh-clients -y  
[root@master magent]# cp magent /usr/bin/  ##放到/usr/bin中
[root@master magent]# scp magent [email protected]:/usr/bin/  
 ##拷貝到從服務器/usr/bin中

3,在主從服務器上配置安裝keepalived

[root@master magent]# yum install keepalived -y  ##安裝keepalived服務
[root@master magent]# vim /etc/keepalived/keepalived.conf  ##修改配置文件
//定義一個函數,建議寫在最前面(主服務器配置)
vrrp_script magent {
                script "/opt/shell/magent.sh"
                interval 2
            }

做如下修改:
router_id MAGENT_HA        //修改id名
interface ens33            //修改網卡信息

virtual_ipaddress {
                192.168.13.100     //定義好虛擬ip地址
        }   

vrrp_instance VI_1 {
.....
//調用函數.以下三行代碼寫在vrrp模塊內
track_script {
                magent
            }

##從服務器上配置如下(可通過scp直接拷貝主服務器配置文件到從服務器)
router_id MAGENT_HB         //id名和第一臺要不一樣
state BACKUP               //從服務器
virtual_router_id 52       //id號和第一臺不一樣
priority 90                 //優先級低與主服務器 
[root@master keepalived]# mkdir /opt/shell
[root@master keepalived]# cd /opt/shell/
[root@master shell]# vim magent.sh   ##編輯magent腳本
#!/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.13.100 -p 12000 -s 192.168.13.128:11211 -b 192.168.13.129:11211
else
pkill -9 magent
fi

//
-n 51200             //定義用戶最大連接數
-l 192.168.13.100   //指定虛擬IP
-p 12000             //指定端口號
-s                   //指定主緩存服務器
-b                   //指定從緩存服務器
[root@master shell]# chmod +x magent.sh   ##執行權限
[root@master shell]# systemctl start keepalived.service   ##開啓服務
[root@master shell]# netstat -natp | grep 12000  ##查看端口
##驗證主從
主服務器 ----- 查看 /var/log/messages 文件,找到關鍵詞:Transition to MASTER STATE
從服務器 ----- 找到關鍵詞:Entering BACKUP STATE
ip addr 命令 ----- 確定漂移地址生效

4,開啓主從服務器memcache

主服務器:
[root@master shell]# memcached -m 512k -u root -d -l 192.168.13.128 -p 11211   
[root@master shell]# netstat -natp | grep 11211
從服務器:
[root@slave shell]# memcached -m 512k -u root -d -l 192.168.13.129 -p 11211 
[root@slave shell]# netstat -ntap | grep 11211

5,用客戶端登錄

[root@client ~]# yum install telnet -y  ##安裝登錄工具
[root@client ~]# telnet 192.168.13.100 12000  ##用虛擬ip登錄
Trying 192.168.13.100...
Connected to 192.168.13.100.
Escape character is '^]'.
add username 0 0 7   ##添加一個數據
1234567
STORED

6,查看是否主從同步

##在主服務器上查看是否有寫入的數據
[root@master shell]# telnet 192.168.13.128 11211
Trying 192.168.13.128...
Connected to 192.168.13.128.
Escape character is '^]'.
get username
VALUE username 0 7
1234567
END
##在從服務器上查看是否有寫入的數據
[root@slave shell]# telnet 192.168.13.129 11211
Trying 192.168.13.129...
Connected to 192.168.13.129.
Escape character is '^]'.
get username
VALUE username 0 7
1234567
END

7,宕機主服務器,查看是否可用,實現高可用

[root@master shell]# systemctl stop keepalived.service
[root@client ~]# telnet 192.168.13.100 12000  ##客戶端仍然可以登錄
Trying 192.168.13.100...
Connected to 192.168.13.100.
Escape character is '^]'.

謝謝閱讀!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章