Memcached內(nèi)存數(shù)據(jù)庫
- 一套開源的高性能分布式內(nèi)存對象緩存系統(tǒng)
- 所有的數(shù)據(jù)都存儲在內(nèi)存中
- 支持任意存儲類型的數(shù)據(jù)
- 提高網(wǎng)址訪問的速度
Memcached緩存機(jī)制
- 當(dāng)程序?qū)懭刖彺鏀?shù)據(jù)請求時,Memcached的API接口將KEY輸入路由算法模塊路由到集群中的一臺服務(wù),之后由API接口與服務(wù)器進(jìn)行通信,完成一次分布式緩存寫入
- Key索引建立在API中,值value數(shù)據(jù)存在后面的memcached中
Memcached分布式
- 要依賴于Memcached的客戶端來實(shí)現(xiàn)
- 多個Memcached服務(wù)器是獨(dú)立的
- 分布式數(shù)據(jù)如何存儲是路由算法所決定
Memcached路由算法
求余數(shù)hash算法
- 先用key做hash運(yùn)算的到一個整數(shù),再去做hash算法,根據(jù)余數(shù)進(jìn)行路由。不適合在動態(tài)變化的環(huán)境中
一致性hash算法
- 按照hash算法把對應(yīng)key通過一定hash算法處理后映射形成一個首尾接閉合循環(huán),然后通過使用與對象存儲一樣的hash算法將機(jī)器也映射到環(huán)中,順時針方向計算將所有對象存儲到離自己最近的機(jī)器中。適合在動態(tài)變化中使用
Memcached是什么
- 在闡述這個問題之前,我們首先要清楚它“不是什么”。很多人把它當(dāng)作和SharedMemory那種形式的存儲載體來使用,雖然memcached使用了同樣的“Key=>Value”方式組織數(shù)據(jù),但是它和共享內(nèi)存、APC等本地緩存有非常大的區(qū)別。Memcached是分布式的,也就是說它不是本地的。它基于網(wǎng)絡(luò)連接(當(dāng)然它也可以使用localhost)方式完成服務(wù),本身它是一個獨(dú)立于應(yīng)用的程序或守護(hù)進(jìn)程(Daemon方式)。
- Memcached使用libevent庫實(shí)現(xiàn)網(wǎng)絡(luò)連接服務(wù),理論上可以處理無限多的連接,但是它和Apache不同,它更多的時候是面向穩(wěn)定的持續(xù)連接的,所以它實(shí)際的并發(fā)能力是有限制的。在保守情況下memcached的最大同時連接數(shù)為200,這和Linux線程能力有關(guān)系,這個數(shù)值是可以調(diào)整的。關(guān)于libevent可以參考相關(guān)文檔。 Memcached內(nèi)存使用方式也和APC不同。APC是基于共享內(nèi)存和MMAP的,memcachd有自己的內(nèi)存分配算法和管理方式,它和共享內(nèi)存沒有關(guān)系,也沒有共享內(nèi)存的限制,通常情況下,每個memcached進(jìn)程可以管理2GB的內(nèi)存空間,如果需要更多的空間,可以增加進(jìn)程數(shù)。
memcached群集搭建實(shí)踐
實(shí)驗(yàn)環(huán)境
- 主服務(wù)器IP地址:192.168.144.238
- 從服務(wù)器IP地址:192.168.144.239
- 客戶端IP地址:192.168.144.185
- 漂移IP地址:192.168.144.188
主服務(wù)器安裝memcached,libevent事件庫,mamgent代理包
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.8/memcached /abc //掛載
Password for root@//192.168.100.8/memcached:
[root@localhost ~]# cd /abc/
[root@localhost abc]# ls
LAMP-php5.6 magent-0.5.tar.gz memcached-1.5.6.tar.gz
libevent-2.1.8-stable.tar.gz memcache-2.2.7.tgz
[root@localhost abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt //解壓事件庫軟件包,memcached依賴于事件庫
[root@localhost abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt/ //解壓服務(wù)端memcached軟件包
[root@localhost abc]# yum install gcc gcc-c++ make -y //安裝環(huán)境包
[root@localhost abc]# mkdir /opt/magent
[root@localhost abc]# tar zxvf magent-0.5.tar.gz -C /opt/magent/
[root@localhost opt]# cd libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr
[root@localhost libevent-2.1.8-stable]# make && make install //編譯安裝
[root@localhost libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure --with-libevent=/usr
make && make install
從服務(wù)器安裝memcached,libevent事件庫
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.8/memcached /abc
Password for root@//192.168.100.8/memcached:
[root@localhost ~]# cd /abc/
[root@localhost abc]# ls
LAMP-php5.6 magent-0.5.tar.gz memcached-1.5.6.tar.gz
libevent-2.1.8-stable.tar.gz memcache-2.2.7.tgz
[root@localhost abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt //解壓事件庫軟件包
[root@localhost abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt/ /解壓服務(wù)端memcached軟件包
[root@localhost abc]# yum install gcc gcc-c++ make -y
[root@localhost opt]# cd libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr
[root@localhost libevent-2.1.8-stable]# make && make install
[root@localhost libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure --with-libevent=/usr
make && make install
主服務(wù)器配置magent
[root@localhost memcached-1.5.6]# cd /opt/
[root@localhost opt]# ls
libevent-2.1.8-stable magent memcached-1.5.6 rh
[root@localhost opt]# cd magent/
[root@localhost magent]# vim ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
[root@localhost magent]# vim Makefile
LIBS = -levent -lm //指定makefile文件
make //編譯
[root@localhost magent]# ls
ketama.c ketama.h ketama.o magent magent.c magent.o Makefile
[root@localhost magent]# yum install openssh-clients -y //安裝scp遠(yuǎn)程同步軟件包
[root@localhost magent]# cp magent /usr/bin/ //把magent腳本放到/usr/local中,讓系統(tǒng)能識別
[root@localhost magent]# scp magent root@192.168.144.239:/usr/bin/ //把mangent文件拷貝到從服務(wù)器上
主從都關(guān)閉防火墻
[root@localhost bin]# systemctl stop firewalld.service
[root@localhost bin]# setenforce 0
在主服務(wù)器、從服務(wù)器同時安裝
[root@localhost bin]# yum install keepalived -y
主服務(wù)器配置主從同步
[root@localhost magent]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script magent { //編輯函數(shù)腳本
script "/opt/shell/magent.sh" //指定腳本位置
interval 2 //檢測腳本時間間隔
}
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 //主服務(wù)器id,兩臺不能一樣
}
vrrp_instance VI_1 {
state MASTER
interface ens33 //主服務(wù)器網(wǎng)卡
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 #默認(rèn)驗(yàn)證
}
track_script { //調(diào)函數(shù)名magent
magent
}
virtual_ipaddress {
192.168.144.188 //客戶端訪問的漂移地址
}
}
從服務(wù)器安裝openssh客戶端
[root@localhost bin]# cd /etc/keepalived/
[root@localhost keepalived]# mv keepalived.conf keepalived.conf.bak //更改名稱
[root@localhost keepalived]# yum install openssh-clients -y
主服務(wù)器用scp把keepalived文件傳到從服務(wù)器
[root@localhost magent]# cd /etc/keepalived/
[root@localhost keepalived]# scp keepalived.conf root@192.168.144.239:/etc/keepalived/
從服務(wù)器配置主從同步
[root@localhost keepalived]# ls
keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vim keepalived.conf
! Configuration File for keepalived
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
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_HB //routed_id不能相同
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 52 //從服務(wù)器虛擬id不能和主服務(wù)器一樣
priority 90 //優(yōu)先級比主服務(wù)器低
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
magent
}
virtual_ipaddress {
192.168.144.188
}
}
主服務(wù)器配置magent腳本
[root@localhost keepalived]# mkdir /opt/shell
[root@localhost keepalived]# cd /opt/shell/
[root@localhost shell]# vim magent.sh
#!/bin/bash
k=`ps -ef | grep keepalived | grep -v grep | wc -l` //檢查keepaliveed進(jìn)程,如果開啟
if [ $k -gt 0 ]; then
magent -u root -n 51200 -l 192.168.144.188 -p 12000 -s 192.168.144.238:11211 -b
// -n連接數(shù)量 -l指定漂移地址,-p指定端口映射到主從服務(wù)器的地址
192.168.144.239:11211
else
pkill -9 magent
fi
[root@localhost shell]# chmod +x magent.sh
[root@localhost shell]# systemctl start keepalived.service
[root@localhost shell]# netstat -ntap | grep 12000
tcp 0 0 192.168.144.188:12000 0.0.0.0:* LISTEN 124720/magent
從服務(wù)器同樣的操作
[root@localhost keepalived]# mkdir /opt/shell
[root@localhost keepalived]# cd /opt/shell/
[root@localhost 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.144.188 -p 12000 -s 192.168.144.238:11211 -b 192.168.144.239:11211
else
pkill -9 magent
fi
[root@localhost shell]# chmod +x magent.sh
[root@localhost shell]# systemctl start keepalived.service
[root@localhost shell]# netstat -ntap | grep 12000 //查看magent端口
tcp 0 0 192.168.144.188:12000 0.0.0.0:* LISTEN 11660/magent
開啟主服務(wù)器memcached
[root@localhost shell]# memcached -m 512k -u root -d -l 192.168.144.238 -p 11211 //啟動主,-m指定空間大小
[root@localhost shell]# netstat -ntap | grep 11211
tcp 0 0 192.168.144.238:11211 0.0.0.0:* LISTEN 44647/memcached
開啟從服務(wù)器memecached
[root@localhost shell]# memcached -m 512k -u root -d -l 192.168.144.239 -p 11211 #啟動從
[root@localhost shell]# netstat -ntap | grep 11211
tcp 0 0 192.168.144.239:11211 0.0.0.0:* LISTEN 42654/memcached
客戶端中驗(yàn)證用telnet去連接memcached 內(nèi)存數(shù)據(jù)庫
[root@localhost ~]# telnet 192.168.144.188 12000
Trying 192.168.144.188...
Connected to 192.168.144.188.
Escape character is '^]'.
add username 0 0 7 //編寫鍵值對數(shù)據(jù)
1234567
STORED
查看主服務(wù)器查看數(shù)據(jù)有沒有
[root@localhost shell]# telnet 192.168.144.238 11211
Trying 192.168.144.238...
Connected to 192.168.144.238.
Escape character is '^]'.
geer^H^H
ERROR
get username
VALUE username 0 7
1234567
END
驗(yàn)證從服務(wù)器數(shù)據(jù)有沒有同步
[root@localhost shell]# telnet 192.168.144.239 11211
Trying 192.168.144.239...
Connected to 192.168.144.239.
Escape character is '^]'.
get username
VALUE username 0 7
1234567
END
分享標(biāo)題:Memcached內(nèi)存數(shù)據(jù)庫群集配置(理論+實(shí)踐篇)
標(biāo)題鏈接:
http://weahome.cn/article/jcechj.html