284354749 发表于 2018-11-9 06:18:29

nginx配合keepalive的方案(双主)


  blog.etiantian.org   10.0.0.17lb01 主      10.0.0.5
  bbs.etiantian.org    10.0.0.18   lb02主       10.0.0.6
  说明:后端的web服务器一个是apacheweb服务,一个nginx服务
  测试,,选用静态的html,不涉及nfs以及mysql
  1.A1-nginx 负载服务器 01-10.0.0.5-lb01
  ①查看keepalive的配置文件
  # cat   /etc/keepalived/keepalived.conf
  global_defs {
  notification_email {
  31333741@qq.com
  }
  notification_email_from31333741@qq.com
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
  }
  vrrp_instance VI_17 {
  state MASTER
  interface eth0
  virtual_router_id 51
  priority 150
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.17/24 dev eth0 label eth0:1
  }
  }
  vrrp_instance VI_18 {
  state BACKUP
  interface eth0
  virtual_router_id 52
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.18/24 dev eth0 label eth0:2
  }
  }
  #
  ②查看nginx的配置文件
  # cat /application/nginx/conf/nginx.conf
  worker_processes1;
  events {
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout65;
  upstream server_pools {
  server 172.16.1.7:80weight=1;
  server 172.16.1.8:80weight=1;
  }
  server {
  listen       10.0.0.17:80;
  server_nameblog.etiantian.org;
  location / {
  proxy_pass http://server_pools;
  proxy_set_header Host$host;
  proxy_set_header X-Forwarded-For $remote_addr;
  }
  }
  server {
  listen       10.0.0.18:80;
  server_namebbs.etiantian.org;
  location / {
  proxy_pass http://server_pools;
  proxy_set_header Host$host;
  proxy_set_header X-Forwarded-For $remote_addr;
  }
  }
  }
  #
  A2-nginx 负载服务器 02 -10.0.0.6-lb02
  ①查看keepalive的配置文件
  # cat /etc/keepalived/keepalived.conf
  global_defs {
  notification_email {
  31333741@qq.com
  }
  notification_email_from31333741@qq.com
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL1
  }
  vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.17/24 dev eth0 label eth0:1
  }
  }
  vrrp_instance VI_18 {
  state MASTER
  interface eth0
  virtual_router_id 52
  priority 150
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.18/24 dev eth0 label eth0:2
  }
  }
  #
  ②查看nginx的配置文件
  # cat/application/nginx/conf/nginx.conf
  worker_processes1;
  events {
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout65;
  upstream server_pools {
  server 172.16.1.7:80weight=1;
  server 172.16.1.8:80weight=1;
  }
  server {
  listen       80;
  server_nameblog.etiantian.org;
  location / {
  proxy_pass http://server_pools;
  proxy_set_header Host$host;
  proxy_set_header X-Forwarded-For $remote_addr;
  }
  }
  server {
  listen      10.0.0.18:80;
  server_namebbs.etiantian.org;
  location / {
  proxy_pass http://server_pools;
  proxy_set_header Host$host;
  proxy_set_header X-Forwarded-For $remote_addr;
  }
  }
  }
  #
  
测试blog和bbs的站点:




  宕掉一台负载均衡器再接着测试
  宕机以后查看IP:可以看出IP全部飘过来了
  # ifconfig
  eth0      Link encap:EthernetHWaddr 00:0C:29:90:A5:31
  inet addr:10.0.0.5Bcast:10.0.0.255Mask:255.255.255.0
  inet6 addr: fe80::20c:29ff:fe90:a531/64 Scope:Link
  UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
  RX packets:299420 errors:0 dropped:0 overruns:0 frame:0
  TX packets:319616 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:1000
  RX bytes:26869861 (25.6 MiB)TX bytes:58054361 (55.3 MiB)
  eth0:1    Link encap:EthernetHWaddr 00:0C:29:90:A5:31
  inet addr:10.0.0.17Bcast:0.0.0.0Mask:255.255.255.0
  UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
  eth0:2    Link encap:EthernetHWaddr 00:0C:29:90:A5:31
  inet addr:10.0.0.18Bcast:0.0.0.0Mask:255.255.255.0
  UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
  eth1      Link encap:EthernetHWaddr 00:0C:29:90:A5:3B
  inet addr:172.16.1.5Bcast:172.16.1.255Mask:255.255.255.0
  inet6 addr: fe80::20c:29ff:fe90:a53b/64 Scope:Link
  UP BROADCAST RUNNING MULTICASTMTU:1500Metric:1
  RX packets:1971 errors:0 dropped:0 overruns:0 frame:0
  TX packets:356 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:1000
  RX bytes:334109 (326.2 KiB)TX bytes:47217 (46.1 KiB)
  lo      Link encap:Local Loopback
  inet addr:127.0.0.1Mask:255.0.0.0
  inet6 addr: ::1/128 Scope:Host
  UP LOOPBACK RUNNINGMTU:65536Metric:1
  RX packets:293602 errors:0 dropped:0 overruns:0 frame:0
  TX packets:293602 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:0
  RX bytes:17072153 (16.2 MiB)TX bytes:17072153 (16.2 MiB)
  #
  web测试(依然OK):

  keepalive和 drbd 都是基于硬件方面的切换
  例如:服务器宕机或者keepalive服务停掉,ip就会自动切换
  但是有一个问题就是nginx服务没有正常开启或者宕掉。但是这个时候
  keepalive的虚拟IP不会自动漂移,以下是解决方案。
  思路:监控nginx服务,如果不正常,就停掉keepalived或者关机
  方法1.守护进程的方式
  说明:可以在每个负载均衡器上让这个脚本在后台执行
  # catcheck_w_proxy.sh
  #!/bin/bash
  while true
  do
  nginxpid=`ps-Cnginx--no-header|wc -l`
  if [ $nginxpid -eq 0 ];then
  /application/nginx/sbin/nginx
  sleep 5
  nginxpid=`ps-Cnginx--no-header|wc -l`
  if [ $nginxpid-eq 0 ];then
  /etc/init.d/keepalived stop
  exit 1
  fi
  fi
  sleep5
  done
  #
  # shcheck_w_proxy.sh&
   3046
  方法2.keepalive内置触发配置
  在每个负载均衡器的虚拟Ip为master内配置,然后调用,
  主节点挂了,备节点来接管
  # cat/etc/keepalived/keepalived.conf
  global_defs {
  notification_email {
  31333741@qq.com
  }
  notification_email_from31333741@qq.com
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL1
  }
  vrrp_scriptcheck_nginx_proxy {
  script"/server/scripts/check_w_proxy.sh"
  interval 2
  weight 2
  }
  vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.17/24 dev eth0 label eth0:1
  }
  }
  vrrp_instance VI_18 {
  state MASTER
  interface eth0
  virtual_router_id 52
  priority 150
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.18/24 dev eth0 label eth0:2
  }
  track_script {
  check_nginx_proxy
  }
  }
  #
  

  


页: [1]
查看完整版本: nginx配合keepalive的方案(双主)