实现要点
- 需要安装keepalived和nginx,不需要haproxy
- keepalived实现两个功能,一是浮动ip,二是nginx的高可用
- nginx实现zuul的负载均衡
- keepalived和nginx必须安装在同一台机器
- keepalived的启动必须用root,而nginx的高可用是通过keepalived,所以keepalived和nginx都用root安装和启动快一点
- 至少两台机器安装keepalived和nginx并启动着,但同一个时间使用的是其中一台,当这台挂了,才会使用另一台,感觉可能两台就够了,不需要三台以上
- 浮动ip需要选择同一个网段的、还没使用的随便一个ip就可以
总述
21机器是vip,18、19两台机器主备。
测试
- 访问http://1.13.22.21/vacorder/api/Subscribe,18和19的zuul轮流打印出日志,nginx负载成功
- 停掉18的nginx,几秒后自动启动,18的keepalived自动启动nginx成功
- 停掉18的keepalived,访问http://1.13.22.21/api/Subs成功,19keepalived接手成功,双机高可用成功
keepalived和nginx的经验少,后续继续分享。
keepalived配置
18keepalived配置
[root[@test](https://my.oschina.net/azibug) keepalived]# more keepalived.conf
! Configuration File for keepalived
global_defs {
router_id test1
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx_pid.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 110
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
1.13.22.21
}
track_script {
chk_nginx
}
}
[root[@test](https://my.oschina.net/azibug) keepalived]#
19keepalived配置
与18基本一样,除了:
- router_id要改为本机名
- state要改为BACKUP
[root[@test2](https://my.oschina.net/u/1253032) keepalived]# more keepalived.conf
! Configuration File for keepalived
global_defs {
router_id test2
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx_pid.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
1.13.22.21
}
}
nginx配置
18nginx配置
[root[@test](https://my.oschina.net/azibug) nginx]# more nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream upstream_nginx_zuul {
server 1.13.22.18:8181;
server 1.13.22.19:8181;
}
server {
listen 80;
location / {
proxy_set_header Host $host:$server_port;
proxy_pass http://upstream_nginx_zuul;
}
}
include /etc/nginx/conf.d/*.conf;
}
[root@test1 nginx]#
19nginx配置
和18配置完全一样。
[root@test nginx]# more nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream upstream_nginx_zuul {
server 1.13.22.18:8181;
server 1.13.22.19:8181;
}
server {
listen 80;
location / {
proxy_set_header Host $host:$server_port;
proxy_pass http://upstream_nginx_zuul;
}
}
include /etc/nginx/conf.d/*.conf;
}
高可用检查脚本
两台机器一样。
[root@test2 keepalived]# more check_nginx_pid.sh
#!/bin/bash
LOG_DIR="/etc/keepalived"
echo $(date "+%Y-%m-%d %H:%M:%S") "check nginx status" >> $LOG_DIR/log_nginx.log
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/sbin/nginx #重启nginx
echo $(date "+%Y-%m-%d %H:%M:%S") "restarting nginx..." >> $LOG_DIR/restart.log
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败,则停掉keepalived服务,进行VIP转移
#killall keepalived
echo $(date "+%Y-%m-%d %H:%M:%S") "killing keepalived..." >> $LOG_DIR/restart.log
fi
fi