成都创新互联网站制作重庆分公司

Memcached集群

配置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集群

成都创新互联公司是一家业务范围包括IDC托管业务,虚拟空间、主机租用、主机托管,四川、重庆、广东电信服务器租用,成都天府联通服务器托管,成都网通服务器托管,成都服务器租用,业务范围遍及中国大陆、港澳台以及欧美等多个国家及地区的互联网数据服务公司。

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集群

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

新闻名称:Memcached集群
标题网址:http://cxhlcq.com/article/jidpid.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部