构建高性能内存对象缓存memcached

高性能内存对象缓存memcached

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2In3bW5K-1595134342012)(https://s1.ax1x.com/2020/04/07/G2nCwR.png)]

主机名ip地址主要软件
memcached01192.168.136.124memcached-1.5.22
libevent-2.1.11
magent-0.5
Keepalived v1.3.5
memcached02192.168.136.125memcached-1.5.22
libevent-2.1.11
magent-0.5
Keepalived v1.3.5
web-servewr192.168.136.126httpd-2.4.6
php-7.2.27
memcached-3.0.4
libmemcached-1.0.16
1.基本环境配置
1.IP地址配置
2.通信测试
3.主机名设置
4.关闭selinux
2.安装libevent(memcached两台)
1.断点续传下载
[root@yulong-mem01 ~]# wget -c https://github.com/libevent/libevent/releases/download/release-2.1.11-stable/libevent-2.1.11-stable.tar.gz

2.解压
[root@yulong-mem01 ~]# tar zxvf libevent-2.1.11-stable.tar.gz -C /usr/src/

3.编译安装
[root@yulong-mem01 ~]# cd /usr/src/libevent-2.1.11-stable/
[root@yulong-mem01 libevent-2.1.11-stable]# ./configure --prefix=/usr/local/libevent   ##脚本指定安装到/usr/local/下
[root@yulong-mem01 libevent-2.1.11-stable]# make 
[root@yulong-mem01 libevent-2.1.11-stable]# make install
3.安装Memcached
1.断点续传下载
[root@yulong-mem01 ~]# wget -c https://www.memcached.org/files/memcached-1.5.22.tar.gz

2.解压到/usr/src/下。
[root@yulong-mem01 ~]# tar zxvf memcached-1.5.22.tar.gz -C /usr/src/

3.编译安装,--with指定库在哪里
[root@yulong-mem01 ~]# cd /usr/src/memcached-1.5.22/
[root@yulong-mem01 memcached-1.5.22]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[root@yulong-mem01 memcached-1.5.22]# make 
[root@yulong-mem01 memcached-1.5.22]# make install
4.设置Memcached服务脚本
1.创建组和启动用户(僵尸用户)
[root@yulong-mem01 ~]# groupadd -g 990 memcached
[root@yulong-mem01 ~]# useradd -u 990 -g memcached -s /sbin/nologin memcached

2.构建memcached的变量文件
[root@yulong-mem01 ~]# vim /etc/sysconfig/memcached
键入:
PORT='11211'      #侦听端口
USER='memcached'  #启动服务的用户
MAXCONN='1024'    #最大连接数
CACHESIZE='512'   #分配内存大小
OPTIONS=""

3.构建systemd的启动单元文件
[root@yulong-mem01 ~]# vim /usr/lib/systemd/system/memcached.service
键入:
[Unit]
Description=Memcached
Before=httpd.service
After=network.target

[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached
ExecStart=/usr/local/memcached/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS

[Install]
WantedBy=multi-user.target
5.启动服务并放行端口
1.启动服务并查看服务状态
[root@yulong-mem01 ~]# systemctl daemon-reload
[root@yulong-mem01 ~]# systemctl restart memcached
[root@yulong-mem01 ~]# systemctl status memcached -l
● memcached.service - Memcached
   Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2020-04-07 11:16:28 CST; 2s ago
 Main PID: 18659 (memcached)
   CGroup: /system.slice/memcached.service
           └─18659 /usr/local/memcached/bin/memcached -u memcached -p 11211 -m 512 -c 1024

4月 07 11:16:28 yulong-mem01 systemd[1]: Started Memcached.

2.查看端口并放行
[root@yulong-mem01 ~]# netstat -antp | grep memcached
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      18659/memcached     
tcp6       0      0 :::11211                :::*                    LISTEN      18659/memcached     
[root@yulong-mem01 ~]# firewall-cmd --add-port=11211/tcp --permanent 
success
[root@yulong-mem01 ~]# firewall-cmd --reload 
success
6.Memcached API客户端
安装LNMP环境(web-server)

6.1安装httpd

1.yum安装
[root@yulong ~]# yum install httpd -y

2.启动并查看状态
[root@yulong ~]# systemctl enable httpd
[root@yulong ~]# systemctl start httpd
[root@yulong ~]# systemctl status httpd | grep Active

3.防火墙放行80,443端口
[root@yulong ~]# firewall-cmd --add-service=http --permanent 
success
[root@yulong ~]# firewall-cmd --add-service=https --permanent 
success
[root@yulong ~]# firewall-cmd --reload 
success

6.2安装mysql8.0

1.下载仓库源
[root@yulong ~]# wget  https://dev.mysql.com/get/mysql80-cunity-release-el7-3.noarch.rpm   (Windows下载,传入Linux中也可以)

2.安装仓库和mysql服务
[root@yulong ~]# rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
[root@yulong ~]# yum install mysql-community-server -y

3.启动服务
[root@yulong ~]# systemctl enable mysqld
[root@yulong ~]# systemctl start mysqld
[root@yulong ~]# systemctl status mysqld | grep Active

4.防火墙放行3306
[root@yulong ~]# firewall-cmd --add-port=3306/tcp --permanent 
success
[root@yulong ~]# firewall-cmd --reload 
success

5.更改初始密码
[root@yulong ~]# grep 'temporary password' /var/log/mysqld.log
2020-04-07T05:07:08.214572Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: qrWte.h>C4aj

[root@yulong ~]# mysql -uroot -p'qrWte.h>C4aj'
mysql> alter user 'root'@'localhost' identified by 'Com.123!';
mysql> exit

6.3安装php

1.安装php7的yum源
[root@yulong ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@yulong ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

2.安装php7.2
[root@yulong ~]# yum install php72w php72w-cli php72w-common php72w-gd php72w-ldap php72w-mbstring php72w-mcrypt php72w-mysql php72w-pdo php72w-opcache -y

3.安装pecl-memcached API组件
[root@yulong ~]# yum install php72w-pecl-memcached -y

6.4建立测试网页文件

[root@yulong ~]# cd /var/www/html/
[root@yulong html]# vim index.php
键入:
<?php
phpinfo();
?>
[root@yulong html]# systemctl restart httpd

6.5测试访问

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0EDfXx3Y-1595134342015)(https://s1.ax1x.com/2020/04/07/Gc5iDK.png)]

6.6测试Memcached-API功能

[root@yulong html]# pwd
/var/www/html
[root@yulong html]# vim test.php
键入:
<?php
$memcache = new Memcached();
$memcache->addServer('192.168.136.124',11211);
$memcache->set('key','Memcache test successful!');
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KkK6EMwl-1595134342016)(https://s1.ax1x.com/2020/04/07/GcIQzR.jpg)]

7.安装magent(现在是只有mem01上安装)
1.创建目录,解压到指定目录
[root@yulong-mem01 ~]# mkdir /opt/magent/
[root@yulong-mem01 ~]# tar zxvf magent-0.5.tar.gz -C /opt/magent/
[root@yulong-mem01 ~]# cd /opt/magent/

2.修改配置文件
[root@yulong-mem01 magent]# vim ketama.h
最开头添加三行内容:
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif

[root@yulong-mem01 magent]# vim Makefile
修改:
LIBS = -levent -lm -L /usr/local/libevent/lib #修改此行,这是自己的编译安装路径
INCLUDE =-I /usr/local/libevent/include  #添加此行

3.编译
[root@yulong-mem01 magent]# make 
gcc -Wall -O2 -g -I /usr/local/libevent/include -c -o magent.o magent.c
gcc -Wall -O2 -g -I /usr/local/libevent/include -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm -L /usr/local/libevent/lib

4.将magent命令放到linux中
[root@yulong-mem01 magent]# cp /opt/magent/magent  /usr/bin/
[root@yulong-mem01 /]# ln  -s  /usr/local/libevent/lib/libevent-2.1.so.7  /usr/lib64/libevent-2.1.so.7
8.启动magent
1.启动magent
[root@yulong-mem01 /]# magent -u root -n 4096 -l 192.168.136.124 -p 12000 -s 192.168.136.124:11211 -b 192.168.136.125:11211
[root@yulong-mem01 /]# netstat -antp | grep magent
tcp        0      0 192.168.136.124:12000   0.0.0.0:*               LISTEN      8011/magent         

2.防火前放行端口12000
[root@yulong-mem01 /]# firewall-cmd --add-port=12000/tcp --permanent 
success
[root@yulong-mem01 /]# firewall-cmd --reload 
success
9.客户端上进行测试
1.web server写入测试
[root@yulong html]# yum install telnet -y 
[root@yulong html]# telnet 192.168.136.124 12000
Trying 192.168.136.124...
Connected to 192.168.136.124.
Escape character is '^]'.
add password 0 0 6
123456
STORED
get password
VALUE password 0 6
123456
END
quit
Connection closed by foreign host

2.在mamcached02上检查
[root@yulong-mem02 /]# telnet 192.168.136.124 12000
Trying 192.168.136.124...
Connected to 192.168.136.124.
Escape character is '^]'.
get password
VALUE password 0 6
123456
END
quit
Connection closed by foreign host.
10.停掉主节点的memcached服务,在客户端主机上测试
1.停止掉主节点
[root@yulong-mem01 /]# systemctl stop memcached.service

2.客户端上继续操作,西安在其实操作的是从节点上的memcached
[root@yulong html]# telnet 192.168.136.124 12000
Trying 192.168.136.124...
Connected to 192.168.136.124.
Escape character is '^]'.
get password
VALUE password 0 6
123456
END
set username 00 10
UNSUPPORTED COMMAND
set username 0 0 10
zhangsan02
STORED
gets username
VALUE username 0 10 2
zhangsan02
END
11.在magent节点上安装keepalived软件,实现magent高可用
1.两个magent节点上安装keepalived软件,并启动服务
[root@yulong-mem01 /]# yum install keepalived -y
[root@yulong-mem01 /]# systemctl enable keepalived.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@yulong-mem01 /]# systemctl start keepalived.service 

2.将主节点生成的magent命令文件,复制到从节点的/usr/bin,从节点上无需安装magent软件
[root@yulong-mem01 /]# scp /usr/bin/magent  192.168.136.125:/usr/bin/

3.在主magent上修改keepalived配置文件
[root@yulong-mem01 /]# vim /etc/keepalived/keepalived.conf 
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script magent{
    script "/opt/shell/magent.sh"   ##添加新脚本magent
    interval 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    track_script{    ##添加使用新脚本
      magent
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {   ##定义虚拟ip地址
        192.168.136.200
    }   
} 

4.把主的keepalived传到从节点
[root@yulong-mem01 magent]# scp /etc/keepalived/keepalived.conf 192.168.136.125:/etc/keepalived/keepalived.conf.bak

5.更改文件名和配置文件
[root@yulong-mem02 opt]# cd /etc/keepalived/
[root@yulong-mem02 keepalived]# rm keepalived.conf
[root@yulong-mem02 keepalived]# mv keepalived.conf.bak  keepalived.conf
[root@yulong-mem02 keepalived]# vim keepalived.conf 

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id magent_hb
}

vrrp_script magent {
   script "/opt/shell/magent.sh"
   interval 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    track_script {
      magent
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.136.200
    }
}

6.主节点上配置magent启动脚本
[root@yulong-mem01 shell]# vim magent.sh
键入:
#!/bin/bash
K= `ip addr | grep 192.168.136.200|grep -v grep|wc -l`
if [$K -gt 0];then
    magent -u root -n 51200 -l 192.168.136.200 -p 12000 -s 192.168.136.124:11211 -b
192.168.136.125:11211
else
    pkill -9 magent
fi
[root@yulong-mem01 shell]# chmod a+x magent.sh 

7.从节点上配置启动脚本(直接scp过去即可)
[root@yulong-mem01 shell]# scp ./magent.sh 192.168.136.125:/opt/shell/


8.主从节点上均启动keepalived服务,防火墙放行服务
[root@yulong-mem01 log]# systemctl restart keepalived.service
[root@yulong-mem01 shell]# firewall-cmd --add-rich-rule='rule protocol value='vrrp' accept ' --permanent 
[root@yulong-mem01 shell]# firewall-cmd --reload 
12.客户端访问vip的12000端口
1.此时vip在mem01上
[root@yulong ~]# telnet 192.168.136.200   12000
Trying 192.168.136.200...
Connected to 192.168.136.200.
Escape character is '^]'.
set username 0 0 8 
zhangsan
STORED
UNSUPPORTED COMMAND
quit
Connection closed by foreign host.

2.停掉mem01上的keepalived
[root@yulong-mem01 shell]# systemctl stop keepalived.service 

3.vip已经转移,客户端依然可以访问memecached
[root@yulong ~]# telnet 192.168.136.200   12000
Trying 192.168.136.200...
Connected to 192.168.136.200.
Escape character is '^]'.
get username
VALUE username 0 8
zhangsan
END

ed to 192.168.136.200.
Escape character is ‘^]’.
set username 0 0 8
zhangsan
STORED
UNSUPPORTED COMMAND
quit
Connection closed by foreign host.

2.停掉mem01上的keepalived
[root@yulong-mem01 shell]# systemctl stop keepalived.service

3.vip已经转移,客户端依然可以访问memecached
[root@yulong ~]# telnet 192.168.136.200 12000
Trying 192.168.136.200…
Connected to 192.168.136.200.
Escape character is ‘^]’.
get username
VALUE username 0 8
zhangsan
END


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值