在CentOS7上部署Memcached主主复制+Keepalived高可用架构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在CentOS7上部署Memcached主主复制+Keepalived高可用架构相关的知识,希望对你有一定的参考价值。

原理:

Memcached主主复制是指在任意一台Memcached服务器修改数据都会被同步到另外一台,但是Memcached API客户端是无法判断连接到哪一台Memcached服务器的,所以需要设置VIP地址,提供给Memcached API客户端进行连接。可以使用Keepalived产生的VIP地址连接主Memcached服务器,并且提供高可用架构。

使用两台Memcached服务器,一台客户机来完成,实验环境表如下:
技术分享图片

1.配置memcached主缓存节点和从缓存节点-----两台配置相同

 [[email protected] ~]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/   //解包//
 [[email protected] ~]# tar zxvf memcached-1.5.6.tar.gz -C /opt/
 [[email protected] ~]# mkdir /opt/magent
 [[email protected] ~]# tar zxvf magent-0.5.tar.gz -C /opt/magent/ 
 [[email protected] opt]#cd libevent-2.1.8-stable/
 [[email protected] libevent-2.1.8-stable]# yum install gcc gcc-c++ make -y
 [[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr
 [[email protected] libevent-2.1.8-stable]# make && make install
 [[email protected] libevent-2.1.8-stable]# cd ../memcached-1.5.6/
 [[email protected] memcached-1.5.6]# ./configure --with-libevent=/usr

 [[email protected] memcached-1.5.6]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6    //软链接//

2.关闭防火墙并开启memcached服务

[[email protected] memcached-1.5.6]# systemctl stop firewalld.service 
[[email protected] memcached-1.5.6]# setenforce 0
[[email protected] memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
[[email protected] memcached-1.5.6]# netstat -ntap | grep 11211
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      11224/memcached     
tcp6       0      0 :::11211                :::*                    LISTEN      11224/memcached

3.在主服务器上安装magent

[[email protected] memcached-1.5.6]# cd /opt/magent/
[[email protected] magent]# ls
ketama.c  ketama.h  magent.c  Makefile
[[email protected] magent]# vim ketama.h

#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
[[email protected] magent]# vim Makefile 
LIBS = -levent -lm //第一行末尾加-lm (不是数字1
LIBS = -levent -lm
CFLAGS = -Wall -O2 -g

[[email protected] 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

4.把生成的mgent程序让系统识别

ls一下可看到magent可执行程序
[[email protected] magent]# ls
ketama.c  ketama.h  ketama.o  magent  magent.c  magent.o  Makefile
[[email protected] magent]# cp magent /usr/bin/

5.把产生的magent文件直接复制到从服务器。

[[email protected] bin]# yum install openssh-clients -y
[[email protected] bin]# scp magent [email protected]:/usr/bin/ 

6.安装keepalived,修改默认配置文件。

[[email protected] bin]# yum install keepalived -y
[[email protected] bin]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
vrrp_script magent {
        script "/opt/shell/magent.sh"
        interval 2
}

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id MAGENT_HA    //主服务器名称//
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33     //网卡名称//
    virtual_router_id 51
    priority 100    //优先级//
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111   
    }
    virtual_ipaddress {
        192.168.126.188     //虚拟IP//
    }
track_script {
        magent     //函数//
}
}

7.从服务器上安装keepalived,配置文件进行修改。

[[email protected] bin]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
vi keepalived.conf
vrrp_script magent {
        script "/opt/shell/magent.sh"
        interval 2
}

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id MAGENT_HB      //从服务器的名称//
}

vrrp_instance VI_1 {
    state BACKUP            //从服务器的热备状态要修改成BACKUP//
    interface ens33  //网卡名称//
    virtual_router_id 52    //不能与主服务器相同//
    priority 90       //从调度器的优先级要小于主的//
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.126.188     //虚拟IP//
    }
track_script {        //函数//
        magent  
}
}

8.在主服务器上设置magent管理脚本

