memcached+keepavlied+magent高可用性を実現
5311 ワード
システム環境
サーバ名
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
インストール構成keepalived nosql1.linuxplus.com
パラメータ:-n:最大接続数 を定義-l:仮想IP を指定-p:仮想IPのポート番号 を指定-s:プライマリキャッシュサーバ を指定-b:キャッシュサーバ から指定
nosql2.linuxplus.com
テスト検証
サーバ名
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
[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
パラメータ:
[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.