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

[经验分享] Howto setup High-Available HAProxy with Keepalived

[复制链接]
发表于 2018-12-29 13:22:32 | 显示全部楼层 |阅读模式
  http://blog.laimbock.com/2014/10/01/howto-setup-high-available-haproxy-with-keepalived/
  Posted on October 1, 2014 by Patrick
Summary
  This blog post explains how to setup HAProxy and Keepalived for use with for example the MariaDB Galera cluster setup described here.
  The combination of HAProxy and Keepalived will make the MariaDB Galera cluster more resilient and high-available by adding load balancing and transparent failover when a MariaDB cluster node goes down. HAProxy also allows you to separate read and write actions to the MariaDB Galera cluster which is very useful if you want to loadbalance read actions across all MariaDB nodes but want to write to only one MariaDB node.
  Since only one of anything is a Single Point of Failure (SPoF) I’ll be using two HAProxy nodes in a high-availability setup. Should the primary HAProxy node fail then Keepalived will initiate an automatic andtransparent failover to the secondary HAProxy node.
Requirements
  This tutorial requires the MariaDB Galera cluster setup mentioned above. You will also need 2 nodes (VMs or bare metal) called haproxy1 and haproxy2 each with one IP address. In addition you will also need a third IP address that will be used as the floating Virtual IP (VIP) address between the two HAProxy nodes. The HAProxy nodes don’t need a lot of power, memory or storage. One CPU with 512MB to 1GB memory and an8GB disk are fine.
  Let’s get started.
