溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Memcached集群

發(fā)布時間:2020-04-24 05:38:57 來源:網(wǎng)絡(luò) 閱讀:540 作者:無雙戰(zhàn)績 欄目:云計算
配置memcached主緩存節(jié)點和從緩存節(jié)點-----兩臺配置相同
開啟三臺虛擬機:
192.168.80.100      配置memcached主緩存節(jié)點 
192.168.80.101      配置memcached從緩存節(jié)點
192.168.80.102
1.關(guān)閉防火墻機及Selinux[重要](三臺開始都要啟動)
systemctl stop firewalld          //關(guān)閉防火墻
setenforce 0                           //關(guān)閉監(jiān)控
2.安裝環(huán)境包
yum install gcc gcc-c++ make -y

Memcached集群

3.編譯安裝libevent(必須先安裝)
安裝上傳軟件
yum install lrz* -y

Memcached集群

#解壓
tar xf libevent-2.1.8-stable.tar.gz -C /opt
tar xf memcached-1.5.6.tar.gz -C /opt/
#切換到libevent目錄
cd /opt/libevent-2.1.8-stable/

Memcached集群

#配置
./configure --prefix=/usr/local/libevent
#編譯及安裝
make && make install
3.編譯安裝memcached
#切換到memcached
cd /opt/memcached-1.5.6/
#配置(指定libevent路徑)
./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent/
#編譯及安裝
make && make install
4.優(yōu)化啟動(支持復(fù)制功能的Mencached需要安裝后的libevent-2.1.so.6模塊,不然啟動服務(wù)會報錯)
ln -s /usr/local/libevent/lib/libevent-2.1.so.6.0.2  /usr/lib64/libevent-2.1.so.6 
5.分別在兩臺服務(wù)器上啟動memcached
#主服務(wù)器
cd /usr/local/memcached/bin/
./memcached -d -m 32m -l 192.168.80.100 -p 11211 -u root

#從服務(wù)器
cd /usr/local/memcached/bin/
./memcached -d -m 32m -l 192.168.80.101 -p 11211 -u root

6.檢查memcached是否啟動
netstat -anpt | grep memcached 
netstat -ntap | grep 11211

Memcached集群Memcached集群

magent在主服務(wù)器安裝,從服務(wù)器不用安裝
編譯安裝magent
1.創(chuàng)建magent目錄
mkdir /opt/magent
2.解壓
tar xf magent-0.5.tar.gz -C /opt/magent
3.切換到目錄
cd /opt/magent
4.讓動態(tài)鏈接庫為系統(tǒng)所共享
/sbin/ldconfig

Memcached集群

5.編輯ketama.h,添加文件頭部信息
    vi ketama.h
    #ifndef SSIZE_MAX
    #define SSIZE_MAX 32767

Memcached集群

6.修改Makefile
vi Makefile     //libevent安裝路徑在/usr/local/libevent中使用以下內(nèi)容:
LIBS = -levent -lm  -L /usr/local/libevent/lib
INCLUDE=-I /usr/local/libevent/include

Memcached集群

Magent + Keepalived實現(xiàn)Memcached高可用群集
7.編譯(完成后會生成一個manage的可執(zhí)行文件)
make

Memcached集群

8.復(fù)制mgent命令到系統(tǒng)管理中,便于使用
cp /opt/magent/magent /usr/bin/
9.發(fā)送至從服務(wù)器
scp /opt/magent/magent root@192.168.80.101:/usr/bin

Memcached集群

安裝keepalived
yum -y install keepalived
1.編輯keepalived配置文件
 vi /etc/keepalived/keepalived.conf     把里面內(nèi)容刪除添加以下內(nèi)容

! Configuration File for keepalived

global_defs {
      router_id magent_H1         #指定router_id
}

vrrp_script magent {                 #定義函數(shù)
    script "/root/shell/magent.sh"
    interval 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens32             #本地網(wǎng)卡名稱
    virtual_router_id 51       #id主從一致
    priority 100                    #主優(yōu)先級大于從優(yōu)先級
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

track_script {                  #調(diào)用函數(shù)
        magent
    }

virtual_ipaddress {         #vip地址
        192.168.80.188
    }
}

Memcached集群

2.建立shell目錄
mkdir /root/shell
3.創(chuàng)建magent.sh腳本
vi /root/shell/magent.sh
#!/bin/bash

Keepalived=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.100:11211 -b 192.168.80.101:11211
else
        pkill -9 magent
fi
參數(shù)說明:
      -n 51200                #定義用戶最大連接數(shù)
      -l 192.168.175.188                #指定虛擬IP
      -p 12000                   #指定端口號
      -s                              #指定主緩存服務(wù)器
      -b                              #指定從緩存服務(wù)器

Memcached集群

4.賦予腳本執(zhí)行權(quán)限
chmod +x /root/shell/magent.sh
5.啟動keepalived服務(wù)
systemctl enable keepalived
systemctl start keepalived
netstat -anpt | grep 12000

Memcached集群

6.檢查keepalived服務(wù)狀態(tài)
systemctl status keepalived

Memcached集群

7.檢查vip地址
ip addr show ens32

Memcached集群

netstat -anpt | grep 11211

Memcached集群

在從服務(wù)器上操作
1.安裝keepalived
yum -y install keepalived
2.將主服務(wù)器上keepalived配置文件復(fù)制過來
scp root@192.168.80.100:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf 

Memcached集群

3.編輯keepalived配置文件
vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
      router_id magent_H2              #指定router_id
}

vrrp_script magent {                #定義函數(shù)
    script "/root/shell/magent.sh"
    interval 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33             #本地網(wǎng)卡名稱
    virtual_router_id 51        #id主從一致
    priority 90             #主優(yōu)先級大于從優(yōu)先級
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {         #vip地址
        192.168.80.188
    }
    track_script {              #調(diào)用函數(shù)
        magent
    }
}

Memcached集群

4.新建shell目錄
mkdir /root/shell
5.創(chuàng)建magent.sh腳本
vi /root/shell/magent.sh
#!/bin/bash

Keepalived=`ip addr | grep 192.168.80.188 | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.100:11211 -b 192.168.80.101:11211
else
        pkill -9 magent
fi

Memcached集群

6.賦予腳本執(zhí)行權(quán)限
chmod +x /root/shell/magent.sh
7.啟動keepalived服務(wù)
systemctl enable keepalived
systemctl start keepalived

Memcached集群

8.檢查keepalived服務(wù)狀態(tài)
systemctl status keepalived

Memcached集群

9.檢查vip地址
ip addr show ens32

Memcached集群

在客戶端測試
1.安裝telnet客戶端
yum install telnet -y

Memcached集群

2.連接memcached
測試復(fù)制功能
//連接vip進行測試,插入user鍵值
telnet 192.168.80.188 12000
Trying 192.168.80.188...
Connected to 192.168.80.188.
Escape character is '^]'.
set user 0(標(biāo)簽) 0(緩存時間永不過期) 5(緩存內(nèi)容)
12345
STORED
get user
VALUE user 0 5
12345
END
quit
Connection closed by foreign host.

//連接Memcached1,查詢user鍵值,可以獲取,成功!
telnet 192.168.80.100 11211
Trying 192.168.80.100...
Connected to 192.168.80.100.
Escape character is '^]'.
get user
VALUE user 0 5
12345
END
quit
Connection closed by foreign host.

Memcached集群

//連接Memcached2,查詢user鍵值,也可以獲取,成功!
[root@centos7-1 ~]# telnet 192.168.80.101 11211
Trying 192.168.80.101...
Connected to 192.168.80.101.
Escape character is '^]'.
get user
VALUE user 0 5
12345
END
quit
Connection closed by foreign host.

Memcached集群

測試單點故障
停止主服務(wù)器,去連接漂移地址,查看有木有出現(xiàn)內(nèi)容
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI