Keepalived+Nginx+Memcached构架高可用

假设场景:

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 {

     [email protected]

     [email protected]

     [email protected]

   }

   #notification_email_from [email protected]

   #smtp_server 192.168.200.1

   #smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_http_port {

    script "

编辑web2的keepalived的配置文件:


! Configuration File for keepalived

global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   #notification_email_from [email protected]

   #smtp_server 192.168.200.1

   #smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_http_port {

    script "

配置nginx

两台服务器的nginx配置一样

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

检验keepalived和nginx

  • 启动keepalived和nginx:
    service keepalived start
     /usr/loca/nginx/sbin/nginx
     /var/log/messages
  • 在web1主机上输入:ip a,查看ip绑定状态:
master

说明虚拟IP配置成功

  • 在web2备份机上输入:ip a,查看IP绑定状态:
slave

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

模拟服务器宕机状况(服务器关机、keepalived宕机)
  • 手动关掉主机keepalived:
    killall keepalived
  • 查看主机的ip a,发现虚拟主机已没有;查看备份机的ip a,发现已经绑定虚拟主机。
  • 手动启动主机的keepalived:
    service keepalived start
  • 再次查看主机的ip a,发现虚拟主机又一次绑定。
  • 说明keepalived可以瞬时切换,运行正常。
模拟nginx挂掉
  • 手动关掉主机的nginx:
ps -ef|grep nginx
kill -9 pid
  • 继续上一步中的验证,keepalived将虚拟主机转到备份机。

    以上验证说明keepalived可以保障nginx的高可用。

配置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
  • 安装memcached:
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
  • 启动memcached:
/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文件
  • 配置tomcat下的conf/context.xml

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

拷贝memcached的jar包

  • 将jar包拷入tomcat/lib/目录下
image.png

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

你可能感兴趣的:(Keepalived+Nginx+Memcached构架高可用)