Memcached主主複製 + Keepalived 高可用架構(內附軟件包)

Memcached 簡介

          Memcached 主主複製是指在任意一臺 Memcached 服務器修改數據都會被同步到另外一臺,但是Memcached  API 客戶端是無法判斷連接到那一臺 Memcached 服務器的,所以需要設置 VIP (虛擬IP)地址,提供給 Memcached  API 客戶端進行連接。可以使用 Keepalived 產生 VIP 地址連接主 Memcached 服務器,並提供高可用架構。

          Memcached 的複製功能支持多個 Memcached 之間進行相互複製(雙向複製,主備都是可讀可寫的),可以解決 Memcached 的容災問題。

         Keepalive 不斷檢測 Memcached 主服務器的 11211 端口,如果檢測到 Memcached 服務器發生宕機或者死機等情況,就會將 VIP 從主服務器移至從服務器,從而實現 Memcached 的高可用性

Memcached 高可用架構

4


軟件包連接:鏈接:https://pan.baidu.com/s/10yic_9NDmhBbWCVhERlgPw 密碼:gf1l


實驗環境:兩臺 Memcached 服務器 和一臺客戶機,如下所示。



名稱
          IP地址操作系統主要軟件包

Memached 1(主)

192.168.91.148

VIP :192.168.91.188

Centos 7libevent-2.1.8-stable.tar.gz
memcached-1.5.6.tar.gz
 magent-0.5.tar.gz
Memached 2(從)192.168.91.150
VIP :192.168.91.188
Centos 7libevent-2.1.8-stable.tar.gz
memcached-1.5.6.tar.gz
客戶端192.168.91.149Centos 7


搭建 Memcached 主主複製架構


1.首先安裝所需的編譯環境,關閉防火牆


[root@localhost ~]# systemctl stop firewalld.service 

[root@localhost ~]# setenforce 0

[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/               //libevent 事件通知庫

[root@master memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt/

[root@master memcached]# mkdir /opt/magent                                                      //創建magent 目錄
[root@master memcached]# tar zxvf magent-0.5.tar.gz -C /opt/magent/
ketama.c          
magent.c
ketama.h                //magent 代理插件
Makefile

3.先編譯安裝 Libevent, 然後安裝 Memcached

[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

[root@master libevent-2.1.8-stable]# 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

以上步驟安裝,主從 Memcached 基本一致,區別在於從服務器不需要解壓安裝 Magent 代理。


4.在Memcached 主服務器中修改 magent 代理插件

[root@master memcached-1.5.6]# cd /opt/magent/
[root@master magent]# ls
ketama.c  ketama.h  magent.c  Makefile

[root@master magent]# vim ketama.h

#ifndef SSIZE_MAX                                         //在文件開頭出新增
#define SSIZE_MAX 32767

[root@master magent]# vim Makefile

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

將代理插件修改完後,直接 make ,會看到一個可執行文件 magent

[root@master magent]# make

gcc -Wall -O2 -g  -c -o magent.o magent.c
gcc -Wall -O2 -g  -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm
[root@master magent]# ls
ketama.c  ketama.h  ketama.o  magent magent.c  magent.o  Makefile

5.在 Memcached 主服務器上安裝 openssh-clients 服務,可以將主服務器上的 magent 的配置複製到從服務器上,

[root@master magent]# yum install openssh-clients –y

[root@master magent]# cp magent /usr/bin/                        //先將主服務器上的magent地配置文件複製到 /usr/bin/
[root@master magent]# scp magent [email protected]:/usr/bin/                  //從服務器地址及目錄
The authenticity of host '192.168.91.150 (192.168.91.150)' can't be established.
ECDSA key fingerprint is SHA256:ABSTPGOHvqKvUsfwD/uf5ESPpd×××RjvucRpzMqcUuzI.
ECDSA key fingerprint is MD5:f5:3a:8c:8b:1e:d5:a3:33:24:32:03:2d:4d:3e:e8:68.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.91.150' (ECDSA) to the list of known hosts.
[email protected]'s password:         //從服務器的登錄密碼
magent                                                        100%  112KB   4.3MB/s   00:00
   

6.安裝配置 Keepalive (主從服務器都要安裝)

[root@master magent]# yum install keepalived –y

(1). 配置主 Keepalived

[root@master magent]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

vrrp_script magent {                                              //添加函數,名稱爲magent,以magnt 爲調用
    script "/opt/shell/magent.sh"                         //腳本位置
    interval 2                                                             //檢測腳本的時間間隔2s

global_defs {                          //全局設定
    notification_email {
      [email protected]
      [email protected]
      [email protected]
    }
    notification_email_from [email protected]
    smtp_server 192.168.200.1
    smtp_connect_timeout 30
    router_id MAGENT_HA           //路由表示,主從不能一樣

vrrp_instance VI_1 {
     state MASTER                    //主服務器狀態爲 : MASTER
     interface ens33                  //網卡名稱 改爲 ens33  (centos 7)
     virtual_router_id 51         //虛擬路由ID ,主從不能相同
     priority 100                         //優先級,主的高於從的
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }  
      track_script {
         magent                     //調用函數名稱 magent

}  
     virtual_ipaddress {
         192.168.91.188                     //定義 VIP 地址
     } 

}

(2)配置從 Keepalived,在主服務器上把 keepalied 的配置文件導入從服務器

[root@master magent]# cd /etc/keepalived/
[root@master keepalived]# scp keepalived.conf [email protected]:/etc/keepalived/
[email protected]'s password:
keepalived.conf                                               100%  660     2.0KB/s   00:00

回到從服務器修改 keepalived 的配置文件,主從 Keepalived 配置文件內容差不多,可直接複製進行修改,以下只把不一樣的地方整理出來

[root@localhost keepalived]# vim keepalived.conf

  router_id MAGENT_HB                 //路由表示與主不同
}

vrrp_instance VI_1 {
     state BACKUP                                 //狀態爲 BACKUP
     interface ens33
     virtual_router_id 52                   //虛擬路由ID 與主不同
     priority 90                                     //優先級小於主

7.在主從設置腳本,(在主從Keepalived 中添加的名爲 magent 的函數)

[root@master keepalived]# mkdir /opt/shell
[root@master keepalived]# cd /opt/shell/
[root@master shell]# vim 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.91.188 -p 12000 -s 192.168.91.148:11211 -b 192.168.91.150:11211
else
pkill -9 magent
fi

其中參數解釋如下

-n   51200      // 定義用戶最多連接數

-l    192.168.91.188    //指定虛擬IP

-p   12000           //指定端口

-s                        //指定主緩存服務器

-b                       //指定從緩存服務器

8.給執行權限,啓動 keepalived 服務

[root@master shell]# chmod +x magent.sh
[root@master shell]# systemctl start keepalived.service                  //啓動服務
[root@master shell]# netstat -ntap | grep 12000                               //查看端口,確認 magent  運行
tcp        0      0 192.168.91.188:12000    0.0.0.0:*               LISTEN      47938/magent
       

查看日誌,驗證主從

vim /var/log/messages    

  1

使用 ip  addr 命令確認漂移地址生效 是否生效

2

從服務器上的腳步也是一樣的操作

[root@localhost shell]# netstat -ntap | grep 12000
tcp        0      0 192.168.91.188:12000    0.0.0.0:*               LISTEN      66801/magent
       

3

9.分別在主從服務器上啓動 memcached

[root@master shell]# memcached -m 512k -u root -d -l 192.168.91.148 -p 11211
[root@master shell]# netstat -ntap | grep 11211
tcp        0      0 192.168.91.148:11211    0.0.0.0:*               LISTEN      51398/memcached

    

從服務器

[root@localhost shell]# memcached -m 512k -u root -d -l 192.168.91.150 -p 11211
[root@localhost shell]# netstat -ntap | grep 11211
tcp        0      0 192.168.91.150:11211    0.0.0.0:*               LISTEN      54741/memcached
    

10.測試驗證

(1)在主服務器在自測連接 Memcached 緩存數據庫,需要安裝 telnet

[root@master shell]# yum install telnet –y

[root@master shell]# telnet 192.168.91.148 11211        //自測連接
Trying 192.168.91.148...
Connected to 192.168.91.148.
Escape character is '^]'.                                //進入緩存數據庫
quit
Connection closed by foreign host.

(2)在從服務器上測試

[root@localhost shell]# telnet 192.168.91.150 11211
Trying 192.168.91.150...
Connected to 192.168.91.150.
Escape character is '^]'.
quit
Connection closed by foreign host.
[root@localhost shell]#

(3)在客戶端上安裝 telnet,使用虛擬IP 登錄,並添加語句,看在主從服務器上能否看到

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install telnet –y

[root@localhost ~]# telnet 192.168.91.188 12000
Trying 192.168.91.188...
Connected to 192.168.91.188.
Escape character is '^]'.
add username 0 0 7                  //添加一條鍵值數據
1234567
STORED
quit
Connection closed by foreign host.
[root@localhost ~]#

(4)分佈登錄主從 memcached 查看添加內容

[root@master shell]# telnet 192.168.91.148 11211
Trying 192.168.91.148...
Connected to 192.168.91.148.
Escape character is '^]'.
get username                      //查詢鍵值數據
VALUE username 0 7
1234567
END

[root@localhost shell]# telnet 192.168.91.150 11211
Trying 192.168.91.150...
Connected to 192.168.91.150.
Escape character is '^]'.
get username
VALUE username 0 7
1234567
END

總結:

(1)Memcached 是分佈式內存對象緩存系統,因爲所有數據都存儲在內存中,從而通常用於網站加速。

(2)Memcached 分佈式實現不是在服務端實現的而是在客戶端實現的。

(3)Memcached 可以通過 Keepalived 實現 Memcached 服務的高可靠性。

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