memcached + keepavlied + magent 实现高可用

系统环境

服务器名称
IP地址
操作系统
软件包
用途
VIP地址
nosql1.linuxplus.com
172.16.216.185
Centos 7.5
libevent-devel,memcached,magent
主节点
172.16.216.200
nosql2.linuxplus.com 172.16.216.186 Centos 7.5 libevent-devel,memcached,magent 备份节点
nosql3.linuxplus.com 172.16.216.187 Centos 7.5 telnet
测试节点

安装magent

[root@nosql1 software]# mkdir /opt/software/magent
[root@nosql1 software]# tar xf magent-0.5.tar.gz -C /opt/software/magent
[root@nosql1 software]# cd magent/
[root@nosql1 magent]# ls
ketama.c  ketama.h  magent.c  Makefile
[root@nosql1 magent]# vim ketama.h
#ifndef _KETAMA_H
#define _KETAMA_H
添加:
#ifndef SSIZE_MAX                       
#define SSIZE_MAX 32767
#endif
[root@nosql1 magent]# vim Makefile
LIBS = -levent -lm                      #首行LIBS = -levent后加上-lm
[root@nosql1 magent]# make
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
[root@nosql1 magent]# ls
ketama.c  ketama.h  ketama.o  magent  magent.c  magent.o  Makefile
[root@nosql1 magent]# cp magent /usr/bin
[root@nosql1 magent]# scp magent [email protected]:/usr/bin

安装配置keepalived

  • nosql1.linuxplus.com

[root@nosql1 ~]# yum install keepalived
[root@nosql1 ~]# cd /etc/keepalived/
[root@nosql1 keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
  notification_email {
  [email protected]
  }
  notification_email_from [email protected]
  smtp_server mail.qq.com
  smtp_connect_timeout 30
  router_id devops_MA
  enable_script_security
}
vrrp_script magent {
       script "/opt/magent.sh"
       interval 2
       user root
}
vrrp_instance devops_ha {
   state MASTER
   interface ens33
   virtual_router_id 51
   priority 180
   advert_int 1
   nopreempt
   authentication {
       auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
       172.16.216.200
   }
  track_script {
       magent
  }
}
[root@nosql1 ~]# vim /opt/magent.sh
#!/bin/bash
mem=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $mem -gt 0 ]
  then
      magent -u devops -n 51200 -l 172.16.216.200 -p 12000 -s 172.16.216.185:11211 -b 172.16.216.186:11211
      /usr/local/memcached/bin/memcached -u devops -m 128 -d
else
 pkill -9 magent
fi
[root@nosql1 ~]# systemctl start keepalived.service
[root@nosql1 ~]# netstat -ntap | grep 12000
tcp        0      0 172.16.216.200:12000    0.0.0.0:*               LISTEN      16649/magent  
[root@nosql2 keepalived]# netstat -tulnp |grep 11211
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      13959/./memcached   
tcp6       0      0 :::11211                :::*                    LISTEN      13959/./memcached

参数:

  • -n:定义最大连接数

  • -l:指定虚拟IP

  • -p:指定虚拟IP的端口号

  • -s:指定主缓存服务器

  • -b:指定从缓存服务器

             

  • nosql2.linuxplus.com

[root@nosql2 ~]# yum install keepalived
[root@nosql2 ~]# cd /etc/keepalived/
[root@nosql2 keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
  notification_email {
  [email protected]
  }
  notification_email_from [email protected]
  smtp_server mail.qq.com
  smtp_connect_timeout 30
  router_id devops_MB
  enable_script_security
}
vrrp_script magent {
       script "/opt/magent.sh"
       interval 2
       user root
}
vrrp_instance devops_ha {
   state BACKUP
   interface ens33
   virtual_router_id 52
   priority 90
   advert_int 1
   nopreempt
   authentication {
       auth_type PASS
       auth_pass 1111
   }
   virtual_ipaddress {
       172.16.216.200
   }
   track_script {
       magent
  }
}
[root@nosql2 ~]# vim /opt/magent.sh
#!/bin/bash
mem=$(ip addr | grep 172.16.216.200 | grep -v grep | wc -l)
if [ $mem -gt 0 ]
  then
      magent -u devops -n 51200 -l 172.16.216.200 -p 12000 -s 172.16.216.185:11211 -b 172.16.216.186:11211
      /usr/local/memcached/bin/memcached -u devops -m 128 -d
else
 pkill -9 magent
fi
[root@nosql2 ~]# systemctl start keepalived.service
[root@nosql2 keepalived]# netstat -tulnp |grep 12000
tcp        0      0 172.16.216.200:12000    0.0.0.0:*               LISTEN      27343/magent        
[root@nosql2 keepalived]# netstat -tulnp |grep 11211
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      13959/./memcached   
tcp6       0      0 :::11211                :::*                    LISTEN      13959/./memcached

             

测试验证

[root@nosql3 ~]# telnet 172.16.216.200 12000
Trying 172.16.216.200...
Connected to 172.16.216.200.
Escape character is '^]'.
set username 0 0 11
devopsadmin
STORED
get username
VALUE username 0 11
devopsadmin
END
[root@nosql1 bin]# telnet 172.16.216.185 11211
Trying 172.16.216.185...
Connected to 172.16.216.185.
Escape character is '^]'.
get username
VALUE username 0 11
devopsadmin
END
quit
Connection closed by foreign host.
[root@nosql2 bin]# telnet 172.16.216.186 11211
Trying 172.16.216.186...
Connected to 172.16.216.186.
Escape character is '^]'.
get username
VALUE username 0 11
devopsadmin
END
quit
Connection closed by foreign host.


猜你喜欢

转载自blog.51cto.com/stuart/2176565
今日推荐