memcached集群

前言

Memcached虽然能够通过分布式缓存,实现其中memcached宕掉不会丢失全部缓存数据,但部分数据还是会有缺失,本文主要利用magent代理来实现memcached主从备份来保证缓存数据完整性

实验环境

IP地址部署

主机名 IP地址
master 192.168.7.128
backup 192.168.7.129
客户端 192.168.7.134

实验步骤

1、主服务器配置
(1)安装环境包

[root@master memcached]# yum install gcc gcc-c++ make -y

(2)安装事件库

[root@master memcached]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
[root@master memcached]# cd /opt/libevent-2.1.8-stable/
[root@master libevent-2.1.8-stable]# ./configure --prefix=/usr
[root@master libevent-2.1.8-stable]# make && make install

(3)安装memcached

[root@master memcached]# tar zxvf memcached-1.5.6.tar.gz -C /opt
[root@master memcached]# cd /opt/memcached-1.5.6/
[root@master memcached-1.5.6]# ./configure --with-libevent=/usr
[root@master memcached-1.5.6]# make &&make install

(4)安装magent

[root@master memcached]# mkdir /opt/magent
[root@master memcached]# cd /opt/magent/
[root@master magent]# vim ketama.h
#更改开头的两行
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
添加#endif,将删除最后一行的#endif删掉

[root@master magent]# vim Makefile
LIBS = -levent -lm

[root@master magent]# make

[root@master magent]# cp magent /usr/bin/
#将/opt/magent/magent文件复制到从服务器上
[root@master magent]# scp magent [email protected]:/usr/bin

(5)安装keepalived

[root@master ~]# yum install keepalived -y
更改keepalived主配置文件
[root@master ~]# vim /etc/keepalived/keepalived.conf
在第二行添加
vrrp_script magent {
        script "/opt/shell/magent.sh"	//此脚本手动添加
        interval 2
}

global_defs {
···
   router_id MAGENT_HA		//从服务器改为MAGET_HB
}

vrrp_instance VI_1 {
    state MASTER		//从服务器改为BACKUP
    interface ens33
    virtual_router_id 51	//从服务器ID不同
    priority 100		//从服务器优先级比主服务要低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    } 
#添加  
    track_script {
        magent
    }   
    virtual_ipaddress {
        192.168.7.188		//定义漂移地址
    }   
}
#剩下的全部删除
[root@master ~]# mkdir /opt/shell
[root@master ~]# cd /opt/shell/
[root@master shell]# vim manage.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.7.188 -p 12000 -s 192.168.7.128:11211 -b 192.168.7.129:11211
else
pkill -9 magent
fi

[root@master shell]# chmod +x manage.sh 
[root@master shell]# systemctl start keepalived

2、从服务器与主服务器安装一致
3、客户端测试

[root@client ~]# telnet 192.168.7.188 12000
Trying 192.168.7.188...
Connected to 192.168.7.188.
Escape character is '^]'.
add user 0 0 4
test
STORED
get user
VALUE user 0 4
test
END

4、关闭主服务器,可正常连接

[root@client ~]# telnet 192.168.7.188 12000
Trying 192.168.7.188...
Connected to 192.168.7.188.
Escape character is '^]'.
get user
VALUE user 0 4
test
END
add bao 0 0 1
2
STORED
get bao
VALUE bao 0 1
2
END
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章