memcached完整的搭建集群过程

Memcached单机搭建详解传送门

集群 

  •  准备三个节点(伪集群-同一台机器的不同端口)

# ------节点----|--端口--|-节点名称

  192.168.16.37 | 11211 | node1

  192.168.16.37 | 11212 | node2

  192.168.16.37 | 11213 | node3

启动命令

# 节点1
[root@localhost memcached]# /mnt/memcached/bin/memcached -u root -p 11211 -v -d

# 节点2
[root@localhost memcached]# /mnt/memcached/bin/memcached -u root -p 11212 -v -d

# 节点3
[root@localhost memcached]# /mnt/memcached/bin/memcached -u root -p 11213 -v -d

# 查看进程
[root@localhost memcached]# ps -fe |grep -v grep  |  grep memcached
root     27617     1  0 12:43 ?        00:00:00 /mnt/memcached/bin/memcached -u root -p 11211 -v -d
root     27632     1  0 12:43 ?        00:00:00 /mnt/memcached/bin/memcached -u root -p 11212 -v -d
root     27647     1  0 12:43 ?        00:00:00 /mnt/memcached/bin/memcached -u root -p 11213 -v -d

PS: 此时仅仅是启动了三台Memcached缓存服务器,但并不能实现集群功能

引入集群插件 twemproxy(nutcracker)

  •  是 Twitter开源的轻量级 memcached / redis 代理服务器,本质就是一个集群管理工具,主要用来弥补 Redis和 Memcached对集群管理的不足,其完成的最大功劳就是通过在后端减少同缓存服务器的连接数从而增加吞吐量

 twemproxy 部署

 安装依赖

# 安装基础依赖autoreconf (需要的依赖)

[root@localhost mnt]# yum install autoconf

[root@localhost mnt]# yum install automake

# 当遇到以下错误的时候
configure.ac:36: error: possibly undefined macro: AC_PROG_LIBTOOL
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
autoreconf: /usr/bin/autoconf failed with exit status: 1


# 需要安装下面的依赖
[root@localhost mnt]# yum install libtool  

 安装 twemproxy代理

# 获取代理资源
[root@localhost mnt]# wget https://github.com/twitter/twemproxy/archive/master.zip

# 解压压缩包
[root@localhost mnt]# unzip master.zip

# 进入目录
[root@localhost mnt]# cd twemproxy-master

[root@localhost mnt]# autoreconf -fvi

# 指定安装目录
[root@localhost mnt]# ./configure --prefix=/mnt/twemproxy

# 执行make命令
[root@localhost mnt]# make && make install

# 生成以下文件
[root@localhost twemproxy]# ll
总用量 0
drwxr-xr-x 2 root root 24 7月  30 13:59 sbin  # 可执行命令文件夹
drwxr-xr-x 3 root root 17 7月  30 13:59 share

帮助文档 

# 查看帮助文档
[root@localhost sbin]# /mnt/twemproxy/sbin/nutcracker -h
Options:
-h, –help                        : 查看帮助文档,显示命令选项
-V, –version                     : 查看nutcracker版本
-t, –test-conf                   : 测试配置脚本的正确性
-d, –daemonize                   : 以守护进程运行
-D, –describe-stats              : 打印状态描述
-v, –verbosity=N                 : 设置日志级别 (default: 5, min: 0, max: 11)
-o, –output=S                    : 设置日志输出路径,默认为标准错误输出 (default: stderr)
-c, –conf-file=S                 : 指定配置文件路径 (default: conf/nutcracker.yml)
-s, –stats-port=N                : 设置状态监控端口,默认22222 (default: 22222)
-a, –stats-addr=S                : 设置状态监控IP,默认0.0.0.0 (default: 0.0.0.0)
-i, –stats-interval=N            : 设置状态聚合间隔 (default: 30000 msec)
-p, –pid-file=S                  : 指定进程pid文件路径,默认关闭 (default: off)
-m, –mbuf-size=N                 : 设置mbuf块大小,以bytes单位 (default: 16384 bytes)

 准备 twemproxy配置文件

  •  默认配置文件在conf文件夹nutcracker.yml中
