0%

linux下Nginx+Tomcat高可用集群搭建

linux下Nginx+Tomcat高可用集群搭建

最近了解到使用keepalived来配置nginx的高可用,搭建一遍记录下,顺便尝试下nginx与tomcat的集群部署,供大家参考。

废话不多,上图:
架构图

how nice ~ great, let’s get started!

[TOC]

准备

软件

名称 版本
keepalived 2.0.19
nginx 1.16.1
tomcat 7.0.96
jdk 1.8.0-231
安装软件所有依赖包(非必须) depends

环境

演示环境为centos7.7-x86_64-minimal 操作系统

IP 部署 说明
192.168.1.91 虚IP映射域名
192.168.1.97 keepalived 主要节点
192.168.1.97 nginx 与98无差别部署
192.168.1.97 tomcat集群 端口 801,802,803
192.168.1.98 keepalived 备份节点
192.168.1.98 nginx 与97无差别部署
192.168.1.98 tomcat集群 端口 801,802,803

nginx 部署

编译安装

安装nginx编译安装的依赖软件包

1
[root@pve-97 nginx]# yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

上传nginx安装包至相应目录,这里我们指定 /wisoft/nginx

1
2
3
[root@pve-97 ~]# ll /wisoft/nginx/
total 1012
-rw-r--r--. 1 root root 1032630 Jan 8 14:35 nginx-1.16.1.tar.gz

解压安装包

1
[root@pve-97 nginx]# tar -zxvf nginx-1.16.1.tar.gz

解压后目录如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@pve-97 nginx-1.16.1]# ll
total 752
drwxr-xr-x. 6 1001 1001 4096 Jan 8 14:47 auto
-rw-r--r--. 1 1001 1001 296463 Aug 13 20:51 CHANGES
-rw-r--r--. 1 1001 1001 452171 Aug 13 20:51 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 Jan 8 14:47 conf
-rwxr-xr-x. 1 1001 1001 2502 Aug 13 20:51 configure
drwxr-xr-x. 4 1001 1001 72 Jan 8 14:47 contrib
drwxr-xr-x. 2 1001 1001 40 Jan 8 14:47 html
-rw-r--r--. 1 1001 1001 1397 Aug 13 20:51 LICENSE
drwxr-xr-x. 2 1001 1001 21 Jan 8 14:47 man
-rw-r--r--. 1 1001 1001 49 Aug 13 20:51 README
drwxr-xr-x. 9 1001 1001 91 Jan 8 14:47 src
  • auto:存放大量脚本,与根目录configure文件相关;
  • conf:存放nginx的配置文件;
  • html:存放nginx首页面和其他的html页面;
  • man:存放nginx的的帮助文档,安装文成后可以使用man命令查看帮助;
  • src:存放nginx源代码
  • configure文件:脚本文件,做一些准备工作,包括系统内核检测、必须软件库检测、参数解析、中间目录生成,生成makefile文件等等;

执行 ./configure 检查并配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@pve-97 nginx-1.16.1]# ./configure 
checking for OS
+ Linux 3.10.0-1062.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found

...

Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

得到配置概要以后,就可以直接编译和安装了 make & make install

安装完成后,nginx被默认安装在 /usr/local/nginx 目录

nginx开机启动

创建文件nginx vi /lib/systemd/system/nginx.service

具体内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]  
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

设置开机启动 systemctl enable nginx.service

1
2
[root@pve-97 init.d]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

使用服务命令启动 systemctl start nginx

image

nginx的操作

启动

1、直接执行二进制程序:

1
2
3
4
[root@iZwz9g2hqiikgs5lncf7f7Z nginx]# pwd
/usr/local/nginx
[root@iZwz9g2hqiikgs5lncf7f7Z nginx]#
[root@iZwz9g2hqiikgs5lncf7f7Z nginx]# ./sbin/nginx

这时,会读取nginx安装目录下的配置文件:/usr/local/nginx/conf/nginx.conf

2、指定配置文件的方式启动:

1
/usr/local/nginx/sbin/nginx –c /tmp/nginx.conf

这时,会读取-c参数后指定的nginx.conf配置文件来启动nginx。

3、指定安装目录的方式启动

1
/usr/local/nginx/sbin/nginx –p /usr/local/nginx/

4、指定全局配置项的启动方式

1
/usr/local/nginx/sbin/nginx –g “pid /var/nginx/test.pid”

这意味着nginx的pid文件会写入到指定的目录。-g参数不能与默认路径下的nginx.conf配置冲突,否则无法成功启动。

停止nginx

1、 快速停止
查找进程 ps -ef | grep nginx
杀掉进程 kill –s SIGTERM 10800kill –s SIGINT 10800

2、使用stop命令
./sbin/nginx –s stop

当快速停止服务时,worker进程与master进程在收到信号后会立刻跳出循环,退出进程。

3、平滑停止
停止master进程:
./sbin/nginx –s quit 等同于 kill -s SIGQUIT
停止work进程:kill -s SIGWINCH

平滑停止服务时,首先会关闭监听端口,停止接收新的连接,然后把当前正在处理的连接全部处理完,最后再退出进程。

重新加载配置文件

/usr/local/nginx/sbin/nginx -s reload

Nginx会先检查新的配置项是否有误,如果全部正确就以“优雅”的方式关闭,再重新启动Nginx来实现这个目的。类似的,-s是发送信号,仍然可以用kill命令发送HUP信号来达到相同的效果。
kill -s SIGHUP

日志文件回滚

使用-s reopen参数可以重新打开日志文件,这样可以先把当前日志文件改名或转移到其他目录中进行备份,再重新打开时就会生成新的日志文件。这个功能使得日志文件不至于过大。例如:
/usr/local/nginx/sbin/nginx -s reopen
当然,这与使用kill命令发送USR1信号效果相同。kill -s SIGUSR1

keepalived 部署

keepalived 编译安装

上传keepalived安装包至相应目录,这里我们指定 /wisoft/keepalived

1
2
3
4
5
[root@pve-97 keepalived]# pwd
/wisoft/keepalived
[root@pve-97 keepalived]# ll
total 1004
-rw-r--r--. 1 root root 1025062 Jan 8 16:59 keepalived-2.0.19.tar.gz

解压命令:tar -zxvf keepalived-2.0.19.tar.gz 并进入目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@pve-97 keepalived-2.0.19]# ll
total 1212
-rw-rw-r--. 1 1000 1000 54387 Oct 20 00:16 aclocal.m4
-rwxr-xr-x. 1 1000 1000 5826 Mar 26 2018 ar-lib
-rw-rw-r--. 1 1000 1000 41 Aug 16 2018 AUTHOR
drwxrwxr-x. 2 1000 1000 44 Oct 20 00:16 bin_install
-rwxrwxr-x. 1 1000 1000 64 Aug 16 2018 build_setup
-rw-rw-r--. 1 1000 1000 494050 Oct 20 00:08 ChangeLog
-rwxr-xr-x. 1 1000 1000 7333 Mar 26 2018 compile
-rwxrwxr-x. 1 1000 1000 405505 Oct 20 00:16 configure
-rw-rw-r--. 1 1000 1000 98443 Oct 20 00:09 configure.ac
-rw-rw-r--. 1 1000 1000 823 Aug 16 2018 CONTRIBUTORS
-rw-rw-r--. 1 1000 1000 18092 Aug 16 2018 COPYING
-rwxr-xr-x. 1 1000 1000 23567 Mar 26 2018 depcomp
drwxrwxr-x. 5 1000 1000 210 Oct 20 00:16 doc
drwxrwxr-x. 3 1000 1000 205 Oct 20 00:16 genhash
-rw-rw-r--. 1 1000 1000 8218 Jul 18 04:10 INSTALL
-rwxr-xr-x. 1 1000 1000 15155 Mar 26 2018 install-sh
drwxrwxr-x. 9 1000 1000 173 Oct 20 00:16 keepalived
-rw-rw-r--. 1 1000 1000 9878 Apr 3 2019 keepalived.spec.in
drwxrwxr-x. 2 1000 1000 4096 Oct 20 00:16 lib
-rw-rw-r--. 1 1000 1000 1807 Feb 3 2019 Makefile.am
-rw-rw-r--. 1 1000 1000 28929 Oct 20 00:16 Makefile.in
-rwxr-xr-x. 1 1000 1000 6872 Mar 26 2018 missing
-rw-rw-r--. 1 1000 1000 2083 Oct 17 01:21 README.md
drwxrwxr-x. 3 1000 1000 41 May 9 2019 snap
-rw-rw-r--. 1 1000 1000 5908 Aug 17 2018 TODO

执行 ./configure --prefix=/wisoft/keepalived 配置安装路径

