Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Installation of haproxy

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Basic installation

Yum install pcre pcre-devel openssl openssl-develtar xf haproxy-1.7.1.tar.gzcd haproxy-1.7.1make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxycd examples/cp haproxy.init / etc/init.d/haproxy chmod + x / etc/init.d/haproxy

two。 Native IP:192.168.56.15

[root@bogon examples] # cat / etc/haproxy/haproxy.cfg globalmaxconn 100000chroot / usr/local/haproxyuid 99gid 99daemonnbproc 1pidfile / usr/local/haproxy/logs/haproxy.pidlog 127.0.0.1 local0 infodefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 300000mstimeout client 300000mstimeout server 300000mslisten stats mode http bind 0.0.0.0:9999 stats enable log global stats uri / haproxy-status stats auth haadmin:123456listen web_port bind 0.0.0.0:80 mode tcp log global [root@bogon examples] # [root @ bogon examples] # / etc/init.d/haproxy restart Restarting haproxy (via systemctl): [OK] [root@bogon examples] #

Http://192.168.56.15:9999/haproxy-status

3. To the backend agent

[root@bogon examples] # cat / etc/haproxy/haproxy.cfg globalmaxconn 100000chroot / usr/local/haproxyuid 99gid 99daemonnbproc 1pidfile / usr/local/haproxy/logs/haproxy.pidlog 127.0.0.1 local0 infodefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 300000mstimeout client 300000mstimeout server 300000mslisten stats mode http bind 0.0.0.0 usr/local/haproxy/logs/haproxy.pidlog 9999 stats enable log global stats uri / haproxy-status stats auth haadmin:123456listen web_port bind 0.0.0.0 etc/haproxy/haproxy.cfg globalmaxconn 100000chroot 80 mode tcp # tcp. 168.56.12 check inter 80 check inter 2000 fall 15 rise 10 server web2 192.168.56.16 rise 80 check inter 2000 fall 15 rise 10 [root@bogon examples] # / etc/init.d/haproxy restart # inter is the interval between monitoring and checking That is, check every 2 seconds. Rise removes the server from the load after 10 consecutive failed checks, and fall is re-added to the load after 15 consecutive monitoring checks. Generally, fall is greater than rise several times.

[root@bogon html] # curl 192.168.56.15192.168.56.12 web1 [root@bogon html] # curl 192.168.56.15192.168.56.16 web2 [root@bogon html] # curl 192.168.56.15192.168.56.12 web1 [root@bogon html] # curl 192.168.56.15192.168.56.16 web2 [root@bogon html] # curl 192.168.56.15192.168.56.12 web1 [root@bogon html] # curl 192. 168.56.15192.168.56.16 web2 [root@bogon html] # curl 192.168.56.15192.168.56.12 web1 [root@bogon html] # curl 192.168.56.15192.168.56.16 web2 [root@bogon html] # curl 192.168.56.15192.168.56.12 web1 [root@bogon html] # curl 192.168.56.15192.168.56.16 web2 [root@bogon html] #

Check the load status

# # #

Haproxy scheduling algorithm

There are eight algorithms for HAProxy: roundrobin, simple polling, static-rr, recommended concern based on weight; leastconn, recommended concern based on least connectors; source, recommended concern based on request source IP; uri, based on request URI;url_param, URl parameter 'balanceurl_param' requires an URL parameter namehdr (name), locking each HTTP request based on HTTP request header Rdp-cookie (name), which means that each TCP request is locked and hashed according to cookie (name).

Haproxy removes the node's yum installation

Globalmaxconn 100000chroot / var/lib/haproxyuser haproxygroup haproxydaemonnbproc 1 pidfile / usr/local/haproxy/logs/haproxy.pid stats socket / var/lib/haproxy.sock mode 600 level adminlog 127.0.0.1 local10 infodefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 5000mstimeout client 50000mstimeout server 50000mslisten statsmode httpbind 0.0.0.0:8888stats enablestats uri / haproxy-status stats auth haproxy:saltstackfrontend frontend_www_example_combind 192.168.56.21:80mode httpoption httploglog global default_backend backend_www_example_combackend backend_www_example _ comoption forwardfor header X-REAL-IPoption httpchk HEAD / HTTP/1.0balance roundrobin server web-node1 192.168.56.11 check inter 2000 rise 30 fall 15server web-node2 192.168.56.12 check inter 2000 rise 30 fall 15 [root@web-node1] #

In this way, the nodes of haproxy can be deleted or added.

Echo "disable server backend_www_example_com/web-node1" | socat / var/lib/haproxy.sock stdioecho "enable server backend_www_example_com/web-node1" | socat / var/lib/haproxy.sock stdio

Haproxy adds log function

Mkdir / var/log/haproxychmod aqiw / var/log/haproxy/etc/rsyslog.conf Open option: $ModLoad imudp$UDPServerRun 51 Save haproxy loglocal0.* / var/log/haproxy/haproxy.logvim / etc/sysconfig/rsyslog [root@web-node1 nginx] # cat / etc/sysconfig/rsyslog # Options for rsyslogd# Syslogd options are deprecated since rsyslog v3.# If you want to use them Switch to compatibility mode 2 by "- c 2" See rsyslogd (8) for more detailsSYSLOGD_OPTIONS= "- r-m 0-c 2" # modify here [root@web-node1 nginx] #

The localhost in the haproxy log is related to the configuration file. This IP can be modified.

Log 127.0.0.1 local10 info

3. Cluster configuration

192.168.56.11 nginx reverse proxy keepalived haproxy

192.168.56.12 nginx reverse proxy keepalived haproxy

192.168.56.13 nginx simulates tomcat

192.168.56.14 nginx simulates tomcat

The VIP of keepalived is 192.168.56.21

Problem: haproxy does not bind VIP that does not exist, so it cannot start so, so you can bind VIP that does not exist.

Echo 1 > / proc/sys/net/ipv4/ip_nonlocal_bind

Vi / etc/sysctl.conf sysctl-p

Net.ipv4.ip_nonlocal_bind = 1

Enable IP that allows you to bind non-native computers

View the configuration file 192.168.56.11 of keepalived (master)

[root@web-node1 nginx] # cat / etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {notification_email {saltstack@example.com} notification_email_from keepalived@example.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id haproxy_ha} vrrp_instance haproxy_ha {state MASTERinterface eth0 virtual_router_id 36priority 150 advert_int 1authentication {auth_type PASS auth_pass 1111} virtual_ipaddress {192.168.56.21}} [root@web-node1 nginx] #

View the configuration file 192.168.56.12 of keepalived (standby)

[root@web-node2 ~] # cat / etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {notification_email {saltstack@example.com} notification_email_from keepalived@example.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id haproxy_ha} vrrp_instance haproxy_ha {state BACKUPinterface eth0 virtual_router_id 36priority 100 advert_int 1authentication {auth_type PASS auth_pass 1111} virtual_ipaddress {192.168.56.21}} [root@web-node2 ~] #

View the configuration file for 192.168.56.11 haproxy

[root@web-node2 ~] # cat / etc/haproxy/haproxy.cfgglobalmaxconn 100000chroot / var/lib/haproxyuser haproxygroup haproxydaemonnbproc 1 pidfile / usr/local/haproxy/logs/haproxy.pid stats socket / var/lib/haproxy.sock mode 600 level adminlog 127.0.0.1 local3 infodefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 5000mstimeout client 50000mstimeout server 50000mslisten statsmode httpbind 0.0.0.0:8888stats enablestats uri / haproxy-status stats auth haproxy:saltstackfrontend frontend_www_example_combind 192.168.56.21:80mode tcpoption httploglog Global default_backend backend_www_example_combackend backend_www_example_comoption forwardfor header X-REAL-IPoption httpchk HEAD / HTTP/1.0balance roundrobin server web-node1 192.168.56.11 check inter 2000 rise 30 fall 15server web-node2 192.168.56.12 check inter 2000 rise 30 fall 15 [root@web-node2] #

View the configuration file for 192.168.56.12 haproxy

[root@web-node1 nginx] # cat / etc/haproxy/haproxy.cfg globalmaxconn 100000chroot / var/lib/haproxyuser haproxygroup haproxydaemonnbproc 1 pidfile / usr/local/haproxy/logs/haproxy.pid stats socket / var/lib/haproxy.sock mode 600 level admin#log 127.0.0.1 local3 infolog 192.168.56.21 local0 debugdefaultsoption http-keep-alivemaxconn 100000mode httptimeout connect 5000mstimeout client 50000mstimeout server 50000mslisten statsmode httpbind 0.0.0.0:8888stats enablestats uri / haproxy-status stats auth haproxy:saltstackfrontend frontend_www_example_ Combind 192.168.56.21:80mode httpoption httploglog global default_backend backend_www_example_combackend backend_www_example_comoption forwardfor header X-REAL-IPoption httpchk HEAD / HTTP/1.0balance roundrobin server web-node1 192.168.56.11:8080 check inter 2000 rise 30 fall 15server web-node2 192.168.56.12:8080 check inter 2000 rise 30 fall 15 [root@web-node1 nginx] #

View the configuration file of the nginx reverse proxy for 192.168.56.11 (the simplest configuration demo)

[root@web-node1 nginx] # cat / etc/nginx/nginx.confworker_processes 1: http {worker_connections 1024;} http {include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_pools {server 192.168.56.13 etc/nginx/nginx.confworker_processes 8080 weight=1; server 192.168.56.14 } server {listen 192.168.56.11 server_name blog.liuhaixiao.com; location 8080; server_name blog.liuhaixiao.com; location / {proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr;}} [root@web-node1 nginx] #

View the configuration file of the nginx reverse proxy for 192.168.56.12 (the simplest configuration demo)

[root@web-node2 ~] # cat / etc/nginx/nginx.confworker_processes 1: http {worker_connections 1024;} http {include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_pools {server 192.168.56.13 etc/nginx/nginx.confworker_processes 8080 weight=1; server 192.168.56.14 weight=1 } server {listen 192.168.56.12 server_name blog.liuhaixiao.com; location 8080; server_name blog.liuhaixiao.com; location / {proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr } [root@web-node2 ~] # View nginx web container of 192.168.56.13 [root@web-node3 html] # cat / usr/share/nginx/html/index.html node-2 View nginx web container of 192.168.56.14 [root@web-node4 html] # cat / usr/share/nginx/html/index.html node-1

Check VIP zai 192.168.56.11

[root@web-node1 nginx] # ip addr1: lo: mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6:: 1/128 scope host valid_lft forever preferred_lft forever2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:10: 66:98 brd ff:ff:ff:ff:ff:ff inet 192.168.56.11/24 brd 192.168.56.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.56.21/32 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe10:6698/64 scope link valid_lft forever preferred_lft forever [root@web-node1 nginx] #

Now parse the local computer blog.liuhaixiao.com to 192.168.56.21

# # #

New addition: 192.168.56.15 add a nginx domain name

The port that haproxy listens on port 80 nginx listens on must not be the same as the backend tomcat, otherwise it cannot be returned.

[root@web-node1 ~] # cat / etc/nginx/nginx.confworker_processes 1: http {worker_connections 1024;} http {include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_pools {server 192.168.56.13 etc/nginx/nginx.confworker_processes 8080 weight=1; server 192.168.56.14 weight=1 } upstream server_tt {server 192.168.56.15 server 8080 weight=1;} server {listen 81; server_name blog.liuhaixiao.com; location / {proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr }} server {listen 81; server_name blog.sanlang.com; location / {proxy_pass http://server_tt; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr;}} [root@web-node1 ~] #

3 is the 192.168.56.15 tomcat/nginx page of another server

For more information on haproxy parameters, please see:

Http://www.cnblogs.com/zhang789/p/6057402.html

Haproxy: multiple domain name referenc

Haproxy configuration based on domain name load balancing

Http://blog.csdn.net/youyudehexie/article/details/7606504

#

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report