本文共 3525 字,大约阅读时间需要 11 分钟。
Memcached 主主复制是指在任意一台 Memcached 服务器修改数据都会被同步到另外一台,但是Memcached API 客户端是无法判断链接到那一台服务器的,所以需要设置 VIP 地址,提供给 Memcached API 客户端进行链接。可以使用keepalived 产生的 VIP 地址链接主 Memcached 服务器,并且提供高可用架构。
主机 | IP地址 | 操作系统 | 主要软件包 |
---|---|---|---|
Memcached 1 | 192.18.217.128 | CentOS 7.3 x86_64 | memcached libevent magent |
Memcached 2 | 192.18.217.129 | CentOS 7.3 x86_64 | memcached libevent |
客户端 | 192.18.217.130 | CentOS 7.3 x86_64 | Telnet |
systemctl stop firewalld.servicesetenforce 0tar xf memcached-1.5.6.tar.gz -C /opt/ #缓存服务tar xf libevent-2.1.8-stable.tar.gz -C /opt/ #事件通知库yum install gcc gcc-c++ make -y #安装环境包cd /opt/libevent-2.1.8-stable ./configure --prefix=/usr #指定工作目录make && make install cd /opt/memcached-1.5.6./configure \--with-libevent=/usr #指定libevent的安装路径make && make installln -s /usr/local/memcached/bin/* /usr/local/bin/ #让系统识别命令memcached -d -m 32m -p 11211 -u root #启动 memcachednetstat -anpt | grep 11211 #查看 memcached 端口yum install telnet -y[root@localhost memcached-1.5.6]# telnet 127.0.0.1 11211 #进入缓存数据库Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.
mkdir /opt/magenttar zxvf magent-0.5.tar.gz -C /opt/magent/ketama.cmagent.cketama.hMakefilecd /opt/magent/vim ketama.h #编辑配置文件#ifndef SSIZE_MAX#define SSIZE_MAX 32767#endifvim Makefile LIBS = -levent -lmmake #生成的mgent程序让系统识别cp magent /usr/bin/ #将magent可执行文件加入usr可执行命令中 scp magent root@192.168.217.129:/usr/bin/ #发送到从缓存服务器上
yum install keepalived -y #两台服务器安装 keepalived vim /etc/keepalived/keepalived.conf ! Configuration File for keepalivedvrrp_script magent { #添加脚本路径 script "/opt/shell/magent.sh" interval 2...... router_id 0001 #删除以下4行 }vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 }track_script { #调用脚本 magent} virtual_ipaddress { 192.168.217.100 #虚拟IP }}以下内容可以删除
mkdir /opt/shellvim /opt/shell/magent.sh#!/bin/bashK=`ps -ef | grep keepalived | grep -v grep | wc -l`if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.217.100 -p 12000 -s 192.168.217.128:11211 -b 192.168.217.129:11211elsepkill -9 magentfi#参数说明 :-n 51200 #定义用户最大连接数-l 192.168.217.100 #虚拟IP-p 12000 #指定端口号-s #指定主缓存服务器-b #指定从缓存服务器chmod +x magent.sh #添加执行权限systemctl start keepalived.servicenetstat -antp | grep magent #查看12000端口是否开启
yum install keepalived -y vim /etc/keepalived/keepalived.conf...... priority 90 #修改优先级 辨别活跃和备份 其它一样 ......
mkdir /opt/shellvim /opt/shell/magent.sh#!/bin/bashK=`ps -ef | grep keepalived | grep -v grep | wc -l`if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.217.100 -p 12000 -s 192.168.217.128:11211 -b 192.168.217.129:11211elsepkill -9 magentfi#参数说明 :-n 51200 #定义用户最大连接数-l 192.168.217.100 #虚拟IP-p 12000 #指定端口号-s #指定主缓存服务器-b #指定从缓存服务器chmod +x magent.sh #添加执行权限systemctl start keepalived.servicenetstat -antp | grep magent #查看12000端口是否开启
yum install telnet -ytelnet 192.168.217.100 12000 #进入缓存数据库Trying 192.168.217.100...Connected to 192.168.217.100.Escape character is '^]'.add username 0 0 7 #添加键值数据1234567STORED
退出缓存数据库,把活跃 keepalived 关闭 ,在进入数据库 ,实现故障切换 。
转载于:https://blog.51cto.com/13640803/2151608