温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Memcached集群

发布时间:2020-04-24 05:38:57 来源:网络 阅读:540 作者:无双战绩 栏目:云计算
配置memcached主缓存节点和从缓存节点-----两台配置相同
开启三台虚拟机:
192.168.80.100      配置memcached主缓存节点 
192.168.80.101      配置memcached从缓存节点
192.168.80.102
1.关闭防火墙机及Selinux[重要](三台开始都要启动)
systemctl stop firewalld          //关闭防火墙
setenforce 0                           //关闭监控
2.安装环境包
yum install gcc gcc-c++ make -y

Memcached集群

3.编译安装libevent(必须先安装)
安装上传软件
yum install lrz* -y

Memcached集群

#解压
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/

Memcached集群

#配置
./configure --prefix=/usr/local/libevent
#编译及安装
make && make install
3.编译安装memcached
#切换到memcached
cd /opt/memcached-1.5.6/
#配置(指定libevent路径)
./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent/
#编译及安装
make && make install
4.优化启动(支持复制功能的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 
5.分别在两台服务器上启动memcached
#主服务器
cd /usr/local/memcached/bin/
./memcached -d -m 32m -l 192.168.80.100 -p 11211 -u root

#从服务器
cd /usr/local/memcached/bin/
./memcached -d -m 32m -l 192.168.80.101 -p 11211 -u root

6.检查memcached是否启动
netstat -anpt | grep memcached 
netstat -ntap | grep 11211

Memcached集群Memcached集群

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

Memcached集群

5.编辑ketama.h,添加文件头部信息
    vi ketama.h
    #ifndef SSIZE_MAX
    #define SSIZE_MAX 32767

Memcached集群

6.修改Makefile
vi Makefile     //libevent安装路径在/usr/local/libevent中使用以下内容:
LIBS = -levent -lm  -L /usr/local/libevent/lib
INCLUDE=-I /usr/local/libevent/include

Memcached集群

Magent + Keepalived实现Memcached高可用群集
7.编译(完成后会生成一个manage的可执行文件)
make

Memcached集群

8.复制mgent命令到系统管理中,便于使用
cp /opt/magent/magent /usr/bin/
9.发送至从服务器
scp /opt/magent/magent root@192.168.80.101:/usr/bin

Memcached集群

安装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 ens32             #本地网卡名称
    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
    }
}

Memcached集群

2.建立shell目录
mkdir /root/shell
3.创建magent.sh脚本
vi /root/shell/magent.sh
#!/bin/bash

Keepalived=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.100:11211 -b 192.168.80.101:11211
else
        pkill -9 magent
fi
参数说明:
      -n 51200                #定义用户最大连接数
      -l 192.168.175.188                #指定虚拟IP
      -p 12000                   #指定端口号
      -s                              #指定主缓存服务器
      -b                              #指定从缓存服务器

Memcached集群

4.赋予脚本执行权限
chmod +x /root/shell/magent.sh
5.启动keepalived服务
systemctl enable keepalived
systemctl start keepalived
netstat -anpt | grep 12000

Memcached集群

6.检查keepalived服务状态
systemctl status keepalived

Memcached集群

7.检查vip地址
ip addr show ens32

Memcached集群

netstat -anpt | grep 11211

Memcached集群

在从服务器上操作
1.安装keepalived
yum -y install keepalived
2.将主服务器上keepalived配置文件复制过来
scp root@192.168.80.100:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf 

Memcached集群

3.编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
      router_id magent_H2              #指定router_id
}

vrrp_script magent {                #定义函数
    script "/root/shell/magent.sh"
    interval 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33             #本地网卡名称
    virtual_router_id 51        #id主从一致
    priority 90             #主优先级大于从优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {         #vip地址
        192.168.80.188
    }
    track_script {              #调用函数
        magent
    }
}

Memcached集群

4.新建shell目录
mkdir /root/shell
5.创建magent.sh脚本
vi /root/shell/magent.sh
#!/bin/bash

Keepalived=`ip addr | grep 192.168.80.188 | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.100:11211 -b 192.168.80.101:11211
else
        pkill -9 magent
fi

Memcached集群

6.赋予脚本执行权限
chmod +x /root/shell/magent.sh
7.启动keepalived服务
systemctl enable keepalived
systemctl start keepalived

Memcached集群

8.检查keepalived服务状态
systemctl status keepalived

Memcached集群

9.检查vip地址
ip addr show ens32

Memcached集群

在客户端测试
1.安装telnet客户端
yum install telnet -y

Memcached集群

2.连接memcached
测试复制功能
//连接vip进行测试,插入user键值
telnet 192.168.80.188 12000
Trying 192.168.80.188...
Connected to 192.168.80.188.
Escape character is '^]'.
set user 0(标签) 0(缓存时间永不过期) 5(缓存内容)
12345
STORED
get user
VALUE user 0 5
12345
END
quit
Connection closed by foreign host.

//连接Memcached1,查询user键值,可以获取,成功!
telnet 192.168.80.100 11211
Trying 192.168.80.100...
Connected to 192.168.80.100.
Escape character is '^]'.
get user
VALUE user 0 5
12345
END
quit
Connection closed by foreign host.

Memcached集群

//连接Memcached2,查询user键值,也可以获取,成功!
[root@centos7-1 ~]# telnet 192.168.80.101 11211
Trying 192.168.80.101...
Connected to 192.168.80.101.
Escape character is '^]'.
get user
VALUE user 0 5
12345
END
quit
Connection closed by foreign host.

Memcached集群

测试单点故障
停止主服务器,去连接漂移地址,查看有木有出现内容
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI