MemCache 是一個自由、源碼開放、高性能、分佈式的分佈式內存對象緩存系統,用於動態Web 應用以減輕數據庫的負載。它經過在內存中緩存數據和對象來減小讀取數據庫的次數,從而提升了網站訪問的速度。數據庫
實驗:
一、安裝 libevent 軟件(四臺)
[root@localhost ~]# tar -zxvf libevent-2.0.21-stable.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/libevent-2.0.21-stabl
[root@localhost libevent-2.0.21-stable]# ./configure --prefix=/usr/
[root@localhost libevent-2.0.21-stable]# make && make install
二、安裝 memcached 軟件(Memcached 服務器 2 臺)
[root@localhost ~]# tar -zxvf memcached-1.4.31.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/memcached-1.4.31/
[root@localhost memcached-1.4.31]# ./configure --enable-memcache --with-libevent=/usr/
[root@localhost memcached-1.4.31]# make && make install
主緩存:
[root@localhost ~]# memcached -d -m 1024 -u root -l 192.168.1.201 -p 11211
備緩存:
[root@localhost ~]# memcached -d -m 1024 -u root -l 192.168.1.202 -p 11211
[root@localhost ~]# netstat -anpt | grep memcached
三、安裝 magent 軟件(Magent 服務器 2 臺)
[root@localhost ~]# mkdir /usr/magent
[root@localhost ~]# tar -zxvf magent-0.6.tar.gz -C /usr/magent/
[root@localhost ~]# cd /usr/magent/
[root@localhost magent]# vim ketama.h
添加(頭部添加):
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
[root@localhost magent]# ln -s /usr/lib64/libm.so /usr/lib64/libm.a
[root@localhost magent]# ln -s /usr/lib64/libevent-1.4.so.2 /usr/lib64/libevent.a
沒法 make 的時候須要安裝 libevent-1.4.so.2
[root@localhost magent]# /sbin/ldconfig
[root@localhost magent]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
[root@localhost magent]# make
[root@localhost magent]# cp magent /usr/bin/
主緩存和備緩存同樣的啓動命令(-l 是漂移 IP 尚未搭建 Keepalived 服務 暫時不要啓動 Magent 服務)
[root@localhost ~]# magent -u root -n 51200 -l 192.168.1.10 -p 12000 -s 192.168.1.201:11211
-b 192.168.1.202:11211
[root@localhost ~]# magent -u root -n 51200 -l 192.168.1.10 -p 12000 -s 192.168.1.201:11211
-b 192.168.1.202:11211
-u:用戶
-n:最大鏈接數
-l:magent 對外監聽 IP 地址
-p:magent 對外監聽端口
-s:magent 主緩存 IP 地址和端口
-b:magent 備緩存 IP 地址和端口
[root@localhost ~]# ps -elf | grep magent
四、安裝 keepalived 軟件(Magent 服務器 2 臺)
[root@localhost ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/keepalived-1.2.13/
[root@localhost keepalived-1.2.13]# ./configure --prefix=/ --with-kerneldir=/usr/src/kernels/2.6.32-431.el6.x86_64
[root@localhost keepalived-1.2.13]# make && make install
配置主緩存服務器
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
修改:
global_defs {
router_id LVS_DEVEL_R1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.10
}
}
[root@localhost ~]# service keepalived restart
[root@localhost ~]# ip add show dev eth0
配置備緩存服務器
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
修改:
global_defs {
router_id LVS_DEVEL_R2
}
vrrp_instance VI_1 {
state BACKUP
priority 99
......
virtual_ipaddress {
192.168.1.10
}
---其餘參數與主緩存服務器保持一致----
}
[root@localhost ~]# service keepalived restart
[root@localhost ~]# ip add show dev eth0
驗證:
客戶端 ping 192.168.1.10 查看 VIP 的變化狀況。
五、驗證:
1)用主緩存節點鏈接上主緩存的 1200 端口插入數據
[root@localhost ~]# telnet 192.168.1.10 12000
Trying 192.168.1.10...
Connected to 192.168.1.10.
Escape character is '^]'.
set key 33 0 5
ggggg
STORED
quit
Connection closed by foreign host.
2)查看插入的數據
[root@localhost ~]# telnet 192.168.1.10 12000
Trying 192.168.1.10...
Connected to 192.168.1.10.
Escape character is '^]'.
get key
VALUE key 33 5
ggggg
END
quit
Connection closed by foreign host.
3)鏈接主緩存節點的 11211 端口進行查看
[root@localhost ~]# telnet 192.168.1.201 11211
Trying 192.168.1.100...
Connected to 192.168.1.100.
Escape character is '^]'.
get key
VALUE key 33 5
ggggg
END
quit
Connection closed by foreign host.
4)鏈接主緩存節點的 11211 端口進行查看
[root@localhost ~]# telnet 192.168.1.202 11211
Trying 192.168.1.200...
Connected to 192.168.1.200.
Escape character is '^]'.
get key
VALUE key 33 5
ggggg
END
quit
Connection closed by foreign host.
說明主緩存節點和備緩存節點都有數據。
宕掉主緩存節點。
1)中止 memcached 進程(或者斷開主緩存節點的網卡),
2)客戶端查看
[root@localhost ~]# telnet 192.168.1.10 12000
Trying 192.168.1.10...
Connected to 192.168.1.10.
Escape character is '^]'.
get key
VALUE key 33 5
ggggg
ENDvim
總結:
memcache 的優勢:能夠作多主或者多從
memcache 的缺點:當主緩存節點當掉又恢復,以前的緩存數據會丟失。緩存
配置主緩存服務器 vim keepalived.conf ! Configuration File for keepalived global_defs { ** router_id magent-1** } vrrp_instance VI_1 { state **MASTER** interface **ens33** virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.164.100 #漂移地址 } }
scp keepalived.conf 192.168.164.7:/etc/keepalived/
備服務器 cd /etc/keepalived/ vim keepalived.conf ! Configuration File for keepalived global_defs { ** router_id magent-2** } vrrp_instance VI_1 { state **BACKUP** interface **ens33** virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.164.100 #漂移地址 } }
啓動keepalived systemctl start keepalived.service
主緩存和備緩存同樣的啓動命令(-l 是漂移 IP 尚未搭建 Keepalived 服務 暫時不要啓動 Magent 服務) magent -uroot -n 51200 -l 192.168.164.100 -p 12000 -s 192.168.164.8:11211 -b 192.168.164.9:11211