{"id":104,"date":"2020-02-10T20:01:26","date_gmt":"2020-02-10T20:01:26","guid":{"rendered":"http:\/\/ictbank.ir\/blog\/?p=104"},"modified":"2022-11-23T07:06:11","modified_gmt":"2022-11-23T07:06:11","slug":"how-to-setup-a-loadbalancer-with-haproxy-in-redhat-7","status":"publish","type":"post","link":"https:\/\/ictbank.ir\/blog\/how-to-setup-a-loadbalancer-with-haproxy-in-redhat-7\/","title":{"rendered":"How to setup a loadbalancer with HAProxy in RedHat 7"},"content":{"rendered":"\n<p>You have many options to install and configure a loadbalancer in RedHat 7. One of the best options is HAPorxy. <\/p>\n\n\n\n<p>To setup HAProxy, you have to download related RPM file and then install it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nrpm -ivh haproxy.rpm\n<\/pre><\/div>\n\n\n<p>Then, you have to make changes in config file (\/etc\/haproxy\/haproxy.conf):<\/p>\n\n\n\n<p>in this sample we have 4 servers as below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>APP1: 192.168.1.2<\/li>\n\n\n\n<li>APP2: 192.168.1.3<\/li>\n\n\n\n<li>APP3: 192.168.1.4<\/li>\n\n\n\n<li>LoadBalancer: 192.168.1.5<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nglobal\n\tlog         127.0.0.1:514 local2 debug\n\tchroot      \/var\/lib\/haproxy\n\tpidfile     \/var\/run\/haproxy.pid\n\tmaxconn     4000\n\tuser        haproxy\n\tgroup       haproxy\n\tdaemon\n\tmaxconn    10000\n\tstats socket \/var\/lib\/haproxy\/stats\n\ttune.ssl.default-dh-param\t2048\n\t\nlisten stats\n\tbind *:4331\n\tmode http\n\tlog global\n\tmaxconn 10\n\ttimeout client 100s\n\ttimeout server 100s\n\ttimeout connect 100s\n\ttimeout queue 100s\n\tstats refresh 30s\n\tstats enable\n\tstats uri \/haproxy?stats\n\tstats realm HAProxy\\ Statistics\n\tstats auth admin:123456\n\tstats show-node\n\n\t\nfrontend public\n\tbind            *:80 name clear\n\tredirect scheme https code 301 if !{ ssl_fc }\n\tbind            *:443 ssl crt \/etc\/pki\/tls\/certs\/app_test.pem\n\tmode            http\n\tlog         127.0.0.1:514 local3 debug\n\tlog-format &quot;%ci:%cp &#x5B;%tr] %ft %b\/%s %TR\/%Tw\/%Tc\/%Tr\/%Ta %ST %B %CC %CS %tsc %ac\/%fc\/%bc\/%sc\/%rc %sq\/%bq %hr %hs %{+Q}r&quot;\n\thttp-request set-log-level debug\n\toption\t\t\ttcplog\n\toption          httplog\n\toption          dontlognull\n\tmonitor-uri     \/monitoruri\n\tmaxconn         8000\n\ttimeout client  30s\n\tdefault_backend app_test\n\nbackend app_test\n\tmode            http\n\tlog         127.0.0.1:514 local4 debug\n\toption\t\t\ttcplog\n\toption          httplog\n\toption          dontlognull\n\tlog-format &quot;%ci:%cp &#x5B;%tr] %ft %b\/%s %TR\/%Tw\/%Tc\/%Tr\/%Ta %ST %B %CC %CS %tsc %ac\/%fc\/%bc\/%sc\/%rc %sq\/%bq %hr %hs %{+Q}r&quot;\n\tmaxconn    10000\n\tbalance roundrobin\n\tcookie SERVERID insert indirect nocache\n\toption prefer-last-server\n\toption\t\t\tforwardfor\n\tretries         2\n\toption redispatch\n\ttimeout connect 50s\n\ttimeout server  50s\n\tserver          s1 192.168.1.2:80   check cookie s1\n\tserver          s2 192.167.1.3:80   check cookie s2\n\tserver          s3 192.167.1.4:80   check cookie s3\n<\/pre><\/div>\n\n\n<p>Then you have to start the haproxy and make it autostart on reboots with these two commands:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsystemctl start haproxy.service\nsystemctl enable haproxy.service\n<\/pre><\/div>\n\n\n<p>you can see the statistics page with this url:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhttp:\/\/192.168.1.5:4331\/haproxy?stats\n<\/pre><\/div>\n\n\n<p>and your application can be accessed via the IP of this server.<\/p>\n\n\n\n<p>Sometimes you encountered the following error:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhaproxy Starting proxy stats: cannot bind socket &#x5B;0.0.0.0:4331]\n<\/pre><\/div>\n\n\n<p>In order to solve this error, you have to run this command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsetsebool -P haproxy_connect_any=1\n<\/pre><\/div>\n\n\n<p>to see the system log:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntail -f \/var\/log\/haproxy.log\n<\/pre><\/div>\n\n\n<p>to see the access log:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntail -f \/var\/log\/haproxy-access-frontend.log\n<\/pre><\/div>\n\n\n<p>As you can see, you have to install rsyslog on your server, to do so, run these commands:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nyum install -y rsyslog\nsystemctl enable rsyslogd\nsystemctl start rsyslog\n<\/pre><\/div>\n\n\n<p>After that, you should config rsyslog to gather HAProxy&#8217;s logs. So you have to edit \/etc\/rsyslog.conf file and add these lines:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlocal2.*  \/var\/log\/haproxy.log\nlocal3.*  \/var\/log\/haproxy-access-frontend.log\nlocal4.*  \/var\/log\/haproxy-access-backend.log\n<\/pre><\/div>\n\n\n<p>after that a new file in \/etc\/rsyslog.d\/ should be created as below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvi \/etc\/rsyslog.d\/haproxy.conf\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;root@s1vlmizbanlvs01 haproxy_reports]# cat \/etc\/rsyslog.d\/haproxy.conf\n# Create an additional socket in haproxy&#039;s chroot in order to allow logging via\n# \/dev\/log to chroot&#039;ed HAProxy processes\n#$AddUnixListenSocket \/var\/lib\/haproxy\/dev\/log\n\n# Send HAProxy messages to a dedicated logfile\n#if $programname startswith &#039;haproxy&#039; then \/var\/log\/haproxy.log\n#&amp;amp;~\n$ModLoad imudp\n$UDPServerAddress 127.0.0.1\n$UDPServerRun 514\nlocal2.* \/var\/log\/haproxy.log\nlocal3.* \/var\/log\/haproxy-access-frontend.log\nlocal4.* \/var\/log\/haproxy-access-backend.log\n\n<\/pre><\/div>\n\n\n<p>then, you have to restart the service:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsystemctl restart rsyslogd\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>You have many options to install and configure a loadbalancer in RedHat 7. One of the best options is HAPorxy. To setup HAProxy, you have to download related RPM file and then install it: Then, you have to make changes in config file (\/etc\/haproxy\/haproxy.conf): in this sample we have 4 servers as below: Then you [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,51,54,50,52],"tags":[40,60,4,58,57,42,56,59],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-centos","category-linxu","category-oraclelinux","category-os","category-redhat","tag-centos","tag-config","tag-linux","tag-loadbalancer","tag-network","tag-oraclelinux","tag-redat","tag-rpm"],"_links":{"self":[{"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":5,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":613,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/posts\/104\/revisions\/613"}],"wp:attachment":[{"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}