Install HAProxy 2.7 On CentOS 7: A Simple Guide

by Alex Braham 48 views

Let's dive into how to get HAProxy 2.7 up and running on CentOS 7. This guide will walk you through each step, making it super easy to follow along. Whether you're a seasoned sysadmin or just starting out, you'll find this straightforward and helpful. So, grab your favorite beverage, and let’s get started!

What is HAProxy?

HAProxy, short for High Availability Proxy, is a free, open-source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications. It's used to improve the performance and reliability of a server environment by distributing the workload across multiple servers. Think of it as a smart traffic controller for your web applications, ensuring no single server gets overwhelmed and your users always have a smooth experience.

HAProxy is designed to handle a massive number of concurrent connections with minimal resource usage, making it perfect for high-traffic websites and applications. It supports various load balancing algorithms, health checks, and SSL/TLS encryption, providing a comprehensive solution for managing your server infrastructure. With HAProxy, you can ensure that your applications are always available, responsive, and secure, regardless of traffic spikes or server issues.

It works by sitting in front of your backend servers and intelligently routing incoming requests based on pre-configured rules. This not only distributes the load but also adds an extra layer of security and resilience. By using HAProxy, you can easily scale your applications, perform maintenance without downtime, and protect against DDoS attacks. It’s a versatile tool that’s essential for any modern web infrastructure.

Prerequisites

Before we jump into the installation, there are a few things you'll need to have in place:

  • A CentOS 7 server: Make sure you have a CentOS 7 server up and running. If you don't have one already, you can easily spin one up on cloud platforms like AWS, Google Cloud, or Azure.
  • Root access: You'll need root or sudo privileges to install software and configure the system. This is essential for making changes to system-level settings.
  • Basic knowledge of Linux commands: Familiarity with basic Linux commands like yum, ssh, and text editors like vi or nano will be helpful.
  • Internet connection: You'll need an active internet connection to download the necessary packages and dependencies.

Having these prerequisites in place will ensure a smooth and hassle-free installation process. If you're missing any of these, take a moment to set them up before proceeding. Once you're ready, we can move on to the next steps.

Step-by-Step Installation Guide

Step 1: Update Your System

First things first, let’s make sure your system is up to date. Open your terminal and run the following command:

sudo yum update -y

This command updates all the installed packages to their latest versions. The -y flag automatically answers 'yes' to any prompts, ensuring a smooth and unattended update process. Keeping your system updated is crucial for security and stability, so it’s always a good practice to start with this step.

Step 2: Install Required Packages

Next, we need to install some packages that HAProxy requires. Run the following command:

sudo yum install -y epel-release vim wget

Here’s what each package does:

  • epel-release: This provides access to the Extra Packages for Enterprise Linux (EPEL) repository, which contains additional software not available in the default CentOS repositories.
  • vim: This is a powerful text editor that you'll use to configure HAProxy.
  • wget: This is a command-line utility for downloading files from the internet.

These packages are essential for downloading, installing, and configuring HAProxy. Make sure you have them installed before moving on to the next step.

Step 3: Download HAProxy 2.7

Since HAProxy 2.7 might not be available in the default repositories for CentOS 7, we'll download it directly from the HAProxy website. First, navigate to the /opt directory:

cd /opt

Then, download the HAProxy 2.7 source code using wget. You can find the latest version’s download link on the official HAProxy website. For example:

sudo wget https://www.haproxy.org/download/2.7/src/haproxy-2.7.0.tar.gz

Make sure to replace the link with the actual download link for the version you want to install.

Step 4: Extract the Source Code

Once the download is complete, extract the source code using the following command:

sudo tar -xzf haproxy-2.7.0.tar.gz

This command unpacks the downloaded archive into a directory named haproxy-2.7.0. Now, navigate into that directory:

cd haproxy-2.7.0

Step 5: Compile and Install HAProxy

Now, it's time to compile and install HAProxy. Before compiling, you might need to install additional development tools. Run:

sudo yum install -y gcc pcre-devel openssl-devel zlib-devel systemd-devel

Then, compile HAProxy using the following command:

make TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1

This command tells the make utility to compile HAProxy with specific options:

  • TARGET=linux-glibc: Specifies the target operating system.
  • USE_PCRE=1: Enables support for Perl Compatible Regular Expressions (PCRE).
  • USE_OPENSSL=1: Enables support for OpenSSL for SSL/TLS encryption.
  • USE_ZLIB=1: Enables support for zlib compression.
  • USE_SYSTEMD=1: Enables support for systemd for managing HAProxy as a service.

After compiling, install HAProxy with:

sudo make install

This command installs the HAProxy binaries to /usr/local/sbin and /usr/local/bin.

Step 6: Configure HAProxy

Now that HAProxy is installed, we need to configure it. Create a configuration file at /etc/haproxy/haproxy.cfg:

sudo mkdir -p /etc/haproxy
sudo vim /etc/haproxy/haproxy.cfg

Add the following basic configuration to the file:

global
    log /dev/log local0
    log /dev/log local1 notice
    chroot /var/lib/haproxy
    stats socket /var/lib/haproxy/stats
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

defaults
    log global
    mode http
    option httplog
    option dontlognull
    timeout connect 5000
    timeout client  50000
    timeout server  50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

frontend main
    bind *:80
    default_backend webservers

backend webservers
    balance roundrobin
    server server1 <SERVER_IP>:80 check
    server server2 <SERVER_IP>:80 check

Replace <SERVER_IP> with the actual IP addresses of your backend servers. This configuration sets up a basic HTTP load balancer that distributes traffic between two backend servers.

Step 7: Create HAProxy User and Group

Create the haproxy user and group:

sudo groupadd -r haproxy
sudo useradd -r -g haproxy -d /var/lib/haproxy -s /sbin/nologin haproxy

Also, create the directory for the stats socket:

sudo mkdir -p /var/lib/haproxy

Step 8: Create a Systemd Service File

To manage HAProxy as a service, create a systemd service file:

sudo vim /etc/systemd/system/haproxy.service

Add the following content to the file:

[Unit]
Description=HAProxy Load Balancer
After=network.target

[Service]
ExecStart=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg
ExecReload=/bin/kill -USR2 $MAINPID
User=haproxy
Group=haproxy

[Install]
WantedBy=multi-user.target

Save the file and exit.

Step 9: Start and Enable HAProxy

Now, start and enable the HAProxy service:

sudo systemctl start haproxy
sudo systemctl enable haproxy

Check the status of the service to ensure it’s running correctly:

sudo systemctl status haproxy

If everything is set up correctly, you should see a message indicating that HAProxy is active and running.

Step 10: Verify HAProxy

To verify that HAProxy is working, you can check the stats page. Add the following to your haproxy.cfg file in the frontend section:

    stats uri     /haproxy-stats
    stats realm   Haproxy Statistics
    stats auth    admin:password

Restart HAProxy:

sudo systemctl restart haproxy

Now, open your web browser and navigate to http://<YOUR_SERVER_IP>/haproxy-stats. You should see the HAProxy statistics page, which provides real-time information about your load balancer and backend servers. Remember to replace <YOUR_SERVER_IP> with the actual IP address of your server.

Conclusion

And there you have it! You've successfully installed and configured HAProxy 2.7 on CentOS 7. This setup provides a basic HTTP load balancer, but HAProxy is highly customizable, allowing you to tailor it to your specific needs. Whether you're improving website performance, ensuring high availability, or managing complex server infrastructures, HAProxy is a powerful tool in your arsenal. Keep exploring its features and options to get the most out of it. Happy load balancing!