memcache集群
1. 实验准备
1)服务器规划
主机 | IP | 角色 |
---|---|---|
host1 | 192.168.1.115 | 客户机 |
host2 | 192.168.1.116 | magnet 1 |
host3 | 192.168.1.118 | magnet 2 |
host4 | 192.168.1.117 | memcache主 |
host5 | 192.168.1.131 | memcache从 |
2)需要软件包
magnet:libevent-2.022 keepalived
memcache:libevent-2.022 memcache-1.436
2. 操作过程
1)host2~5安装libevent
5 tar -zxf libevent-2.0.22-stable.tar.gz -C /usr/src
6 cd /usr/src/libevent-2.0.22-stable/
7 ./configure --prefix=/usr/
8 make && make install
2)host4 host5安装memcache
7 tar -zxf memcached-1.4.36.tar.gz -C /usr/src
8 cd /usr/src/memcached-1.4.36/
9 ./configure --prefix=/usr/local/memcached --with-libevent=/usr/
10 make
11 make install
16 ln -s /usr/local/memcached/bin/* /usr/local/bin/
3)启动两台memcache
host4 启动memcache
[root@localhost ~]# memcached -d -m 512 -u root -l 192.168.1.117 -p 11211
host 5 启动memcache
[root@localhost ~]# memcached -d -m 512 -u root -l 192.168.1.131 -p 11211
4)host2 host3 安装magnet
10 tar -zxf magent-0.6.tar.gz -C /usr/local/magnet/
11 cd /usr/local/magnet/
12 vim ketama.h
添加(头部添加):
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
13 ln -s /usr/lib64/libm.so /usr/lib64/libm.a
23 ln -s /usr/lib64/libevent-2.0.so.5 /usr/lib64/libevent.a
15 ldconfig
16 sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
18 make
26 cp magent /usr/bin/
5) host2 host3 上搭建keepalived 虚拟IP设定为192.168.1.100
32 yum -y install openssl-devel popt-devel
31 tar -zxf keepalived-1.2.13.tar.gz -C /usr/src/
32 cd /usr/src/keepalived-1.2.13/
33 ./configure --prefix=/
34 make && make install
[root@localhost keepalived-1.2.13]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER #第二台改为BACKUP
interface ens33 #本机内网网卡名
virtual_router_id 51
priority 100 # 第二台改为90
virtual_ipaddress {
192.168.1.100
}
[root@localhost keepalived-1.2.13]# /etc/init.d/keepalived start
6)启动两台magent(启动命令相同)
magent 启动参数
-u:用户
-n:最大连接数
-l:magent 对外监听IP 地址
-p:magent 对外监听端口
-s:magent 主缓存IP 地址和端口
-b:magent 备缓存IP 地址和端口
# magent -u root -n 51200 -l 192.168.1.100 -p 12000 -s 192.168.1.117:11211 -b 192.168.1.131:11211
7)各主机关闭防火墙
systemctl stop firewalld
7)客户机登陆验证
# yum -y install telnet
[root@localhost ~]# telnet 192.168.1.100 12000
Trying 192.168.1.100...
Connected to 192.168.1.100.
Escape character is '^]'.
set today 0 0 6
sunday
STORED
去主memcache上查看键值对情况
[root@localhost ~]# telnet 192.168.1.117 11211
Trying 192.168.1.117...
Connected to 192.168.1.117.
Escape character is '^]'.
get today
VALUE today 0 6
sunday
END
去从memcache查看键值对情况
[root@localhost ~]# telnet 192.168.1.131 11211
Trying 192.168.1.131...
Connected to 192.168.1.131.
Escape character is '^]'.
get today
VALUE today 0 6
sunday
END
memcache主从转进行同步,集群搭建完成