memcached 集群搭建 magent+keepalived

十年热恋 提交于 2019-12-11 20:07:46
  • memcached安装
M 192.168.10.252
S  192.168.10.253

cd /tmp
#安装环境
yum install gcc gcc-c++ make -y

#下载 libevent-2.1.8-stable.tar.gz
http://libevent.org/

#解压
tar xf libevent-2.1.8-stable.tar.gz -C /opt
cd /opt/libevent-2.1.8-stable/
./configure --prefix=/usr/local/libevent
make && make install

#下载memcached安装包
cd /tmp
wget https://memcached.org/files/memcached-1.5.20.tar.gz
tar xf memcached-1.5.20.tar.gz -C /opt/
cd /opt/memcached-1.5.20/
./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent/
make && make install

#memcached 复制功能需要模块
ln -s /usr/local/libevent/lib/libevent-2.1.so.6.0.2  /usr/lib64/libevent-2.1.so.6

#启动 M节点
cd /usr/local/memcached/bin/
./memcached -d -m 32m -l 192.168.10.252 -p 11211 -u root

#启动 S节点
cd /usr/local/memcached/bin/
./memcached -d -m 32m -l 192.168.10.253 -p 11211 -u root

#检查进程和端口
ps -ef | grep memcached
lsof -i:11211

 

  • magent 安装
    cd /tmp
    mkdir /usr/local/magent -p
    tar xf magent-0.5.tar.gz -C /usr/local/magent
    cd /usr/local/magent
    #让动态链接库为系统所共享
    /sbin/ldconfig
    
    #编辑ketama
    vi ketama.h
    #ifndef SSIZE_MAX
    #define SSIZE_MAX 32767
    
    [root@k8s-node02 magent]# cat ketama.h 
    #ifndef SSIZE_MAX
    #define SSIZE_MAX 32767
    
    #配置libevent
    vi Makefile
    LIBS = -levent -lm  -L /usr/local/libevent/lib
    INCLUDE=-I /usr/local/libevent/include
    
    [root@k8s-node02 magent]# cat Makefile
    LIBS = -levent -lm  -L /usr/local/libevent/lib
    INCLUDE=-I /usr/local/libevent/include
    CFLAGS = -Wall -O2 -g
    CC = gcc
    #编译
    [root@k8s-node02 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
    
    #复制编译完的可执行文件到系统管理
    cp /usr/local/magent/magent /usr/bin/
    #复制到从服务器
    scp /usr/local/magent/magent root@192.168.10.253:/usr/bin

     

  • keepalived M安装
#keepalived 安装
yum -y install keepalived

cat >> /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived

global_defs {
      router_id magent_meM         #router_id  magent_meM
}

vrrp_script magent {                 
    script "/opt/scripts/magent.sh"
    interval 2
}

vrrp_instance VI_1 {
    state MASTER
    interface ens160             #本地网卡名称
    virtual_router_id 51       #id主从一致
    priority 100                    #主优先级大于从优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
#调用函数
track_script {                  
        magent
    }
    
#vip地址
virtual_ipaddress {         
        192.168.10.222
    }
}
EOF

#magent.sh 脚本
cat >>  /opt/scripts/magent.sh << EOF
#!/bin/bash

Keepalived=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then	
        magent -u root -n 51200 -l 192.168.10.222 -p 12000 -s 192.168.10.252:11211 -b 192.168.10.253:11211
else
        pkill -9 magent
fi
EOF

参数说明:
      -n 51200                #定义用户最大连接数
      -l 192.168.10.222  #指定虚拟IP
      -p 12000                #指定端口号
      -s                          #指定主缓存服务器
      -b                          #指定从缓存服务器

#授权和启动
chmod +x /opt/scripts/magent.sh
systemctl enable keepalived
systemctl start keepalived

#查看代理端口
netstat -anpt | grep 12000
#keepalived 状态
systemctl status keepalived
#查看vip
ip addr show ens160
#memcached 端口
netstat -anpt | grep 11211
  • keepalived S安装

 

#keepalived S安装
yum -y install keepalived

#keepalived.conf 文件和master相比修改了三处
#state BACKUP
#router_id magent_meS
#priority 90  

cat >> /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived

global_defs {
      router_id magent_meS         #router_id  magent_meS 
}

vrrp_script magent {                 
    script "/opt/scripts/magent.sh"
    interval 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens160             #本地网卡名称
    virtual_router_id 51       #id主从一致
    priority 90                    #主优先级大于从优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
#调用函数
track_script {                  
        magent
    }
    
#vip地址
virtual_ipaddress {         
        192.168.10.222
    }
}
EOF

#magent.sh 脚本
mkdir  /opt/scripts
cat >>  /opt/scripts/magent.sh << EOF
#!/bin/bash

Keepalived=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then	
        magent -u root -n 51200 -l 192.168.10.222 -p 12000 -s 192.168.10.252:11211 -b 192.168.10.253:11211
else
        pkill -9 magent
fi
EOF

#授权启动
chmod +x /opt/scripts/magent.sh
systemctl enable keepalived
systemctl start keepalived

#keepalived 状态
systemctl status keepalived
#查看vip
ip addr show ens160
#memcached 端口
netstat -anpt | grep 11211
  • 测试
    #安装telnet
    yum install telnet -y
    
    #连接VIP
    [root@k8s-node01 ~]# telnet 192.168.10.222 12000
    Trying 192.168.10.222...
    Connected to 192.168.10.222.
    Escape character is '^]'.
    set u1 0 0 5
    12345
    STORED
    get u1
    VALUE u1 0 5
    12345
    END
    quit
    #连接M节点
    Connection closed by foreign host.
    [root@k8s-node01 ~]# telnet 192.168.10.252 12000
    Trying 192.168.10.252...
    telnet: connect to address 192.168.10.252: Connection refused
    [root@k8s-node01 ~]# telnet 192.168.10.252 11211
    Trying 192.168.10.252...
    Connected to 192.168.10.252.
    Escape character is '^]'.
    get u1
    VALUE u1 0 5
    12345
    END
    quit
    Connection closed by foreign host.
    #连接S节点
    [root@k8s-node01 ~]# telnet 192.168.10.253 11211
    Trying 192.168.10.253...
    Connected to 192.168.10.253.
    Escape character is '^]'.
    get u1
    VALUE u1 0 5
    12345
    END
     

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!