文章目录
前言
一:环境介绍
1.1:环境
- VMware软件
- 一台centos7虚拟机作为memcache主服务器,IP地址:192.168.79.133
- 一台centos7虚拟机作为memcache从服务器,IP地址:192.168.79.134
- 一台centos7虚拟机作为memcache客户端,IP地址:192.168.79.135
- 漂移IP地址:192.168.79.100,是客户端登陆的IP地址
1.2:试验目的
- 通过搭建memcache高可用群集,实现主从同步,群集高可用功能
1.3:magent概述
- Magent是一款开源的Memcached代理服务器软件,采用的是:Consistent Hashing原理,可以通过它来实现缓存数据的同步
二:memcache主从服务器搭建
- 主服务器需要安装:memcached,libevent,keepalived,magent
- 从服务器需要安装:memcached,libevent,keepalived
2.1:主从服务器搭建memcached
-
以下的步骤主从服务器都需要操作,再次仅展示主服务器的操作
-
'//解压源码包' [root@master ~]# mount.cifs //192.168.23.1/ccc /mnt Password for root@//192.168.23.1/ccc: [root@master ~]# cd /mnt/memcache/ [root@master memcache]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt [root@master memcache]# tar zxvf memcached-1.5.6.tar.gz -C /opt [root@master memcache]# mkdir /opt/magent '//此步骤从服务器不需要操作' [root@master memcache]# tar zxvf magent-0.5.tar.gz -C /opt/magent/ '//此步骤从服务器不需要操作' '//编译安装libevent和memcached' [root@master memcache]# yum install gcc gcc-c++ make -y [root@master memcache]# cd /opt/libevent-2.1.8-stable/ [root@master libevent-2.1.8-stable]# ./configure --prefix=/usr '//路径不在/usr的话,后面magent在make的时候会报错:magent.c:64:19: 致命错误:event.h:没有那个文件或目录' [root@master libevent-2.1.8-stable]# make && make install [root@master libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/ [root@master memcached-1.5.6]# ./configure \ > --prefix=/usr/local/memcached \ > --with-libevent=/usr/local/libevent [root@master memcached-1.5.6]# make && make install '//关闭防火墙' [root@master memcached-1.5.6]# systemctl stop firewalld.service [root@master memcached-1.5.6]# setenforce 0
2.2:主服务器上安装magent代理
-
在主服务器上安装magent,从服务器不需要安装
-
[root@master memcached-1.5.6]# cd /opt/magent/ [root@master magent]# ls ketama.c ketama.h magent.c Makefile [root@master magent]# vim ketama.h '//修改magent配置文件' '//前两行修改如下' #ifndef SSIZE_MAX #define SSIZE_MAX 32767 ...省略内容 [root@master magent]# vim Makefile '//修改Makefile配置文件' LIBS = -levent -lm '//首行后面添加-lm' ...省略内容 [root@master magent]# make '//编译后会产生一个magent可执行程序' [root@master magent]# yum install openssh-clients -y [root@master magent]# cp magent /usr/bin/ '//将magent可执行程序复制到/usr/bin中' [root@master magent]# scp magent [email protected]:/usr/bin '//将magent可执行程序复制到从服务器的/usr/bin中' '//此命令需要输入yes,输入从服务器的密码才能将文件拷贝过去,根据提示操作即可'
2.3:主从服务器搭建keepalived
-
[root@master ~]# yum install keepalived -y '//主从服务器都要安装keepalived'
-
主服务器中keepalived.conf配置文件修改
[root@master ~]# 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 '//router_id自定义,不可与从服务器相同' } '//实例区域' vrrp_instance VI_1 { state MASTER '//主服务器为MASTER,从服务器要改为BACKUP' interface ens33 '//网卡改为本机网卡ens33' virtual_router_id 51 '//主不可相同,从服务器需要修改' priority 100 '//优先级要高于从服务器' advert_int 1 track_script { '//此三行为调用函数的段落,配置文件开头已经添加了函数,此处需要调用' magent } authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.79.100 '//定义漂移地址' } }
-
从服务器中keepalived.conf配置文件修改
[root@slave ~]# cd /etc/keepalived/ [root@slave keepalived]# mv keepalived.conf keepalived.conf.bak '//备份配置文件' '//回到主服务器,将配置文件复制到从服务器' [root@master ~]# scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived [root@slave keepalived]# vim keepalived.conf '//其他配置都相同,只需要修改如下几个配置' router_id MAGENT_HB '//id名和第一台要不一样,从服务器改为MAGENT_HB' state BACKUP '//从服务器为BACKUP' virtual_router_id 52 '//id号和第一台不一样即可' priority 90 '//优先级低与主服务器 '
-
在主从服务器创建magent脚本
'//主从服务器都要创建magent脚本' [root@master ~]# mkdir /opt/shell [root@master ~]# cd /opt/shell/ [root@master 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.79.100 -p 12000 -s 192.168.79.133:11211 -b 192.168.79.134:11211 else pkill -9 magent fi '//如下解释' -n 51200 '//定义用户最大连接数' -l 192.168.79.100 '//指定虚拟IP' -p 12000 '//指定端口号' -s '//指定主缓存服务器192.168.79.133:11211' -b '//指定从缓存服务器192.168.79.134:11211' 11211 '//端口号'
-
主从服务器启动服务
'//主从服务器都要做' [root@master shell]# chmod +x magent.sh '//增加magent脚本执行权限' [root@master shell]# systemctl start keepalived.service '//开启keepalived服务' [root@master shell]# netstat -ntap |grep 12000 '//keepalived启动会有点慢,我们需要稍等一下' tcp 0 0 192.168.79.100:12000 0.0.0.0:* LISTEN 67513/magent
-
验证主从
[root@master shell]# vim /var/log/messages 搜索'//Transition to MASTER STATE',有即成功 [root@master shell]# ip addr '//查看漂移地址是否绑定成功' ...省略内容 inet 192.168.79.100/32 scope global ens33 '//绑定成功 ' ...省略内容 [root@slave keepalived]# vim /var/log/messages 搜索'//Entering BACKUP STATE',有即成功 [root@slave keepalived]# ip addr '//查看漂移地址是否绑定成功' ...省略内容 inet 192.168.79.100/32 scope global ens33 '//绑定成功 ' ...省略内容
2.4:主从服务器开启memcache并测试本地连接
-
主从服务器开启memcache
-
'//主服务器开启' [root@master ~]# ln -s /usr/local/memcached/bin/* /usr/local/bin/ [root@master ~]# memcached -m 512k -u root -d -l 192.168.79.133 -p 11211 [root@master ~]# netstat -ntap |grep 11211 tcp 0 0 192.168.79.133:11211 0.0.0.0:* LISTEN 88178/memcached [root@master ~]# '//从服务器开启' [root@slave keepalived]# ln -s /usr/local/memcached/bin/* /usr/local/bin/ [root@slave keepalived]# memcached -m 512k -u root -d -l 192.168.79.134 -p 11211 [root@slave keepalived]# netstat -ntap |grep 11211 tcp 0 0 192.168.79.134:11211 0.0.0.0:* LISTEN 124927/memcached
-
测试本地能否连接memcache
'//主服务器测试' [root@master ~]# yum install telnet -y '//安装Telnet远程登陆程序' [root@master ~]# telnet 192.168.79.133 11211 Trying 192.168.79.133... '//连接成功' Connected to 192.168.79.133. Escape character is '^]'. quit '//退出' Connection closed by foreign host. '//从服务器测试' [root@slave keepalived]# yum install telnet -y [root@slave keepalived]# telnet 192.168.79.134 11211 Trying 192.168.79.134... '//连接成功' Connected to 192.168.79.134. Escape character is '^]'. quit '//退出' Connection closed by foreign host.
三:memcache客户端测试
-
客户端登陆memcache
[root@client ~]# yum install telnet -y [root@client ~]# telnet 192.168.79.100 12000 '//使用漂移地址登陆' Trying 192.168.79.100... Connected to 192.168.79.100. Escape character is '^]'.
-
测试主从同步
[root@client ~]# telnet 192.168.79.100 12000 Trying 192.168.79.100... Connected to 192.168.79.100. Escape character is '^]'. add username 0 0 5 '//客户端连接并新建一个数据' 12345 STORED '//返回主服务器查看是否同步生成数据' [root@master ~]# telnet 192.168.79.133 11211 Trying 192.168.79.133... Connected to 192.168.79.133. Escape character is '^]'. get username VALUE username 0 5 12345 '//主服务器成功生成' END '//返回从服务器查看是否同步生成数据' [root@slave keepalived]# telnet 192.168.79.134 11211 Trying 192.168.79.134... Connected to 192.168.79.134. Escape character is '^]'. get username VALUE username 0 5 12345 '//从服务器成功生成' END '//测试成功,主从都成功生成数据'
-
测试高可用群集
'//宕机主服务器,看从服务器是否正常使用' [root@master ~]# systemctl stop keepalived.service '//关闭主服务器keepalived服务' [root@client ~]# telnet 192.168.79.100 12000 '//客户端登陆成功' Trying 192.168.79.100... Connected to 192.168.79.100. Escape character is '^]'. set username UNSUPPORTED COMMAND set username 0 0 4 '//修改一下数据' 1234 STORED '//从服务器查看数据是否同步' get username VALUE username 0 4 1234 '//成功同步' END
-
高可用群集搭建成功