Memcached High-availability clusters
Primary server installation 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.cmagent.cketama.hMakefile[[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.cgcc -Wall -O2 -g -c -o ketama.o ketama.cgcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm
Installing openssh-clients remote connections from a server
[[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: NOKEYPreparing... ################################# [100%] package openssh-clients-7.4p1-11.el7.x86_64 is already installed
Remotely copy the Magent files generated by the compilation on the master server to the slave server
[[email protected] opt] lsketama.c ketama.o magent magent.o memcached-1.5.6ketama.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)? yesWarning: Permanently added ‘192.168.32.218‘ (ECDSA) to the list of known hosts.[email protected]‘s password: magent 100% 112KB 10.5MB/s
Installing keepalived on the master and slave servers
#主从都要装! Configuration File for Keepalivedglobal_defs {notification_email {[email protected] [email protected ] [email protected]} notification_email_from [email protected] smtp_server 192.168.200.1 Smtp_conne Ct_timeout 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-Advert_int 1 Authentication {A Uth_type PASS auth_pass 1111} track_script {magent} virtual_ipaddress {192.168.32.188 }} #下面多余的部分可以全部删除 # Modify the keepalived.conf configuration file from the server to the backup file with MV [[email protected] memcached-1.5.6]# cd/etc/keepalived /[[email protected] keepalived]# lskeepalived.conf[[email protected] keepalived]# mv keepalived.conf Keepalived.conf.bak[[email protected] keepalived]# lskeepalived.conf.bak# Use SCP to remotely replicate the keepalived.conf configuration file on the master server to the/etc/keeplived/from the server [[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]# lskeepalived.conf keepalived.conf.bak# Modify keepalived.conf configuration file router_id 2 #改为2 State BACKUP #master改为BACKUP Priority #优先级改为小于100
Configure the magent.sh script inside the configuration file (note that the path is the same as the path inside the configuration file)
主[[email protected] opt] mkdir shell[[email protected] opt] cd shell/[[email protected] shell] vim magent.sh#!/bin/bashK=`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:11211elsepkill -9 magentfi-n 51200 //定义用户最大连接数-l 192.168.x.x //指定虚拟IP-p 12000 //指定端口号-s //指定主缓存服务器-b //指顶从缓存服务器[[email protected] shell] chmod +x magent.sh从#!/bin/bashK=`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:11211elsepkill -9 magentfi
Open keepalived
[[email protected] shell] systemctl start keepalived.service# use IP addr to see if the virtual IP is bound successfully [[email protected] Shell ]# IP addr1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN Qlen 1 Link/loopback 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 forever2:ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc Pfifo_fast state up Qlen 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 EN S33 Valid_lft Forever Preferred_lft forever inet6 fe80::20c:29ff:feb9:5108/64 scope link Valid_lft forever Preferred_lft forever# View 12000 port on the primary server has no on [[email protected] shell]# netstat-ntapactive Internet connections ( Servers and established) Proto recv-Q send-q Local Address Foreign address State Pid/program name TCP 0 0 192.168.32.21 9: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 1 92.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 This 12000 port is a virtual port used to log in using from the server without this port if the primary server is down after the 12000 virtual port is automatically bound to the slave server for no impact login
Log on to the memcached server
memcached -m 512k -u root -d -l 192.168.32.219 主 -p 11211memcached -m 512k -u root -d -l 192.168.32.218 从 -p 11211netstat -ntap |grep 11211有11211端口表示服务开启成功
Memcache Cache server Storage Data application
add username 0 0 7 //不进行压缩和序列化标识 数据过期时间为永不过期 标识号是7就需要输入7位数。example //输入数据get username //获取数据VALUE username 0 7examplegets usernameVALUE username 0 7 1 //最后一位是更新因子会自增1exampleset username 0 0 10 //更新信息,若键名不存在,则自行添加everythingreplace username 0 0 8 //更新信息,若键名不存在,则报错12345678gets usernameVALUE username 0 8 412345678cas username 0 0 7 4 //检查更新,更新因子相等则更新否则返回EXISTSlodgingSTOREDappend username 0 0 7 //键值后追加数据exampleSTOREDprepend username 0 0 2 //键值前追加数据unSTOREDdelete usernameflush_all //清除所有缓存数据OKstats //显示状态信息quit //退出
Memcached High-availability clusters