概述:memcached盡管是“分布式”緩存服務器,但服務器端並沒有分布式功能,各個memcached不會互相通信以共享數據,這完全取決memcached api的客戶端所使用的路由算法;
客戶端路由算法:
1.求余數hash算法:先用key做一個hash運算得到一個整數,再去做hash算法,根據余數進行路由選擇,這種算法適用於大多數據需求,但不適合用在動態變化的環境中,比如:有大量機器添加或者刪除時,會導致大量對象的存儲位置失效;
2.一致性hash算法:適用於動態變化的環境中,原理是按照hash算法把對應的key通過一定的hash算法處理后,映射形成一個首尾相接的閉合循環,然后通過使用與對象存儲一樣的hash算法將節點機器也映射到環中,按順時針方向將所有對象存儲到離客戶端最近的node節點上,如下圖;
Memcached主主復制集群;
案例環境:
注意點:
1.magent概述:Magent是一款開源的Memcached代理服務器軟件,采用 Magent 緩存代理,防止單點現象,緩存代理也可以做備份,通過客戶端連接到緩存代理服務器,緩存代理服務器連接緩存服務器,緩存代理服務器可以連接多台Memcached機器;
2.常見memcached+magent運行架構:
上圖此模型已經能夠很好的解決一個節點,一組服務器的緩存數據服務,但是如果在北方網通架設了一組服務器,同時在南方電信又架設了另外一組服務器,那么這兩組相對獨立的節點之間如何做到數據的同步與共享,基於magent與memcached的解決方案如下:
架構詳解:
http://blog.51cto.com/ultrasql/1633897
3.為避免magent節點單點故障問題,可以使用keepalived服務為其實現高可用;
案例步驟:
部署兩個節點的Memcached程序(在此兩個節點的安裝一致,在此列舉master節點的配置);
部署master節點的magent程序,部署完成傳送給slave節點;
安裝兩個節點的Keepalived程序(在此兩個節點的安裝一致,在此列舉master節點的配置);
配置master節點的keepalived服務;
配置slave節點的keepalived服務;
client節點測試雙主復制集群;
部署兩個節點的Memcached程序(在此兩個節點的安裝一致,在此列舉master節點的配置);
[root@mastermem ~]# ls
libevent-release-1.4.15-stable.tar.gz memcached-1.5.10.tar.gz
[root@mastermem ~]# tar zxvf libevent-release-1.4.15-stable.tar.gz -C /usr/src/
[root@mastermem ~]# cd /usr/src/libevent-release-1.4.15-stable/
[root@mastermem libevent-release-1.4.15-stable]# ./autogen.sh
[root@mastermem libevent-release-1.4.15-stable]# ./configure --prefix=/usr/local/libevent
[root@mastermem libevent-release-1.4.15-stable]# make && make install
[root@mastermem libevent-release-1.4.15-stable]# cd
[root@mastermem ~]# ln -s /usr/local/libevent/lib/libevent* /usr/lib64/
[root@mastermem ~]# tar zxvf memcached-1.5.10.tar.gz -C /usr/src/
[root@mastermem ~]# cd /usr/src/memcached-1.5.10/
[root@mastermem memcached-1.5.10 ~]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
[root@mastermem memcached-1.5.10 ~]# make && make install
[root@mastermem memcached-1.5.10 ~]# cd
[root@mastermem ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/
部署master節點的magent程序,部署完成傳送給slave節點;
[root@mastermem ~]# ls magent-0.5.tar.gz
magent-0.5.tar.gz
[root@mastermem ~]# mkdir magent
[root@mastermem ~]# tar zxvf magent-0.5.tar.gz -C magent/
ketama.c
magent.c
ketama.h
Makefile
[root@mastermem ~]# cd magent
[root@mastermem magent]# ls
ketama.c ketama.h magent.c Makefile
[root@mastermem magent]# vi ketama.h ##在文件開頭添加
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
[root@mastermem magent]# vi Makefile
1 LIBS = -levent -lm -L /usr/local/libevent/lib ##選項為小-L
2 INCLUDE= -I /usr/local/libevent/include ##選項為大-i
[root@mastermem magent]# make
gcc -Wall -O2 -g -I /usr/local/libevent/include -c -o magent.o magent.c
gcc -Wall -O2 -g -I /usr/local/libevent/include -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm -L /usr/local/libevent/lib
[root@mastermem magent]# ls
ketama.c ketama.h ketama.o magent magent.c magent.o Makefile
[root@mastermem magent]# cp magent /usr/bin/
[root@mastermem magent]# scp magent 192.168.100.102:/usr/bin/
[root@mastermem magent]# cd
安裝兩個節點的Keepalived程序(在此兩個節點的安裝一致,在此列舉master節點的配置);
[root@mastermem ~]# yum -y install keepalived
配置master節點的keepalived服務;
[root@mastermem ~]# vi /etc/keepalived/keepalived.conf
global_defs {
router_id R1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.250
}
}
[root@mastermem ~]# vi /opt/check.sh
#!/bin/bash
while true;do
K=$(ip a|grep 192.168.100.250|wc -l)
if [ $K -ne 0 ];then
magent -u root -n 51200 -l 192.168.100.250 -p 12000 -s 192.168.100.101:11211 -b 192.168.100.102:11211
else
pkill -9 magent
fi
M=$(netstat -utpln |grep mem|wc -l)
if [ $M -eq 0 ];then
systemctl stop keepalived
fi
done
注解:magent參數詳解:
-u ##指定運行用戶
-n ##最大的連接數,默認為4096
-l ##小寫L,magent監聽的ip地址
-p ##magent監聽的端口
-s ##設置memcached主緩存的ip地址和端口
-b ##設置memcached備緩存的ip地址和端口
[root@mastermem ~]# chmod +x /opt/check.sh
[root@mastermem ~]# memcached -u root -d -m 128m
[root@mastermem ~]# netstat -utpln |grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 7117/memcached
[root@mastermem ~]# /opt/check.sh &
[root@mastermem ~]# jobs -l
[1]+ 16273 運行中 /opt/check.sh &
[root@mastermem ~]# ip a|grep 192.168.100.250
inet 192.168.100.250/32 scope global eth0
[root@mastermem ~]# netstat -utpln |grep magent
tcp 0 0 192.168.100.250:12000 0.0.0.0:* LISTEN 22834/magent
配置slave節點的keepalived服務;
[root@slavemem ~]# vi /etc/keepalived/keepalived.conf
global_defs {
router_id R2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.250
}
}
[root@slavemem ~]# vi /opt/check.sh
#!/bin/bash
while true;do
K=$(ip a|grep 192.168.100.250|wc -l)
if [ $K -ne 0 ];then
memcached -u root -d -m 128m
magent -u root -n 51200 -l 192.168.100.250 -p 12000 -s 192.168.100.102:11211 -b 192.168.100.101:11211
else
pkill magent
fi
done
[root@slavemem ~]# chmod +x /opt/check.sh
[root@slavemem ~]# /opt/check.sh &
[1] 18066
[root@slavemem ~]# jobs -l
[1]+ 18066 完成 /opt/check.sh
[root@slavemem ~]# memcached -u root -d -m 128m
[root@slavemem ~]# netstat -utpln |grep mem
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 17920/memcached
[root@slavemem ~]# systemctl start keepalived
[root@slavemem ~]# ip a|grep 192.168.100.250
[root@slavemem ~]# netstat -utpln |grep magent
client節點測試雙主復制集群;
此時192.168.100.101節點為master,VIP在master節點,運行memcahed、magent進程;
[root@client ~]# yum -y install telnet
[root@client ~]# telnet 192.168.100.250 12000
Trying 192.168.100.250...
Connected to 192.168.100.250.
Escape character is '^]'.
set k1 0 0 5
hello
STORED
get k1
VALUE k1 0 5
hello
END
quit
Connection closed by foreign host.
將192.168.100.101節點的memcached進程關閉,模擬故障,后台運行的腳本導致keepalived服務關閉,keepalived服務關閉,導致magent進程關閉;
[root@mastermem ~]# netstat -utpln |grep mem
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 63140/memcached
[root@mastermem ~]# kill -9 63140
[root@mastermem ~]# netstat -utpln |grep mem
[root@mastermem ~]# netstat -utpln |grep magent
[root@mastermem ~]# ip a |grep 192.168.100.250
此時驗證slave節點的進程情況,VIP轉移到slave節點上;
[root@slavemem ~]# ip a|grep 192.168.100.250
inet 192.168.100.250/32 scope global eth0
[root@slavemem ~]# netstat -utpln |grep mem
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 17920/memcached
[root@slavemem ~]# netstat -utpln |grep magent
tcp 0 0 192.168.100.250:12000 0.0.0.0:* LISTEN 53789/magent
客戶端測試讀寫數據;
[root@client ~]# telnet 192.168.100.250 12000
Trying 192.168.100.250...
Connected to 192.168.100.250.
Escape character is '^]'.
get k1
VALUE k1 0 5
hello
END
set k2 0 0 2
ha
STORED
get k2
VALUE k2 0 2
ha
END
quit
Connection closed by foreign host.
————————————————
原文鏈接:https://blog.csdn.net/Richardlygo/article/details/81710859