Memcached高可用群集
主服务器安装Memcached、magent、libevent
安装 libevent memcached magent (主服务器)
[[email protected] asd] cd /opt/libevent-2.1.8-stable
[[email protected] libevent-2.1.8-stable] ./configure --prefix=/usr/
[[email protected] asd] cd /opt/memcached-1.5.6
[[email protected] memcached-1.5.6] ./configure --with-libevent=/usr
#主服务器上需要libevent的这个模块
[[email protected] opt]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
#解压出来magent会解压出来以下几个文件
[[email protected] asd] tar zxvf magent-0.5.tar.gz -C /opt/
ketama.c
magent.c
ketama.h
Makefile
[[email protected] opt] vi ketama.h #修改开头两个声明,最后一行有#endif声明不需要做修改
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
[[email protected] opt] vim Makefile #修改开头声明文件
LIBS = -levent -lm
[[email protected] opt] make #把magent生成的文件让系统能识别
gcc -Wall -O2 -g -c -o magent.o magent.c
gcc -Wall -O2 -g -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm
在从服务器上安装openssh-clients远程连接
[[email protected] Packages] rpm -ivh openssh-clients-7.4p1-11.el7.x86_64.rpm
warning: /mnt/Packages/openssh-clients-7.4p1-11.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing... ################################# [100%]
package openssh-clients-7.4p1-11.el7.x86_64 is already installed
把主服务器上编译产生的magent的文件远程复制到从服务器上
[[email protected] opt] ls
ketama.c ketama.o magent magent.o memcached-1.5.6
ketama.h libevent-2.1.8-stable magent.c Makefile
[[email protected] opt] scp magent [email protected]:/usr/bin/
The authenticity of host ‘192.168.32.218 (192.168.32.218)‘ can‘t be established.
ECDSA key fingerprint is SHA256:gJPuAr8ixvbw23RR67aEnt+w9zT1936PhJNzk0nD/Bg.
ECDSA key fingerprint is MD5:1c:34:a1:74:e8:59:a3:54:7d:cd:61:bd:2d:7e:50:32.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.32.218‘ (ECDSA) to the list of known hosts.
[email protected]‘s password:
magent 100% 112KB 10.5MB/s 00:00
在主从服务器上安装keepalived
#主从都要装
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id MAGENT_HA
}
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
magent
}
virtual_ipaddress {
192.168.32.188
}
}
#下面多余的部分可以全部删除
#把从服务器上的keepalived.conf配置文件用mv修改为备份文件
[[email protected] memcached-1.5.6]# cd /etc/keepalived/
[[email protected] keepalived]# ls
keepalived.conf
[[email protected] keepalived]# mv keepalived.conf keepalived.conf.bak
[[email protected] keepalived]# ls
keepalived.conf.bak
#用scp远程复制主服务器上的keepalived.conf配置文件到从服务器上/etc/keeplived/底下
[[email protected] keepalived]# scp keepalived.conf [email protected]:/etc/keepalived/
[email protected]‘s password:
keepalived.conf 100% 634 309.0KB/s 00:00
#查看从服务器上有没有这个配置文件
[[email protected] keepalived]# ls
keepalived.conf keepalived.conf.bak
#修改keepalived.conf配置文件
router_id 2 #改为2
state BACKUP #master改为BACKUP
priority 90 #优先级改为小于100
配置配置文件里面的magent.sh脚本(注意路径要与配置文件里面路径相同)
主
[[email protected] opt] mkdir shell
[[email protected] opt] cd shell/
[[email protected] 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.32.188 -p 12000 -s 192.168.32.219:11211 -b 192.168.32.218:11211
else
pkill -9 magent
fi
-n 51200 //定义用户最大连接数
-l 192.168.x.x //指定虚拟IP
-p 12000 //指定端口号
-s //指定主缓存服务器
-b //指顶从缓存服务器
[[email protected] shell] chmod +x magent.sh
从
#!/bin/bash
K=`ip addr | grep 192.168.32.188 | grep -v grep | wc -l`
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.175.188 -p 12000 -s 192.168.175.128:11211 -b 192.168.175.132:11211
else
pkill -9 magent
fi
开启keepalived
[[email protected] shell] systemctl start keepalived.service
#用ip addr查看虚拟ip有没有绑定成功
[[email protected] shell]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:b9:51:08 brd ff:ff:ff:ff:ff:ff
inet 192.168.32.219/24 brd 192.168.32.255 scope global dynamic ens33
valid_lft 1238sec preferred_lft 1238sec
inet 192.168.32.188/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feb9:5108/64 scope link
valid_lft forever preferred_lft forever
#查看12000端口在主服务器上有没有开启
[[email protected] shell]# netstat -ntap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.32.219:11211 0.0.0.0:* LISTEN 27084/memcached
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1005/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1469/master
tcp 0 0 192.168.32.188:12000 0.0.0.0:* LISTEN 27418/magent
tcp 0 0 192.168.32.219:55552 192.168.32.1:445 ESTABLISHED -
tcp 0 52 192.168.32.219:22 192.168.32.1:52961 ESTABLISHED 26820/sshd: [email protected]
tcp6 0 0 :::22 :::* LISTEN 1005/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1469/master
这个12000端口是虚拟端口用于登陆使用
从服务器没有这个端口
如果主服务器down掉后
12000虚拟端口会自动绑定到从服务器上用于
不影响登陆
登录Memcached服务器
memcached -m 512k -u root -d -l 192.168.32.219 主 -p 11211
memcached -m 512k -u root -d -l 192.168.32.218 从 -p 11211
netstat -ntap |grep 11211
有11211端口表示服务开启成功
Memcache缓存服务器存储数据应用
add username 0 0 7 //不进行压缩和序列化标识 数据过期时间为永不过期 标识号是7就需要输入7位数。
example //输入数据
get username //获取数据
VALUE username 0 7
example
gets username
VALUE username 0 7 1 //最后一位是更新因子会自增1
example
set username 0 0 10 //更新信息,若键名不存在,则自行添加
everything
replace username 0 0 8 //更新信息,若键名不存在,则报错
12345678
gets username
VALUE username 0 8 4
12345678
cas username 0 0 7 4 //检查更新,更新因子相等则更新否则返回EXISTS
lodging
STORED
append username 0 0 7 //键值后追加数据
example
STORED
prepend username 0 0 2 //键值前追加数据
un
STORED
delete username
flush_all //清除所有缓存数据
OK
stats //显示状态信息
quit //退出
原文地址:http://blog.51cto.com/13645280/2152416
时间: 2024-10-30 21:33:24