标签:memcached magent keepalived
单台memcached服务的安装和测试很简单,考虑到老生常谈的单点故障问题,可以使用本例:
主memcached服务器1 192.168.80.80
从memcached服务器2 192.168.80.81
主从都要安装:memcached-1.5.6.tar.gz libevent-2.1.8-stable.tar.gz magent-0.5.tar.gz
测试机 192.168.80.82
1.首先安装libevent:
tar xf libevent-2.1.8-stable.tar.gz -C /opt/
cd /opt/libevent-2.1.8-stable
./configure --prefix=/usr/
make && make install
2.然后安装memcached:
tar xf memcached-1.5.6.tar.gz -C /opt/
cd /opt/memcached-1.5.6
./configure \
--with-libevent=/usr
make && make install
3.最后安装magent:
mkdir /opt/magent
tar xf magent-0.5.tar.gz -C /opt/magent
cd /opt/magent
vi ketama.h //在文件开头处新增
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
……
vi Makefile
LIBS = -levent -lm //第一行末尾加-lm (不是数字1)
make //修改完成后可以编译了
ls //会产生magent可执行程序
cp magent /usr/bin/ //将编译好的magent程序复制到PATH路径中
ln -s /usr/lib/libevent-1.4.so.2.1.2 /usr/lib64/libevent-1.4.so.2 //不可缺少
yum install openssh-clients
scp magent root@192.168.80.82:/usr/bin/ //把产生的magent文件直接复制到从服务器。
4.安装keepalived
yum install keepalived -y
vi /etc/keepalived/keepalived.conf //原来的全部删除,添加以下
! Configuration File for keepalived
global_defs {
router_id MAGENT_HA //主调度器名
}
vrrp_script magent {
script "/opt/shell/magent.sh" //主magent脚本
interval 2
}
vrrp_instance VI_1 {
state MASTER //主状态
interface ens33
virtual_router_id 51
priority 99 //主优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
magent //跟踪magent脚本
}
virtual_ipaddress {
192.168.80.188 //虚拟ip VIP
}
}
scp keepalived.conf root@192.168.80.82:/etc/keepalived/keepalived.conf ./ //把主服务器的配置文件复制到从,并相应修改
5.制作主跟踪脚本
mkdir /opt/shell
cd /opt/shell
vi 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.80.188 -p 12000 -s 192.168.80.81:11211 -b 192.168.80.82:11211
else
pkill -9 magent
fi
-n 51200 //定义用户最大连接数
-l 192.168.80.188 //指定虚拟IP
-p 12000 //指定端口号
-s //指定主缓存服务器
-b //指定从缓存服务器
chmod +x magent.sh
service keepalived start
netstat -anpt | grep 12000 //确认magent运行
cat /var/log/messages //验证主从
……
Transition to MASTER STATE
……
ip addr //确认漂移地址生效
6.memcached -m 512k -u root -d -l 192.168.80.80 -p 11211 //启动主
netstat -anptu | grep 11211
1.首先安装libevent:
tar xf libevent-2.1.8-stable.tar.gz -C /opt/
cd /opt/libevent-2.1.8-stable
./configure --prefix=/usr/
make && make install
2.然后安装memcached:
tar xf memcached-1.5.6.tar.gz -C /opt/
cd /opt/memcached-1.5.6
./configure \
--with-libevent=/usr
make && make install
ln -s /usr/lib/libevent-1.4.so.2.1.2 /usr/lib64/libevent-1.4.so.2 //不可缺少
3.先安装keepalived
yum install keepalived -y
后修改主传过来的配置文件
vi /etc/keepalived/keepalived.conf
router_id MAGENT_HB //修改调度器名
state BACKUP //修改状态
priority 90 //修改优先级
4.制作从跟踪脚本
mkdir /opt/shell
cd /opt/shell
vi magent.sh
#!/bin/bash
#!/bin/bash
K=ip addr | grep 192.168.80.188 | grep -v grep | wc -l
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.80:11211 -b 192.168.80.81:11211
else
pkill -9 magent
fi
chmod +x magent.sh
service keepalived start
cat /var/log/messages //验证主从
……
Entering BACKUP STATE
……
5.
memcached -m 512k -u root -d -l 192.168.80.81 -p 11211 //启动从
netstat -anptu | grep 11211
1.在客户端80.82上,首先测试magent是否备份:
telnet 192.168.80.188 12000 //连接VIP漂移地址,新建数据
set key 33 0 4
aaaa
STORED
quit
telnet 192.168.80.188 12000 //连接VIP漂移地址,查看数据
get key
VALUE key 33 4
aaaa
END
telnet 192.168.80.81 11211 //连接到主memcached上,查看数据,应该会有新建的数据
get key
VALUE key 33 4
aaaa
END
telnet 192.168.80.83 11211 //连接到从memcached上,查看数据,应该会有新建的数据
get key
VALUE key 33 4
aaaa
END
2.测试keepalived
主80.81 service keepalived stop,通过ip addr查看80.188应该漂移到从80.82上
且telnet 192.168.80.188 12000 //连接VIP漂移地址,还能查看数据,说明业务切换到从上,实现了memecached业务不中断
标签:memcached magent keepalived
原文地址:http://blog.51cto.com/13469709/2092913