xml地图|网站地图|网站标签 [设为首页] [加入收藏]
来自 每日资讯 2019-10-14 12:42 的文章
当前位置: 新濠国际登录平台 > 每日资讯 > 正文

在PATH环境变量中添加java跟目录的bin子目录,Ke

  1. 准备好tomcat

   工作原理:两台Nginx通过Keepalived生成二个实例,二台Nginx的VIP互为备份,任何一台Nginx机器如果发生硬件损坏,Keepalived会自动将它的VIP地址切换到另一台机器,不影响客户端的访问。

环境:CenOS 6.5
Nginx-Master:10.10.10.128
Nginx-Backup:10.10.10.129
Tomcat1:10.10.10.130
Tomcat2:10.10.10.131
VIP:10.10.10.100

姓名:荣皓宇

第一台

IP 信息列表:

一、环境基础配置
1、更换国内yum源
2、关闭防火墙、SELinux
3、时间同步

学号:17101223406

tar vxf apache-tomcat-7.0.54.tar.gz

名称     IP

VIP1      192.168.200.254
VIP2      192.168.200.253
Nginx1     192.168.200.202
Nginx2     192.168.200.203

1、在Nginx1/2上编译安装nginx服务
首先搭建Nginx1,
[root@Nginx-1 ~]# yum -y install pcre-devel zlib-devel
[root@Nginx-1 ~]# useradd -M -s /sbin/nologin  nginx
[root@Nginx-1 ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
[root@Nginx-1 ~]# cd /usr/src/nginx-1.6.2
[root@Nginx-1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@Nginx-1 nginx-1.6.2]# cd /usr/local/nginx/html/
[root@Nginx-1 html]# echo "server 192.168.200.202" > index.html
[root@Nginx-1 html]# /usr/local/nginx/sbin/nginx
[root@Nginx-1 html]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4503/nginx 

搭建Nginx2, 同Nginx1搭建方式是一样的。
与Nginx1唯一不同的是:
[root@Nginx-2 html]# echo "server 192.168.200.203" > index.html

2、在Nginx1/2上编译安装keepalived服务
[root@Nginx-1 ~]# yum -y install kernel-devel openssl-devel

[root@Nginx-1 ~]# tar xf keepalived-1.2.13.tar.gz
[root@Nginx-1 ~]# cd keepalived-1.2.13
[root@Nginx-1 keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686 && make && make install

[root@Nginx-1 ~]# chkconfig --add keepalived
[root@Nginx-1 ~]# chkconfig keepalived on
[root@Nginx-1 ~]# chkconfig --list keepalived
[root@Nginx-1 ~]# service keepalived start|stop   

3、修改keepalived配置文件
[root@Nginx-1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        crushlinux@163.com
}
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.254
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.253
    }
}

==================================================================
[root@Nginx-2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
        crushlinux@163.com
}
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.254
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123
    }
    virtual_ipaddress {
        192.168.200.253
    }
}