# 创建文件夹
[root@localhost twemproxy]# mkdir conf

# 编辑配置文件
[root@localhost twemproxy]# vi nutcracker.yml
 

# 内容为
memcached:
  listen: 127.0.0.1:10010           #使用哪个端口启动Twemproxy
  hash: fnv1a_64                    #指定具体的hash函数
  distribution: ketama              #具体的hash算法
  timeout: 400                      #是否在结点无法响应的时候临时摘除结点
  backlog: 1024                     #超时时间(毫秒)
  preconnect: true                  #是否是Redis的proxy
  auto_eject_hosts: true            #重试的时间(毫秒)
  server_retry_timeout: 30000       #结点故障多少次就算摘除掉
  server_failure_limit: 3           #下面表示所有的Redis节点(IP:端口号:权重  别名)
  servers:
    - 192.168.16.37:11211:1
    - 192.168.16.37:11212:1
    - 192.168.16.37:11213:1

启动twemproxy

# 检查语法是否有问题
[root@localhost twemproxy]# ./nutcracker -t -c ../conf/nutcracker

# 控制台启动
[root@localhost twemproxy]# ./nutcracker -c ../conf/nutcracker

# 后台启动
[root@localhost twemproxy]# ./nutcracker -d -c ../conf/nutcracker

# 查看进程
[root@localhost sbin]# ps -fe | grep nutcracker
root      4234     1  0 14:39 ?        00:00:00 ./nutcracker -d -c ../conf/nutcracker.yml

 数据读/写测试

# 直连代理的端口
[root@localhost bin]# telnet localhost 10010
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set name 0 0 3
tom
STORED
get name
VALUE name 0 3
tom
END

# 连接每个memcached节点发现,最终的数据在端口为11212的节点上
[root@master ~]# telnet 192.168.16.37 11212
Trying 192.168.16.37...
Connected to 192.168.16.37.
Escape character is '^]'.
get name
VALUE name 0 3
tom
END

# 查看所有的memcache进程
[root@localhost bin]# ps -aux | grep -v grep |grep mem
root      6130  0.0  0.0 414980  2136 ?        Ssl  16:50   0:00 /mnt/memcached/bin/memcached -u root -p 11212 -v -d
root     27617  0.0  0.0 416008  3180 ?        Ssl  12:43   0:03 /mnt/memcached/bin/memcached -u root -p 11211 -v -d
root     27647  0.0  0.0 413952  3132 ?        Ssl  12:43   0:02 /mnt/memcached/bin/memcached -u root -p 11213 -v -d
# 杀掉11212的节点
[root@localhost bin]# kill -9 6130

[root@localhost bin]# ps -aux | grep -v grep |grep mem
root     27617  0.0  0.0 416008  3180 ?        Ssl  12:43   0:03 /mnt/memcached/bin/memcached -u root -p 11211 -v -d
root     27647  0.0  0.0 413952  3132 ?        Ssl  12:43   0:02 /mnt/memcached/bin/memcached -u root -p 11213 -v -d


# 在代理端口上重新存储age字段
[root@localhost bin]# telnet localhost 10010
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set age 0 0 2
24
STORED
get age
VALUE age 0 2
24
END


# 连接每个memcached节点发现,最终的数据在端口为11211的节点上(此时说明代理动态的做了跳转)
[root@elastic-slave ~]# telnet 192.168.16.37 11211
Trying 192.168.16.37...
Connected to 192.168.16.37.
Escape character is '^]'.
get age
VALUE age 0 2
12
END

PS: memcached的集群实际上数据没办法做到数据共享到各个节点上,当然似乎这个本身也符合缓存的定位,因为当缓存不存在的时候从真正的数据源获取数据

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值