Memcached内存数据库群集配置(理论+实践篇)

发布时间:2020-07-05 08:25:41 作者:SiceLc
来源:网络 阅读:160

Memcached内存数据库

Memcached缓存机制

Memcached分布式

Memcached路由算法

求余数hash算法

一致性hash算法

Memcached是什么

memcached群集搭建实践

实验环境

主服务器安装memcached,libevent事件库,mamgent代理包

[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.8/memcached /abc     //挂载
Password for root@//192.168.100.8/memcached:  
[root@localhost ~]# cd /abc/
[root@localhost abc]# ls
LAMP-php5.6                   magent-0.5.tar.gz   memcached-1.5.6.tar.gz
libevent-2.1.8-stable.tar.gz  memcache-2.2.7.tgz
[root@localhost abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt   //解压事件库软件包,memcached依赖于事件库
[root@localhost abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt/     //解压服务端memcached软件包
[root@localhost abc]# yum install gcc gcc-c++ make -y     //安装环境包
[root@localhost abc]# mkdir /opt/magent
[root@localhost abc]# tar zxvf magent-0.5.tar.gz -C /opt/magent/
[root@localhost opt]# cd libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr 
[root@localhost libevent-2.1.8-stable]# make && make install       //编译安装
[root@localhost libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure --with-libevent=/usr
make && make install

从服务器安装memcached,libevent事件库

[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.8/memcached /abc
Password for root@//192.168.100.8/memcached:  
[root@localhost ~]# cd /abc/
[root@localhost abc]# ls
LAMP-php5.6                   magent-0.5.tar.gz   memcached-1.5.6.tar.gz
libevent-2.1.8-stable.tar.gz  memcache-2.2.7.tgz
[root@localhost abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt //解压事件库软件包
[root@localhost abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt/   /解压服务端memcached软件包
[root@localhost abc]# yum install gcc gcc-c++ make -y
[root@localhost opt]# cd libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr

[root@localhost libevent-2.1.8-stable]# make && make install
[root@localhost libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure --with-libevent=/usr
make && make install

主服务器配置magent

[root@localhost memcached-1.5.6]# cd /opt/
[root@localhost opt]# ls
libevent-2.1.8-stable  magent  memcached-1.5.6  rh
[root@localhost opt]# cd magent/
[root@localhost magent]# vim ketama.h
#ifndef SSIZE_MAX 
#define SSIZE_MAX 32767 
[root@localhost magent]# vim Makefile
LIBS = -levent -lm              //指定makefile文件
make                 //编译
[root@localhost magent]# ls
ketama.c  ketama.h  ketama.o  magent  magent.c  magent.o  Makefile
[root@localhost magent]# yum install openssh-clients -y     //安装scp远程同步软件包
[root@localhost magent]# cp magent /usr/bin/      //把magent脚本放到/usr/local中,让系统能识别
[root@localhost magent]# scp magent root@192.168.144.239:/usr/bin/   //把mangent文件拷贝到从服务器上

主从都关闭防火墙

[root@localhost bin]# systemctl stop firewalld.service 
[root@localhost bin]# setenforce 0

在主服务器、从服务器同时安装

[root@localhost bin]# yum install keepalived -y

主服务器配置主从同步

[root@localhost magent]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

vrrp_script magent {          //编辑函数脚本
        script "/opt/shell/magent.sh"      //指定脚本位置
        interval 2              //检测脚本时间间隔
}

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        //主服务器id,两台不能一样
}

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 {               //调函数名magent
        magent
}       
    virtual_ipaddress {
        192.168.144.188        //客户端访问的漂移地址
    }   
}

从服务器安装openssh客户端

[root@localhost bin]# cd /etc/keepalived/
[root@localhost keepalived]# mv keepalived.conf keepalived.conf.bak   //更改名称
[root@localhost keepalived]# yum install openssh-clients -y

主服务器用scp把keepalived文件传到从服务器

[root@localhost magent]# cd /etc/keepalived/
[root@localhost keepalived]# scp keepalived.conf root@192.168.144.239:/etc/keepalived/

从服务器配置主从同步

[root@localhost keepalived]# ls
keepalived.conf  keepalived.conf.bak
[root@localhost keepalived]# vim keepalived.conf
! Configuration File for keepalived

vrrp_script magent {
        script "/opt/shell/magent.sh"
        interval 2
}

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_HB        //routed_id不能相同
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 52        //从服务器虚拟id不能和主服务器一样
    priority 90         //优先级比主服务器低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }   
    track_script {
        magent
}       
    virtual_ipaddress {
        192.168.144.188
    }   
}

主服务器配置magent脚本

[root@localhost keepalived]# mkdir /opt/shell
[root@localhost keepalived]# cd /opt/shell/
[root@localhost shell]# vim magent.sh
#!/bin/bash
k=`ps -ef | grep keepalived | grep -v grep | wc -l`     //检查keepaliveed进程,如果开启
if [ $k -gt 0 ]; then             
        magent -u root -n 51200 -l 192.168.144.188 -p 12000 -s 192.168.144.238:11211 -b
        // -n连接数量 -l指定漂移地址,-p指定端口映射到主从服务器的地址
        192.168.144.239:11211
else
pkill -9 magent
fi
[root@localhost shell]# chmod +x magent.sh 
[root@localhost shell]# systemctl start keepalived.service 
[root@localhost shell]# netstat -ntap | grep 12000
tcp        0      0 192.168.144.188:12000   0.0.0.0:*               LISTEN      124720/magent       

从服务器同样的操作

[root@localhost keepalived]# mkdir /opt/shell
[root@localhost keepalived]# cd /opt/shell/
[root@localhost shell]# vim 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.144.188 -p 12000 -s 192.168.144.238:11211 -b 192.168.144.239:11211
else
pkill -9 magent
fi
[root@localhost shell]# chmod +x magent.sh 
[root@localhost shell]# systemctl start keepalived.service 
[root@localhost shell]# netstat -ntap | grep 12000        //查看magent端口
tcp        0      0 192.168.144.188:12000   0.0.0.0:*               LISTEN      11660/magent 

开启主服务器memcached

[root@localhost shell]# memcached -m 512k -u root -d -l 192.168.144.238 -p 11211    //启动主,-m指定空间大小
[root@localhost shell]# netstat -ntap | grep 11211 
tcp        0      0 192.168.144.238:11211   0.0.0.0:*               LISTEN      44647/memcached

开启从服务器memecached

[root@localhost shell]# memcached -m 512k -u root -d -l 192.168.144.239 -p 11211 #启动从
[root@localhost shell]# netstat -ntap | grep 11211
tcp        0      0 192.168.144.239:11211   0.0.0.0:*               LISTEN      42654/memcached    

客户端中验证用telnet去连接memcached 内存数据库

[root@localhost ~]# telnet 192.168.144.188 12000
Trying 192.168.144.188...
Connected to 192.168.144.188.
Escape character is '^]'.
add username 0 0 7           //编写键值对数据
1234567 
STORED

查看主服务器查看数据有没有

[root@localhost shell]# telnet 192.168.144.238 11211
Trying 192.168.144.238...
Connected to 192.168.144.238.
Escape character is '^]'.
geer^H^H
ERROR
get username
VALUE username 0 7
1234567
END

验证从服务器数据有没有同步

[root@localhost shell]# telnet 192.168.144.239 11211
Trying 192.168.144.239...
Connected to 192.168.144.239.
Escape character is '^]'.
get username
VALUE username 0 7
1234567
END
推荐阅读:
  1. LVS负载均衡群集(理论)
  2. memcached内存数据库——部署及操作

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

linux memcached he

上一篇:php将某一模板内容解析过后,并获取其返回值

下一篇:cent os7搭建vsftp服务器配置

相关阅读

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

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