Install HAProxy and Keepalived on both HAProxy nodes
  On haproxy1 install HAProxy and Keepalived.

  [root@haproxy1 ~]# yum -y install haproxy keepalivedLoaded plugins: fastestmirror, prestoLoading mirror speeds from cached hostfileSetting up Install ProcessResolving Dependencies--> Running transaction check---> Package haproxy.x86_64 0:1.4.24-2.el6 will be installed---> Package keepalived.x86_64 0:1.2.7-3.el6 will be installed--> Finished Dependency ResolutionDependencies Resolved================================================================================Package           Arch          Version                Repository        >  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  [root@haproxy1 ~]# yum -y install haproxy keepalived
  Loaded plugins: fastestmirror, presto
  Loading mirror speeds from cached hostfile
  Setting up Install Process
  Resolving Dependencies
  --> Running transaction check
  ---> Package haproxy.x86_64 0:1.4.24-2.el6 will be installed
  ---> Package keepalived.x86_64 0:1.2.7-3.el6 will be installed
  --> Finished Dependency Resolution
  Dependencies Resolved
  ================================================================================

  Package           Arch          Version                Repository        >  ================================================================================
  Installing:
  haproxy           x86_64        1.4.24-2.el6           base-local        457 k
  keepalived        x86_64        1.2.7-3.el6            base-local        174 k
  Transaction Summary
  ================================================================================
  Install       2 Package(s)

  Total download>
  Installed>  Downloading Packages:
  Setting up and reading Presto delta metadata
  Processing delta metadata
  Package(s) data still to download: 632 k
  (1/2): haproxy-1.4.24-2.el6.x86_64.rpm                   | 457 kB     00:00
  (2/2): keepalived-1.2.7-3.el6.x86_64.rpm                 | 174 kB     00:00
  --------------------------------------------------------------------------------
  Total                                           5.6 MB/s | 632 kB     00:00
  Running rpm_check_debug
  Running Transaction Test
  Transaction Test Succeeded
  Running Transaction
  Installing : keepalived-1.2.7-3.el6.x86_64                                1/2
  Installing : haproxy-1.4.24-2.el6.x86_64                                  2/2
  Verifying  : haproxy-1.4.24-2.el6.x86_64                                  1/2
  Verifying  : keepalived-1.2.7-3.el6.x86_64                                2/2
  Installed:
  haproxy.x86_64 0:1.4.24-2.el6         keepalived.x86_64 0:1.2.7-3.el6
  Complete!
  Now make sure both services start at boot:
  [root@haproxy1 ~]# chkconfig haproxy on[root@haproxy1 ~]# chkconfig keepalived on
  1
  2
  [root@haproxy1 ~]# chkconfig haproxy on
  [root@haproxy1 ~]# chkconfig keepalived on
  Check if both services are properly activated:
  [root@haproxy1 ~]# chkconfig | egrep 'haproxy|keepalived'haproxy        0:off1:off2:on3:on4:on5:on6:offkeepalived     0:off1:off2:on3:on4:on5:on6:off
  1
  2
  3
  [root@haproxy1 ~]# chkconfig | egrep 'haproxy|keepalived'
  haproxy        0:off1:off2:on3:on4:on5:on6:off
  keepalived     0:off1:off2:on3:on4:on5:on6:off
  That looks good so on to the next node.
  Do the same on haproxy2:

  [root@haproxy2 ~]# yum -y install haproxy keepalivedLoaded plugins: fastestmirror, prestoLoading mirror speeds from cached hostfileSetting up Install ProcessResolving Dependencies--> Running transaction check---> Package haproxy.x86_64 0:1.4.24-2.el6 will be installed---> Package keepalived.x86_64 0:1.2.7-3.el6 will be installed--> Finished Dependency ResolutionDependencies Resolved================================================================================Package           Arch          Version                Repository        >  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  [root@haproxy2 ~]# yum -y install haproxy keepalived
  Loaded plugins: fastestmirror, presto
  Loading mirror speeds from cached hostfile
  Setting up Install Process
  Resolving Dependencies
  --> Running transaction check
  ---> Package haproxy.x86_64 0:1.4.24-2.el6 will be installed
  ---> Package keepalived.x86_64 0:1.2.7-3.el6 will be installed
  --> Finished Dependency Resolution
  Dependencies Resolved
  ================================================================================

  Package           Arch          Version                Repository        >  ================================================================================
  Installing:
  haproxy           x86_64        1.4.24-2.el6           base-local        457 k
  keepalived        x86_64        1.2.7-3.el6            base-local        174 k
  Transaction Summary
  ================================================================================
  Install       2 Package(s)

  Total download>
  Installed>  Downloading Packages:
  Setting up and reading Presto delta metadata
  Processing delta metadata
  Package(s) data still to download: 632 k
  (1/2): haproxy-1.4.24-2.el6.x86_64.rpm                   | 457 kB     00:00
  (2/2): keepalived-1.2.7-3.el6.x86_64.rpm                 | 174 kB     00:00
  --------------------------------------------------------------------------------
  Total                                           5.6 MB/s | 632 kB     00:00
  Running rpm_check_debug
  Running Transaction Test
  Transaction Test Succeeded
  Running Transaction
  Installing : keepalived-1.2.7-3.el6.x86_64                                1/2
  Installing : haproxy-1.4.24-2.el6.x86_64                                  2/2
  Verifying  : haproxy-1.4.24-2.el6.x86_64                                  1/2
  Verifying  : keepalived-1.2.7-3.el6.x86_64                                2/2
  Installed:
  haproxy.x86_64 0:1.4.24-2.el6         keepalived.x86_64 0:1.2.7-3.el6
  Complete!
  Now make sure both services start at boot:
  [root@haproxy2 ~]# chkconfig haproxy on[root@haproxy2 ~]# chkconfig keepalived on
  1
  2
  [root@haproxy2 ~]# chkconfig haproxy on
  [root@haproxy2 ~]# chkconfig keepalived on
  Check if both services are properly activated:
  [root@haproxy2 ~]# chkconfig | egrep 'haproxy|keepalived'haproxy        0:off1:off2:on3:on4:on5:on6:offkeepalived     0:off1:off2:on3:on4:on5:on6:off
  1
  2
  3
  [root@haproxy2 ~]# chkconfig | egrep 'haproxy|keepalived'
  haproxy        0:off1:off2:on3:on4:on5:on6:off
  keepalived     0:off1:off2:on3:on4:on5:on6:off
  That looks good so on to the next step.
