nan00zzu 2019-12-25
[ ~]# mkdir /abc [ ~]# mount.cifs //192.168.100.8/memcached /abc //挂载 Password for //192.168.100.8/memcached: [ ~]# cd /abc/ [ 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 [ abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt //解压事件库软件包,memcached依赖于事件库 [ abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt/ //解压服务端memcached软件包 [ abc]# yum install gcc gcc-c++ make -y //安装环境包 [ abc]# mkdir /opt/magent [ abc]# tar zxvf magent-0.5.tar.gz -C /opt/magent/ [ opt]# cd libevent-2.1.8-stable/ [ libevent-2.1.8-stable]# ./configure --prefix=/usr [ libevent-2.1.8-stable]# make && make install //编译安装 [ libevent-2.1.8-stable]# cd ../memcached-1.5.6/ [ memcached-1.5.6]# ./configure --with-libevent=/usr make && make install
[ ~]# mkdir /abc [ ~]# mount.cifs //192.168.100.8/memcached /abc Password for //192.168.100.8/memcached: [ ~]# cd /abc/ [ 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 [ abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt //解压事件库软件包 [ abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt/ /解压服务端memcached软件包 [ abc]# yum install gcc gcc-c++ make -y [calhost opt]# cd libevent-2.1.8-stable/ [ libevent-2.1.8-stable]# ./configure --prefix=/usr [ libevent-2.1.8-stable]# make && make install [ libevent-2.1.8-stable]# cd ../memcached-1.5.6/ [ memcached-1.5.6]# ./configure --with-libevent=/usr make && make install
[ memcached-1.5.6]# cd /opt/ [ opt]# ls libevent-2.1.8-stable magent memcached-1.5.6 rh [ opt]# cd magent/ [ magent]# vim ketama.h #ifndef SSIZE_MAX #define SSIZE_MAX 32767 [ magent]# vim Makefile LIBS = -levent -lm //指定makefile文件 make //编译 [ magent]# ls ketama.c ketama.h ketama.o magent magent.c magent.o Makefile [ magent]# yum install openssh-clients -y //安装scp远程同步软件包 [ magent]# cp magent /usr/bin/ //把magent脚本放到/usr/local中,让系统能识别 [ magent]# scp magent :/usr/bin/ //把mangent文件拷贝到从服务器上
[ bin]# systemctl stop firewalld.service [ bin]# setenforce 0
[ bin]# yum install keepalived -y
[ magent]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script magent { //编辑函数脚本
script "/opt/shell/magent.sh" //指定脚本位置
interval 2 //检测脚本时间间隔
}
global_defs {
notification_email {
}
notification_email_from
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 //客户端访问的漂移地址
}
}[ bin]# cd /etc/keepalived/ [ keepalived]# mv keepalived.conf keepalived.conf.bak //更改名称 [ keepalived]# yum install openssh-clients -y
[ magent]# cd /etc/keepalived/ [ keepalived]# scp keepalived.conf :/etc/keepalived/
[ keepalived]# ls
keepalived.conf keepalived.conf.bak
[ keepalived]# vim keepalived.conf
! Configuration File for keepalived
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
global_defs {
notification_email {
}
notification_email_from
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
}
}[ keepalived]# mkdir /opt/shell
[ keepalived]# cd /opt/shell/
[ 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
[ shell]# chmod +x magent.sh
[ shell]# systemctl start keepalived.service
[ shell]# netstat -ntap | grep 12000
tcp 0 0 192.168.144.188:12000 0.0.0.0:* LISTEN 124720/magent[ keepalived]# mkdir /opt/shell
[ keepalived]# cd /opt/shell/
[ 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
[ shell]# chmod +x magent.sh
[ shell]# systemctl start keepalived.service
[ shell]# netstat -ntap | grep 12000 //查看magent端口
tcp 0 0 192.168.144.188:12000 0.0.0.0:* LISTEN 11660/magent[ shell]# memcached -m 512k -u root -d -l 192.168.144.238 -p 11211 //启动主,-m指定空间大小 [ shell]# netstat -ntap | grep 11211 tcp 0 0 192.168.144.238:11211 0.0.0.0:* LISTEN 44647/memcached
[ shell]# memcached -m 512k -u root -d -l 192.168.144.239 -p 11211 #启动从 [ shell]# netstat -ntap | grep 11211 tcp 0 0 192.168.144.239:11211 0.0.0.0:* LISTEN 42654/memcached
[ ~]# 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
[ 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
[ 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