423212 发表于 2017-9-7 18:15:42

keepalived+nginx+tomcat的简单部署


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/
# yum install wget –y            ##安装wget工具

3、下载nginx包:
# wget http://nginx.org/download/nginx-1.12.0.tar.gz

4、添加nginx运行的用户:
# useradd nginx

5解压nginx包,并且安装:
# tar zxvf nginx-1.12.0.tar.gz
# cd nginx-1.12.0/

6、安装nginx包:
# ./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

###编译安装

# make && make install

7、然后我们可以启动nginx:
# cd /usr/local/nginx/sbin/
# ./nginx

8、查看一下是否监听到端口:
# netstat -ntlp
Active 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、# cd /usr/local/src
Wget http://download.oracle.com/otn-pub/java/jdk/8u144-b01/090f390dda5b47b9b721c7dfaa008135/jdk-8u144-linux-x64.tar.gz?AuthParam=1503469020_c118f54d6bc26126e3ac84591f244ed5
# tar -xf jdk-8u111-linux-x64.gz
# ln -s /usr/local/src/jdk1.8.0_111/usr/local/jdk


2、设置环境变量JAVA_HOME,添加以下三项:
#cat/etc/procfile
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
3、测试看是否安装成功:
# java -version
java 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、#Wgethttp://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz
5、安装tomcat:
# tar zxvf apache-tomcat-8.5.20.tar.gz
# ln -s /usr/local/src/apache-tomcat-8.5.20/usr/local/tomcat
# chown -R root.root /usr/local/tomcat/
设置环境变量:
# tail -1 /etc/profile
export TOMCAT_HOME=/usr/local/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" />

# cd /usr/local/tomcat/bin/
# ./catalina.sh start

配置Keepalived服务
在nginx主机上配置该服务
#wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

#tar zxvf keepalived-1.2.7.tar.gz-C /usr/local
#cd ../keepalived-1.2.7
#./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/
#mkdir /etc/keepalived
cp/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#cp/usr/local/sbin/keepalived /usr/sbin/
加入启动服务
#echo"/usr/local/nginx/sbin/nginx" >> /etc/rc.local
#echo"/etc/init.d/keepalived start" >> /etc/rc.local

二、服务的配置:
2.1配置两台的nginx反向代理
#vim/usr/local/nginx/conf/nginx.conf
upstream web_up{
      server 172.25.0.31max_fails=3 fail_timeout=20s weight=2;
      server 172.25.0.32max_fails=3 fail_timeout=20s weight=2;
      }
server {
      listen       80;
       server_name127.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:
# cd /etc/keepalived/
#vimkeepalived.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:
# cd /etc/keepalived/
#vimkeepalived.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的监控脚本
【简单脚本】
cd /etc/keepalived/
Vim chk_nginx.sh
#!/bin/bash
Pgrep ngnix
port=80
if [ $? –eq 0 ] ; then
echo ”nginx isrunning”
else
   /usr/local/nginx/sbin/nginx
fi
#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
}
测试结果:当一台主机的nginx停止时,另一台nginx将会接管web服务。




mayiwen123456 发表于 2017-9-9 11:10:20

不错,借鉴借鉴
页: [1]
查看完整版本: keepalived+nginx+tomcat的简单部署