This guide provides step-by-step instructions on installing and configuring HAProxy and Keepalived from scratch on RedHat Linux systems. This setup ensures high availability and load balancing for network services.
Prerequisites
- Two or more RedHat Linux servers
- Root or sudo privileges on all servers
- Basic knowledge of Linux and networking
- Access to the internet or RedHat repository to download packages
Installing Required Packages
1. **Update System Packages:**
Ensure that all your system packages are up to date:
```bash
sudo yum update
```
2. **Install Required Dependencies:**
Install the development tools and libraries needed to compile HAProxy and Keepalived:
```bash
sudo yum groupinstall 'Development Tools'
sudo yum install openssl-devel pcre-static pcre-devel libnl3-devel
Compiling and Installing HAProxy
1. **Download the Latest HAProxy:**
Obtain the latest version of HAProxy from the [official site](http://www.haproxy.org/):
```bash
wget http://www.haproxy.org/download/2.4/src/haproxy-2.4.4.tar.gz
tar xzvf haproxy-2.4.4.tar.gz
cd haproxy-2.4.4
```
2. **Compile HAProxy:**
Compile HAProxy with the necessary features:
```bash
make TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_ZLIB=1 USE_LUA=1 LUA_LIB=/usr/lib/x86_64-linux-gnu/ LUA_INC=/usr/include/lua5.3
```
3. **Install HAProxy:**
Install the compiled binary to the appropriate location:
```bash
sudo make install
```
4. **Configure Systemd:**
Set up a systemd service for HAProxy:
```bash
sudo tee /etc/systemd/system/haproxy.service <<EOF
[Unit]
Description=HAProxy Load Balancer
After=network.target
[Service]
ExecStart=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
[Install]
WantedBy=multi-user.target
EOF
```
5. **Create Configuration Directory:**
```bash
sudo mkdir -p /etc/haproxy
sudo touch /etc/haproxy/haproxy.cfg
```
Installing and Configuring Keepalived
1. **Compile and Install Keepalived:**
Download and compile Keepalived:
```bash
wget https://www.keepalived.org/software/keepalived-2.2.4.tar.gz
tar xzvf keepalived-2.2.4.tar.gz
cd keepalived-2.2.4
./configure --disable-dynamic-linking
make && sudo make install
```
2. **Configure Keepalived:**
Create a basic Keepalived configuration to manage a virtual IP:
```bash
sudo tee /etc/keepalived/keepalived.conf <<EOF
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
EOF
```
3. **Create Systemd Service for Keepalived:**
```bash
sudo tee /etc/systemd/system/keepalived.service <<EOF
[Unit]
Description=Load Balancer Monitor
After=network.target
[Service]
ExecStart=/usr/local/sbin/keepalived --dont-fork
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
EOF
```
Starting Services
1. **Enable and Start HAProxy:**
```bash
sudo systemctl enable haproxy
sudo systemctl start haproxy
```
2. **Enable and Start Keepalived:**
```bash
sudo systemctl enable keepalived
sudo systemctl start keepalived
```
Conclusion
By following these instructions, you will have installed and configured HAProxy and Keepalived on your RedHat Linux systems for high availability and load balancing. Make sure to adjust the configuration files according to your specific network requirements and environments.