操作系统 | 配置 | 主机名 | IP | 所需软件 |
---|---|---|---|---|
CentOS 7.9 | 2C4G | memcache1 | 192.168.93.102 | libevent-2.1.8-stable.tar.gz memcached-1.5.1.tar.gz |
CentOS 7.9 | 2C4G | memcache-apr | 192.168.93.101 | LAMP架构 libmemcached-1.0.18.tar.gz memcached-2.2.0.tgz |
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
hostnamectl set-hostname memcache1
hostnamectl set-hostname memcache-api
[root@memcache1 ~]# yum -y install gcc gcc-c++ make
[root@memcache1 ~]# tar -zxvf libevent-2.1.8-stable.tar.gz -C /usr/src/
[root@memcache1 ~]# cd /usr/src/libevent-2.1.8-stable/
[root@memcache1 libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@memcache1 libevent-2.1.8-stable]# make && make install
# 至此Libevnet安装完毕,接下来就可以安装Memcached
[root@memcache1 ~]# tar -zxvf memcached-1.5.1.tar.gz -C /usr/src/
[root@memcache1 ~]# cd /usr/src/memcached-1.5.1/
[root@memcache1 memcached-1.5.1]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[root@memcache1 memcached-1.5.1]# make && make install
[root@memcache1 ~]# vim /usr/local/memcached/
#!/bin/bash
CMD="/usr/local/memcached/bin/memcached"
start() {
$CMD -d -m 128 -u root
}
stop() {
killall memcached
}
ACTION=$1
case $ACTION in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
sleep 2
start
;;
*)
echo "Usage:$0 {start|stop|restart}"
esac
# 启动Memcached服务时选项作用
-d:以守护进行的方式运行Memcached服务
-m:为Memcached服务分配128M的内存(应根据企业需要调整)
-u:选项指定运行的用户账号
# 脚本编写完成之后即可设置脚本权限并启动Memcached服务
[root@memcache1 ~]# chmod 755 /usr/local/memcached/memcached_service.sh
[root@memcache1 ~]# /usr/local/memcached/memcached_service.sh start
[root@memcache1 ~]# netstat -anpt | grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 17582/memcached
tcp6 0 0 :::11211 :::* LISTEN 17582/memcached
[root@memcache-api ~]# yum -y install gcc gcc-c++ make
[root@memcache-api ~]# tar -zxvf libmemcached-1.0.18.tar.gz
[root@memcache-api ~]# cd libmemcached-1.0.18/
[root@memcache-api libmemcached-1.0.18]# ./configure --prefix=/usr/local/libmemcached --with-memcached=/usr/local/memcached
[root@memcache-api libmemcached-1.0.18]# make && make install
# 下载地址
wget http://pecl.php.net/get/memcached-2.2.0.tgz
[root@memcache-api ~]# tar zxvf memcached-2.2.0.tgz
[root@memcache-api ~]# cd memcached-2.2.0/
# 安装autoconf软件不然生成脚本的时候会报错
yum -y install autoconf
[root@memcache-api memcached-2.2.0]# /usr/local/php5/bin/phpize
Configuring for:
PHP Api Version: 20121113
Zend Module Api No: 20121212
Zend Extension Api No: 220121212
[root@memcache-api memcached-2.2.0]# cp -r /usr/local/php5/include/php/ext/ ./
[root@memcache-api memcached-2.2.0]# ./configure --enable-memcached \
> --with-php-config=/usr/local/php5/bin/php-config \
> --with-libmemcached-dir=/usr/local/libmemcached \
> --disable-memcached-sasl
# 选项作用
--enable-memcached:启用memcached支持
--with-php-config:指定PHP配置文件的路径
--with-libmemcached-dir:指定libmemcached库的路径
--disable-memcached-sasl:关闭memcached的SASL认证功能,否则会报错
[root@memcache-api memcached-2.2.0]# make
[root@memcache-api memcached-2.2.0]# make test
[root@memcache-api memcached-2.2.0]# make install
Installing shared extensions: /usr/local/php5/lib/php/extensions/no-debug-zts-20121212/ # 共享组件的位置
[root@memcache-api ~]# cd /usr/local/php5/
[root@memcache-api php5]# vim php.ini
# 720行下方插入
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-zts-20121212/"
extension=memcached.so
[root@memcache-api ~]# vim /usr/local/httpd/htdocs/index.php
<?php
phpinfo();
?>
[root@memcache-api ~]# systemctl restart httpd
[root@memcache-api ~]# vim /usr/local/httpd/htdocs/test.php
# 下面的IP地址是Memcached服务器的IP地址,
<?php
$memcache = new Memcached();
$memcache->addServer('192.168.93.101', 11211);
$memcache->set('key', 'Memcache test successful!', 0, 60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>
[root@memcache1 ~]# yum -y install telnet*
[root@memcache1 ~]# telnet 192.168.93.102 11211
Trying 192.168.93.102...
Connected to 192.168.93.102.
Escape character is '^]'.
# 输入操作执行的地址
add username 0 0 7
example
STORED
get username
VALUE username 0 7
example
END
gets username
VALUE username 0 7 3
example
END
set username 0 0 10
everything
STORED
get username
VALUE username 0 10
everything
END
replace username 0 0 7
lodging
STORED
gets username
VALUE username 0 7 5
lodging
END
replace username1 0 0 7
example
NOT_STORED
delete username
DELETED
get username
END
add username 0 0 7
example
STORED
gets username
VALUE username 0 7 6
example
END
cas username 0 0 7 1
lodging
EXISTS
cas username 0 0 7 6
lodging
STORED
gets username
VALUE username 0 7 7
lodging
END
append username 0 0 7 # 向后追加7个字节
example
STORED
gets username
VALUE username 0 14 8
lodgingexample
END
prepend username 0 0 2
un
STORED
gets username
VALUE username 0 16 9
unlodgingexample
END
flush_all
OK
stats # 系统状态统计信息
stats items # 返回所有键值对统计信息
stats cachedump 1 0 # 返回指定存储空间的键值对
stats slabs # 显示各个slab的信息,包括chunk的大小、数目、使用情况等
stats sizes # 输出所有item的大小和个数
stats reset # 清空统计数据(不建议使用)
操作系统 | 配置 | 主机名 | IP | 所需软件 |
---|---|---|---|---|
CentOS 7.9 | 2C4G | memcache1 | 192.168.93.102 | memcached-1.2.8-repcached-2.2/ |
CentOS 7.9 | 2C4G | memcache2 | 192.168.93.103 | memcached-1.2.8-repcached-2.2/ |
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
hostnamectl set-hostname memcache-api
hostnamectl set-hostname memcache1
hostnamectl set-hostname memcache2
# psmisc用于管理系统上进程的实用程序,提供了killall工具
[root@memcache1 ~]# yum -y install psmisc
[root@memcache1 ~]# /usr/local/memcached/memcached_service.sh stop
[root@memcache1 ~]# netstat -anpt | grep 11211
[root@memcache1 ~]# rm -rf /usr/local/memcached/
[root@memcache1 ~]# tar -zxvf memcached-1.2.8-repcached-2.2.tar.gz
[root@memcache1 ~]# cd memcached-1.2.8-repcached-2.2/
[root@memcache1 memcached-1.2.8-repcached-2.2]# ./configure --prefix=/usr/local/memcached_replication --enable-replication --with-libevent=/usr/local/libevent
# 修改memcached.c配置文件
[root@memcache1 memcached-1.2.8-repcached-2.2]# vim memcached.c
# 删除57和59行
[root@memcache1 memcached-1.2.8-repcached-2.2]# make && make install
[root@memcache1 ~]# ln -s /usr/local/libevent/lib/libevent-2.1.so.6 /usr/lib64/
[root@memcache1 ~]# /usr/local/memcached_replication/bin/memcached -d -u root -m 128 -x 192.168.93.103 # -x执行对象IP地址
[root@memcache1 ~]# netstat -anpt | grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 29537/memcached
tcp6 0 0 :::11211 :::* LISTEN 29537/memcached
yum -y install telnet*
[root@memcache1 ~]# telnet 192.168.93.102 11211 连接memcached
Trying 192.168.93.102...
Connected to 192.168.93.102.
Escape character is '^]'.
set username 0 0 8
12345678
STORED
get username
VALUE username 0 8
12345678
END
quit
Connection closed by foreign host.
[root@memcache2 ~]# telnet 192.168.93.103 11211
Trying 192.168.93.103...
Connected to 192.168.93.103.
Escape character is '^]'.
get username
VALUE username 0 8
12345678
END
本实验环境基于主主复制架构进行的衍生
Keepalived不断检测Memcached主服务器的11211端口,如果检测到Memcached服务发生宕机或者死机等,就会将VIP从主服务器移动到从服务器,从而实现Memcached的高可用
yum -y install keepalived
cp /etc/keepalived/keepalived.conf /tmp/keepalived.conf.bak
[root@memcache1 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id route # 路由标识,主从保持一致
}
vrrp_instance VI_1 {
state BACKUP # 主备均为BACKUP
interface ens33
virtual_router_id 51 # 虚拟路由ID,主从相同
priority 100 # 优先级,主高于备
advert_int 1
nopreempt # 不主动抢占资源,只在Master或者高优先级服务器上设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { # 定义VIP虚拟地址
192.168.93.100
}
}
virtual_server 192.168.93.100 11211 # VIP故障检测
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 20
protocol TCP
sorry_server 192.168.93.103 11211 # 对端
real_server 192.168.93.102 11211 { # 本机
weight 3
notify_down /root/memcached.sh # 当memcached宕机,停止keepalived服务
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 11211
}
}
}
[root@memcache1 ~]# echo "/usr/bin/systemctl stop keepalived" > /root/memcached.sh
[root@memcache1 ~]# chmod +x memcached.sh
[root@memcache1 ~]# scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/keepalived.conf
global_defs {
router_id route
}
vrrp_instance VI_1 {
state BACKUP # 备机状态也为BACKUP
interface ens33
virtual_router_id 51 # 虚拟路由ID,主从相同
priority 99 # 优先级低于主
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.93.100
}
}
virtual_server 192.168.93.100 11211 # VIP故障检测
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 20
protocol TCP
sorry_server 192.168.93.102 11211 # 对端
real_server 192.168.93.103 11211 { # 本机
weight 3
notify_down /root/memcached.sh # 当memcached 宕机,停止 keepalive
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 11211
}
}
}
[root@memcache2 ~]# echo "/usr/bin/systemctl stop keepalived" > /root/memcached.sh
[root@memcache2 ~]# chmod +x memcached.sh
[root@memcache1 ~]# systemctl start keepalived.service
[root@memcache2 ~]# systemctl start keepalived.service
[root@memcache1 ~]# ip address show dev ens33
# 已获得VIP地址
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c2:6c:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.93.102/24 brd 192.168.93.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.93.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::8777:f1a8:c3dd:6a2b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
关闭Memcached1服务器的Memcached服务,在Memcached2服务器上查看地址信息
注意:一定要关闭两台服务器的selinux和firewalld
[root@memcache1 ~]# killall memcached
# 已获取VIP地址
[root@memcache2 ~]# ip addr show dev ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:df:24:ff brd ff:ff:ff:ff:ff:ff
inet 192.168.93.103/24 brd 192.168.93.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.93.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::1bf6:d1c7:dafb:4484/64 scope link noprefixroute
valid_lft forever preferred_lft forever