cmkzjw 发表于 2018-12-29 12:53:40

Nginx负载均衡+keepalived高可用

  注:环境介绍:VMware环境下4台虚拟机 两台做负载均衡服务器 两台做realserver (模拟物理web服务器)
1配置keepalived实现nginx负载均衡高可用,keepalived 主要功能是是实现主备负载    均衡服务器之间的高可用,当一台宕机另一台会自动接管.实现热备。
2 在主备负载均衡服务器个配置nginx反向代理服务,并在本地(你的windows电      脑)/etc/hosts 做域名解析。
http://i2.运维网.com/images/blog/201801/11/20c438dd99bdadf4b8de03331be715dc.png

# cat /etc/keepalived/keepalived.conf
! 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 130
}
vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth2
virtual_router_id 130
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.232.80/24 dev eth0 label eth0:1
}
}
virtual_server 192.168.232.80 80 {
delay_loop 6            
lb_algo wrr               
lb_kind DR               
nat_mask 255.255.255.0
persistence_timeout 50   
protocol TCP               
}
lvs01-nginx
# cat /application/nginx/conf/nginx.conf
#usernobody;
worker_processes1;
error_loglogs/error.log;
error_loglogs/error.lognotice;
error_loglogs/error.loginfo;
#pid      logs/nginx.pid;
events {
worker_connections1024;
}
http {
include       mime.types;
default_typeapplication/octet-stream;
log_formatcommonlog'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfile      on;
keepalive_timeout65;
include extra/upstream01.conf;
}
# cat /application/nginx/conf/nginx.conf
#usernobody;
worker_processes1;
error_loglogs/error.log;
error_loglogs/error.lognotice;
error_loglogs/error.loginfo;
#pid      logs/nginx.pid;
events {
worker_connections1024;
}
http {
include       mime.types;
default_typeapplication/octet-stream;
log_formatcommonlog'$remote_addr - $remote_user [$time_local] "$request" '
'$status
$body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfile      on;
keepalive_timeout65;
include extra/upstream01.conf;
}
#cat /application/nginx/conf/extra/upstream01.conf
#blog lb by cyt at 20180107
upstream server_pools {                                 server 192.168.232.132:80 weight=5;
server 192.168.232.133:80 weight=5;
}
server {
listen   80;
server_name www.etiantian01.org;
location/ {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For   $remote_addr;
}
}
lvs02:
# cat /etc/keepalived/keepalived.conf
! 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 131
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth2
virtual_router_id 130
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.232.80/24 dev eth0 label eth0:1
}
}
virtual_server 192.168.232.80 80 {
delay_loop 6            
lb_algo wrr               
lb_kind DR               
nat_mask 255.255.255.0
persistence_timeout 50   
protocol TCP               
}
lvs02-nginx
# cat /application/nginx/conf/nginx.conf
#usernobody;
worker_processes1;
error_loglogs/error.log;
error_loglogs/error.lognotice;
error_loglogs/error.loginfo;
#pid      logs/nginx.pid;
events {
worker_connections1024;
}
http {
include       mime.types;
default_typeapplication/octet-stream;
log_formatcommonlog'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfile      on;
keepalive_timeout65;
include extra/upstream01.conf;
}
#cat /application/nginx/conf/extra/upstream01.conf
#blog lb by cyt at 20180107
upstream server_pools {
server 192.168.232.132:80 weight=5;
server 192.168.232.133:80 weight=5;
}
server {
listen   80;
server_name www.etiantian01.org;
location/ {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
web01 和web02部署nginx和抑制ARP和绑定vip (后面两部写脚本执行)
# ps -ef | grep nginx
root       1342      10 11:20 ?      00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx      1343   13420 11:20 ?      00:00:00 nginx: worker process      
root       1346   12980 11:29 pts/0    00:00:00 grep nginx
# netstat -lnt | grep 80
tcp      0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      
# lsof -i tcp:80
COMMANDPIDUSER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   1342root    6uIPv411119      0t0TCP *:http (LISTEN)
nginx   1343 nginx    6uIPv411119      0t0TCP *:http (LISTEN)
#
然后抑制arp和绑定vip我们写如下脚本实现:
#!/bin/bash
# chkconfig: 3 52 23
#created by chenyant 11:43 2013/8/11
. /etc/init.d/functions
VIP=(
192.168.232.80
)
start(){
for ((i=0;i/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
}
start
服务启动后脚本执行了以后然后就检查:
可以模拟主备负载均衡服务器宕机 然后客户端持续访问看下是否会有影响。
拆除单台webserver 看下访问是否会受影响。
此文的只是模拟环境学习使用,生产环境相对复杂些需要结合实践情况学习。


页: [1]
查看完整版本: Nginx负载均衡+keepalived高可用