`
sillycat
  • 浏览: 2543393 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

HAProxy and HA Solution(1)Setup KeepAlived on Ubuntu

 
阅读更多
HAProxy and HA Solution(1)Setup KeepAlived on Ubuntu

Find the right Version for KeepAlived from here
https://www.keepalived.org/download.html

> wget https://www.keepalived.org/software/keepalived-2.0.17.tar.gz

Unzip and build/install
> tar zxvf keepalived-2.0.17.tar.gz
> ./configure --prefix=/home/carl/tool/keepalived-2.0.17

I get console message about this
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

I do not think I need IPv6
> make
> sudo make install

Copy and prepare related files
> sudo ln -s /home/carl/tool/keepalived-2.0.17 /opt/keepalived-2.0.17
> sudo ln -s /opt/keepalived-2.0.17 /opt/keepalived

It seems not that easy to set up in service init.d
HAProxy author prefer keepalived.
http://www.voidcn.com/article/p-essgxbcx-bka.html

Directly install from ubuntu APT
> sudo apt-get install keepalived
> sudo snap install keepalived --classic

Check Version
> keepalived -version
Keepalived v1.3.9 (10/21,2017)
Copyright(C) 2001-2017 Alexandre Cassen, <acassen@gmail.com>

> keepalived -version
Keepalived v2.0.10 (11/12,2018)

Need to Make sure the 2 machines are having the same version of KeepAlived

Finally, I get the 2 machine with the same version
> keepalived -version
Keepalived v2.0.17 (06/27,2019), git commit v2.0.17-8-g836230e+

> keepalived -version
Keepalived v2.0.17 (06/27,2019), git commit v2.0.17-8-g836230e+

Change the Setting
> sudo vi /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1

Check the Settings
> sudo sysctl -p
vm.max_map_count = 262144
net.ipv4.ip_nonlocal_bind = 1

Prepare the configuration on the master machine
> cat /etc/keepalived/keepalived.conf
global_defs {
  lvs_id haproxy_DH
}
vrrp_script check_haproxy {
  script "killall -0 haproxy"
  interval 2
  weight 2
}
vrrp_instance VI_01 {
  state MASTER
  interface eth0
  virtual_router_id 51
  priority 101
  virtual_ipaddress {
    192.168.56.110
  }
  track_script {
    check_haproxy
  }
}

Create the configuration on the Slave Machine
> cat /etc/keepalived/keepalived.conf
global_defs {
  lvs_id haproxy_DH_passive
}
vrrp_script check_haproxy {
  script "killall -0 haproxy"
  interval 2
  weight 2
}
vrrp_instance VI_01 {
  state SLAVE
  interface eth0
  virtual_router_id 51
  priority 100
  virtual_ipaddress {
    192.168.56.110
  }
  track_script {
    check_haproxy
  }
}

Suppose this command can start the service
> sudo service keepalived start

But I received these information
> sudo service keepalived start
Failed to start keepalived.service: Unit keepalived.service is masked.

> sudo service keepalived status
● keepalived.service
   Loaded: masked (/dev/null; bad)
   Active: inactive (dead)

> sudo service keepalived status
● keepalived.service
   Loaded: masked (Reason: Unit keepalived.service is masked.)
   Active: inactive (dead)

Solution:
https://serverfault.com/questions/678692/unable-to-start-keepalived
http://gayangunarathne.blogspot.com/2015/06/lvs-setup-in-oracle-virtualbox.html

Set Up HAProxy
Find the latest version from here
http://www.haproxy.org/#down

> wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.20.tar.gz
> tar zxvf haproxy-1.8.20.tar.gz

Prepare
> sudo apt-get install libpcre3 libpcre3-dev

> make TARGET=generic ARCH=x86_64 USE_PCRE=1
> make install DESTDIR='/home/carl/tool/haproxy-1.8.20' PREFIX=''


> sudo ln -s /home/carl/tool/haproxy-1.8.20 /opt/haproxy-1.8.20
> sudo ln -s /opt/haproxy-1.8.20 /opt/haproxy

Add sbin to the PATH
export PATH="/opt/haproxy/sbin:$PATH"

Check the installation and version
> haproxy -v
HA-Proxy version 1.8.20 2019/04/25
Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>

Make sure we have the haproxy Settings right
> mkdir conf

Sample configuration is as follow:
> cat conf/haproxy.conf
global
        log 127.0.0.1 local0 info
        maxconn 5120
        chroot /opt/haproxy
        uid 99
        gid 99
        daemon
        quiet
        nbproc  2
        pidfile /opt/haproxy/haproxy.pid
defaults
       log        global
       mode       tcp
       option     tcplog
       option     dontlognull
       retries    3
       option redispatch
       maxconn 2000
       contimeout      5s
       clitimeout      120s
       srvtimeout      120s
      
listen rabbitmq_local_cluster
       bind 0.0.0.0:5670
       mode      tcp
       balance roundrobin
       server rabbit1 ubuntu-master:5672 check inter 5000 rise 2 fall 2
       server rabbit2 ubuntu-dev2:5672 check inter 5000 rise 2 fall 2
listen monitor
        bind 0.0.0.0:8100
        mode http
        option httplog
        stats enable
        stats uri /stats
        stats refresh 5s

Start RabbitMQ Cluster, Start HAProxy on Both Server
> sudo RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit1 sbin/rabbitmq-server -detached

> sudo RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit2 sbin/rabbitmq-server -detached

We can visit the UI to make sure it is running
http://ubuntu-master:15672/#/
http://ubuntu-dev2:15672/#/

Start HAProxy on Both Server
>sudo sbin/haproxy -f conf/haproxy.conf

Visit the UI to verify
http://ubuntu-master:8100/stats
http://ubuntu-dev2:8100/stats

Check the HAProxy
> pidof haproxy
2469 2468

Check IP Address
> ip addr

The latest Configuration, still not working
> cat /etc/keepalived/keepalived.conf
global_defs {
  lvs_id haproxy_DH
}
vrrp_script check_haproxy {
  script "pidof haproxy"
  interval 2
  weight 2
}
vrrp_instance VI_01 {
  state MASTER
  interface enp0s8
  virtual_router_id 51
  priority 101
  unicast_peer {
    192.168.56.3
  }
  virtual_ipaddress {
    192.168.56.110/27 label enp0s8:10
  }
  track_script {
    check_haproxy
  }
}

Try the older version
https://www.jianshu.com/p/dd93bc6d45f5

> wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
> tar zxvf keepalived-1.2.2.tar.gz

Try to compile
> ./configure --prefix=/usr/local/keepalived

Exception:
configure: error: Popt libraries is required

Solution:
> sudo apt-get install libpopt-dev
> ./configure --prefix=/usr/local/keepalived

> make
> make install

Prepare the files
> sudo cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
> sudo cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
> sudo mkdir /etc/sysconfig
> sudo cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
> sudo mkdir -p /etc/keepalived/
> sudo cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

I guess this is too old to use on the latest Ubuntu System
> /etc/init.d/keepalived start
/etc/init.d/keepalived: 12: .: Can't open /etc/rc.d/init.d/functions

Does not work based on these information, plan to try again tomorrow.
https://www.cnblogs.com/xxoome/p/8621677.html
https://blog.csdn.net/bbwangj/article/details/80346428

Checked some docs from systemctl, I think I get it right, I will try again here.
Start RabbitMQ both
> sudo RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit1 sbin/rabbitmq-server -detached

> sudo RABBITMQ_NODE_PORT=5672 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]" RABBITMQ_NODENAME=rabbit2 sbin/rabbitmq-server -detached

Start HAProxy on both machines
> sudo sbin/haproxy -f conf/haproxy.conf

Visit the HAProxy Page
http://ubuntu-master:8100/stats
http://ubuntu-dev2:8100/stats

Finally, with these configuration, I get the keepalived working fine.
> cat /etc/keepalived/keepalived.conf
global_defs {
  lvs_id haproxy_DH
}
vrrp_script check_haproxy {
  script "pidof haproxy"
  interval 2
  weight 2
}
vrrp_instance VI_01 {
  state MASTER
  interface enp0s8
  virtual_router_id 51
  priority 110
  unicast_peer {
    192.168.56.106
  }
  virtual_ipaddress {
    192.168.56.110/27 label enp0s8:10
  }
  track_script {
    check_haproxy
  }
}

> cat /etc/keepalived/keepalived.conf
global_defs {
  lvs_id haproxy_DH_passive
}
vrrp_script check_haproxy {
  script "pidof haproxy"
  interval 2
  weight 2
}
vrrp_instance VI_01 {
  state BACKUP
  interface enp0s8
  virtual_router_id 51
  priority 100
  unicast_peer {
    192.168.56.101
  }
  virtual_ipaddress {
    192.168.56.110/27 label enp0s8:10
  }
  track_script {
    check_haproxy
  }
}


We can see the IP working on the master
enp0s8:10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.110  netmask 255.255.255.224  broadcast 0.0.0.0
        ether 08:00:27:7d:a0:7e  txqueuelen 1000  (Ethernet)

Visit this page, it will give us the status for HAproxy
http://192.168.56.110:8100/stats

Kill the KeepAlived on ubuntu-master

The floating IP will be on the ubuntu-dev2

References:
https://www.lisenet.com/2015/setting-up-a-load-balancing-haproxy-cluster-with-keepalived/
https://www.jianshu.com/p/dd93bc6d45f5
https://docs.oracle.com/cd/E37670_01/E41138/html/section_sm3_svy_4r.html
https://dasunhegoda.com/how-to-setup-haproxy-with-keepalived/833/
https://vexxhost.com/resources/tutorials/highly-available-web-servers-keepalived-floating-ips-ubuntu-16-04/





分享到:
评论

相关推荐

    keepalived+haproxy+mycat配置文件.rar

    在构建高可用性、负载均衡的数据库系统时,`keepalived`、`haproxy` 和 `mycat` 是三个关键组件。本配置文件集合主要用于实现基于 CentOS 7 的多节点环境中,MySQL 双主读写分离以及故障自动切换的解决方案。下面将...

    mysql+mycat+keepalived+haproxy集群(生产环境实测).doc

    1. 安装Keepalived:`yum install -y keepalived` 2. 配置Keepalived:在`/etc/keepalived/keepalived.conf`中定义主、备节点的角色,设置监控的检查脚本和VIP。 3. 启动Keepalived:`systemctl start keepalived` 4...

    Keepalived+HAProxy配置高可用负载均衡,解决keepalived无法安装问题

    ### Keepalived与HAProxy构建高可用负载均衡系统 #### 一、背景介绍 在现代互联网应用中,为了确保服务的稳定性和可用性,通常采用负载均衡技术来分散请求到多台服务器上处理。其中,Keepalived 和 HAProxy 是构建...

    基于docker-compose实现haproxy+keepalived+teleport的负载均衡和高可用1

    主机名IP地址操作系统组件备注环境说明架构图目录结构配置文件docker-compose配置文件keepalived配置文件keepalived检测脚本hapr

    haproxy_keepalived_Downloads.rar

    标题中的"haproxy_keepalived_Downloads.rar"表明这是一个关于HAProxy和Keepalived的下载包,可能包含了这两个组件的安装文件和配置示例。描述中的"zookeeper3.4_haproxy1.7_keepalive 结对好用"暗示了Zookeeper也...

    kubeadm+haproxy+keepalived部署高可用k8s集群-版本k8s1.20.4—详细文档

    kubeadm部署高可用k8s集群,haproxy+keepalived-版本k8s1.20.4,详细笔记总结文档

    Haproxy+keepalived配置文档

    1. **VRRP**:通过VRRP协议,`Keepalived` 可以监控主HAProxy实例的状态,并在主节点故障时无缝地将流量切换到备份节点,实现零中断的故障恢复。 2. **健康检查**:除了Haproxy自身的健康检查,Keepalived也可以独立...

    HaProxy + Keepalived实现高可用

    HaProxy + Keepalived实现高可用负载均衡

    HAProxy+Keepalived+MySQL.pdf

    本文将详细介绍如何在Linux环境中搭建HAProxy+Keepalived+MySQL的高可用架构,以实现MySQL数据库服务的负载均衡和故障切换。 首先,HAProxy是一个高性能的负载均衡器,用于分发网络流量到多个后端服务器,以提高...

    HAProxy+Keepalived+LVS实现负载平衡高可用集群最佳实践

    HAProxy+Keepalived+LVS实现负载平衡高可用集群最佳实践

    haproxy+apache+keepalived.tar.gz

    1. 安装必要的软件包:在所有目标服务器上安装haproxy、Apache和keepalived。 2. 配置haproxy:设置haproxy的配置文件,指定监听端口、后端服务器列表、负载均衡策略等。 3. 配置Apache:调整Apache的配置以接受来自...

    Keepalived+haproxy安装.docx

    Keepalived+Haproxy 安装与配置 Keepalived 和 Haproxy 是两种常用的高可用性解决方案,分别提供负载均衡和健康检测功能。下面是 Keepalived 和 Haproxy 的安装和配置过程。 一、安装环境 本文档使用 CentOS 7.3 ...

    haproxy-keepalived:适用于Docker和kubernetes的HAProxy和Keepalived

    haproxy-keepalived [v1.0.0]重构,使其再次伟大。 并增加对Kubernetes的支持适用于Docker和Kubernetes的带有Keepalived的HAProxy DockerHub: ://hub.docker.com/r/pelin/haproxy-keepalived/ 版本HAProxy 保持活力...

    MyCat高可用负载均衡集群实现(HAProxy+Keepalived+MyCat)

    部署环境规划中,需要设置不同的主机以及对应的IP地址,其中HAProxy主机1和HAProxy主机2用于提供负载均衡和高可用性,MyCat主机1和MyCat主机2则是MyCat服务器的部署节点,MySQL主节点和从节点用于实现数据的同步复制...

    Keepalived+HAProxy实现MySQL高可用负载均衡的配置

    配置Keepalived时,会在`/etc/keepalived/keepalived.conf`文件中定义VRRP实例,如`VI_1`,设置监控的接口、虚拟路由ID、优先级等参数。同时,配置VRRP脚本(如`chk_haproxy`)进行健康检查,并在状态变化时调用相应...

    HAproxy+Keepalived 负载均衡架构搭建

    在构建高可用性服务系统时,HAproxy+Keepalived的组合被广泛应用于负载均衡架构。这个方案确保了服务的连续性和效率,特别是在处理大量网络请求时。在本篇文章中,我们将深入探讨如何在Red Hat Enterprise Linux 5.8...

    keepdalive 配置

    keepalived +haproxy 配置keepalived +haproxy 配置keepalived +haproxy 配置keepalived +haproxy 配置keepalived +haproxy 配置keepalived +haproxy 配置keepalived +haproxy 配置keepalived +haproxy 配置

    实验手册keepalived+haproxy

    ### 实验手册:Keepalived + Haproxy 高可用架构搭建 #### 一、实验背景及概述 在现代互联网架构中,为了提高系统的稳定性和可用性,通常会采用多种技术手段来构建高可用(High Availability,简称HA)的网络环境...

Global site tag (gtag.js) - Google Analytics