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

Memcached 簡介

公司主營業(yè)務:成都網(wǎng)站建設、網(wǎng)站建設、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出通川免費做網(wǎng)站回饋大家。

          Memcached 主主復制是指在任意一臺 Memcached 服務器修改數(shù)據(jù)都會被同步到另外一臺,但是Memcached  API 客戶端是無法判斷連接到那一臺 Memcached 服務器的,所以需要設置 VIP (虛擬IP)地址,提供給 Memcached  API 客戶端進行連接??梢允褂?Keepalived 產(chǎn)生 VIP 地址連接主 Memcached 服務器,并提供高可用架構。

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

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

Memcached 高可用架構

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


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


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



名稱
          IP地址操作系統(tǒng)主要軟件包

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.首先安裝所需的編譯環(huán)境,關閉防火墻


[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                                                      //創(chuàng)建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 基本一致,區(qū)別在于從服務器不需要解壓安裝 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 (不是數(shù)字1)

將代理插件修改完后,直接 make ,會看到一個可執(zhí)行文件 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 root@192.168.91.150:/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.
root@192.168.91.150'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 {                                              //添加函數(shù),名稱為magent,以magnt 為調用
    script "/opt/shell/magent.sh"                         //腳本位置
    interval 2                                                             //檢測腳本的時間間隔2s

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_instance VI_1 {
     state MASTER                    //主服務器狀態(tài)為 : MASTER
     interface ens33                  //網(wǎng)卡名稱 改為 ens33  (centos 7)
     virtual_router_id 51         //虛擬路由ID ,主從不能相同
     priority 100                         //優(yōu)先級,主的高于從的
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }  
      track_script {
         magent                     //調用函數(shù)名稱 magent

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

}

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

[root@master magent]# cd /etc/keepalived/
[root@master keepalived]# scp keepalived.conf root@192.168.91.150:/etc/keepalived/
root@192.168.91.150'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                                 //狀態(tài)為 BACKUP
     interface ens33
     virtual_router_id 52                   //虛擬路由ID 與主不同
     priority 90                                     //優(yōu)先級小于主

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

[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

其中參數(shù)解釋如下

-n   51200      // 定義用戶最多連接數(shù)

-l    192.168.91.188    //指定虛擬IP

-p   12000           //指定端口

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

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

8.給執(zhí)行權限,啟動 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    

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

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

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

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

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

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

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 緩存數(shù)據(jù)庫,需要安裝 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 '^]'.                                //進入緩存數(shù)據(jù)庫
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                  //添加一條鍵值數(shù)據(jù)
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                      //查詢鍵值數(shù)據(jù)
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 是分布式內存對象緩存系統(tǒng),因為所有數(shù)據(jù)都存儲在內存中,從而通常用于網(wǎng)站加速。

(2)Memcached 分布式實現(xiàn)不是在服務端實現(xiàn)的而是在客戶端實現(xiàn)的。

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

本文標題:Memcached主主復制+Keepalived高可用架構(內附軟件包)
地址分享:http://muchs.cn/article22/jopojc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、ChatGPT軟件開發(fā)、Google搜索引擎優(yōu)化、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營