Memcached构建缓存加速集群部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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软件

[[email protected] ~]# tar -zxvf libevent-2.0.21-stable.tar.gz -C /usr/src/

[[email protected] ~]# cd /usr/src/libevent-2.0.21-stable/

[[email protected] libevent-2.0.21-stable]# ./configure --prefix=/usr/

[[email protected] libevent-2.0.21-stable]# make && make install

2、安装memcached软件

[[email protected] ~]# tar -zxvf memcached-1.4.31.tar.gz -C /usr/src/

[[email protected] ~]# cd /usr/src/memcached-1.4.31/

[[email protected] memcached-1.4.31]# ./configure --enable-memcache --with-libevent=/usr/

[[email protected] memcached-1.4.31]# make && make install

主缓存:

[[email protected] ~]# memcached -d -m 1024 -u root -l 192.168.1.3 -p 11211

-d:开启守护进程daemon -m:制定分配的内存memory

-u:用户  -l:监听的IP地址  -P:监听的端口号

备缓存:

[[email protected] ~]# memcached -d -m 1024 -u root -l 192.168.1.4 -p 11211

[[email protected] ~]# netstat -anpt | grep memcached

3、安装magent软件

[[email protected] ~]# mkdir /usr/magent

[[email protected] ~]# tar -zxvf magent-0.6.tar.gz -C /usr/magent/

[[email protected] ~]# cd /usr/magent/

[[email protected] magent]# vim ketama.h

添加(头部添加):

#ifndef SSIZE_MAX

#define SSIZE_MAX 32767

#endif

[[email protected] magent]# ln -s /usr/lib64/libm.so /usr/lib64/libm.a

[[email protected] magent]# ln -s /usr/lib64/libevent-1.4.so.2 /usr/lib64/libevent.a

[[email protected] magent]# /sbin/ldconfig

[[email protected] magent]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile

[[email protected] magent]# vim Makefile

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

[[email protected] magent]# make

[[email protected] magent]# cp magent /usr/bin/

主缓存和备缓存一样的启动命令

[[email protected] ~]# 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地址,后面要用到!

[[email protected] ~]# 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地址和端口

[[email protected] ~]# ps -elf | grep magent

4、安装keepalived软件--在magent1和magent2上面都安装

[[email protected] ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/

[[email protected] ~]# cd /usr/src/keepalived-1.2.13/

[[email protected] 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 才能出现如上文件

[[email protected] keepalived-1.2.13]# make && make install


配置mangent1上面的keepalived

[[email protected] ~]# 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地址

}

}

后面的部分全部删除或注释掉

[[email protected] ~]# service keepalived restart

[[email protected] ~]# ip add show dev eth0

[[email protected] 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

[[email protected] ~]# 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

}

---其他参数与主缓存服务器保持一致----

}

[[email protected] ~]# service keepalived restar


验证:

客户端ping 192.168.1.10 查看VIP的变化情况。

5、验证:

1)用主缓存节点连接上主缓存的1200端口插入数据

[[email protected] ~]# 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)查看插入的数据

[[email protected] ~]# 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端口进行查看

[[email protected] ~]# 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端口进行查看

[[email protected] ~]# 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)客户端查看

[[email protected] ~]# telnet 192.168.1.10 12000

Trying 192.168.1.10...

Connected to 192.168.1.10.

Escape character is ‘^]‘.


OK,实验完毕,加油,送给一直坚持的你!嘿嘿。。。。

本文只做了简单的安装测试,更深层次的请查看其它精选文档


总结:

memcache的优点:可以做多主或者多从

memcache的缺点:当主缓存节点当掉又恢复,之前的缓存数据会丢失。


本文出自 “11000174” 博客,请务必保留此出处http://11010174.blog.51cto.com/11000174/1896926

以上是关于Memcached构建缓存加速集群部署的主要内容,如果未能解决你的问题,请参考以下文章

Memcached 分布式集群

Memcached + Magent + keepalived高可用集群

CentOS 7部署memcached缓存服务器

memcahced部署

Centos 7.5安装部署redis 5.0.0集群

Memcached 与 Wowza 项目的集成