三台服务器:

memcached主缓存服务器  仅主机模式  192.168.80.181

memcached从缓存服务器  仅主机模式  192.168.80.182

客户端(telnet)  192.168.80.183

  软件包 


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

1.关闭防火墙机及Selinux[重要]

systemctl stop firewalld  setenforce 0

2.安装环境包

yum -y install gcc gcc-c++ make

3.编译安装libevent(必须先安装)

#解压

tar xf libevent-2.1.8-stable.tar.gz -C /opt
tar xf memcached-1.5.6.tar.gz -C /opt/

#切换到libevent目录

cd /opt/libevent-2.1.8-stable/

#配置

./configure --prefix=/usr/local/libevent

#编译及安装

make && make install

4.编译安装memcached

#切换到memcached

cd /opt/memcached-1.5.6/

#配置(指定libevent路径)

./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent/

5.优化启动(支持复制功能的Mencached需要安装后的libevent-2.1.so.6模块,不然启动服务会报错)

ln -s /usr/local/libevent/lib/libevent-2.1.so.6.0.2  /usr/lib64/libevent-2.1.so.6

6.分别在两台服务器上启动memcached

#主服务器

./memcached -m 512k -u root -d -l 192.168.80.181 -p 11211

#从服务器

./memcached -m 512k -u root -d -l 192.168.80.182 -p 11211

7.检查memcached是否启动

netstat -ntap | grep 11211

image.png


magent在主服务器安装,从服务器不用安装

编译安装magent

1.创建magent目录

mkdir /opt/magent

2.解压

tar xf magent-0.5.tar.gz -C /opt/magent

3.切换到目录

cd /opt/magent

4.让动态链接库为系统所共享

/sbin/ldconfig

5.编辑ketama.h,添加文件头部信息

    vi ketama.h

    #ifndef SSIZE_MAX

    #define SSIZE_MAX 32767
   image.png

6.修改Makefile

vi Makefile     //libevent安装路径在/usr/local/libevent中使用以下内容:

LIBS = -levent -lm  -L /usr/local/libevent/lib
INCLUDE=-I /usr/local/libevent/include

image.png

Magent + Keepalived实现Memcached高可用群集

7.编译(完成后会生成一个manage的可执行文件)

make

image.png

8.复制mgent命令到系统管理中,便于使用

cp /opt/magent/magent /usr/bin/

9.发送至从服务器

scp /opt/magent/magent root@192.168.80.182:/usr/bin


安装keepalived(主从都安装)

主服务器:

yum -y install keepalived

1.编辑keepalived配置文件

vi /etc/keepalived/keepalived.conf

---

! Configuration File for keepalived

global_defs {

      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.80.188
    }

}

---

image.png

2.建立shell目录

mkdir /root/shell

3.创建magent.sh脚本

    vi /root/shell/magent.sh

---

#!/bin/bash

A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.181:11211 -b 192.168.80.182:11211
else
        pkill -9 magent
fi

---

image.png

参数说明:

        -n 51200                #定义用户最大连接数

        -l 192.168.175.188                #指定虚拟IP
        -p 12000                   #指定端口号
        -s                              #指定主缓存服务器
        -b                              #指定从缓存服务器

4.赋予脚本执行权限

chmod +x /root/shell/magent.sh

image.png

5.启动keepalived服务

systemctl enable keepalived

systemctl start keepalived

6.检查keepalived服务状态

systemctl status keepalived

7.检查vip地址

ip addr

image.png

从服务器:

1.安装keepalived

yum -y install keepalived

2.将主服务器上keepalived配置文件复制过来

scp root@192.168.80.181:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

3.编辑keepalived配置文件

image.png

image.png

image.png

4.建立shell目录

mkdir /root/shell

5.创建magent.sh脚本

    vi /root/shell/magent.sh

---

#!/bin/bash

A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.181:11211 -b 192.168.80.182:11211
else
        pkill -9 magent
fi

---

6.赋予脚本执行权限

chmod +x /root/shell/magent.sh

...


在客户端测试:

1.安装telnet客户端

yum -y install telnet

2.连接memcached

测试复制功能

//连接vip进行测试,插入user键值

image.png

//连接Memcached1,查询user键值,可以获取,成功!

image.png

//连接Memcached2,查询user键值,也可以获取,成功!

image.png