{"id":615,"date":"2022-12-03T09:39:19","date_gmt":"2022-12-03T09:39:19","guid":{"rendered":"http:\/\/ictbank.ir\/blog\/?p=615"},"modified":"2023-05-01T16:01:53","modified_gmt":"2023-05-01T16:01:53","slug":"setup-high-availability-for-haproxy","status":"publish","type":"post","link":"https:\/\/ictbank.ir\/blog\/setup-high-availability-for-haproxy\/","title":{"rendered":"Setup High Availability for HAProxy"},"content":{"rendered":"\n<p>To prevent service failure during disasters like server crash or so on, we have to find Single Point of Failure components and setup High Availability solutions for them.<\/p>\n\n\n\n<p>In this article I am going to define how to setup High Availability for HAProxy service. The structure of service would be something like below:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"682\" height=\"407\" src=\"http:\/\/ictbank.ir\/blog\/wp-content\/uploads\/2022\/12\/HAProxy-High-Availability.png\" alt=\"\" class=\"wp-image-621\" srcset=\"https:\/\/ictbank.ir\/blog\/wp-content\/uploads\/2022\/12\/HAProxy-High-Availability.png 682w, https:\/\/ictbank.ir\/blog\/wp-content\/uploads\/2022\/12\/HAProxy-High-Availability-600x358.png 600w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><figcaption class=\"wp-element-caption\">HAProxy structure map<\/figcaption><\/figure>\n\n\n\n<p>In this scenario, there is a Virtual IP which is 192.168.1.205, and can be used for both HAProxy servers. This VIP points to the master server (192.168.1.201) in normal conditions, and if something bad happens to this server, the VIP will point to the other server (192.168.1.200).<\/p>\n\n\n\n<p>First of all you have to install HAProxy on both servers using this <a href=\"http:\/\/ictbank.ir\/blog\/how-to-setup-a-loadbalancer-with-haproxy-in-redhat-7\/\" data-type=\"post\" data-id=\"104\">link<\/a>.<\/p>\n\n\n\n<p>After that all you need is installing and configurating keepalived service on your OS (here we use CentOS 7):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nyum install keepalived -y\n<\/pre><\/div>\n\n\n<p>Then you should change the service&#8217;s configurations:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvi \/etc\/keepalived\/keepalived.conf\n<\/pre><\/div>\n\n\n<p>And delete all it&#8217;s content and replace them with the following. For config file of Master server:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvrrp_script chk_haproxy {\n    script &quot;\/usr\/sbin\/pidof haproxy&quot;\n    interval 1\n}\n\nvrrp_instance VI_1 {\n    interface ens160\n    state MASTER\n    priority 200\n\n    virtual_router_id 33\n    unicast_src_ip 192.168.1.201\n    unicast_peer {\n        192.168.1.200\n    }\n\n    authentication {\n        auth_type PASS\n        auth_pass 1234567890\n    }\n\n        virtual_ipaddress {\n        192.168.1.205\n    }\n\n    track_script {\n        chk_haproxy\n    }\n\n    notify_master \/etc\/keepalived\/master.sh\n}\n<\/pre><\/div>\n\n\n<p>And for config file of Backup server:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nvrrp_script chk_haproxy {\n    script &quot;\/usr\/sbin\/pidof haproxy&quot;\n    interval 1\n}\n\nvrrp_instance VI_1 {\n    interface ens160\n    state BACKUP\n    priority 100\n\n    virtual_router_id 33\n    unicast_src_ip 192.168.1.200\n    unicast_peer {\n        192.168.1.201\n    }\n\n    authentication {\n        auth_type PASS\n        auth_pass 1234567890\n    }\n\n        virtual_ipaddress {\n        192.168.1.205\n    }\n\n    track_script {\n        chk_haproxy\n    }\n\n    notify_master \/etc\/keepalived\/master.sh\n}\n<\/pre><\/div>\n\n\n<p>And finally you have to restart the service and enable it for next startups:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsystemctl enable keepalived\nsystemctl restart keepalived\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>To prevent service failure during disasters like server crash or so on, we have to find Single Point of Failure components and setup High Availability solutions for them. In this article I am going to define how to setup High Availability for HAProxy service. The structure of service would be something like below: In this [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":691,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,1,51,54,50,52],"tags":[],"class_list":["post-615","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-installation","category-linxu","category-oraclelinux","category-os","category-redhat"],"_links":{"self":[{"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/posts\/615","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=615"}],"version-history":[{"count":4,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":622,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/posts\/615\/revisions\/622"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/media\/691"}],"wp:attachment":[{"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ictbank.ir\/blog\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}