Memcached構建緩存加速集羣部署

前言:

Memcached 是一個高性能的分佈式內存對象緩存系統,用於動態Web應用以減輕數據庫負載。它通過在內存中緩存數據和對象來減少讀取數據庫的次數,從而提高動態、數據庫驅動網站的速度。Memcached基於一個存儲鍵/值對的hashmap。其守護進程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,並通過memcached協議與守護進程通信。


Centos6.5最下化安裝系統(Redhat6/Centos6系列)

實驗環境:magent1:192.168.1.1   magent2:192.168.1.2  漂移IP:192.168.1.10

      主緩存memcached:192.168.1.3   

      從緩存memcached:192.168.1.4   


1、四臺機器都要安裝libevent軟件

[root@localhost ~]# tar -zxvf libevent-2.0.21-stable.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/libevent-2.0.21-stable/

[root@localhost libevent-2.0.21-stable]# ./configure --prefix=/usr/

[root@localhost libevent-2.0.21-stable]# make && make install

2、安裝memcached軟件

[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.3 -p 11211

-d:開啓守護進程daemon -m:制定分配的內存memory

-u:用戶  -l:監聽的IP地址  -P:監聽的端口號

備緩存:

[root@localhost ~]# memcached -d -m 1024 -u root -l 192.168.1.4 -p 11211

[root@localhost ~]# netstat -anpt | grep memcached

3、安裝magent軟件

[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

#endif

[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

[root@localhost magent]# /sbin/ldconfig

[root@localhost magent]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile

[root@localhost magent]# vim Makefile

CFLAGS = -Wall -O2 -g修改爲 CFLAGS = -lrt -Wall -O2 -g

[root@localhost magent]# make

[root@localhost magent]# cp magent /usr/bin/

主緩存和備緩存一樣的啓動命令

[root@localhost ~]# magent -u root -n 51200 -l 192.168.1.10 -p 12000 -s 192.168.1.3:11211 -b 192.168.1.4:11211

注意:IP192.168.1.10是後面要做的keepalived虛擬IP地址,後面要用到!

[root@localhost ~]# magent -u root -n 51200 -l 192.168.1.10 -p 12000 -s 192.168.1.3:11211 -b 192.168.1.4:11211

-u:用戶

-n:最大連接數

-l:magent對外監聽IP地址

-p:magent對外監聽端口

-s:magent主緩存IP地址和端口

-b:magent備緩存IP地址和端口

[root@localhost ~]# ps -elf | grep magent

4、安裝keepalived軟件--在magent1和magent2上面都安裝

[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-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64

PS:yum -y install kernel-devel 才能出現如上文件

[root@localhost keepalived-1.2.13]# make && make install


配置mangent1上面的keepalived

[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     漂移IP地址

}

}

後面的部分全部刪除或註釋掉

[root@localhost ~]# service keepalived restart

[root@localhost ~]# ip add show dev eth0

[root@localhost keepalived]# ip add show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:82:0f:58 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.1/24 brd 192.168.1.255 scope global eth0

    inet 192.168.1.10/32 scope global eth0

    inet6 fe80::20c:29ff:fe82:f58/64 scope link 

       valid_lft forever preferred_lft forever


配置magent2的keepalived

[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 restar


驗證:

客戶端ping 192.168.1.10 查看VIP的變化情況。

5、驗證:

1)用主緩存節點連接上主緩存的1200端口插入數據

[root@localhost ~]# telnet 192.168.1.10 12000

Trying 192.168.1.10...

Connected to 192.168.1.10.

Escape character is '^]'.

輸入quit  回車後即可退出telnet

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 '^]'.

輸入quit  回車後即可退出telnet

Connection closed by foreign host.

3)連接主緩存節點的11211端口進行查看

[root@localhost ~]# telnet 192.168.1.3 11211

Trying 192.168.1.3...

Connected to 192.168.1.3.

Escape character is '^]'.

輸入quit  回車後即可退出telnet

Connection closed by foreign host.

4)連接主緩存節點的11211端口進行查看

[root@localhost ~]# telnet 192.168.1.4 11211

Trying 192.168.1.4...

Connected to 192.168.1.4.

Escape character is '^]'.

輸入quit  回車後即可退出telnet

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 '^]'.


OK,實驗完畢,加油,送給一直堅持的你!嘿嘿。。。。

本文只做了簡單的安裝測試,更深層次的請查看其它精選文檔


總結:

memcache的優點:可以做多主或者多從

memcache的缺點:當主緩存節點當掉又恢復,之前的緩存數據會丟失。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章