标签:yum cache 部署 一个 inter str 数据同步 cas 系统环境
Memcached的特点:
- 在Memcached中可以保存的item数据量是没有限制的,只要内存足够;
- Memcached单进程最大使用内存为2GB,要使用更多内存,可以分别在不同端口启动多个Memcached进程;
- Memcached是一种无阻塞的socket通信方式的服务,基于libevent库,由于无阻塞通信,对内存读写速度非常之快;
- Memcached分为服务器和客户端,可以配置多个服务器和客户端,应用于分布式的服务非常广泛;
- Memcached作为小规模的数据分布式平台是非常高效的;
本身没有内置分布式功能,无法实现使用多台Memcachd服务器来存储不同的数据,最大程度的使用相同的资源;无法同步数据,容易造成单点故障,这里可以通过Memcached代理实现集群功能;
由于Mamcached服务器与服务器之间没有任何通信,并且不进行任何数据复制备份,所以当任何服务器节点出现故障时,会出现单点故障,如果需要实现高可用,就需要通过其他方式。这里通过Magent缓存代理,防止单点现象,缓存代理也可以做备份功能,通过客户端连接到缓存代理了服务器,缓存代理服务器连接缓存连接复苏期,缓存代理服务器可以连接多台Memcached机器可以将每台Memcached机器进行数据同步,如果其中有一台缓存服务器down了,系统依然可以继续工作,如果其中一台Memcached机器down了,数据不会丢失并且可以保证数据的完整性。
主机名 | 操作系统 | IP地址 | 软件包 |
---|---|---|---|
Memcached1 | Centos 7.3 x86_64 | 192.168.96.16 | libevent-2.1.8-stable.tar.gz、memcached-1.5.9.tar.gz、magent-0.5.tar.gz、keepalived |
Memcached2 | Centos 7.3 x86_64 | 192.168.96.17 | libevent-2.1.8-stable.tar.gz、memcached-1.5.9.tar.gz、keepalived |
客户端 | Centos 7.3 x86_64 | 192.168.96.22 | telnet |
相关软件包:百度云盘 密码:b7za
setenforce 0
systemctl stop firewalld.service
yum -y install gcc gcc-c++ make
#解压
tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
#切换到libevent目录
cd /opt/libevent-2.1.8-stable/
#配置
./configure --prefix=/usr/
#编译及安装
make && make install
#解压
tar zxvf memcached-1.5.9.tar.gz -C /opt/
#切换到memcached
cd /opt/memcached-1.5.9/
#配置(指定libevent路径)
./configure --with-libevent=/usr/
#编译及安装
make && make install
ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
#主服务器
memcached -m 512k -u root -d -l 192.168.96.16 -p 11211
#从服务器
memcached -m 512k -u root -d -l 192.168.96.17 -p 11211
netstat -ntap | grep 11211
mkdir /opt/magent
tar zxvf magent-0.5.tar.gz -C /opt/magent
cd /opt/magent
/sbin/ldconfig
vim ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
make
cp /opt/magent/magent /usr/bin/
scp /opt/magent/magent root@192.168.96.22:/usr/bin
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
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_H1 #指定router_id
}
vrrp_script magent { #定义函数
script "/root/shell/magent.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33 #本地网卡名称
virtual_router_id 51 #id主从一致
priority 100 #主优先级大于从优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { #调用函数
magent
}
virtual_ipaddress { #vip地址
192.168.96.100
}
}
mkdir /root/shell
vim /root/shell/magent.sh
#!/bin/bash
Keepalived=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then
magent -u root -n 51200 -l 192.168.96.100 -p 12000 -s 192.168.96.16:11211 -b 192.168.96.17:11211
else
pkill -9 magent
fi
参数说明:
-n 51200 #定义用户最大连接数
-l 192.168.175.188 #指定虚拟IP
-p 12000 #指定端口号
-s #指定主缓存服务器
-b #指定从缓存服务器
chmod +x /root/shell/magent.sh
systemctl enable keepalived.service
systemctl start keepalived.service
systemctl status keepalived.service
ip addr
yum -y install keepalived
scp root@192.168.175.128:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
vim keepalived.conf
! Configuration File for keepalived
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_H2 #指定router_id
}
vrrp_script magent { #定义函数
script "/root/shell/magent.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33 #本地网卡名称
virtual_router_id 51 #id主从一致
priority 90 #主优先级大于从优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #vip地址
192.168.96.100
}
track_script { #调用函数
magent
}
}
mkdir /root/shell
vim /root/shell/magent.sh
#!/bin/bash
Keepalived=`ip addr | grep 192.168.96.100 | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then
magent -u root -n 51200 -l 192.168.96.100 -p 12000 -s 192.168.96.12:11211 -b 192.168.96.17:11211
else
pkill -9 magent
fi
chmod +x /root/shell/magent.sh
systemctl enable keepalived.service
systemctl start keepalived.service
systemctl status keepalived.service
ip addr
yum -y install telnet
telnet 192.168.175.188 12000
//连接vip进行测试,插入user键值
[root@centos7-1 ~]# telnet 192.168.96.100 12000
Trying 192.168.96.100...
Connected to 192.168.96.100.
Escape character is ‘^]‘.
set user 0 0 5
test1
STORED
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
//连接Memcached1,查询user键值,可以获取,成功!
[root@centos7-1 ~]# telnet 192.168.96.16 11211
Trying 192.168.96.16...
Connected to 192.168.96.16.
Escape character is ‘^]‘.
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
//连接Memcached2,查询user键值,也可以获取,成功!
[root@centos7-1 ~]# telnet 192.168.96.17 11211
Trying 192.168.96.17...
Connected to 192.168.96.17.
Escape character is ‘^]‘.
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
Magent + Keepalived实现Memcached高可用群集
标签:yum cache 部署 一个 inter str 数据同步 cas 系统环境
原文地址:http://blog.51cto.com/10316297/2152202