简介: Memcached主主复制是指在任意一台Memcached服务器修改数据都会被同步到另外一台,但是Memcached API客户端是无法判断连接到哪一台Memcached服务器的,所以需要设置VIP地址,提供给Memcached API客户端进行连接。可以使用keepalived产生的VIP地址连接主Memcached服务器,并且提供高可用架构。 Memcached架构图: 案例环境: 三台CentOs-7(主从服务器各一台,客户端一台) 主服务器:192.168.120.128 从服务器:192.168.120.129 客户端:192.168.120.133

具体实验操作: -------------------主服务器上配置memcached主缓存节点----- [root@localhost ~]#systemctl stop firewalld.service [root@localhost ~]#setenforce 0 [root@localhost ~]# mkdir /abc [root@localhost ~]# mount.cifs //192.168.100.10/rhel7 /abc Password for root@//192.168.100.10/rhel7:
[root@localhost ~]# cd /abc [root@localhost abc]# ls cobbler KVM memcached MongoDB mysql mysql.rar NEWYUM pv Y2C7 [root@localhost abc]# cd memcached/ [root@localhost memcached]# ls LAMP-php5.6 libevent-2.1.8-stable.tar.gz magent-0.5.tar.gz memcache-2.2.7.tgz memcached-1.5.6.tar.gz [root@localhost memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/ [root@localhost memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt/ [root@localhost memcached]# mkdir /opt/magent [root@localhost memcached]# tar zxvf magent-0.5.tar.gz -C /opt/magent/ ketama.c magent.c ketama.h Makefile

[root@localhost memcached]# yum install gcc gcc-c++ make -y [root@localhost memcached]# cd /opt/libevent-2.1.8-stable/ [root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/ [root@localhost libevent-2.1.8-stable]# make && make install [root@localhost libevent-2.1.8-stable]# cd ../ [root@localhost opt]# cd memcached-1.5.6/ [root@localhost memcached-1.5.6]# ./configure --with-libevent=/usr/ [root@localhost memcached-1.5.6]# make && make install [root@localhost memcached-1.5.6]# cd /opt/magent/ [root@localhost magent]# vim ketama.h

1 #ifndef SSIZE_MAX 2 #define SSIZE_MAX 32767 3 #endif [root@localhost magent]# vim Makefile

1 LIBS = -levent -lm

[root@localhost magent]# make gcc -Wall -O2 -g -c -o magent.o magent.c gcc -Wall -O2 -g -c -o ketama.o ketama.c gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm

-------------------从服务器同主服务器一样操作---------------- [root@localhost ~]#systemctl stop firewalld.service [root@localhost ~]#setenforce 0 [root@localhost ~]# mkdir /abc [root@localhost ~]# mount.cifs //192.168.100.10/rhel7 /abc Password for root@//192.168.100.10/rhel7:
[root@localhost ~]# cd /abc [root@localhost abc]# ls cobbler KVM memcached MongoDB mysql mysql.rar NEWYUM pv Y2C7 [root@localhost abc]# cd memcached/ [root@localhost memcached]# ls LAMP-php5.6 libevent-2.1.8-stable.tar.gz magent-0.5.tar.gz memcache-2.2.7.tgz memcached-1.5.6.tar.gz [root@localhost memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/ [root@localhost memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt/ [root@localhost memcached]# yum install gcc gcc-c++ make -y [root@localhost memcached]# cd /opt/libevent-2.1.8-stable/ [root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/ root@localhost libevent-2.1.8-stable]# make && make install [root@localhost memcached-1.5.6]# ./configure --prefix=/usr/local/memcached/ --with-libevent=/usr/ [root@localhost memcached-1.5.6]# make && make install [root@localhost memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin/

-----------------------在主上操作---------------------------- [root@localhost magent]# yum install openssh-clients -y [root@localhost magent]# cp magent /usr/bin/ [root@localhost magent]# scp magent root@192.168.120.129:/usr/bin/ [root@localhost magent]# yum install keepalived -y

-------------------在从上查看------------------------------ [root@localhost memcached-1.5.6]# cd /usr/bin/ [root@localhost bin]# yum install keepalived -y

----------------------编辑配置文件----------------- [root@localhost magent]# vim /etc/keepalived/keepalived.conf

#删除原有文件,编辑 1 ! Configuration File for keepalived 2 3 vrrp_script magent { 4 script "/opt/shell/magent.sh" 5 interval 2 6 7 } 8 9 global_defs { 10 notification_email { 11 acassen@firewall.loc 12 failover@firewall.loc 13 sysadmin@firewall.loc 14 } 15 notification_email_from Alexandre.Cassen@firewall.loc 16 smtp_server 192.168.200.1 17 smtp_connect_timeout 30 18 router_id MAGENT_HA 19 } 20 21 vrrp_instance VI_1 { 22 state MASTER 23 interface ens33 24 virtual_router_id 51 25 priority 100 26 advert_int 1 27 authentication { 28 auth_type PASS 29 auth_pass 1111 30 } 31 track_script { 32 magent 33 } 34 virtual_ipaddress { 35 192.168.120.188 #漂移地址 36 37 } 38 }

-----------------------在从服务器上-------------------------- [root@localhost ~]# cd /etc/keepalived/ [root@localhost keepalived]# ls keepalived.conf [root@localhost keepalived]# mv keepalived.conf keepalived.conf.bk [root@localhost keepalived]# ls keepalived.conf.bk [root@localhost keepalived]# yum install openssh-clients -y

----------------------到主上将配置文件发送给从服务器----------- [root@localhost magent]# cd /etc/keepalived/ [root@localhost keepalived]# ls keepalived.conf [root@localhost keepalived]# scp keepalived.conf root@192.168.120.129:/etc/keepalived/ root@192.168.120.129's password: keepalived.conf 100% 663 351.3KB/s 00:00

---------------------到从上更改配置文件---------------------------- [root@localhost keepalived]# vim keepalived.conf 18 router_id MAGENT_HB 22 state BACKUP 24 virtual_router_id 52 25 priority 90

---------------------回到主服务器上-------------------------------- [root@localhost keepalived]# mkdir /opt/shell [root@localhost keepalived]# cd /opt/shell/ [root@localhost shell]# vim 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.120.188 -p 12000 -s 192.168.120.128:11211 -b 192.168.120.129:11211 else pkill -9 magent fi

[root@localhost shell]# chmod +x magent.sh [root@localhost shell]# systemctl start keepalived.service #(等待几分钟,如果端口没启动,可能虚拟机有问题,可以更换虚拟机再试)# [root@localhost shell]# netstat -ntap | grep 12000 tcp 0 0 192.168.120.188:12000 0.0.0.0:* LISTEN 12088/magent [root@localhost shell]# memcached -m 512k -u root -d -l 192.168.120.128 -p 11211 [root@localhost shell]# netstat -ntap | grep 11211 tcp 0 0 192.168.120.128:11211 0.0.0.0:* LISTEN 15982/memcached
[root@localhost shell]# yum install telnet -y [root@localhost shell]# telnet 192.168.175.128 11211 Trying 192.168.175.128... Connected to 192.168.175.128. Escape character is '^]'. ---------------------------在从上同样操作------------------------------

[root@localhost keepalived]# mkdir /opt/shell [root@localhost keepalived]# cd /opt/shell/ [root@localhost shell]# vim magent.sh

#!/bin/bash K=ip addr | grep 192.168.120.188 | grep -v grep | wc -l if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.120.188 -p 12000 -s 192.168.120.128:11211 -b 192.168.120.129:11211 else pkill -9 magent fi

[root@localhost shell]# chmod +x magent.sh [root@localhost shell]# systemctl start keepalived.service [root@localhost shell]# netstat -ntap | grep 12000 tcp 0 0 192.168.120.188:12000 0.0.0.0:* LISTEN 12088/magent [root@localhost shell]# memcached -m 512k -u root -d -l 192.168.120.129 -p 11211 [root@localhost shell]# netstat -ntap | grep 11211 tcp 0 0 192.168.120.129:11211 0.0.0.0:* LISTEN 51747/memcached
[root@localhost shell]# yum install telnet -y [root@localhost shell]# telnet 192.168.120.129 11211 Trying 192.168.120.129... Connected to 192.168.120.129. Escape character is '^]'.

------------------------在客户端上测试--------------------------

[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 [root@localhost ~]# yum install telnet -y [root@localhost ~]# telnet 192.168.120.188 12000 Trying 192.168.120.188... Connected to 192.168.120.188. Escape character is '^]'. add username 0 0 7 #插入数据检测 1234567 STORED

------------------打开两台从查看(主从同步)---------------------- [root@localhost memcached-1.5.6]# telnet 192.168.120.133 11211 Trying 192.168.120.133... Connected to 192.168.120.133. Escape character is '^]'. get username 0 7 #查看数据 VALUE username 0 7 1234567 #同步过来了 END