Skip to content

Commit

Permalink
2016-05-08补充Keepalived内容
Browse files Browse the repository at this point in the history
  • Loading branch information
judasn committed May 8, 2016
1 parent ce7518a commit b6aabaa
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 46 deletions.
6 changes: 6 additions & 0 deletions Keepalived-Settings/nginx_check.sh
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
114 changes: 99 additions & 15 deletions Nginx-Install-And-Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,104 @@
- 如果访问不了,或是出现其他信息看下错误立即:`vim /var/log/nginx/error.log`


## 把 Nginx 添加到系统服务中

- 新建文件:`vim /etc/init.d/nginx`
- 添加如下内容:

``` nginx
#!/bin/bash


#nginx执行程序路径需要修改
nginxd=/usr/local/nginx/sbin/nginx

# nginx配置文件路径需要修改
nginx_config=/usr/local/nginx/conf/nginx.conf

# pid 地址需要修改
nginx_pid=/var/local/nginx/nginx.pid


RETVAL=0
prog="nginx"

# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0

# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi

echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}

# Stop nginx daemons functions.
# pid 地址需要修改
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/local/nginx/nginx.pid
}

# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)

echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1

esac
exit $RETVAL
```

- 修改权限:`chmod 755 /etc/init.d/nginx`
- 启动服务:`service nginx start`
- 停止服务:`service nginx stop`
- 重启服务:`service nginx restart`


## Nginx 配置

- Nginx 默认配置文件:`vim /usr/local/nginx/conf/nginx.conf`
Expand Down Expand Up @@ -358,25 +456,11 @@ limit_conn slimits 5;
- [nginx实现简体繁体字互转以及中文转拼音](https://www.ttlsa.com/nginx/nginx-modules-ngx_set_cconv/)
- [nginx记录分析网站响应慢的请求(ngx_http_log_request_speed)](https://www.ttlsa.com/nginx/nginx-modules-ngx_http_log_request_speed/)
- [nginx空白图片(empty_gif模块)](https://www.ttlsa.com/nginx/nginx-modules-empty_gif/)
- []()
- []()
- []()
- []()
- []()
- []()
- []()



## 资料

- <https://help.aliyun.com/knowledge_detail/5974693.html?spm=5176.788314853.2.18.s4z1ra>
- <>
- <>
- <>
- <>
- <>
- <>
- <>
- <>
- <http://www.ydcss.com/archives/466>

157 changes: 131 additions & 26 deletions Nginx-Keepalived-Install-And-Settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`
Expand Down Expand Up @@ -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`
Expand All @@ -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`
- 如果第一台机重新接管了,则表示成功
- 可以优化的地方,改为双主热备,监控脚本上带有自启动相关细节,后续再进行。



Expand All @@ -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>
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

## 初衷(Original Intention)

- 本系列历时半年之久,初稿预计 2016-04 期间可以结束,所有有些文章是空白或是不完善的。感谢自己坚持下来了。
- 整理下自己所学
- 带动更多的人进入 Linux 世界,特别是做 Java 开发的人
- **学得越多越是明白一个人的力量是不够的,希望你能一起参与,真心希望!!!(鞠躬)**
- Github 项目地址,欢迎 `Fork``Star`<https://github.com/judasn/Linux-Tutorial>
- 文章中用到的一些安装包归纳整理:<http://pan.baidu.com/s/1skAwrFn>
- **学得越多越是明白一个人的力量是不够的,我只是起了一个引子,希望你能一起参与,真心希望!!!(鞠躬)**


## 目录(Contents)

Expand Down Expand Up @@ -61,8 +62,8 @@
## 下一步(Next)

- Apache 安装和配置
- Keepalived 安装和配置
- LVS 安装和配置
- LVS + Keepalived 高可用
- MySQL + Keepalived 高可用
- Zookeeper 安装和配置
- ActiveMQ 服务安装和配置
- GitLab 安装和配置
Expand All @@ -81,7 +82,7 @@
- 欢迎捐赠 ^_^:<http://www.youmeek.com/donate>


## Gtihub 协同视频教程(Participate)
## Github 协同视频教程(Participate)

- 如果您不会使用 Git 或是 Github 也没关系,请认真学习下面视频教程:
- Judas.n 录制
Expand Down
1 change: 1 addition & 0 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@
- [RabbitMQ 安装和配置](RabbitMQ-Install-And-Settings.md)
- [Openfire 安装和配置](Openfire-Install-And-Settings.md)
- [Rap 安装和配置](Rap-Install-And-Settings.md)
- [Nginx + Keepalived 高可用](Nginx-Keepalived-Install-And-Settings.md)
- [黑客入侵检查](Was-Hacked.md)

0 comments on commit b6aabaa

Please sign in to comment.