keepalived+nginx
Keepalived是一个基于VRRP协议来实现的服务高可用方案,也是一种高性能的服务器高可用或热备解决方案,可以利用其来避免IP单点故障,类似的工具还有heartbeat、corosync、pacemaker。但是它一般不会单独出现,而是与其它负载均衡技术(如lvs、haproxy、nginx)一起工作来达到集群的高可用。为了更好的实现页面的调用,通过部署Keepalived+nginx来实现负载均衡,使得nginx高可靠性运行。基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接,实现Web服务器负载均衡。
架构规划:
tomcat服务器:172.25.0.31、172.25.0.32
负载均衡服务器nginx、调度器:172.25.0.41、172.25.0.42
虚拟服务器(VIP):172.25.0.251
一、服务的安装:
两台负载均衡服务器服务分别安装nginx
安装nginx
1、安装软件依赖包:
yum -y groupinstall "Development Tools""Server Platform Deveopment"
yum -y install gcc pcre-devel zlib-devel openssl-devel
2、在所有的主机上面都操作,在两台nginx上执行以下操作;下载nginx包:
cd /usr/local/src/[root@nginx src]# yum install wget �Cy ##安装wget工具
3、下载nginx包:
[root@ nginx src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
4、添加nginx运行的用户:
[root@ nginx sbin]# useradd nginx
5解压nginx包,并且安装:
[root@nginx src]# tar zxvf nginx-1.12.0.tar.gz[root@nginx src]# cd nginx-1.12.0/
6、安装nginx包:
[root@nginx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx--user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module--with-http_stub_status_module --with-http_gzip_static_module --with-pcre
###编译安装
[root@nginx nginx-1.12.0]# make && make install
7、然后我们可以启动nginx:
[root@nginx nginx]# cd /usr/local/nginx/sbin/[root@nginx sbin]# ./nginx
8、查看一下是否监听到端口:
[root@nginx sbin]# netstat -ntlpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8035/nginx:master
两台tomcat服务器都安装tomcat
1、[root@tomcat ~]# cd /usr/local/src [root@tomcat src]#wget http://download.oracle.com/otn-pub/java/jdk/8u144-b01/090f390dda5b47b9b721c7dfaa008135/jdk-8u144-linux-x64.tar.gz?AuthParam=1503469020_c118f54d6bc26126e3ac84591f244ed5 [root@tomcat src]# tar -xf jdk-8u111-linux-x64.gz [root@ tomcat src]# ln -s /usr/local/src/jdk1.8.0_111 /usr/local/jdk
2、设置环境变量JAVA_HOME,添加以下三项:
[root@ tomcat src]#cat /etc/procfileexport JAVA_HOME=/usr/local/jdkexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATHexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
3、测试看是否安装成功:
[root@ centos src]# java -versionjava version "1.8.0_111"Java(TM) SE Runtime Environment (build 1.8.0_111-b14)Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixedmode)
4
[root@tomcat src]#Wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz
5、安装tomcat:
[root@tomcat src]# tar zxvf apache-tomcat-8.5.20.tar.gz [root@tomcat src]# ln -s /usr/local/src/apache-tomcat-8.5.20/usr/local/tomcat[root@tomcat src]# chown -R root.root /usr/local/tomcat/
设置环境变量:
[root@tomcat src]# tail -1 /etc/profileexport TOMCAT_HOME=/usr/local/tomcat/ [root@ tomcat ~]# vim /usr/local/tomcat/conf/server.xml在<Hostname="localhost" appBase="webapps" unpackWARs="true"autoDeploy="true">
添加<Context path="" docBase=""debug="0" reloadable="false"crossContext="true"/>把监听端口改为80<Connector port="80"protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> [root@ tomcat ~]# cd /usr/local/tomcat/bin/[root@ tomcat bin]# ./catalina.sh start
配置Keepalived服务
在nginx主机上配置该服务
[root@nginx src]#yum -y install libnl libnl-devel libnfnetlink-devel popt-devel[root@nginx src]#wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz[root@nginx src]#tar zxvf keepalived-1.2.7.tar.gz -C /usr/local[root@nginx src]#cd ../keepalived-1.2.7[root@nginx keepalived-1.2.7]#./configure [root@nginx keepalived-1.2.7]# make && make install[root@nginx keepalived-1.2.7]#cp/usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/[root@nginx keepalived-1.2.7]#cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/[root@nginx keepalived-1.2.7]#mkdir /etc/keepalived[root@nginx keepalived-1.2.7]#cp/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ [root@nginx keepalived-1.2.7]#cp/usr/local/sbin/keepalived /usr/sbin/
加入启动服务
[root@nginx keepalived-1.2.7]#echo"/usr/local/nginx/sbin/nginx" >> /etc/rc.local[root@nginx keepalived-1.2.7]#echo"/etc/init.d/keepalived start" >> /etc/rc.local
二、服务的配置:
2.1配置两台的nginx反向代理
[root@nginx keepalived-1.2.7]#vim /usr/local/nginx/conf/nginx.confupstream web_up{ server 172.25.0.31 max_fails=3 fail_timeout=20s weight=2; server 172.25.0.32 max_fails=3 fail_timeout=20s weight=2; }server { listen 80; server_name 127.0.0.1; location / { proxy_pass http://web_up; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwared-For $proxy_add_x_forwarded_for; }
2.2 配置keepalived
按照上面的安装方法,keepalived的配置文件在/etc/keepalived/keepalived.conf。主、从服务器的配置相关联但有所不同。如下:
Master:[root@nginx keepalived-1.2.7]# cd /etc/keepalived/[root@nginx keepalived-1.2.7]#vim keepalived.conf
! Configuration File for keepalived global_defs {notification_email { notification_email { #acassen@firewall.loc #failover@firewall.loc #sysadmin@firewall.loc } #notification_email_from wanghaikuo@hysec.com #smtp_serversmtp.hysec.com #smtp_connect_timeout 30 router_id LVS_DEVEL }
vrrp_instance VI_1 { state MASTER interface eth1 virtual_router_id51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.25.0.251 }}
2.3另一台nginx配置keepalived的状态为backup。
Backup:
[root@nginx keepalived-1.2.7]# cd /etc/keepalived/
[root@ nginx keepalived-1.2.7]#vim keepalived.conf
! Configuration File for keepalived global_defs {notification_email { notification_email { #acassen@firewall.loc #failover@firewall.loc #sysadmin@firewall.loc } #notification_email_from wanghaikuo@hysec.com #smtp_serversmtp.hysec.com #smtp_connect_timeout 30 router_id LVS_DEVEL }
vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.25.0.251 }}
三、测试keepalived获取的vip情况:
先后在主、从服务器上启动keepalived:/etc/init.d/keepalived start
在主服务器上查看是否已经绑定了虚拟IP: ip addr
停止主服务器上的keepalived:/etc/init.d/keepalived stop 然后在从服务器上查看是否已经绑定了虚拟IP:
启动主服务器上的keepalived,看看主服务器能否重新接管虚拟IP
四、配置nginx的监控脚本
【简单脚本】
[root@/]#cd /etc/keepalived/[root@keepalived/]#Vim chk_nginx.sh
#!/bin/bashPgrep ngnixport=80if [ $? �Ceq 0 ] ; then echo ”nginx isrunning”else /usr/local/nginx/sbin/nginxfi
[root@keepalived/]#chmod a+x chk_nginx.sh
然后在keepalived.conf添加脚本路径,在keepalived.conf中增加如下配置:
vrrp_script chk_http_port { script " /etc/keepalived/chk_nginx.sh" interval 2 weight 2}track_script { chk_http_port}
测试结果:当一台主机的keepalived停止时,另一台keepalived将会接管web服务。
另加一台测试主机:
[root@cobbler html]# curl 172.25.0.251web1[root@cobbler html]# curl 172.25.0.251web2
本文出自 “我的运维” 博客,谢绝转载!