网站建设资讯

NEWS

网站建设资讯

Magent搭建Memcached高可用群集-创新互联

Magent缓存代理介绍

成都创新互联公司致力于互联网品牌建设与网络营销,包括做网站、成都网站制作、SEO优化、网络推广、整站优化营销策划推广、电子商务、移动互联网营销等。成都创新互联公司为不同类型的客户提供良好的互联网应用定制及解决方案,成都创新互联公司核心团队十年专注互联网开发,积累了丰富的网站经验,为广大企业客户提供一站式企业网站建设服务,在网站建设行业内树立了良好口碑。

     因为Memcached服务器与服务器之间没有任何通讯,所以当任何服务器节点出现故障时,会出现单点故障。通过Magent缓存代理,防止单点现象。通过客户端连接到缓存代理服务器,缓存代理服务器可以连接多台Memcached机器并同时将每台Memcached机器进行数据同步。如果其中一台缓存服务器down机,系统依然可以继续工作,如果其中一台Memcached机器down掉,数据不会丢失并且可以保证数据的完整性。

原理演示图:

Magent搭建Memcached高可用群集

实验环境介绍:

三台服务器:

master/slave/client

Magent搭建Memcached高可用群集

实验步骤

在master和slave上同时安装libevent、memcached

master&&slave:

systemctl stop firewalld.service

setenforce 0

tar zxvf memcached-1.5.6.tar.gz -C /opt

tar zxvf libevent-2.1.8-stable.tar.gz -C /opt

yum install gcc gcc-c++ -y

cd /opt/libevent-2.1.8-stable/

./configure --prefix=/usr/local

make && make install

cp /usr/local/lib/libevent-2.1.so.6 /usr/lib64/

cd /opt/memcached-1.5.6/

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

make && make install

在master上安装配置magent

master:

mkdir /opt/magent

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

vim /opt/magent/ketama.h

    #ifndef SSIZE_MAX

    #define SSIZE_MAX 32767

    #endif

vim /opt/magent/Makefile

    LIBS = -levent -lm                此处为英文单词‘l’,不是1

make

将magent命令复制到slave服务器上

注:标记地方为需要更改的位置

yum install openssl-clients -y

cp magent /usr/bin

scp magent root@192.168.218.157:/usr/bin

yum install keepalived -y

vim /etc/keepalived/keepalived.conf

      ! Configuration File for keepalived

       global_defs {

       notification_email {

       acassen@firewall.loc

       failover@firewall.loc

       sysadmin@firewall.loc

      }

       notification_email_from Alexandre.Cassen@firewall.loc

       smtp_server 192.168.200.1

       smtp_connect_timeout 30

       router_id MAGENT_HA

       }

      vrrp_script magent {

      script "/opt/shell/magent.sh"            脚本位置

      interval 2                                                 检测脚本为2秒

      }

      vrrp_instance VI_1 {

      state MASTER

      interface ens33

      virtual_router_id 51

      priority 100

     advert_int 1

     authentication {

     auth_type PASS

     auth_pass 1111

     }

     track_script {                  调用vrrp_script magent

          magent

    }

    virtual_ipaddress {

    192.168.218.16

     }

      }

从服务器上安装配置keepalived

slave:

yum install openssl-clients -y

yum install keepalived -y

cd /etc/keepalived/

mv keepalived.conf keepalived.conf.bk

master:

cd /etc/keepalived/

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

slave:

vim /etc/keepalived/keepalived.conf

           global_defs {

           router_id MAGENT_HB

          vrrp_instance VI_1 {

           state BACKUP

           virtual_router_id 52

            priority 90

master:

mkdir /opt/shell

vim /opt/shell/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.218.16 -p 12000 -s 192.168.218.130:11211 -b 192.168.218.157:11211          else             pkill -9 magent        fi

chmod +x /opt/shell/magent.sh

systemctl start keepalived.servic

netstat -ntap | grep 12000                 #此时检查应有12000端口号

slave:

mkdir /opt/shell

vim /opt/shell/magent.sh

       #!/bin/bash        K=`ip addr | grep 192.168.218.16 | grep -v grep | wc -l`        if [ $K -gt 0 ];then           magent -u root -n 51200 -l 192.168.218.16 -p 12000 -s 192.168.218.130:11211 -b 192.168.218.157:11211        else            pkill -9 magent        fi

#magent -u指定用户、-n指定连接数量、-l指定漂移地址、-p指定端口、-s指定主服务器地址、-b指定从服务器地址

chmod +x /opt/shell/magent.sh

systemctl start keepalived.servic

netstat -ntap | grep 12000                  #此时检查应有12000端口号

master:

memcached -m 512k -u root -d -l 192.168.218.130 -p 11211

netstat -ntap | grep 11211

slave:

memcached -m 512k -u root -d -l 192.168.218.157 -p 11211

netstat -ntap | grep 11211

测试:

master:

yum install telnet -y

telnet 192.168.218.130 11211

slave:

yum install telnet -y

telnet 192.168.218.157 11211

client:

yum install telnet -y

telnet 192.168.218.16 12000

        Trying 192.168.218.16...

        Connected to 192.168.218.16.

        Escape character is '^]'.

        add username 0 0 7               #写入一段数据

        1231231

         STORED

slave:

telnet 192.168.218.157 11211

         Trying 192.168.218.157...

         Connected to 192.168.218.157.

        Escape character is '^]'.

        get username

        VALUE username 0 7

       1231231                                 #在主服务器和从服务器上可以看到同步过来的数据

       END

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享标题:Magent搭建Memcached高可用群集-创新互联
当前网址:http://cdweb.net/article/iesho.html