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端口,使用集群策略,再分发到具体服务器。
1.安装nginx
nginx依赖openssl、pcre、zlib。
nginx具体安装方案查看:Linux系统 - 源码编译安装Nginx
2.安装keepalived
#yuminstall-ykeepalived
#keepalived-v
或者通过源码编译方式:
wgethttp://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
3.配置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 "
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 "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
}
}
4. 配置nginx
两台服务器的nginx配置一样
vim /usr/local/nginx/conf/nginx.conf
5.检验keepalived和nginx
启动keepalived和nginx:
service keepalived start
/usr/loca/nginx/sbin/nginx
/var/log/messages
在web1主机上输入:ip a,查看ip绑定状态:
说明虚拟IP配置成功
在web2备份机上输入:ip a,查看IP绑定状态:
没有虚拟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的高可用。
6.配置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
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 7.拷贝memcached的jar包 将jar包拷入tomcat/lib/目录下
分享文章:Keepalived+Nginx+Memcached构架高可用
文章分享:http://azwzsj.com/article/cgssji.html