{"id":18032,"date":"2023-08-02T13:45:14","date_gmt":"2023-08-02T13:45:14","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=18032"},"modified":"2023-07-23T14:09:21","modified_gmt":"2023-07-23T14:09:21","slug":"how-to-setup-haproxy-for-high-availability-with-keepalived","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-setup-haproxy-for-high-availability-with-keepalived\/","title":{"rendered":"How to Setup HAProxy for High Availability with Keepalived"},"content":{"rendered":"<p><img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-1024x768.jpg\" alt=\"How to Setup HAProxy for High Availability with Keepalived\" width=\"1024\" height=\"768\" class=\"alignnone size-large wp-image-18033 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-1024x768.jpg 1024w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-300x225.jpg 300w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-1536x1152.jpg 1536w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-2048x1536.jpg 2048w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-128x96.jpg 128w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-420x315.jpg 420w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-540x405.jpg 540w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-720x540.jpg 720w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-960x720.jpg 960w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-1140x855.jpg 1140w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-1320x990.jpg 1320w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/How-to-Setup-HAProxy-for-High-Availability-with-Keepalived-1440x1080.jpg 1440w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/768;\" \/><\/p>\n<p>In server administration, ensuring high availability of services is a crucial task. One common challenge that webmasters and server administrators often face is how to effectively balance the load of network traffic and ensure that no single <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-are-web-servers-and-why-are-they-needed\/\">server<\/a> becomes a bottleneck. This is where HAProxy and Keepalived come into play.<\/p>\n<p><a href=\"https:\/\/webhostinggeeks.com\/blog\/haproxy-features-functions-benefits\/\">HAProxy<\/a> is a free, open-source <a href=\"https:\/\/webhostinggeeks.com\/best\/proxy-servers\/\">software<\/a> that provides a high availability <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-load-balancing\/\">load balancer<\/a> and <a href=\"https:\/\/webhostinggeeks.com\/blog\/proxy-server-definition-functionality-types\/\">proxy server<\/a> for TCP and HTTP-based applications. It is widely used for its high performance and reliability, and it offers a wealth of features such as SSL support, HTTP compression, health checks, and more. On the other hand, <a href=\"https:\/\/www.keepalived.org\/\" target=\"_blank\" rel=\"nofollow noopener\">Keepalived<\/a> is a routing software written in C that provides simple and robust facilities for load balancing and high availability to Linux and Unix-like servers.<\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-1024x517.png\" alt=\"HAProxy High Availability Keepalived\" width=\"1024\" height=\"517\" class=\"alignnone size-large wp-image-18034 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-1024x517.png 1024w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-300x151.png 300w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-1536x775.png 1536w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-128x65.png 128w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-420x212.png 420w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-540x273.png 540w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-720x363.png 720w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-960x485.png 960w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-1140x575.png 1140w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-1320x666.png 1320w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived-1280x646.png 1280w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/07\/HAProxy-High-Availability-Keepalived.png 1870w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/517;\" \/><\/p>\n<p>The combination of HAProxy and Keepalived can provide a robust, high-performance, and highly available load balancing solution for your servers. This setup can help distribute network or application traffic across a number of servers, enhancing the overall performance, improving response times and increasing redundancy in case of server failure.<\/p>\n<p>In this tutorial, we will guide you through the process of setting up HAProxy for high availability with Keepalived on your <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS<\/a>, or <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-cloud-hosting\/\">cloud hosting<\/a> machines. This guide will be a step-by-step walkthrough, ensuring that even beginners can follow along.<\/p>\n<p>Let&#8217;s get started.<\/p>\n<h2>Step 1: Installing HAProxy<\/h2>\n<p>The first step in setting up HAProxy for high availability with Keepalived is to install HAProxy on your server. Here&#8217;s how to do it:<\/p>\n<pre>\r\nsudo apt-get update\r\nsudo apt-get install haproxy\r\n<\/pre>\n<p>These commands will update your server&#8217;s package list and then install HAProxy.<\/p>\n<h2>Step 2: Configuring HAProxy<\/h2>\n<p>After installing HAProxy, the next step is to configure it. The main configuration file for HAProxy is located at \/etc\/haproxy\/haproxy.cfg. You can open this file with a text editor of your choice.<\/p>\n<pre>\r\nsudo nano \/etc\/haproxy\/haproxy.cfg\r\n<\/pre>\n<p>In this file, you can define the settings for your load balancer, such as the frontend (where HAProxy listens for connections), the backend (where HAProxy forwards the connections), and any other settings that are relevant to your specific setup.<\/p>\n<p>For instance, a basic configuration might look like this:<\/p>\n<pre>\r\nglobal\r\n    log \/dev\/log    local0\r\n    log \/dev\/log    local1 notice\r\n    chroot \/var\/lib\/haproxy\r\n    stats socket \/run\/haproxy\/admin.sock mode 660 level admin expose-fd listeners\r\n    stats timeout 30s\r\n    user haproxy\r\n    group haproxy\r\n    daemon\r\n\r\ndefaults\r\n    log     global\r\n    mode    http\r\n    option  httplog\r\n    option  dontlognull\r\n    timeout connect 5000\r\n    timeout client  50000\r\n    timeout server  50000\r\n    errorfile 400 \/etc\/haproxy\/errors\/400.http\r\n    errorfile 403 \/etc\/haproxy\/errors\/403.http\r\n    errorfile 408 \/etc\/haproxy\/errors\/408.http\r\n    errorfile 500 \/etc\/haproxy\/errors\/500.http\r\n    errorfile 502 \/etc\/haproxy\/errors\/502.http\r\n    errorfile 503 \/etc\/haproxy\/errors\/503.http\r\n    errorfile 504 \/etc\/haproxy\/errors\/504.http\r\n\r\nfrontend http_front\r\n   bind *:80\r\n   stats uri \/haproxy?stats\r\n   default_backend http_back\r\n\r\nbackend http_back\r\n   balance roundrobin\r\n   server server1 192.168.1.2:80 check\r\n   server server2 192.168.1.3:80 check\r\n<\/pre>\n<p>In this example:<\/p>\n<ul>\n<li>The global section contains settings that apply globally across all HAProxy instances.<\/li>\n<li>The defaults section contains default settings that apply to all frontend and backend sections.<\/li>\n<li>The frontend section, named http_front, is where HAProxy listens for connections. In this case, it&#8217;s listening on all interfaces (*) on port 80.<\/li>\n<li>The backend section, named http_back, is where HAProxy forwards the connections. In this case, it&#8217;s using a round-robin load balancing algorithm to distribute incoming requests to two servers (server1 and server2), both running on port 80.<\/li>\n<\/ul>\n<p>Remember to replace the IP addresses and port numbers with those of your actual servers. After editing the configuration file, save and close it. Then, restart HAProxy for the changes to take effect:<\/p>\n<pre>\r\nsudo systemctl restart haproxy\r\n<\/pre>\n<h2>Step 3: Installing Keepalived<\/h2>\n<p>After setting up HAProxy, the next step is to install Keepalived. This can be done by running the following commands:<\/p>\n<pre>\r\nsudo apt-get update\r\nsudo apt-get install keepalived\r\n<\/pre>\n<p>These commands will update your server&#8217;s package list and then install Keepalived.<\/p>\n<h2>Step 4: Configuring Keepalived<\/h2>\n<p>Once Keepalived is installed, you need to configure it. The main configuration file for Keepalived is located at \/etc\/keepalived\/keepalived.conf. You can open this file with a text editor of your choice.<\/p>\n<pre>\r\nsudo nano \/etc\/keepalived\/keepalived.conf\r\n<\/pre>\n<p>In this file, you can define the settings for your high availability setup, such as the virtual IP address, priority of the server, and the check script to monitor the status of HAProxy.<\/p>\n<p>For instance, a basic configuration might look like this:<\/p>\n<pre>\r\nvrrp_script chk_haproxy {\r\n    script \"killall -0 haproxy\"\r\n    interval 2\r\n    weight 2\r\n}\r\n\r\nvrrp_instance VI_1 {\r\n    state MASTER\r\n    interface eth0\r\n    virtual_router_id 51\r\n    priority 101\r\n    advert_int 1\r\n    authentication {\r\n        auth_type PASS\r\n        auth_pass 1111\r\n    }\r\n    virtual_ipaddress {\r\n        192.168.1.1\r\n    }\r\n    track_script {\r\n        chk_haproxy\r\n    }\r\n}\r\n<\/pre>\n<p>In this example:<\/p>\n<ul>\n<li>The vrrp_script section defines a script named chk_haproxy that checks the status of HAProxy every 2 seconds.<\/li>\n<li>The vrrp_instance section defines a VRRP instance named VI_1. This instance is set to the MASTER state, uses the eth0 network interface, and has a virtual router ID of 51.<\/li>\n<li>The priority is set to 101, which means this server will become the master server if the current master fails.<\/li>\n<li>The advert_int is the advertisement interval, which is set to 1 second.<\/li>\n<li>The authentication section defines the authentication type and password for the VRRP instance.<\/li>\n<li>The virtual_ipaddress is the virtual IP address that will be used by the master server.<\/li>\n<li>The track_script section specifies that the chk_haproxy script should be used to monitor the status of HAProxy.<\/li>\n<\/ul>\n<p>Remember to replace the network interface, virtual router ID, priority, authentication password, and virtual IP address with those of your actual setup. After editing the configuration file, save and close it. Then, restart Keepalived for the changes to take effect:<\/p>\n<pre>\r\nsudo systemctl restart keepalived\r\n<\/pre>\n<h2>Step 5: Starting and Enabling HAProxy and Keepalived<\/h2>\n<p>After configuring both HAProxy and Keepalived, you need to start and enable them so that they start automatically at boot time. This can be done by running the following commands:<\/p>\n<pre>\r\nsudo systemctl start haproxy\r\nsudo systemctl enable haproxy\r\nsudo systemctl start keepalived\r\nsudo systemctl enable keepalived\r\n<\/pre>\n<p>These commands will start HAProxy and Keepalived and enable them to start at boot time.<\/p>\n<h2>Commands Mentioned:<\/h2>\n<ul>\n<li><span class=\"fw-bold\">sudo apt-get update<\/span> \u2013 Updates the package list on your server<\/li>\n<li><span class=\"fw-bold\">sudo apt-get install haproxy<\/span> \u2013 Installs HAProxy on your server<\/li>\n<li><span class=\"fw-bold\">sudo nano \/etc\/haproxy\/haproxy.cfg<\/span> \u2013 Opens the HAProxy configuration file in a text editor<\/li>\n<li><span class=\"fw-bold\">sudo apt-get install keepalived<\/span> \u2013 Installs Keepalived on your server<\/li>\n<li><span class=\"fw-bold\">sudo nano \/etc\/keepalived\/keepalived.conf<\/span> \u2013 Opens the Keepalived configuration file in a text editor<\/li>\n<li><span class=\"fw-bold\">sudo systemctl start haproxy<\/span> \u2013 Starts the HAProxy service<\/li>\n<li><span class=\"fw-bold\">sudo systemctl enable haproxy<\/span> \u2013 Enables the HAProxy service to start at boot time<\/li>\n<li><span class=\"fw-bold\">sudo systemctl start keepalived<\/span> \u2013 Starts the Keepalived service<\/li>\n<li><span class=\"fw-bold\">sudo systemctl enable keepalived<\/span> \u2013 Enables the Keepalived service to start at boot time<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>In this tutorial, we&#8217;ve walked you through the process of setting up HAProxy for high availability with Keepalived on your dedicated, VPS, or cloud hosting machines. We started by installing HAProxy, followed by its configuration. We then installed and configured Keepalived. Finally, we started and enabled both services to ensure they start automatically at boot time.<\/p>\n<p>By following these steps, you&#8217;ve set up a robust, high-performance, and highly available load balancing solution for your servers. This setup will help distribute network or application traffic across a number of servers, enhancing the overall performance, improving response times, and increasing redundancy in case of server failure.<\/p>\n<p>Remember, the key to a successful high availability setup is regular monitoring and maintenance. Always keep an eye on your server&#8217;s performance and make necessary adjustments as needed.<\/p>\n<p>If you&#8217;re interested in exploring more about high availability, load balancing, and server administration, feel free to check out other articles on our website, for instance, <a href=\"https:\/\/webhostinggeeks.com\/blog\/varnish-mechanism-features-capabilities-advantages\/\">Varnish Explained<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/squid-proxy-server-features-functions-benefits\/\">Squid Explained<\/a>, and <a href=\"https:\/\/webhostinggeeks.com\/blog\/\">many others<\/a>.<\/p>\n<p>We hope this guide has been helpful. If you have any questions or run into any issues, feel free to leave a comment below. We&#8217;ll do our best to assist you.<\/p>\n<h2>FAQ<\/h2>\n<ol itemscope itemtype=\"https:\/\/schema.org\/FAQPage\">\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is the main function of HAProxy?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">HAProxy is a free, open-source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications. It is widely used for its high performance and reliability.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is the role of Keepalived in this setup?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Keepalived is a routing software that provides simple and robust facilities for load balancing and high availability to Linux and Unix-like servers. In this setup, it is used in conjunction with HAProxy to ensure high availability and load balancing.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What types of servers can this setup be used on?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">This setup can be used on various types of servers, including dedicated servers, Virtual Private Servers (VPS), and cloud hosting machines. The choice of server depends on your specific needs and resources.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What are the benefits of using HAProxy and Keepalived together?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Using HAProxy and Keepalived together provides a robust, high-performance, and highly available load balancing solution. This setup helps distribute network or application traffic across a number of servers, enhancing the overall performance, improving response times, and increasing redundancy in case of server failure.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What should I do if I encounter issues during the setup?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">If you encounter any issues during the setup, it&#8217;s recommended to check the configuration files for any errors. You can also check the system logs for any error messages. If you&#8217;re still unable to resolve the issue, consider seeking help from online communities or professional services.<\/span>\n            <\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In server administration, ensuring high availability of services is a crucial task. One common challenge that webmasters and server administrators often face is how to effectively balance the load of&#8230;<\/p>\n","protected":false},"author":6,"featured_media":18033,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[2134],"tags":[2135,2144,2103],"class_list":["post-18032","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-haproxy","tag-haproxy","tag-high-availability","tag-keepalived"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/18032","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=18032"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/18032\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/18033"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=18032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=18032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=18032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}