首页 Memcached教程memcached集群部署

memcached安装

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

memcached集群部署架构

基于magent的部署架构

该部署方式依赖于magent实现高可用,应用端通过负载均衡服务器连接到magent,然后再由magent代理用户应用请求到memcached处理,底层的memcached为双主结构会自动同步数据,本部署方式存在magent单点问题因此需要两个magent做高可用。

memcached集群部署插图

项目站点:https://code.google.com/archive/p/memagent/

memcached集群部署插图1

Repcached实现原理

项目站点:http://repcached.sourceforge.net/

memcached集群部署插图2

在 master上可以通过 -X指定 replication port,在 slave上通过 -x/-X找到 master并 connect上去,事实上,如果
同时指定了 -x/-X, repcached一定会尝试连接,但如果连接失败,它就会用 -X参数来自己 listen(成为
master);如果 master坏掉, slave侦测到连接断了,它会自动 listen而成为 master;而如果 slave坏掉,
master也会侦测到连接断,它就会重新 listen等待新的 slave加入。
从这方案的技术实现来看,其实它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可
以相互同步,所以从功能上看,也可以认为它是双机 master-master方案。

简化后的部署架构

magent已经有很长时间没有更新,因此可以不再使用magent,直接通过负载均衡连接到memcached,仍然有两
台memcached做高可用,repcached版本的memcached之间会自动同步数据,以保持数据一致性,即使其中的一台memcached故障也不影响业务正常运行,故障的memcached修复上线后再自动从另外一台同步数据即可保持数据一致性。

memcached集群部署插图3

部署repcached
[root@centos7 ~]# yum install libevent libevent-devel
[root@centos7 ~]# wget https://jaist.dl.sourceforge.net/project/repcached/repcached/2.2.1-1.2.8/memcached-1.2.8-repcached-2.2.1.tar.gz
[root@centos7 ~]# tar xvf memcached-1.2.8-repcached-2.2.1.tar.gz
[root@centos7 ~]# cd memcached-1.2.8-repcached-2.2.1
[root@centos7 memcached-1.2.8-repcached-2.2.1]# ./configure --prefix=/usr/local/repcached --enable-replication
[root@centos7 memcached-1.2.8-repcached-2.2.1]# make #报错如下

memcached集群部署插图4

解决办法:

[root@centos7 memcached-1.2.8-repcached-2.2.1]# vim memcached.c
56 #ifndef IOV_MAX
57 #if defined(__FreeBSD__) || defined(__APPLE__)
58 # define IOV_MAX 1024
59 #endif
60 #endif
改为如下内容,即删除原有的第57,59行
56 #ifndef IOV_MAX
57 # define IOV_MAX 1024
58 #endif

再次编译安装:

[root@centos7 memcached-1.2.8-repcached-2.2.1]#make && make install 
[root@centos7 ~]#tree /apps/repcached/
/apps/repcached/
├── bin
│?? ├── memcached
│?? └── memcached-debug
└── share
    └── man
        └── man1
            └── memcached.1

4 directories, 3 files
[root@centos7 ~]#

memcached集群部署插图5

验证是否可执行
[root@centos7 ~]#echo 'PATH=/apps/repcached/bin:$PATH' > /etc/profile.d/repcached.sh
[root@centos7 ~]#. /etc/profile.d/repcached.sh
[root@centos7 ~]#memcached -h
memcached 1.2.8
repcached 2.2.1
-p <num>      TCP port number to listen on (default: 11211)
-U <num>      UDP port number to listen on (default: 11211, 0 is off)
-s <file>     unix socket path to listen on (disables network support)
-a <mask>     access mask for unix socket, in octal (default 0700)
-l <ip_addr>  interface to listen on, default is INDRR_ANY
-d            run as a daemon
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes, default is 64 MB
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections, default is 1024
-k            lock down all paged memory.  Note that there is a
              limit on how much memory you may lock.  Trying to
              allocate more than that would fail, so be sure you
              set the limit correctly for the user you started
              the daemon with (not for -u <username> user;
              under sh this is done with 'ulimit -S -l NUM_KB').
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-h            print this help and exit
-i            print memcached and libevent license
-P <file>     save PID in <file>, only used with -d option
-f <factor>   chunk size growth factor, default 1.25
-n <bytes>    minimum space allocated for key+value+flags, default 48
-R            Maximum number of requests per event
              limits the number of requests process for a given con nection
              to prevent starvation.  default 20
-b            Set the backlog queue limit (default 1024)
-x <ip_addr>  hostname or IP address of peer repcached
-X <num:num>  TCP port number for replication. <listen:connect> (default: 11212)
启动memcache
server 1相关操作
[root@centos7 ~]#useradd -r -s /sbin/nologin memcached
#-x 10.0.0.17为对端memcached的地址 
[root@centos7 ~]#memcached -d -m 2048 -p 11211 -u memcached  -c 2048 -x 10.0.0.17 
[root@centos7 ~]#ss -ntl
State      Recv-Q Send-Q          Local Address:Port                 Peer Address:Port   
LISTEN     0      100                 127.0.0.1:25                             *:*       
LISTEN     0      128                         *:11211                          *:*  
LISTEN     0      128                         *:11212                          *:*
LISTEN     0      128                         *:22                             *:*       
LISTEN     0      100                     [::1]:25                            [::]:*     
LISTEN     0      128                      [::]:11211                         [::]:*     
LISTEN     0      128                      [::]:22                            [::]:* 
server 2相关操作
[root@centos7 ~]#memcached -d -m 2048 -p 11211 -u memcached -c 2048 -x 10.0.0.7
[root@centos7 ~]#ss -ntl
State      Recv-Q Send-Q          Local Address:Port                Peer Address:Port     
LISTEN     0      100                 127.0.0.1:25                              *:*       
LISTEN     0      128                      *:11211                              *:*       LISTEN     0      128                         *:22                                *:*     
LISTEN     0      100                     [::1]:25                            [::]:*     
LISTEN     0      128                   [::]:11211                            [::]:*     
LISTEN     0      128                      [::]:22                            [::]:*
连接到memcache验证数据
shell命令
#在第一台memcached上创建数据
[root@centos7 ~]#telnet 10.0.0.7 11211
Trying 10.0.0.7...
Connected to 10.0.0.7.
Escape character is '^]'.
add name 0 0 4  #key 的名为name,且永不超时
wang
STORED
get name
VALUE name 0 4
wang
END
quit
Connection closed by foreign host.
[root@centos7 ~]#

#到另外一台memcached服务器验证是否有同步过来的数据
[root@centos7 ~]#telnet 10.0.0.17 11211 
Trying 10.0.0.17...
Connected to 10.0.0.17.
Escape character is '^]'.
get name
VALUE name 0 4
wang
END
quit
Connection closed by foreign host.
python 脚本连接memcached

示例:测试连接memcached的Python脚本

#!/usr/bin/env python
#coding:utf-8

import memcache
m = memcache.Client(['10.0.0.100:11211'], debug=True)
for i in range(100):
    m.set("key%d" % i,"v%d" % i)
    ret = m.get('key%d' % i)
    print ret

范例:

[root@centos7 ~]#yum -y install python-memcached 
[root@centos7 ~]#cat  ./m.py
#!/usr/bin/env python
#coding:utf-8

import memcache
m = memcache.Client(['10.0.0.7:11211'], debug=True)
for i in range(10):
    m.set("key%d" % i,"v%d" % i)
    ret = m.get('key%d' % i)
    print ret
[root@centos7 ~]#chmod +x m.py
[root@centos7 ~]#./m.py
v0
v1
v2
v3
v4
v5
v6
v7
v8
v9
[root@centos7 ~]#telnet 10.0.0.7 11211
Trying 10.0.0.7...
Connected to 10.0.0.7.
Escape character is '^]'.
get key1
VALUE key1 0 2
v1
END
get key6
VALUE key6 0 2
v6
END
get key10
END
quit
Connection closed by foreign host.

在第二台memcached上验证同步的数据
[root@centos7 ~]#telnet 10.0.0.17 11211
Trying 10.0.0.17...
Connected to 10.0.0.17.
Escape character is '^]'.
get key1
VALUE key1 0 2
v1
END
get key6
VALUE key6 0 2
v6
END
get key10
END
quit
Connection closed by foreign host.

本文链接:https://www.yunweipai.com/35557.html

memcached安装

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

暂无评论

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部