如果提示

1
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

需要安装依赖软件 yum -y install libnl libnl-devel

如果提示  

1
configure: error: libnfnetlink headers missing

需要安装依赖软件 yum install -y libnfnetlink-devel

再执行 ./configure --prefix=/wisoft/keepalived

最后 make && make install

编译安装成功后,会自动在路径/usr/lib/systemd/system/下生成keepalived.service文件

1
2
[root@pve-97 keepalived]# ll /usr/lib/systemd/system/|grep keepalive
-rw-r--r--. 1 root root 398 Jan 8 17:25 keepalived.service

配置开机启动服务

keepalived默认执行/etc/keepalived/keepalived.conf,所以先创建该目录并拷贝配置

1
2
3
4
5
6
7
mkdir /etc/keepalived

cp /wisoft/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

cp /wisoft/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/keepalived

cp /wisoft/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived

配置开启启动 

1
systemctl enable keepalived.service

新建nginx检测脚本

在路径/wisoft/keepalived/check_nginx_pid.sh脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y/%m/%d-%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
if [ $n -eq "0" ]; then
/usr/local/nginx/sbin/nginx #尝试启动nginx
n2=`ps -C nginx --no-heading|wc -l`
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /wisoft/keepalived/check_ng.log
systemctl stop keepalived # 停止keepalived
fi
fi

修改keepalived配置

修改 /etc/keepalived/keepalived.conf 以下是修改后keepalived.conf全部配置,加#号注释的为需要修改的地方。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
! 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 pve-97
vrrp_skip_check_adv_addr
# vrrp_strict #这个要注释掉,不然会ping不通 vip
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_script chk_nginx { # 检测nginx脚本 定义
script "/wisoft/keepalived/check_nginx_pid.sh" #最后手动执行下此脚本,以确保此脚本能够正常执行
interval 2 #(检测脚本执行的间隔,单位是秒)
weight 2
}

vrrp_instance VI_1 {
# 指定keepalived的角色,“MASTER”表示此主机是主服务器,“BACKUP”表示此主机是备用服务器
state MASTER
# 指定网卡接口,这里改为我们当前使用的网卡
interface ens18
# 虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识
# 即同一vrrp_instance下,MASTER和BACKUP必须是一致的
virtual_router_id 51
# 定义优先级;数字越大,优先级越高(0-255)
# 在同一个vrrp_instance下,“MASTER”的优先级必须大于“BACKUP”的优先级
priority 100
# 设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
advert_int 1
# 设置验证类型和密码
authentication {
# 设置验证类型,主要有PASS和AH两种
auth_type PASS
# 设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
auth_pass 1111
}

virtual_ipaddress {
# 虚拟IP为10.10.0.10/8;绑定接口为ens18;别名ha:net,主备相同
192.168.1.91 dev ens18 label ha:net
}

track_script {
chk_nginx #调用检测nginx脚本
}
}

使用命令 systemctl start keepalived.service 启动keepalived。

使用命令 ip addr show | grep inet 前后观察,可以看到 VIP 192.168.1.91已经绑定。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@pve-97 ~]# ip addr show | grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.97/24 brd 192.168.1.255 scope global noprefixroute ens18
inet6 2002:c064:6401:f:a35e:b5ec:a220:c79d/64 scope global noprefixroute dynamic
inet6 fec0::f:bad3:87a4:760d:3c0b/64 scope site noprefixroute dynamic
inet6 fe80::338d:1893:770:6678/64 scope link noprefixroute
[root@pve-97 ~]# systemctl start keepalived.service
[root@pve-97 ~]# ip addr show | grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.97/24 brd 192.168.1.255 scope global noprefixroute ens18
inet 192.168.1.91/32 scope global ha:net
inet6 2002:c064:6401:f:a35e:b5ec:a220:c79d/64 scope global noprefixroute dynamic
inet6 fec0::f:bad3:87a4:760d:3c0b/64 scope site noprefixroute dynamic
inet6 fe80::338d:1893:770:6678/64 scope link noprefixroute

可以通过另一台服务器来ping 192.168.1.91

1
2
3
4
5
6
[root@pve-98 keepalived]# ping 192.168.1.91
PING 192.168.1.91 (192.168.1.91) 56(84) bytes of data.
64 bytes from 192.168.1.91: icmp_seq=1 ttl=64 time=0.545 ms
64 bytes from 192.168.1.91: icmp_seq=2 ttl=64 time=0.240 ms
64 bytes from 192.168.1.91: icmp_seq=3 ttl=64 time=0.218 ms
64 bytes from 192.168.1.91: icmp_seq=4 ttl=64 time=0.254 ms

