在CentOS7上部署Memcached主主復制+Keepalived高可用架構
阿新 • • 發佈:2018-07-26
track bin dba 1.8 修改 oot 腳本 ever defs 原理:
Memcached主主復制是指在任意一臺Memcached服務器修改數據都會被同步到另外一臺,但是Memcached API客戶端是無法判斷連接到哪一臺Memcached服務器的,所以需要設置VIP地址,提供給Memcached API客戶端進行連接。可以使用Keepalived產生的VIP地址連接主Memcached服務器,並且提供高可用架構。
使用兩臺Memcached服務器,一臺客戶機來完成,實驗環境表如下:
1.配置memcached主緩存節點和從緩存節點-----兩臺配置相同
[root@localhost ~]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/ //解包// [root@localhost ~]# tar zxvf memcached-1.5.6.tar.gz -C /opt/ [root@localhost ~]# mkdir /opt/magent [root@localhost ~]# tar zxvf magent-0.5.tar.gz -C /opt/magent/ [root@localhost opt]#cd libevent-2.1.8-stable/ [root@localhost libevent-2.1.8-stable]# yum install gcc gcc-c++ make -y [root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr [root@localhost libevent-2.1.8-stable]# make && make install [root@localhost libevent-2.1.8-stable]# cd ../memcached-1.5.6/ [root@localhost memcached-1.5.6]# ./configure --with-libevent=/usr [root@localhost memcached-1.5.6]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6 //軟鏈接//
2.關閉防火墻並開啟memcached服務
[root@localhost memcached-1.5.6]# systemctl stop firewalld.service [root@localhost memcached-1.5.6]# setenforce 0 [root@localhost memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root [root@localhost memcached-1.5.6]# netstat -ntap | grep 11211 tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 11224/memcached tcp6 0 0 :::11211 :::* LISTEN 11224/memcached
3.在主服務器上安裝magent
[root@localhost memcached-1.5.6]# cd /opt/magent/ [root@localhost magent]# ls ketama.c ketama.h magent.c Makefile [root@localhost magent]# vim ketama.h #ifndef SSIZE_MAX #define SSIZE_MAX 32767 #endif [root@localhost magent]# vim Makefile LIBS = -levent -lm //第一行末尾加-lm (不是數字1 LIBS = -levent -lm CFLAGS = -Wall -O2 -g [root@localhost 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
4.把生成的mgent程序讓系統識別
ls一下可看到magent可執行程序
[root@localhost magent]# ls
ketama.c ketama.h ketama.o magent magent.c magent.o Makefile
[root@localhost magent]# cp magent /usr/bin/
5.把產生的magent文件直接復制到從服務器。
[root@localhost bin]# yum install openssh-clients -y
[root@localhost bin]# scp magent [email protected]:/usr/bin/
6.安裝keepalived,修改默認配置文件。
[root@localhost bin]# yum install keepalived -y
[root@localhost bin]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
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_instance VI_1 {
state MASTER
interface ens33 //網卡名稱//
virtual_router_id 51
priority 100 //優先級//
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.126.188 //虛擬IP//
}
track_script {
magent //函數//
}
}
7.從服務器上安裝keepalived,配置文件進行修改。
[root@localhost bin]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vi keepalived.conf
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
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_HB //從服務器的名稱//
}
vrrp_instance VI_1 {
state BACKUP //從服務器的熱備狀態要修改成BACKUP//
interface ens33 //網卡名稱//
virtual_router_id 52 //不能與主服務器相同//
priority 90 //從調度器的優先級要小於主的//
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.126.188 //虛擬IP//
}
track_script { //函數//
magent
}
}
8.在主服務器上設置magent管理腳本
[root@localhost bin]# mkdir /opt/shell
[root@localhost bin]# vim /opt/shell/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.126.188 -p 12000 -s 192.168.126.138:11211 -b 192.168.126.166:11211
else
pkill -9 magent
fi
參數註解:
-n 51200 //定義用戶最大連接數
-l 192.168.126.188 //指定虛擬IP
-p 12000 //指定端口號
-s //指定主緩存服務器
-b //指定從緩存服務器
[root@localhost shell]# chmod +x magent.sh // 增加執行權限//
9.在從服務器上操作
[root@localhost bin]# mkdir /opt/shell
[root@localhost bin]# cd /opt/shell/
[root@localhost shell]# vim magent.sh
[root@localhost shell]# vim magent.sh
腳本內容如下,與主服務器腳本有區別!
#!/bin/bash
K=`ip addr | grep 192.168.126.188 | grep -v grep | wc -l`
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.126.188 -p 12000 -s 192.168.126.138:11211 -b 192.168.126.166:11211
else
pkill -9 magent
fi
[root@localhost shell]# chmod +x magent.sh
10.開始驗證
1)啟動主服務器
[root@localhost shell]# systemctl start keepalived.service
[root@localhost shell]# netstat -ntap | grep 12000 //確認magent運行//
tcp 0 0 192.168.126.188:12000 0.0.0.0:* LISTEN 12422/magent
2)啟動從服務器
[root@localhost shell]# systemctl start keepalived.service
[root@localhost shell]# netstat -ntap | grep 12000
tcp 0 0 192.168.126.188:12000 0.0.0.0:* LISTEN 11716/magent
3)在主服務器上使用telnet進行簡單驗證復制功能
[root@localhost shell]# telnet 192.168.126.188 12000 //用漂移地址登陸服務//
Trying 192.168.126.188...
Connected to 192.168.126.188.
Escape character is ‘^]‘.
add username 0 0 7 //添加一條鍵值數據//
1234567
STORED
在從服務器上查看
[root@localhost shell]# telnet 192.168.126.188 12000
Trying 192.168.126.188...
Connected to 192.168.126.188.
Escape character is ‘^]‘.
get username //查看鍵值數據
VALUE username 0 7
1234567 //內容存在,寫入成功//
END
11.在客戶端用漂移地址登陸服務
[root@localhost ~]# yum install telnet -y
[root@localhost ~]# telnet 192.168.126.188 12000
Trying 192.168.126.188...
Connected to 192.168.126.188.
Escape character is ‘^]‘.
add username 0 0 8 //添加一條鍵值數據//
12345678
STORED
1)在主服務器和從服務器上查看是否寫入成功。
主服務器
get username
VALUE username 0 8
12345678
END
從服務器
get username
VALUE username 0 8
12345678
END
2)把主服務器停了業務不影響
[root@localhost shell]# systemctl stop keepalived.service
[root@localhost shell]# ip addr
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
3)在從服務器上查看
[root@localhost shell]# ip addr
inet 192.168.126.166/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1146sec preferred_lft 1146sec
inet 192.168.126.188/32 scope global ens33
可以看到漂移地址已經轉移到從服務器上了,說明從已接受工作。
3)再把主服務器開啟
[root@localhost shell]# systemctl start keepalived.service
[root@localhost shell]# ip addr
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1145sec preferred_lft 1145sec
inet 192.168.126.188/32 scope global ens33
valid_lft forever preferred_lft forever
漂移地址再次轉移到主服務器上,接手地址,服務依然不受影響。
實驗成功
在CentOS7上部署Memcached主主復制+Keepalived高可用架構