程序员

Keepalived+Nginx+Memcached构架高可用

2018-12-03  本文已影响0人  Lc_fly

假设场景:

web应用服务器1 : 192.168.21.21:8001

web应用服务器2 : 192.168.21.22:8001

需要虚拟出IP:192.168.21.201:8805

解决方案:

在web1和web2上部署同样的两套应用程序,端口号都为8001.

在web1和web2上部署nginx,nginx使用集群方式,将请求到的8805端口分发到集群服务器上。

在web1和web2上部署keepalived,keepalived配置虚拟ip:192.168.21.201

在web1和web2上部署Memcached,进行session复制。

实际路由:

外部访问192.168.21.201:8805,keepalived监听到后,确认分发到哪一台nginx。nginx通过箭筒8805端口,使用集群策略,再分发到具体服务器。

安装nginx

nginx依赖openssl、pcre、zlib。

nginx具体安装方案查看: Linux系统 - 源码编译安装Nginx

安装keepalived

yum install -y keepalived
keepalived -v

或者通过源码编译方式:

wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

tar xzf keepalived-1.2.15.tar.gz

cd keepalived-1.2.15

./configure

make && make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/sbin/keepalived /usr/sbin/

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

chkconfig keepalived on

配置keepalived

假设web1为主机,web2为备份机。

编辑web1的keepalived的配置文件:


vim /etc/keepalived/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 LVS_DEVEL

}

vrrp_script chk_http_port {

    script "</dev/tcp/127.0.0.1/8801"   #监听nginx状态

    interval 1

    weight -2

}

vrrp_instance VI_1 {

    state MASTER     #主机设置为MASTER

    interface eth0     #使用ifconfig,查看当前网卡名称

    virtual_router_id 51   #主机ID

    priority 100         #主机优先级

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.21.201    #监听的虚拟IP

    }

    track_script {

        chk_http_port

    }

}

编辑web2的keepalived的配置文件:


! 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 LVS_DEVEL

}

vrrp_script chk_http_port {

    script "</dev/tcp/127.0.0.1/8801"

    interval 1

    weight -2

}

vrrp_instance VI_1 {

    state BACKUP     #备份机

    interface eth0     #备份机ifconfig的网卡

    virtual_router_id 51   #必须与主机相同

    priority 99           #必须小于主机

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.21.201

    }

    track_script {

        chk_http_port

    }

}

配置nginx

两台服务器的nginx配置一样

 vim /usr/local/nginx/conf/nginx.conf

检验keepalived和nginx

    service keepalived start
     /usr/loca/nginx/sbin/nginx
     /var/log/messages
master

说明虚拟IP配置成功

slave

没有虚拟IP地址,说明主机良好的状态下,备份机没有接管。

模拟服务器宕机状况(服务器关机、keepalived宕机)
模拟nginx挂掉
ps -ef|grep nginx
kill -9 pid

配置Memcached

在两台服务器上都装上Memcached。
memcached需要依赖libevent:

tar xf libevent-2.0.22-stable.tar.gz

cd libevent-2.0.22-stable

./configure --prefix=/usr/local/libevent

make && make install

echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf

ldconfig
tar xf memcached-1.4.24.tar.tar

cd memcached-1.4.24

./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

make && make install
/usr/local/bin/memcached -d -m 1024 -u root  -p 11211 -c 1024

启动参数说明:

  • -d 选项是启动一个守护进程,
  • -m 是分配给Memcache使用的内存数量,单位是MB,默认64MB
  • -M return error on memory exhausted (rather than removing items)
  • -u 是运行Memcache的用户,如果当前为root 的话,需要使用此参数指定用户。
  • -l 是监听的服务器IP地址,默认为所有网卡。
  • -p 是设置Memcache的TCP监听的端口,最好是1024以上的端口
  • -c 选项是最大运行的并发连接数,默认是1024
  • -P 是设置保存Memcache的pid文件
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:192.168.21.21:11211,n2:192.168.21.22:11211"
        sticky="false"
        sessionBackupAsync="false"
        lockingMode="auto"
        requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"                  
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>

其中n1和n2 即需要负载的机器上所装memcached的ip和端口。如家政的iFly-Housekeep项目需要
192.168.21.21和192.168.21.22两台机器,这两台上都需要安装memcached,端口都为11211

拷贝memcached的jar包

image.png

最近在把之前写在印象笔记里的一些压箱底的资料发布出来,这一篇是16年时候写的。。

上一篇 下一篇

猜你喜欢

热点阅读