[[email protected] bin]# mkdir /opt/shell
[[email protected] bin]# vim /opt/shell/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.126.188 -p 12000 -s 192.168.126.138:11211 -b 192.168.126.166:11211
else
pkill -9 magent
fi

参数注解:
-n 51200 //定义用户最大连接数
-l 192.168.126.188 //指定虚拟IP
-p 12000  //指定端口号
-s //指定主缓存服务器
-b //指定从缓存服务器

[[email protected] shell]# chmod +x magent.sh   // 增加执行权限//

9.在从服务器上操作

[[email protected] bin]# mkdir /opt/shell
[[email protected] bin]# cd /opt/shell/
[[email protected] shell]# vim magent.sh
[[email protected] shell]# vim magent.sh
脚本内容如下,与主服务器脚本有区别!
#!/bin/bash
K=`ip addr | grep 192.168.126.188 | grep -v grep | wc -l`
if [ $K -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.126.188 -p 12000 -s 192.168.126.138:11211 -b 192.168.126.166:11211
else
pkill -9 magent
fi  
[[email protected] shell]# chmod +x magent.sh 

10.开始验证

1)启动主服务器

[[email protected] shell]# systemctl start keepalived.service 
[[email protected] shell]# netstat -ntap | grep 12000  //确认magent运行//
tcp        0      0 192.168.126.188:12000   0.0.0.0:*               LISTEN      12422/magent 

2)启动从服务器

[[email protected] shell]# systemctl start keepalived.service 
[[email protected] shell]# netstat -ntap | grep 12000
tcp        0      0 192.168.126.188:12000   0.0.0.0:*               LISTEN      11716/magent  

3)在主服务器上使用telnet进行简单验证复制功能

[[email protected] shell]# telnet 192.168.126.188 12000  //用漂移地址登陆服务//
Trying 192.168.126.188...
Connected to 192.168.126.188.
Escape character is ‘^]‘.
add username 0 0 7      //添加一条键值数据//
1234567
STORED

在从服务器上查看
[[email protected] shell]# telnet 192.168.126.188 12000 
Trying 192.168.126.188...
Connected to 192.168.126.188.
Escape character is ‘^]‘.
get username    //查看键值数据
VALUE username 0 7
1234567         //内容存在,写入成功//
END

11.在客户端用漂移地址登陆服务

[[email protected] ~]# yum install telnet -y
[[email protected] ~]# telnet 192.168.126.188 12000 
Trying 192.168.126.188...
Connected to 192.168.126.188.
Escape character is ‘^]‘.
add username 0 0 8    //添加一条键值数据//
12345678
STORED

1)在主服务器和从服务器上查看是否写入成功。

主服务器
get username
VALUE username 0 8
12345678
END

从服务器
get username
VALUE username 0 8
12345678
END

2)把主服务器停了业务不影响

[[email protected] shell]# systemctl stop keepalived.service
[[email protected] shell]# ip addr
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33

3)在从服务器上查看

[[email protected] shell]# ip addr
inet 192.168.126.166/24 brd 192.168.126.255 scope global dynamic ens33
       valid_lft 1146sec preferred_lft 1146sec
    inet 192.168.126.188/32 scope global ens33
可以看到漂移地址已经转移到从服务器上了,说明从已接受工作。

3)再把主服务器开启

[[email protected] shell]# systemctl start keepalived.service 
[[email protected] shell]# ip addr
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
       valid_lft 1145sec preferred_lft 1145sec
    inet 192.168.126.188/32 scope global ens33
       valid_lft forever preferred_lft forever
漂移地址再次转移到主服务器上,接手地址,服务依然不受影响。

实验成功

以上是关于在CentOS7上部署Memcached主主复制+Keepalived高可用架构的主要内容,如果未能解决你的问题,请参考以下文章

领会Memcached高可用群集(Memcached主主复制+Keepalived)

memcached实现主主复制和keepalive高可用方式

Memcached 主主复制 + Keepalived 高可用架构附上原理

Memcached 主主复制+ Keepalived 实现 Memcached 高可用架构集群

Linux-Memcached主主复制+Keepalived高可用

Memcached高可用群集(Memcached主主复制+Keepalived)