Allow non-local Virtual IPs on all HAProxy nodes
  Make binding to non-local Virtual IPs on all nodes:
  [root@haproxy1 ~]# vi /etc/sysctl.confnet.ipv4.ip_nonlocal_bind = 1[root@haproxy1 ~]# sysctl -p
  1
  2
  3
  4
  [root@haproxy1 ~]# vi /etc/sysctl.conf
  net.ipv4.ip_nonlocal_bind = 1
  [root@haproxy1 ~]# sysctl -p
  [root@haproxy2 ~]# vi /etc/sysctl.confnet.ipv4.ip_nonlocal_bind = 1[root@haproxy2 ~]# sysctl -p
  1
  2
  3
  4
  [root@haproxy2 ~]# vi /etc/sysctl.conf
  net.ipv4.ip_nonlocal_bind = 1
  [root@haproxy2 ~]# sysctl -p
Add HAProxy user to the MariaDB database
  Make sure a HAProxy user exists on all MAriaDB nodes so HAProxy can access all the MariaDB nodes to check if they are still up. I’m using the user ‘haproxy’ and use the IPs of all the ethernet interfaces on thetwo HAProxy nodes (not the VIP address):
  [patrick@db1 ~]$ mysql -u root -pEnter password:MariaDB [(none)]> CREATE USER 'haproxy'@'10.0.0.15';MariaDB [(none)]> CREATE USER 'haproxy'@'10.0.0.17';MariaDB [(none)]> FLUSH PRIVILEGES;MariaDB [(none)]> exit
  1
  2
  3
  4
  5
  6
  [patrick@db1 ~]$ mysql -u root -p
  Enter password:
  MariaDB [(none)]> CREATE USER 'haproxy'@'10.0.0.15';
  MariaDB [(none)]> CREATE USER 'haproxy'@'10.0.0.17';
  MariaDB [(none)]> FLUSH PRIVILEGES;
  MariaDB [(none)]> exit
  Since this information is automatically replicated to db2 and db3 we only
  have to do this on db1. You can check if the information was replicated
  with:
  [patrick@db2 ~]$ mysql -u root -p -e "select * from mysql.user" | grep haproxy | cut -d$'\t' -f1,2Enter password: 10.0.0.15haproxy10.0.0.17haproxy
  1
  2
  3
  4
  [patrick@db2 ~]$ mysql -u root -p -e "select * from mysql.user" | grep haproxy | cut -d$'\t' -f1,2
  Enter password:
  10.0.0.15haproxy
  10.0.0.17haproxy
  That looks good. On to the next step.
Setup rsyslog logging for HAProxy
  By default HAProxy does not do any logging which obviously needs fixing. To enable HAProxy to log to rsyslog we need to add a rsyslog config file on both HAProxy nodes.
  On node haproxy1:
  [root@haproxy1 ~]# cat >> /etc/rsyslog.d/haproxy.conf > /etc/rsyslog.d/haproxy.conf > /etc/rsyslog.d/haproxy.conf > /etc/rsyslog.d/haproxy.conf > /etc/haproxy/haproxy.cfg > /etc/haproxy/haproxy.cfg > /etc/haproxy/haproxy.cfg > /etc/haproxy/haproxy.cfg Add Keepalived config
  On node haproxy1 first save the default Keepalived configuration:
  [root@haproxy1 ~]# cd /etc/keepalived/[root@haproxy1 keepalived]# mv keepalived.cfg keepalived.cfg.orig
  1
  2
  [root@haproxy1 ~]# cd /etc/keepalived/
  [root@haproxy1 keepalived]# mv keepalived.cfg keepalived.cfg.orig
  On node haproxy1 create the Keepalived configuration file:
  [root@haproxy1 ~]# cat >> /etc/keepalived/keepalived.cfg > /etc/keepalived/keepalived.cfg > /etc/keepalived/keepalived.cfg > /etc/keepalived/keepalived.cfg

运维网声明 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-657279-1-1.html 上篇帖子: Keepalived+Master-Master-Slave组合 下篇帖子: keepalived + lvs + nginx 双主配置案例 (2)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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