部署备份服务器

备份服务器同样部署,只是配置需要修改,其中 priority 要低于MASTER 的配置值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
! 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 pve-98
vrrp_skip_check_adv_addr
# vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_script chk_nginx { # 检测nginx脚本 定义
script "/wisoft/keepalived/check_nginx_pid.sh" #最后手动执行下此脚本,以确保此脚本能够正常执行
interval 2 #(检测脚本执行的间隔,单位是秒)
weight 2
}

vrrp_instance VI_1 {
# 指定keepalived的角色,“MASTER”表示此主机是主服务器,“BACKUP”表示此主机是备用服务器
state BACKUP
# 指定网卡接口,这里改为我们当前使用的网卡
interface ens18
# 虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识
# 即同一vrrp_instance下,MASTER和BACKUP必须是一致的
virtual_router_id 51
# 定义优先级;数字越大,优先级越高(0-255)
# 在同一个vrrp_instance下,“MASTER”的优先级必须大于“BACKUP”的优先级
priority 50
# 设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
advert_int 1
# 设置验证类型和密码
authentication {
# 设置验证类型,主要有PASS和AH两种
auth_type PASS
# 设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
auth_pass 1111
}
# 有故障时是否激活邮件通知
#smtp_alert
# 禁止抢占服务
# 默认情况,当MASTER服务挂掉之后,BACKUP自动升级为MASTER并接替它的任务
# 当MASTER服务恢复后,升级为MASTER的BACKUP服务又自动降为BACKUP,把工作权交给原MASTER
# 当配置了nopreempt,MASTER从挂掉到恢复,不再将服务抢占过来。
#nopreempt
# 虚拟IP,两个节点设置必须一样。可以设置多个,一行写一个

virtual_ipaddress {
# 虚拟IP为10.10.0.10/8;绑定接口为ens18;别名ha:net,主备相同
192.168.1.91 dev ens18 label ha:net
}

track_script {
chk_nginx #调用检测nginx脚本
}
}

验证高可用性

模拟宕机

使用 arp -a 命令可以看到 浮动ip 192.168.1.91192.168.1.97 的mac 地址相同,说明绑定在97服务器上

arp1

通过浏览器访问,可以轻松访问到97的nginx
nginx

把97服务器keepalived 停止,浮动ip 192.168.1.91 飘在 192.168.1.98

1
2
3
4
5
6
7
8
[root@pve-97 ~]# systemctl stop keepalived.service
[root@pve-97 ~]# ip addr show | grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.97/24 brd 192.168.1.255 scope global noprefixroute ens18
inet6 2002:c064:6401:f:a35e:b5ec:a220:c79d/64 scope global noprefixroute dynamic
inet6 fec0::f:bad3:87a4:760d:3c0b/64 scope site noprefixroute dynamic
inet6 fe80::338d:1893:770:6678/64 scope link noprefixroute
1
2
3
4
5
6
7
8
[root@pve-98 keepalived]# ip addr show | grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.98/24 brd 192.168.1.255 scope global noprefixroute ens18
inet 192.168.1.91/32 scope global ha:net
inet6 2002:c064:6401:f:e8d:1b19:6be2:930f/64 scope global noprefixroute dynamic
inet6 fec0::f:e831:5c3b:a61f:e311/64 scope site noprefixroute dynamic
inet6 fe80::e48:6d46:5d45:6f37/64 scope link noprefixroute

192.168.1.91192.168.1.98 的mac 地址相同
arp2

模拟单台nginx不可用

破坏nginx的配置文件,使其无法正常启动,比如加个无效字符串

1
2
3
4
5
6
7
8
events {
worker_connections 1024;
}

