1. 程式人生 > >Memcached記憶體資料快取群集實驗

Memcached記憶體資料快取群集實驗

實驗環境:
Memcached記憶體資料快取群集實驗
實驗目的:client端通過虛擬IP地址登陸memcached主和從快取伺服器插入資料,主快取和從快取是否有資料
步驟:主伺服器
[[email protected] ~]# yum install -y gcc gcc-c++ make //搭建安裝環境
[[email protected] ~]# tar xvf libevent-2.1.8-stable.tar.gz
[[email protected] ~]# tar xvf memcached-1.5.6.tar.gz
[[email protected] ~]# mkdir /opt/magent
[[email protected]

~]# tar xvf magent-0.5.tar.gz -C /opt/magent/ //解壓軟體包
[[email protected] ~]# cd libevent-2.1.8-stable
[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr/
[[email protected] libevent-2.1.8-stable]# make && make install
[[email protected] memcached-1.5.6]# cd ../memcached-1.5.6
[
[email protected]
memcached-1.5.6]# ./configure --with-libevent=/usr
[[email protected] memcached-1.5.6]# make && make install //編譯安裝
[[email protected] libevent-2.1.8-stable]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
[[email protected] ~]# cd /opt/magent/
[[email protected]
magent]# vi ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
[[email protected] magent]# vi Makefile
LIBS = -levent -lm //第一行末尾加-lm (不是數字1)
[[email protected] magent]# make
[[email protected] magent]# ls //會產生magent可執行程式
[[email protected] magent]# cp magent /usr/bin/ //將編譯好的magent程式複製到PATH路徑中
[[email protected] magent]# scp magent [email protected]:/usr/bin/ //把產生的magent檔案直接複製到從伺服器
[[email protected] magent]# yum install keepalived -y //下載健康檢查工具
[[email protected] shell]# vi /etc/keepalived/keepalived.conf //配置keepalived檔案
! Configuration File for keepalived
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
global_defs {
notification_email {br/>[email protected]
[email protected]
br/>[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
}
track_script {
magent
}
virtual_ipaddress {
192.168.100.188
}
} //定義漂移地址,×××字型要改或新增其餘全刪除
[[email protected] shell]# mkdir /opt/shell //建立magent指令碼目錄
[[email protected] shell]# cd /opt/shell/
[[email protected] shell]# vi 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.100.188 -p 12000 -s 192.168.100.10:11211 -b 192.168.100.20:11211
else
pkill -9 magent
Fi
[[email protected] shell]# chmod +x magent.sh //給執行許可權
[[email protected] shell]# systemctl start keepalived.service //啟動健康檢查工具
[[email protected] shell]# netstat -anpt | grep 12000
tcp 0 0 192.168.100.188:12000 0.0.0.0:* LISTEN 9582/magent //說明magent 啟動工作了
[[email protected] shell]# cat /var/log/messages //檢視日誌
……
Transition to MASTER STATE
……
[[email protected] shell]# ip addr //確定定義的漂移地址存在
---從伺服器配置--
[[email protected] shell]# yum install -y gcc gcc-c++ make //搭建安裝環境
[[email protected] shell]# tar xvf libevent-2.1.8-stable.tar.gz
[[email protected] shell]# tar xvf memcached-1.5.6.tar.gz
[[email protected] ~]# cd libevent-2.1.8-stable
[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr/
[[email protected] libevent-2.1.8-stable]# make && make install
[[email protected] libevent-2.1.8-stable]# cd ../memcached-1.5.6
[[email protected] memcached-1.5.6]# ./configure --with-libevent=/usr
[[email protected] memcached-1.5.6]# make && make install //編譯安裝
[[email protected] memcached-1.5.6]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
[[email protected] memcached-1.5.6]# yum install keepalived -y //下載健康檢查工具
[[email protected] memcached-1.5.6]# cd /etc/keepalived/
[[email protected] keepalived]# mv keepalived.conf keepalived.conf.bak
[[email protected] keepalived]# scp [email protected]:/etc/keepalived/keepalived.conf ./ 把主伺服器的配置檔案複製過來
[[email protected] keepalived]# vi keepalived.conf
! Configuration File for keepalived
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
global_defs {
notification_email {
br/>[email protected]
[email protected]
br/>[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 BACKUO
interface ens33
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
magent
}
virtual_ipaddress {
192.168.100.188
}
} //×××字型需要修改

[[email protected] keepalived]# mkdir /opt/shell
[[email protected] keepalived]# cd /opt/shell/
[[email protected] shell]# vi magent.sh
#!/bin/bash
K=ip addr | grep 192.168.100.188 | grep -v grep | wc -l
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.100.188 -p 12000 -s 192.168.100.10:11211 -b 192.168.100.20:11211
else
pkill -9 magent
Fi
[[email protected] shell]# chmod +x magent.sh
[[email protected] shell]# systemctl start keepalived.service
[[email protected] shell]# cat /var/log/messages //驗證主從
……
Entering BACKUP STATE
……
[[email protected] shell]# memcached -m 512k -u root -d -l 192.168.100.10 -p 11211 //啟動主
[[email protected] shell]# memcached -m 512k -u root -d -l 192.168.100.20 -p 11211 //啟動從
---在客戶端測試---
[[email protected] ~]# yum install -y telnet
[[email protected] ~]# telnet 192.168.100.188 12000
Trying 192.168.100.188...
Connected to 192.168.100.188.
Escape character is '^]'.
add username 0 0 7
1234567
主伺服器
[[email protected] shell]# telnet 192.168.100.10 11211
Trying 192.168.100.10...
Connected to 192.168.100.10.
Escape character is '^]'.
get username
VALUE username 0 7
1234567
從伺服器
[[email protected] shell]# telnet 192.168.100.20 11211
Trying 192.168.100.20...
Connected to 192.168.100.20.
Escape character is '^]'.
get username
VALUE username 0 7
1234567
客戶端再進
[[email protected] ~]# telnet 192.168.100.188 12000
Trying 192.168.100.188...
Connected to 192.168.100.188.
Escape character is '^]'.
add ok 0 0 6
123456
STORED
get ok
VALUE ok 0 6
123456 //新建ok
從伺服器跟主伺服器都檢證一下是否都有資料插入
主---
[[email protected] shell]# telnet 192.168.100.10 11211
Trying 192.168.100.10...
Connected to 192.168.100.10.
Escape character is '^]'.
get ok
VALUE ok 0 6
123456
從---
[[email protected] shell]# telnet 192.168.100.20 11211
Trying 192.168.100.20...
Connected to 192.168.100.20.
Escape character is '^]'.
get username
VALUE username 0 7
1234567
END
get ok
VALUE ok 0 6
123456

//把主停了業務不影響