干货,memcached+keepalived实现主主复制和高可用,详解!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了干货,memcached+keepalived实现主主复制和高可用,详解!相关的知识,希望对你有一定的参考价值。
keepalived+memcached主主复制高可用介绍:
客户端连接两台服务器之间的VIP地址,如果后端服务器故障自动切换。
Keepalive原理:
1.故障检测:
Keepalived的作用是检测memcached服务器的状态是否正常。
2.主从切换:
Keepalived如果检测到memcached服务发生DOWN机或者死机等,能将VIP从主服务器移至从服务器
3.Keepalived如何发现memcached异常:
1) keepalived在memcached主服务器产生一个虚拟IP
2)keepalived可以通过不断的检测memcached主服务器的11211端口是否正常工作,如果发现memcached Down机,虚拟IP就从主服务器移到从服务器
高可用架构应用场景:
如果memcached分布式节点比较多,那么完全不需要做基于复制的高可用架构。
基于复制的高可用架构一般用在memcached单节点存放缓存或者session。
部署环境介绍:
操作系统:Centos 7 64位
Node1:192.168.100.12 主节点
Node2:192.168.100.13 备节点
1、环境部署:
两台服务器部署方式一样,按照如下方式部署好即可
(1)安装memcached依赖libevent
[[email protected] ~]# yum -y install libevent libevent-devel
(2)安装c语言编译环境
[[email protected] ~]# yum -y install c++ gcc
(3)安装具有复制功能的memcached版本
wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz
tar zxvf memcached-1.2.8-repcached-2.2.tar.gz
cd memcached-1.2.8-repcached-2.2
./configure --prefix=/usr/local/memcached_replication --enable-replication
make
make install
注:编译报错解决
修改memcached.c文件即可:
修改前
[[email protected] memcached-1.2.8-repcached-2.2]#
vim memcached.c
56 #ifndef IOV_MAX
57 #if defined(__FreeBSD__) || defined(__APPLE__)
58 # define IOV_MAX 1024
59 #endif
60 #endif
修改后
56 #ifndef IOV_MAX
57 # define IOV_MAX 1024
58 #endif
安装完成目录结构如下:
[[email protected] ~]# tree /usr/local/memcached_replication/
/usr/local/memcached_replication/
├── bin
│ ├── memcached
│ └── memcached-debug
└── share
└── man
└── man1
└── memcached.1
查询memcached复制版本的的命令帮助只是比普通的memcached多了一个-x选项,就是指定复制的memcached服务器主机名或IP地址
[[email protected] ~]# /usr/local/memcached_replication/bin/memcached -h
-x <ip_addr> hostname or IP address of peer repcached
2、以主从复制架构启动memcached
1)创建memcached用户
[[email protected] ~]# useradd -r -s /sbin/nologin memcached
2)启动memcached,带复制功能
Node1节点启动:
[[email protected] ~]# cd /usr/local/memcached_replication/bin/
[[email protected] bin]# ./memcached -d -u memcached -m 512 -x 192.168.100.13
[[email protected] bin]# ps aux | grep mem
memcach+ 20208 0.0 0.0 11704 872 ? Ss 23:14 0:00 ./memcached -d -u memcached -m 512 -x 192.168.100.13
node2节点启动:
[[email protected] ~]# cd /usr/local/memcached_replication/bin/
[[email protected] bin]# ./memcached -d -u memcached -m 512 -x 192.168.100.12
[[email protected] bin]# ps aux | grep memcached
memcach+ 12162 0.0 0.0 11648 872 ? Ss 18:22 0:00 ./memcached -d -u memcached -m 512 -x 192.168.100.12
验证复制架构:
在node1节点插入数据
[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
set username 0 0 9
node1xmfb
STORED
get username
VALUE username 0 9
node1xmfb
END
Node2节点验证数据是否存在
[[email protected] ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is ‘^]‘.
get username
VALUE username 0 9
node1xmfb
END
如果在节点1写入数据节点2可以看到那么就说明memcached的主从复制架构是没有问题的,就可以在memcached主从复制的基础上使用keepalived产生VIP了。
3.Keepalived+memcached高可用架构部署与配置
1)keepalived包安装
yum install keepalived* -y
2)配置keepalived
Keepalived主节点配置文件设置如下所示:
[[email protected] bin]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id memcached_replication #路由标识,主从保持一致
}
vrrp_instance mem_1 { #定义VRRP热备实例
state MASTER #MASTER表示主节点
interface eth0 #承载VIP地址的物理接口
virtual_router_id 51 #虚拟路由器的ID号,主备相同
priority 100 #优先级,数值越高优先级越大
advert_int 1 #通知间隔秒数(心跳频率)
nopreempt #不主动抢占资源,只在master或者高优先级服务器设置
authentication { #认证信息配置,主从服务器保持一致
auth_type PASS #设置认证类型为密码
auth_pass asdfgh #设置认证的密码
}
# 定义VIP是什么
virtual_ipaddress {
192.168.100.100
}
}
# VIP故障检测
virtual_server 192.168.100.100 11211 {
deploy_loop 6
protocol TCP
# 配置主服务器地址端口
real_server 192.168.100.10 11211 {
weight 3
notify_down /shell/memcached.sh # 当memcached当机时,执行keepalived服务停止
}
TCP_CHECK {
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试时间间隔
connect_port 11211 #连接端口
}
}
keepalived备节点配置文件设置如下所示:
[[email protected] bin]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id memcached_replication #路由标识,主从保持一致
}
vrrp_instance mem_1 { #定义VRRP热备实例
state BACKUP #MASTER表示主节点
interface eth0 #承载VIP地址的物理接口
virtual_router_id 51 #虚拟路由器的ID号,主备相同
priority 50 #优先级,数值越高优先级越大
advert_int 1 #通知间隔秒数(心跳频率)
nopreempt #不主动抢占资源,只在master或者高优先级服务器设置
authentication { #认证信息配置,主从服务器保持一致
auth_type PASS #设置认证类型为密码
auth_pass asdfgh #设置认证的密码
}
# 定义VIP是什么
virtual_ipaddress {
192.168.100.100
}
}
virtual_server 192.168.100.100 11211 {
deploy_loop 6
protocol TCP
# 配置主服务器地址端口
real_server 192.168.100.11 11211 {
weight 3
notify_down /shell/memcached.sh
}
TCP_CHECK {
connect_timeout 3 #连接超时
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试时间间隔
connect_port 11211 #连接端口
}
}
两个节点都添加脚本:如果监听的memcached故障,那么就本地keepalived,实现VIP漂移
[[email protected] ~]# cat /shell/memcached.sh
systemctl stop keepalived
[[email protected] ~]# chmod +x /shell/memcached.sh
3)两个节点启动keepalived服务
[[email protected] ~]# systemctl start keepalived.service
4、高可用验证
验证vip已经启动
[[email protected] ~]# ip address show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:85:36:db brd ff:ff:ff:ff:ff:ff
inet 192.168.100.12/24 brd 192.168.100.255 scope global dynamic eth0
valid_lft 20059sec preferred_lft 20059sec
inet 192.168.100.100/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe85:36db/64 scope link
valid_lft forever preferred_lft forever
You have new mail in /var/spool/mail/root
强制关闭主节点memcached进程,可以看到vip地址已经不见了
[[email protected] ~]# ps aux | grep keepliaved
root 45424 0.0 0.0 112648 980 pts/0 S+ 11:24 0:00 grep --color=auto keepliaved
[[email protected] ~]# ps aux | grep mem
memcach+ 45419 0.0 0.1 12672 1924 ? Ss 11:22 0:00 ./memcached -d -u memcached -m 512 -x 192.168.100.13
root 45427 0.0 0.0 112648 976 pts/0 S+ 11:24 0:00 grep --color=auto mem
[[email protected] ~]# kill -9 45419
[[email protected] ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:85:36:db brd ff:ff:ff:ff:ff:ff
inet 192.168.100.12/24 brd 192.168.100.255 scope global dynamic eth0
valid_lft 16561sec preferred_lft 16561sec
inet6 fe80::20c:29ff:fe85:36db/64 scope link
valid_lft forever preferred_lft forever
在从节点查看,memcached地址又出现了。
[[email protected] ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9e:47:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.13/24 brd 192.168.100.255 scope global dynamic eth0
valid_lft 14759sec preferred_lft 14759sec
inet 192.168.100.100/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe9e:4761/64 scope link
valid_lft forever preferred_lft forever
操作成功,谢谢观看!
以上是关于干货,memcached+keepalived实现主主复制和高可用,详解!的主要内容,如果未能解决你的问题,请参考以下文章
Memcached+keepalived+magent实现主从复制和高可用
Centos7 下实现 memcached + keepalived 高可用群集
Magent + Keepalived实现Memcached高可用群集
memcached+magent+keepalived实现高可用群集