wisoft
http {
include mime.types;
default_type application/octet-stream;

调用检测脚本/wisoft/keepalived/check_nginx_pid.sh,发现nginx报错

1
2
[root@pve-97 keepalived]# /wisoft/keepalived/check_nginx_pid.sh 
nginx: [emerg] unknown directive "wisoft" in /usr/local/nginx/conf/nginx.conf:17

keepalived 被自己通过 检测脚本停止,查看检测日志 more /wisoft/keepalived/check_ng.log

1
2
3
4
5
[root@pve-97 keepalived]# more check_ng.log 
2020/01/10-09:41:25 nginx down,keepalived will stop
2020/01/10-09:41:27 nginx down,keepalived will stop
2020/01/10-09:41:29 nginx down,keepalived will stop
2020/01/10-09:41:31 nginx down,keepalived will stop

查看keepalived状态和ip信息,已经切换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@pve-97 keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Fri 2020-01-10 09:43:01 CST; 16min ago
Process: 6023 ExecStart=/wisoft/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 6024 (code=exited, status=0/SUCCESS)

Jan 10 09:43:00 pve-97 Keepalived_vrrp[6025]: Assigned address fe80::338d:1893:770:6678 for interface ens18
Jan 10 09:43:00 pve-97 Keepalived_vrrp[6025]: Registering gratuitous ARP shared channel
Jan 10 09:43:00 pve-97 Keepalived_vrrp[6025]: (VI_1) removing VIPs.
Jan 10 09:43:00 pve-97 Keepalived_vrrp[6025]: (VI_1) Entering BACKUP STATE (init)
Jan 10 09:43:00 pve-97 Keepalived_vrrp[6025]: VRRP sockpool: [ifindex(2), family(IPv4), proto(112), unicast(0), fd(11,12)]
Jan 10 09:43:00 pve-97 systemd[1]: Stopping LVS and VRRP High Availability Monitor...
Jan 10 09:43:00 pve-97 Keepalived[6024]: Stopping
Jan 10 09:43:01 pve-97 Keepalived_vrrp[6025]: Stopped - used 0.003279 user time, 0.000000 system time
Jan 10 09:43:01 pve-97 Keepalived[6024]: Stopped Keepalived v2.0.19 (10/19,2019)
Jan 10 09:43:01 pve-97 systemd[1]: Stopped LVS and VRRP High Availability Monitor.
[root@pve-97 keepalived]# ip addr show | grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.97/24 brd 192.168.1.255 scope global noprefixroute ens18
inet6 2002:c064:6401:f:a35e:b5ec:a220:c79d/64 scope global noprefixroute dynamic
inet6 fec0::f:bad3:87a4:760d:3c0b/64 scope site noprefixroute dynamic
inet6 fe80::338d:1893:770:6678/64 scope link noprefixroute

恢复nginx的配置文件,启动keepalived,VIP正常飘回

1
2
3
4
5
6
7
8
9
[root@pve-97 keepalived]# systemctl start keepalived
[root@pve-97 keepalived]# ip addr show | grep inet
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.97/24 brd 192.168.1.255 scope global noprefixroute ens18
inet 192.168.1.91/32 scope global ha:net
inet6 2002:c064:6401:f:a35e:b5ec:a220:c79d/64 scope global noprefixroute dynamic
inet6 fec0::f:bad3:87a4:760d:3c0b/64 scope site noprefixroute dynamic
inet6 fe80::338d:1893:770:6678/64 scope link noprefixroute

Tomcat集群部署

Tomcat分别启动

Tomcat 的集群部署简单实现。首先使用之前的教程 在Linux下优雅地使用Tomcat部署多个WEB应用,在97、98模拟部署多个TOMCAT应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@pve-97 wisoft]# echo $JAVA_HOME
/wisoft/java/jdk1.8.0_231
[root@pve-97 wisoft]# echo $CATALINA_HOME
/wisoft/tomcat/apache-tomcat-7.0.96
[root@pve-97 wisoft]# ll
total 0
drwxr-xr-x. 2 root root 58 Jan 9 12:36 bin
drwxr-xr-x. 3 root root 26 Jan 9 11:08 java
drwxr-xr-x. 7 root root 110 Jan 8 17:25 keepalived
drwxr-xr-x. 3 root root 53 Jan 8 14:47 nginx
drwxr-xr-x. 5 root root 45 Jan 9 12:32 servers
drwxr-xr-x. 3 root root 69 Jan 9 11:09 tomcat
[root@pve-97 wisoft]# ll bin
total 3748
-rwxr-xr-x. 1 root root 3825096 Jan 9 12:36 nginx
-rwxr-xr-x. 1 root root 1628 Jan 9 12:36 uias1
-rwxr-xr-x. 1 root root 1628 Jan 9 12:36 uias2
-rwxr-xr-x. 1 root root 1628 Jan 9 12:36 uias3
[root@pve-97 wisoft]# ll servers
total 0
drwxr-xr-x. 7 root root 89 Jan 9 12:32 uias1
drwxr-xr-x. 7 root root 89 Jan 9 12:32 uias2
drwxr-xr-x. 7 root root 89 Jan 9 12:32 uias3

如上,部署了3个Tomcat应用,分别为 uias1uias2uias3 它们端口分别为801803803

分别启动 uias1 uias2 uias3

1
2
3
[root@pve-97 wisoft]# uias1 start
[root@pve-97 wisoft]# uias2 start
[root@pve-97 wisoft]# uias3 start

tomcat1

nginx Tomcat 负载均衡配置

打开路径 /usr/local/nginx/conf/nginx.conf 编辑nginx配置文件

找到 http 模块配置段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
...略
http {
...略

# 增加这个配置 (在这儿配置多个服务器)
upstream servers_uias {
# ip_hash指令,将同一用户引入同一服务器。
#ip_hash;
server 127.0.0.1:801 weight=1; #weigth参数表示权值,权值越高被分配到的几率越大。详细配置可见下章表格
server 127.0.0.1:802 weight=1;
server 127.0.0.1:803 weight=1;
}

#修改server
server{
# nginx监听80端口
listen 80;
  # 特别注意server_name配置,这儿在实际使用中配置多个域名,比如test.com,www.test.com。
server_name uias.wisoft.com.cn 192.168.1.91;
location / {
root html;
index index.html;
proxy_pass http://servers_uias; #这里http://后面指向增加的upstream
client_max_body_size 100m;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
...略
}
...略

upstream的server标签参数说明

  • server 127.0.0.1:801

    负载均衡后面的RS配置,可以是IP或域名,如果端口不写,默认是80端口。高并发场景下,IP可以换成域名,通过DNS做负载均衡。

  • weight

    代表服务器的权重,默认值是1.权重数字越大表示接受的请求比例越大。
    max_fails=1|nginx尝试连接后端主机的次数,这个数值是配合proxy_next_upstream,fastcgi_next_upstream和memcached_next_upstream这三个参数来使用,当nginx接收后返回这三个参数定义的状态码时,会将这个请求转发给正常工作的后端服务器,列如404,502,503,max_fails的默认值是1;企业场景:建议2-3次,京东1次,蓝汛10次(CDN),根据业务需求去配置。

  • fail_timeout

    在max_fails定义的失败次数后,距离下次检查的间隔时间,默认是10s,如果max_fails是5,它就检测5次,如果5次都是502。那么,他就会根据fail_timeout的值,等待10s再去检查,还是只检查一次,如果持续502,在不重新加载nginx配置的情况下,每隔10s都只检测一次,常规业务2-3秒比较合理,比如京东3秒,蓝汛3秒,可根据业务需求去配置。

  • backup

    热备配置(RS节点的高可用),当前面激活的RS都失败后会自动启用热备RS,这标志这个服务器作为备份服务器,若主服务器全部宕机了,就会向他转发请求;注意,当负载调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不能使weight和backup。

  • down

    表示单前的server暂时不参与负载。

修改完成后 使用命令/usr/local/nginx/sbin/nginx -s reloadsystemctl restart nginx

验证

简单验证

nginx 默认使用轮询策略,使用VIP 192.168.1.91
通过修改客户端的HOSTS文件,增加一条规则来模拟绑定一个域名 uias.wisoft.com.cn
访问,多次刷新,可以看到轮询请求了不同的tomcat应用

domain1

在98服务器上同样增加Tomcat,将它们加入到nginx配置中来

1
2
3
4
5
6
7
8
9
upstream servers_uias {
#ip_hash;指令,将同一用户引入同一服务器。
server 127.0.0.1:801 weight=1; #weigth参数表示权值,权值越高被分配到的几率越大。详细配置可见下章表格
server 127.0.0.1:802 weight=1;
server 127.0.0.1:803 weight=1;
server 192.168.1.98:801 weight=1;
server 192.168.1.98:802 weight=1;
server 192.168.1.98:803 weight=1;
}

jmeter验证

通过jmeter请求uias.wisoft.com.cn并提取返回的标签值,通过switch判断来跳转固定的采样器,完成脚本。如下图:
JMETER2

使用该脚本简单轮询600遍请求得到如下结果,可以看到所有请求均匀分布在6个Tomcat上,Tomcat分别得到100个请求,验证正确。
jmeter1

the end!