Use Haproxy Keepalived for dual-active load balancing installation and configuration

Use Haproxy Keepalived for dual-active load balancing installation and configuration

Saved it!

Environment Preparation

1. Start four virtual machines (centos is used as an example here), the ip is:

192.168.130

192.168.132

192.168.128

192.168.129

2. Install haproxy

Open the external network access rights for the corresponding ports 130 and 132 of these two machines

1 2 3 4 5 /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 8081 -j ACCEPT /etc/rc .d /init .d /iptables restart 执行安装 /etc/rc .d /init .d /iptables save /etc/rc .d /init .d /iptables restart

Install haproxy on machines 130 and 132 respectively:

1 yum -y install haproxy

After both machines are installed, perform file configuration and modify the configuration file on 132:

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 #vi /etc/haproxy/haproxy.cfg global    log 127.0.0.1 local2    chroot /var/lib/haproxy    pidfile /var/run/haproxy .pid    maxconn 4000    user haproxy    group haproxy    daemon    stats socket /var/lib/haproxy/stats 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    stats refresh 5s    stats uri /haproxy    stats realm baison- test -Haproxy    stats auth admin:admin123    stats hide-version    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    stats refresh 5s    stats uri /haproxy #haproxy監控界面    stats realm baison- test -Haproxy    stats auth admin:admin123    stats hide-version frontend main *:80 #監聽80端口    acl url_static path_beg -i /static /images /javascript /stylesheets    acl url_static path_end -i .jpg .gif .png .css .js    use_backend static if url_static #匹配url_static條件的url使用static轉發    default_backend web #其他的url使用后面定義的web轉發backend static #static轉發服務    balance roundrobin    server static1 192.168.230.128:8080 check inter 2000 fall 3 weight 30 #128這臺機器上運行8080端口的tomcat,用于提供靜態文件訪問    server static2 192.168.230.129:8080 check inter 2000 fall 3 weight 30 #129這臺機器上運行8080端口的tomcat,用于提供靜態文件訪問backend web #定義名為”web“的轉發服務    balance roundrobin    server web1 192.168.230.128:8081 check inter 2000 fall 3 #128這臺機器上運行8081端口的tomcat,用于提供非靜態資源訪問    server web2 192.168.230.129:8081 check inter 2000 fall 3 #129這臺機器上運行8081端口的tomcat,用于提供非靜態資源訪問

Copy the configuration file to the same directory on the 130 server and perform the following operations on the 130 server:

1 2 rm /etc/haproxy/haproxy .cfg //刪除130自己的配置文件scp [email protected]: /etc/haproxy/haproxy .cfg /etc/haproxy/

The haproxy installation and configuration of the two servers are complete.

3. Install keepalived on 132 and 130 respectively:

First check to install openssl-devel

1 yum install openssl-devel

Create a keepalived installation directory and download and install it:

1 2 3 4 5 6 7 mkdir /usr/local/ha install cd . /usr/local/ha /configure tar .gz三w.keepalived.org wget http: // tar .gz /software/keepalived-1 zxvf keepalived-1.2.23. .2.23. tar keepalived-1.2.23 cd & make make

After both machines are installed, configure keepalived (132 machines):

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 #mkdir /etc/keepalived/ #cp /usr/local/ha/keepalived-1.2.23/doc/samples/keepalived.conf.sample /etc/keepalived/keepalived.conf #vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs {  router_id Node_132 ###名稱隨便取} vrrp_script chk_http_port {  script "/opt/check_haproxy.sh" ###檢測腳本防止不自動切換的問題,接下來會創建  interval 2  weight 2 } vrrp_instance VI_1 {    state MASTER ###主服務,表示訪問下面虛擬ip 200時,映射到當前機器    interface eth1 ###你的網卡名稱,使用ip a命令查看,并修改    virtual_router_id 50 ###路由id,主從保持一致    priority 100 ###權重,比從服務器大    advert_int 1    track_script {        chk_http_port ### 執行監控的服務    }    virtual_ipaddress {        192.168.230.200 ###虛擬ip地址    } } vrrp_instance VI_2 {    state BACKUP ###從服務,當另一臺機器不工作時,訪問下面虛擬ip 201就映射到當前臺機器    interface eth1    virtual_router_id 52 ###路由id,主從保持一致    priority 99 權重,比主服務器小    advert_int 1    virtual_ipaddress {       192.168.230.201 ###虛擬ip地址    } }

Create a detection file

1 2 3 4 5 6 7 8 #vi /opt/check_haproxy.sh f [ $( ps -C haproxy --no-header | wc -l) - eq 0 ]; then     /etc/init .d /haproxy start fi sleep 2 if [ $( ps -C haproxy --no-header | wc -l) - eq 0 ]; then       /etc/init .d stop /keepalived fi

Copy the configuration and detection files to another server and perform the following operations on 130:

1 2 3 mkdir /etc/keepalived/ scp [email protected]: /etc/keepalived/keepalived .conf /etc/keepalived/ scp [email protected]: /opt/check_haproxy .sh /opt/check_haproxy .sh

Here keepalived is installed.

4. Install the tomcat service environment

Copy two tomcats in 128, one with port 8080 and one with port 8081:

a. Modify the tomcat port, which is too low. Execute the operation.

b. Modify the tomcat page in tomcat port 8080 to facilitate testing and verification:

1 vi webapps /ROOT/index .jsp
  • Import js in head: <script type="text/javascript" src="test.js"></script>
  • Add server description html under body: <h1>web-128:8080</h1><h1 id="js_h">js location</h1> (indicates that this is the service page of port 8080 of host 128)
  • Create a new test.js in the same directory as index.jsp: window.onload = function(){document.getElementById("js_h").innerHTML='js-128:8080′;}

The js file will modify the "js location" content, indicating that this js file comes from the 8080 port service of the 128 host.

c. Make similar changes in tomcat port 8081:

  • Import js in head: <script type="text/javascript" src="test.js"></script>
  • Add server description html under body: <h1>web-128:8081</h1><h1 id="js_h">js location</h1>
  • test.js: window.onload = function() {document.getElementById("js_h").innerHTML='js-128:8081';}

Similarly, copy two tomcats on the 129 server, one with port 8080 and the other with port 8081. When modifying the tomcat file, just change ip 128 to 129.

Now the environment is installed.

Run 4 tomcats

Run haproxy: service haproxy start

Run keepalived:

1 keepalived -D -f /etc/keepalived/keepalived .conf

Open the page to view the haproxy monitoring page: http://192.168.230.132/haproxy, http://192.168.230.130/haproxy

Through monitoring, you can see the number of visits and other messages

Refresh the page multiple times by visiting http://192.168.230.200 or http://192.168.230.201:

By observing the changes in the source of the js file and the page, we can see that the js file comes from the server with port 8080 of 128 and 129, and the page comes from the server with port 8081 of 128 and 129. Describes the different haproxy service rules used for page and static file loads

Visit http://192.168.230.200, http://192.168.230.201. By monitoring the data changes on the page, we can find that 200 is forwarded by the 132 server, and access 201 is forwarded by 130

In actual scenarios, when accessing dynamic resources, the 200 address is used, and when accessing static resources, the 201 address is used. By shutting down tomcat or haproxy, you can see the server switching. This achieves dual-master backup, ensuring high availability while also improving server utilization.

Full text from: http://www.javaseo.cn/article/63/

<<:  Netcup: Detailed tips for passing identity verification [Recently, identity verification has been relaxed, it is recommended to do it]

>>:  Java Photo Station Beauty Installation Tutorial

Recommend

$1.69/month/20G space/200G traffic virtual host——ServNode

ServNode is a newly established hosting provider....

PieLayer: $36/year/512MB memory/400GB space/2TB traffic/KVM/Los Angeles/Germany

PieLayer, old and stable. Here are some good pack...

ComputeLabs: £13/month/2GB memory/50GB space/unlimited traffic/KVM/UK

ComputeLabs is a newly established British busine...

AlphaRacks 1GB RAM Los Angeles OpenVZ VPS Review

Details: AlphaRacks: $7/year/1GB memory/20GB spac...

Chicago VPS: $28/month/8GB RAM/255GB HDD/5TB traffic/5 IP/Chicago

ChicagoVPS, dedicated servers have a 30% discount...

HawkHost US Hosting Coupon Code

25% off resale hosting Reseller Hosting / r25perc...

HostPapa-Chinese Introduction to American Hosting Providers

Hostpapa is a multinational hosting company locat...

PieLayer: VPS starting at $10, with locations in Germany, France, etc.

PieLayer, old and stable. The latest offers are a...

HostFav: $12/year/1GB memory/15GB space/1TB traffic/KVM/Fremont

HostFav, a newly established hosting provider, pr...