forked from judasn/Linux-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
243 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
# 如果进程中没有nginx则将keepalived进程kill掉 | ||
A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A | ||
if [ $A -eq 0 ];then ## 如果没有进程值得为 零 | ||
service keepalived stop ## 则结束 keepalived 进程 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
- Keepalived:**1.2.20** | ||
- JDK:**8u72** | ||
- Tomcat:**8.0.32** | ||
- 部署环境: | ||
- 部署环境(下文中以第几台来代表这些主机): | ||
- 虚拟 IP(VIP):192.168.1.50 | ||
- 第一台主机:Nginx 1 + Keepalived 1 == 192.168.1.120(Master) | ||
- 第二台主机:Nginx 2 + Keepalived 2 == 192.168.1.121(Backup) | ||
|
@@ -32,7 +32,7 @@ | |
- 第三、第四台主机部署: | ||
- JDK 的安装:[JDK 安装](JDK-Install.md) | ||
- Tomcat 的安装:[Tomcat 安装和配置、优化](Tomcat-Install-And-Settings.md) | ||
- 第一台主机部署(第二台主机也是按着这样完全配置): | ||
- 第一、二台主机部署(两台部署内容一样): | ||
- Nginx 的安装:[Nginx 安装和配置](Nginx-Install-And-Settings.md) | ||
- 添加虚拟 IP: | ||
- 复制一个网卡信息:`sudo cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0` | ||
|
@@ -63,7 +63,7 @@ | |
- 解压包:`cd /opt/setups/ ; tar zxvf keepalived-1.2.20.tar.gz` | ||
- 编译:`cd /opt/setups/keepalived-1.2.20 ; ./configure --prefix=/usr/program/keepalived` | ||
- 编译安装:`make && make install` | ||
- Keepalived 设置随机启动 | ||
- Keepalived 设置服务和随机启动 | ||
- 复制配置文件到启动脚本目录:`cp /usr/program/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived` | ||
- 增加权限:`chmod +x /etc/init.d/keepalived` | ||
- 编辑配置文件:`vim /etc/init.d/keepalived` | ||
|
@@ -87,30 +87,140 @@ | |
把 14 行的:KEEPALIVED_OPTIONS="-D",改为: | ||
KEEPALIVED_OPTIONS="-D -f /usr/program/keepalived/etc/keepalived/keepalived.conf" | ||
``` | ||
- 启动服务:`service keepalived start` | ||
- 加入随机启动:`chkconfig keepalived on` | ||
- 第一台主机配置: | ||
- 健康监测脚本:`` | ||
- Keepalived 配置文件编辑:`` | ||
- `` | ||
- `` | ||
- `` | ||
- `` | ||
- `` | ||
- `` | ||
- `` | ||
- 第二台主机配置: | ||
- 第一、二台主机配置(两台在 Keepalived 配置上稍微有不一样): | ||
- 健康监测脚本(我个人放在:/opt/bash 目录下):[nginx_check.sh](Keepalived-Settings/nginx_check.sh) | ||
- 健康监测脚本添加执行权限:`chmod 755 /opt/bash/nginx_check.sh` | ||
- 运行监测脚本,看下是否有问题:`sh /opt/bash/nginx_check.sh`,如果没有报错,则表示改脚本没有问题 | ||
- nginx 配置(两台一样配置): | ||
|
||
``` nginx | ||
worker_processes 1; | ||
|
||
events { | ||
worker_connections 1024; | ||
} | ||
|
||
http { | ||
include mime.types; | ||
default_type application/octet-stream; | ||
|
||
sendfile on; | ||
keepalive_timeout 65; | ||
# (重点) | ||
upstream tomcatCluster { | ||
server 192.168.1.122:8080 weight=1; | ||
server 192.168.1.123:8080 weight=1; | ||
} | ||
# (重点) | ||
server { | ||
listen 80; | ||
server_name 192.168.1.50; | ||
|
||
location / { | ||
proxy_pass http://tomcatCluster; | ||
index index.html index.htm; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
- Keepalived 配置文件编辑(第一、二台配置稍微不同,不同点具体看下面重点说明) | ||
- 编辑:`vim /usr/program/keepalived/etc/keepalived/keepalived.conf` | ||
|
||
``` nginx | ||
! Configuration File for keepalived | ||
|
||
# 全局配置 | ||
global_defs { | ||
# 邮箱通知配置,keepalived 在发生切换时需要发送 email 到的对象,一行一个 | ||
notification_email { | ||
#[email protected] | ||
#[email protected] | ||
#[email protected] | ||
} | ||
# 指定发件人 | ||
#notification_email_from [email protected] | ||
# 指定smtp服务器地址 | ||
#smtp_server 192.168.200.1 | ||
# 指定smtp连接超时时间,单位秒 | ||
#smtp_connect_timeout 30 | ||
router_id LVS_DEVEL | ||
vrrp_skip_check_adv_addr | ||
vrrp_strict | ||
} | ||
|
||
# (重点)脚本监控实现 | ||
vrrp_script check_nginx { | ||
# 运行脚本 | ||
script "/opt/bash/nginx_check.sh" | ||
# 时间间隔,2秒 | ||
interval 2 | ||
# 权重 | ||
weight 2 | ||
} | ||
|
||
|
||
vrrp_instance VI_1 { | ||
# (重点)Backup 机子这里是设置为:BACKUP | ||
state MASTER | ||
interface eth0 | ||
virtual_router_id 51 | ||
# (重点)Backup 机子要小于当前 Master 设置的 100,建议设置为 99 | ||
priority 100 | ||
# Master 与 Backup 负载均衡器之间同步检查的时间间隔,单位是秒 | ||
advert_int 1 | ||
authentication { | ||
auth_type PASS | ||
auth_pass 1111 | ||
} | ||
# (重点)配置虚拟 IP 地址,如果有多个则一行一个 | ||
virtual_ipaddress { | ||
192.168.1.50 | ||
} | ||
# (重点)脚本监控调用 | ||
track_script { | ||
check_nginx | ||
} | ||
} | ||
``` | ||
|
||
|
||
### 启动各自服务 | ||
|
||
- 四台机子都停掉防火墙:`service iptables stop` | ||
- 先启动两台 Tomcat:`sh /usr/program/tomcat8/bin/startup.sh ; tail -200f /usr/program/tomcat8/logs/catalina.out` | ||
- 检查两台 Tomcat 是否可以单独访问,最好给首页加上不同标识,好方便等下确认是否有负载 | ||
- `http://192.168.1.122:8080` | ||
- `http://192.168.1.123:8080` | ||
- 启动两台 Nginx 服务:`/usr/local/nginx/sbin/nginx` | ||
- 启动两台 Keepalived 服务:`service keepalived start` | ||
- 查看 Master 和 Backup 两台主机的对应日志:`tail -f /var/log/messages` | ||
|
||
|
||
### 高可用测试 | ||
|
||
- 模拟 Keepalived 挂掉 | ||
- 关闭 Master 主机的 Keepalived,查看 Master 和 Backup 两台主机的对应日志:`cat /var/log/messages` | ||
- 重新开启 Master 主机的 Keepalived,查看 Master 和 Backup 两台主机的对应日志:`cat /var/log/messages` | ||
- 关闭 Master 主机的 Keepalived,查看 Master 和 Backup 两台主机的对应日志:`tail -f /var/log/messages` | ||
- 关闭服务:`service keepalived stop` | ||
- 如果第二台机接管了,则表示成功 | ||
- 重新开启 Master 主机的 Keepalived,查看 Master 和 Backup 两台主机的对应日志:`tail -f /var/log/messages` | ||
- 重启服务:`service keepalived restart` | ||
- 如果第一台机重新接管了,则表示成功 | ||
- 模拟 Nginx 挂掉 | ||
- 关闭 Master 主机的 Nginx,查看 Master 和 Backup 两台主机的对应日志:`cat /var/log/messages` | ||
- 重新开启 Master 主机的 Nginx,查看 Master 和 Backup 两台主机的对应日志:`cat /var/log/messages` | ||
- 完善脚本,增加 Nginx 挂掉后自动重启脚本 | ||
- 关闭 Master 主机的 Nginx,查看 Master 和 Backup 两台主机的对应日志:`tail -f /var/log/messages` | ||
- 关闭服务:`/usr/local/nginx/sbin/nginx -s stop` | ||
- 如果第二台机接管了,则表示成功 | ||
- 重新开启 Master 主机的 Nginx,查看 Master 和 Backup 两台主机的对应日志:`tail -f /var/log/messages` | ||
- 重启 Nginx 服务:`/usr/local/nginx/sbin/nginx -s reload` | ||
- 重启 Keepalived 服务:`service keepalived restart` | ||
- 如果第一台机重新接管了,则表示成功 | ||
- 可以优化的地方,改为双主热备,监控脚本上带有自启动相关细节,后续再进行。 | ||
|
||
|
||
|
||
|
@@ -119,9 +229,4 @@ | |
- <http://xutaibao.blog.51cto.com/7482722/1669123> | ||
- <https://m.oschina.net/blog/301710> | ||
- <http://blog.csdn.net/u010028869/article/details/50612571> | ||
- <> | ||
- <> | ||
- <> | ||
- <> | ||
- <> | ||
|
||
- <http://blog.csdn.net/wanglei_storage/article/details/51175418> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters