设为首页 收藏本站
查看: 1162|回复: 0

[经验分享] keepalived+haproxy+nginx

[复制链接]

尚未签到

发表于 2018-11-15 12:12:34 | 显示全部楼层 |阅读模式
  keepalived+haproxy+nginx
DSC0000.jpg

  vm1-keepalived+haproxy
  eth0 172.16.3.2/16 up
  eth1 192.168.1.2/24 up
  vm2-keepalived+haproxy
  eth0 172.16.3.3/16 up
  eth1 192.168.1.3/24 up
  vm3-web:
  eth0 192.168.1.1/24 up
  vm4-web:
  ech0 192.168.1.10/24 up
  VRRP: 172.16.3.88/16
  测试机本机:
  vm1-keepalived+haproxy配置
  # ifconfig eth0 172.16.3.2/16 up
  # ifconfig eth1 192.168.1.2/24 up
  # yum install keepalived haproxy -y
  haproxy配置文件:
  # vim /etc/haproxy/haproxy.cfg
  #---------------------------------------------------------------------
  # Example configuration for a possible web application.  See the
  # full configuration options online.
  #
  #   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
  #
  #---------------------------------------------------------------------
  #---------------------------------------------------------------------
  # Global settings
  #---------------------------------------------------------------------
  global
  # to have these messages end up in /var/log/haproxy.log you will
  # need to:
  #
  # 1) configure syslog to accept network log events.  This is done
  #    by adding the '-r' option to the SYSLOGD_OPTIONS in
  #    /etc/sysconfig/syslog
  #
  # 2) configure local2 events to go to the /var/log/haproxy.log
  #   file. A line like the following can be added to
  #   /etc/sysconfig/syslog
  #
  #    local2.*                       /var/log/haproxy.log
  #
  log         127.0.0.1 local2
  chroot      /var/lib/haproxy
  pidfile     /var/run/haproxy.pid
  maxconn     4000
  user        haproxy
  group       haproxy
  daemon
  # turn on stats unix socket
  stats socket /var/lib/haproxy/stats
  #---------------------------------------------------------------------
  # common defaults that all the 'listen' and 'backend' sections will
  # use if not designated in their block
  #---------------------------------------------------------------------
  defaults
  mode                    http
  log                     global
  option                  httplog
  option                  dontlognull
  option http-server-close
  option forwardfor       except 127.0.0.0/8
  option                  redispatch
  retries                 3
  timeout http-request    10s
  timeout queue           1m
  timeout connect         10s
  timeout client          1m
  timeout server          1m
  timeout http-keep-alive 10s
  timeout check           10s
  maxconn                 3000
  frontend main
  bind :80
  bind :8088
  #    acl clear dst_port 8088
  #acl login path_beg /login
  #redirect location http://www.baidu.com if login
  #redirect prefix / if clear
  #    reqadd X-Proto:\ SSL if clear
  #rspadd X-Via:\ haproxy if clear
  #option forwardfor except 127.0.0.0/8
  #acl url_static path_beg -i /images /stylesheets /vedios /javascript
  #acl url_static path_end -i .jpg .html .css .js .png .gif
  #use_backend static if url_static
  default_backend webservers
  #backend static
  #    balance roundrobin
  #    server s1 192.168.1.10:80  check port 80
  #    server b1 127.0.01:8080 backup check port 8080
  backend webservers
  balance roundrobin
  #cookie ninghongliang insert nocache
  server s2 192.168.1.1:80 check port 80
  server s1 192.168.1.10:80  check port 80
  server b1 127.0.01:8080 backup check port 8080
  stats enable
  stats hide-version
  stats uri /haha?stats
  stats scope .
  stats realm  HAPorx\ boy
  stats auth   admin1:admin
  stats admin if TRUE
  keepalived配置文件
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  root@localhost
  }
  notification_email_from ning@qq.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  }
  vrrp_script chk_sched_down {
  script "[ -e /etc/keepalived/down ] && exit 1 || exit 0"
  interval 2
  weight -50
  fall 2
  rise 1
  }
  vrrp_script chk_haproxy {
  script "/etc/keepalived/check_haproxy.sh"
  interval 2
  weight -50
  fall 2
  rise 1
  }
  vrrp_instance VI_1 {
  state MASTER
  interface eth0
  virtual_router_id 77
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass ning
  }
  virtual_ipaddress {
  172.16.3.88
  }
  track_script {
  chk_sched_down
  chk_haproxy
  }
  }
  vm2-keepalived+haproxy配置
  # ifconfig eth0 172.16.3.3/16 up
  # ifconfig eth1 192.168.1.3/24 up
  # yum install keepalived haproxy -y
  haproxy配置文件:(这里的配置和上面的一样,不在此说明)
  keepalived配置文件
  ! Configuration File for keepalived
  global_defs {
  notification_email {
  root@localhost
  }
  notification_email_from ning@qq.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  }
  vrrp_script chk_sched_down {
  script "[ -e /etc/keepalived/down ] && exit 1 || exit 0"
  interval 2
  weight -50
  fall 2
  rise 1
  }
  vrrp_script chk_haproxy {
  script "/etc/keepalived/check_haproxy.sh"
  interval 2
  weight -50
  fall 2
  rise 1
  }
  vrrp_instance VI_1 {
  state  BACKUP
  interface eth0
  virtual_router_id 77
  priority 99
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass ning
  }
  virtual_ipaddress {
  172.16.3.88
  }
  track_script {
  chk_sched_down
  chk_haproxy
  }
  }
  这里面使用的检测脚本haprxoy脚本chk_haproxy(测试检测为3秒后如果haproxy还不能上线,就关闭keepalived)
  #!/bin/bash
  A=`ps -C haproxy --no-header |wc -l`
  if [ $A -eq 0 ];then
  /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg
  sleep 3
  if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
  /etc/init.d/keepalived stop
  fi
  fi
  vm3-web:配置
  # ifconfig eth0 192.168.1.1/24 up
  # yum install nginx -y
  # echo "192.168.1.1" > /usr/share/nginx/html/index.html
  vm4-web:配置
  # ifconfig eth0 192.168.1.10/24 up
  # yum install nginx -y
  # echo "192.168.1.10" > /usr/share/nginx/html/index.html
  测试:
  (1)主备同时执行 # ip addr show
  主: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:0c:29:d7:f7:9c brd ff:ff:ff:ff:ff:ff
  inet 172.16.3.2/16 brd 172.16.255.255 scope global eth0
  inet 172.16.3.88/32 scope global eth0
  valid_lft forever preferred_lft forever
  从: eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:0c:29:0b:35:6a brd ff:ff:ff:ff:ff:ff
  inet 172.16.3.3/16 brd 172.16.255.255 scope global eth0
  inet6 fe80::20c:29ff:fe0b:356a/64 scope link
  valid_lft forever preferred_lft forever
  (2)停掉主上的haproxy,2秒后keepalived会自动将其再次启动
  (3)停掉主的keepalived,备机马上接管服务
  eth0:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
  link/ether 00:0c:29:0b:35:6a brd ff:ff:ff:ff:ff:ff
  inet 172.16.3.3/16 brd 172.16.255.255 scope global eth0
  inet 172.16.3.88/32 scope global eth0
  valid_lft forever preferred_lft forever
  (4)本机测试:(因为上面只是简单的定义了轮训,没有定义动静分离)
  http://172.16.3.88
  有图有真相
DSC0001.jpg

DSC0002.jpg



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-635422-1-1.html 上篇帖子: nginx慢查询 下篇帖子: nginx 解决跨域
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表