您好,登錄后才能下訂單哦!
這篇文章給大家介紹Memcached高可用群集的示例分析,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
1.在Memcached中可以保存的item數(shù)據(jù)量是沒有限制的,只要內(nèi)存足夠;
2.Memcached單進程最大使用內(nèi)存為2GB,要使用更多內(nèi)存,可以分別在不同端口啟動多個Memcached進程;
3.Memcached是一種無阻塞的socket通信方式的服務(wù),基于libevent庫,由于無阻塞通信,對內(nèi)存讀寫速度非常之快;
4.Memcached分為服務(wù)器和客戶端,可以配置多個服務(wù)器和客戶端,應(yīng)用于分布式的服務(wù)非常廣泛;
5.Memcached作為小規(guī)模的數(shù)據(jù)分布式平臺是非常高效的;
服務(wù)器 | IP地址 | 軟件包 |
---|---|---|
主Memcached | 192.168.5.129 | libevent-2.1.8-stable.tar.gz、memcached-1.5.9.tar.gz、magent-0.5.tar.gz、keepalived |
從Memcached | 192.168.5.139 | libevent-2.1.8-stable.tar.gz、memcached-1.5.9.tar.gz、magent-0.5.tar.gz、keepalived |
Client | 192.168.5.141 | telnet |
Memcached服務(wù)器在兩臺服務(wù)器上都需要搭建,方式完全相同,在安裝memcached服務(wù)器是需先安裝libevent,然后再安裝memcached。
yum -y install gcc gcc-c++ make
#解壓
tar zxvf libevent-2.1.8-stable.tar.gz -C /opt#切換到libevent目錄
cd /opt/libevent-2.1.8-stable/#配置
./configure --prefix=/usr/#編譯及安裝
make && make install
#解壓
tar zxvf memcached-1.5.9.tar.gz -C /opt/#切換到memcached
cd /opt/memcached-1.5.9/#配置(指定libevent路徑)
./configure --with-libevent=/usr/#編譯及安裝
make && make install
ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
magent是一個memcached代理軟件,防止單點故障,緩存代理也可以著備份,通過客戶端連接緩存代理服務(wù)器,緩存代理服務(wù)器連接緩存服務(wù)器。
#創(chuàng)建magent目錄
mkdir /opt/magent
#解壓
tar zxvf magent-0.5.tar.gz -C /opt/magent
#切換到目錄
cd /opt/magent
#讓動態(tài)鏈接庫為系統(tǒng)所共享
/sbin/ldconfig
#編輯ketama.h,添加文件頭部信息
vim ketama.h
#ifndef SSIZE_MAX #define SSIZE_MAX 32767 #endif
#編輯Makefile
vi Makefile
LIBS = -levent -lm
#編譯(完成后會生成一個manage的可執(zhí)行文件)
make
#讓服務(wù)便于系統(tǒng)識別
cp /opt/magent/magent /usr/bin/ scp /opt/magent/magent root@192.168.5.139:/usr/bin/ //發(fā)送到從服務(wù)器上
#主從服務(wù)器安裝keepalived
yum -y install keepalived
#修改主從服務(wù)器配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived 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_H1 //router_id自定義,但是要確保主從一致 } vrrp_script magent { script "/root/shell/magent.sh" interval 2 //定義一個函數(shù),此時該腳本還未創(chuàng)建,要在實例外面定義 } vrrp_instance VI_1 { state MASTER //從為BACKUP interface ens33 virtual_router_id 51 //此處id也相同 priority 100 //主服務(wù)器優(yōu)先級要高于從服務(wù)器 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { magent //應(yīng)用函數(shù) } virtual_ipaddress { 192.168.5.100 //定義vtp自定義,只要不是在使用中的地址即可 } } //配置文件下面還有多余的內(nèi)容此時就可以直接刪除
#建立shell目錄
mkdir /root/shell
#創(chuàng)建magent.sh腳本
vim /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.5.100 -p 12000 -s 192.168.5.129:11211 -b 192.168.5.139:11211 else pkill -9 magent fi
參數(shù)說明:
-n 51200 #定義用戶最大連接數(shù) -l 192.168.5.100 #指定虛擬IP -p 12000 #指定端口號 -s #指定主緩存服務(wù)器 -b #指定從緩存服務(wù)器
#賦予腳本執(zhí)行權(quán)限
chmod +x /root/shell/magent.sh
#啟動keepalived服務(wù)
systemctl enable keepalived.service
systemctl start keepalived.service
#將主服務(wù)器上keepalived配置文件復(fù)制過來
scp root@192.168.5.129:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
#編輯keepalived配置文件
vim keepalived.conf
! Configuration File for keepalived 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_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 99 #主服務(wù)器優(yōu)先級大于從服務(wù)器優(yōu)先級 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { #vip地址 192.168.5.100 } track_script { #調(diào)用函數(shù) magent } }
#創(chuàng)建magent.sh腳本
mkdir /root/shell
vim /root/shell/magent.sh
#!/bin/bash Keepalived=`ip addr | grep 192.168.5.100 | grep -v grep | wc -l` if [ $Keepalived -gt 0 ]; then magent -u root -n 51200 -l 192.168.5.100 -p 12000 -s 192.168.5.129:11211 -b 192.168.5.139:11211 else pkill -9 magent fi
#賦予腳本執(zhí)行權(quán)限
chmod +x /root/shell/magent.sh
#啟動keepalived服務(wù)
systemctl enable keepalived.service
systemctl start keepalived.service
memcached -m 512k -u root -d -l 192.168.5.129 -p 11211 //主服務(wù)器
memcached -m 512k -u root -d -l 192.168.5.139 -p 11211 //從服務(wù)器
netstat -ntap | grep 11211
#安裝telnet客戶端
yum -y install telnet
#連接memcached
telnet 192.168.175.188 12000
#測試復(fù)制功能
//連接vip進行測試,插入user鍵值
[root@localhost ~]# telnet 192.168.5.100 12000 Trying 192.168.5.100... Connected to 192.168.5.100. Escape character is '^]'. set user 0 0 5 test1 STORED get user VALUE user 0 5 test1 END quit Connection closed by foreign host. //連接主Memcached,查詢user鍵值,可以獲取,成功! [root@localhost ~]# telnet 192.168.5.129 11211 Trying 192.168.5.129... Connected to 192.168.5.129. Escape character is '^]'. get user VALUE user 0 5 test1 END quit Connection closed by foreign host. //連接從Memcached,查詢user鍵值,也可以獲取,成功! [root@centos7-1 ~]# telnet 192.168.5.139 11211 Trying 192.168.96.17... Connected to 192.168.96.17. Escape character is '^]'. get user VALUE user 0 5 test1 END quit Connection closed by foreign host.
關(guān)于Memcached高可用群集的示例分析就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(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)容。