[root@Nginx-1 ~]# service keepalived start
[root@Nginx-1 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
       valid_lft forever preferred_lft forever

[root@Nginx-2 ~]# service keepalived start
[root@Nginx-2 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:6f:7d:87 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.203/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.253/32 scope global eth0
    inet6 fe80::20c:29ff:fe6f:7d87/64 scope link
       valid_lft forever preferred_lft forever
   
[root@client ~]# elinks --dump
   server 192.168.200.202
[root@client ~]# elinks --dump
   server 192.168.200.203   

  
Nginx-1/2 二台机器都执行监控Nginx进程的脚本
[root@Nginx-1 ~]# cat nginx_pidcheck
#!/bin/bash
while :
do
 nginxpid=`ps -C nginx --no-header | wc -l`
 if [ $nginxpid -eq 0 ]
 then
  /usr/local/nginx/sbin/nginx
  sleep 5
  nginxpid=`ps -C nginx --no-header | wc -l`
  echo $nginxpid
  if [ $nginxpid -eq 0 ]
  then
   /etc/init.d/keepalived stop
  fi
 fi
 sleep 5
done

[root@Nginx-1 ~]# nohup sh nginx_pidcheck &

这是执行无限循环的脚本,两台Nginx机器上都有执行此脚本,每隔5秒执行一次,用ps -C是命令来收集nginx的PID值到底是否为0,如果是0的话,即Nginx已经进程死掉,尝试启动nginx进程;如果继续为0,即Nginx启动失败,则关闭本机的Keeplaived服务,VIP地址则会由备机接管,当然了,整个网站就会全部由备机的Nginx来提供服务了,这样保证Nginx服务的高可用。

脚本测试:
[root@Nginx-1 ~]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4321/nginx         
[root@Nginx-1 ~]# killall -s QUIT nginx
[root@Nginx-1 ~]# netstat -anpt |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      59418/nginx

VIP转移测试:
[root@Nginx-1 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
       valid_lft forever preferred_lft forever
   
[root@Nginx-2 ~]# service keepalived stop
停止 keepalived:                                          [确定]

[root@Nginx-1 ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2d:3d:97 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.202/24 brd 192.168.200.255 scope global eth0
    inet 192.168.200.254/32 scope global eth0
    inet 192.168.200.253/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:3d97/64 scope link
       valid_lft forever preferred_lft forever

[root@client ~]# elinks --dump
   server 192.168.200.202
[root@client ~]# elinks --dump
   server 192.168.200.202

 

二、Web端安装Web服务

转载自知乎:

mv apache-tomcat-7.0.54 /usr/local/tomcat

1、查看是否安装JDK
[root@Tomcat1 ~]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
2、安装JDK
官网下载二进制包
解压到/usr/local/java
    tar zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local/java/
修改环境变量/etc/profile
    JAVA_HOME=/usr/local/java
    PATH=$JAVA_HOME/bin:$PATH
    CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
    export PATH JAVA_HOME CLASSPATH
使环境变量生效:
    source /etc/profile
3、下载Tomcat源码包:
    wget -O /opt/apache-tomcat-9.0.7.tar.gz
4、解压到/usr/local/tomcat
    tar zxvf /opt/apache-tomcat-9.0.7.tar.gz -C /usr/local/
5、修改Tomcat的主页
    rm -rf /usr/local/apache-tomcat-9.0.7/webapps/ROOT/*
    echo "Tomcat1" >/usr/local/apache-tomcat-9.0.7/webapps/ROOT/index.html  #Tomcat1
    echo "Tomcat2" >/usr/local/apache-tomcat-9.0.7/webapps/ROOT/index.html  #Tomcat2
6、测试Tomcat能否正常启动
[root@Tomcat1 bin]# ./usr/local/apache-tomcat-9.0.7/bin
/startup.sh
Using CATALINA_BASE:  /usr/local/apache-tomcat-9.0.7
Using CATALINA_HOME:  /usr/local/apache-tomcat-9.0.7
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-9.0.7/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:      /usr/local/apache-tomcat-9.0.7/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.7/bin/tomcat-juli.jar
Tomcat started.

【嵌牛导读】:lnmp和lamp都已经介绍过了。现在介绍下lnamp的部署

 

curl 10.10.10.130:8080  #返回Tomcat1
curl 10.10.10.131:8080  #返回Tomcat2

【嵌牛鼻子】:lnamp,负载均衡

tar vxf jdk-7u65-linux-x64.tar.gz

至此web端配置完成。

【嵌牛提问】:这个Lnamp部署出来理论最大容量是多少呢

mv jdk1.7.0_65/ /usr/local/java

二、Nginx反向代理安装

【嵌牛正文】:

编辑vim /etc/profile加

1、安装依赖软件
    yum install -y gcc gcc-c++
    yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel
2、官网下载源码包
3、解压源码包
    tar zxvf nginx-1.14.0.tar.gz -C /tmp/
4、编译安装
    useradd -s /bin/false -M nginx
    ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.14.0/ --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
    make && make install
5、配置反向代理/usr/local/nginx/conf/nginx.conf
worker_processes  1;
pid        /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
  use epoll;
  worker_connections  51200;
}
http {
  include      mime.types;
  default_type  application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
  sendfile        on;
  tcp_nopush      on;
  keepalive_timeout  65;
  tcp_nodelay on;
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types    test/plain application/x-javascript test/css application/xml;
  gzip_vary on;

1 概述

export JAVA_HOME=/usr/local/java       #设置java根目录

  upstream backend {
        server 10.10.10.130:8080;
        server 10.10.10.131:8080;

初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已无法承载现有的业务量,需要进行服务集群化部署,本文主要介绍服务端Tomcat多实例部署,以及如何保证web服务的高可用方案。

export PATH=$PATH:$JAVA_HOME/bin  #在PATH环境变量中添加java跟目录的bin子目录

  }
  server {
      listen      80;
      server_name  10.10.10.128;  #Nginx2改为:10.10.10.129
      location / {
          root  /var/www/html;
          index  index.php index.html index.htm;
          proxy_pass ;
      }
  }
}     
先测试再启动:
[root@Nginx1 ~]# /usr/local/nginx-1.14.0/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.14.0//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.14.0//conf/nginx.conf test is successful
[root@Nginx1 ~]# /usr/local/nginx-1.14.0/sbin/nginx
[root@Nginx1 ~]# lsof -i:80
COMMAND  PID  USER  FD  TYPE DEVICE SIZE/OFF NODE NAME
nginx  4896  root    6u  IPv4  18439      0t0  TCP *:http (LISTEN)
nginx  4897 nginx    6u  IPv4  18439      0t0  TCP *:http (LISTEN)

Nginx 是一个高性能的 HTTP反向代理服务器

启动tomcat服务

curl 10.10.10.128  #轮询返回Tomcat1 Tomcat2
Nginx2同样的搭建。
至此Nginx反向代理搭建完成。

Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以利用其来避免服务的单点故障

/usr/local/tomcat/bin/startup.sh

三、使用Keepalived实现高可用

Tomcat 是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器。

   打开浏览器,ip:8080显示tomcat主页

1、安装:
    yum install keepalived -y
2、修改配置文件/etc/keepalived/keepalived.conf
MASTER 端:
! Configuration File for keepalived
vrrp_script check_nginx {
        script "/etc/keepalived/check_nginx.sh"
        interval 2
        weight 2
}
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_DEVEL1
}

2 Nginx的高可用负载均衡架构

关闭服务;

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.100/24 dev eth0
  }
 track_script {
        check_nginx
  }
}
BACKUP端:
修改:
router_id LVS_DEVEL2
state BACKUP
priority 90

如下图:为典型的Tomcat服务多实例部署的架构图

  /usr/local/tomcat/bin/shutdown.sh

检测Nginx脚本:
#!/bin/bash
nginxpid=`ps -C nginx --no-header | wc -l`
if [ $nginxpid -eq 0 ];then
        /etc/init.d/keepalived stop
fi
chmod +x /etc/keepalived/check_nginx.sh

新濠国际登录平台 1

建立一个web 站点

重启Keepalived服务
service keepalived restart

(1)用户通过域名请求到DNS,由DNS解析域名后返回对应的IP地址,该IP及为Keepalived映射服务器的虚拟IP

cd webapps/

四、检验服务的高可用

(2)通过该虚拟IP访问到对应的负载均衡器(Nginx),这里Nginx部署两个,然后通过Keepalived来保证NG的高可用,正常情况下由Keepalived-M将虚拟IP映射转发至Nginx-M,如果Nginx-M出现故障,此时Keepalived会切换至Keepalived-S开始工作,从而保证了NG的单点故障问题。

mkdir web

Nginx1执行:killall nginx
发现 web访问依然正常

(3)通过Nginx负载均衡器,将请求路由到对应的Tomcat服务。

vim index.jsp

Tomcat1执行:/usr/local/apache-tomcat-9.0.7/bin/shutdown.sh
发现 web访问依然正常

3 搭建Keepalived + Nginx + Tomcat的高可用负载均衡架构

Server Info:  

高可用的环境搭建完毕。

3.1 需要准备的软件

SessionID:<%=session.getId()%>

Linux公社的RSS地址

(1)apache-tomcat-8.5.16.tar.gz

<br>

本文永久更新链接地址:https://www.linuxidc.com/Linux/2018-06/152740.htm

(2)nginx-1.12.2.tar.gz

SessionIP:<%=request.getServerName()%> 

新濠国际登录平台 2

(3)keepalived-1.3.9.tar.gz

<br>

3.2 服务器准备

SessionPort:<%=request.getServerPort()%>

两台服务器如:192.168.10.11,192.168.10.12

<br>

3.3 安装需要的依赖包

<%

$ yum -yinstallgcc gcc-c++ automake pcre pcre-devel zlib zlib-developenopenssl-devel

  out.println("server one");

3.4 安装

%>

3.4.1 安装Tomcat

修改Tomcat的server.xml文件

(1)分别在两台服务器中安装Tomcat,解压apache-tomcat-8.5.16.tar.gz及可完成安装。

cp server.xml server.xml.bak

3.4.2 安装Nginx

vim server.xml

(1)解压安装包:tar -zxvf nginx-1.12.2.tar.gz

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">

(2)进入到nginx-1.12.2目录:cd nginx-1.12.2

      <Context docBase="web" path="" reloadable="flase">                                                  

(3)编译:

      </Context>

a) ./configure --with-http_stub_status_module --with-http_ssl_module  --prefix=/usr/local/nginx

      </Host>

b) sudo make && sudo make install

/usr/local/tomcat/bin/shutdown.sh

3.4.3 安装Keepalived

/usr/local/tomcat/bin/startup.sh

(1)解压安装包:tar -zxvf keepalived-1.3.9.tar.gz

打开浏览器,ip:8080显示tomcat主页

(2)进入到keepalived-1.3.9目录:cd keepalived-1.3.9

  第二台

(3)执行编译:

与第一台基本一致,更改测试页的ip地址

a)./configure --prefix=/usr/local/keepalived --sysconf=/etc

scp root@172.16.1.2:/usr/local/tomcat/webapps/web/index.jsp .

b)sudo make && sudo make install

scp root@172.16.1.2:/usr/local/tomcat/conf/server.xml  .

3.5 配置

启动服务/usr/local/tomcat/bin/startup.sh

3.5.1 分别配置两台服务器的Nginx

查看服务

(1)分别修改两台服务器nginx配置文件,vi /usr/local/nginx/conf/nginx.conf

netstat -anpult |grep java

(2)内容如下:

浏览器访问测试 http://ip:8080

#nginx进程数worker_processes1;#单个进程最大连接数events{worker_connections1024;}#http服务器配置http{includemime.types;default_typeapplication/octet-stream;sendfileon;#长连接超时时间,单位是秒keepalive_timeout65;#upstream负载均衡配置,配置路由到tomcat的服务地址以及权重upstreamlocalhost{server192.168.10.11:8080weight=2;server192.168.10.12:8080weight=2;    }#虚拟主机的配置server{#监听端口listen80;#域名可以有多个,用空格隔开server_namelocalhost;location/ {roothtml;indexindex.html index.htm;#nginx跟后端服务器连接超时时间(代理连接超时)proxy_connect_timeout3;#后端服务器数据回传时间(代理发送超时)proxy_send_timeout30;#连接成功后,后端服务器响应时间(代理接收超时)proxy_read_timeout30;proxy_pass;        }error_page500502503504/50x.html;location=/50x.html {roothtml;        }    }}

显示正常

3.5.2 主Keepalived配置

本文由新濠国际登录平台发布于每日资讯,转载请注明出处:在PATH环境变量中添加java跟目录的bin子目录,Ke

关键词: