본문 바로가기

리눅스

CentOS 7에서 HAProxy를 Source Compile 방식으로 설치하는 방법

반응형

CentOS 7에서 HAProxy를 Source Compile 방식으로 설치하는 방법(source compile)

테스트 환경

운영체제 정보

$ cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

1. 의존성 패키지 설치

HAProxy 컴파일에 필요한 필수 패키지를 설치합니다.

yum install -y make gcc perl pcre-devel zlib-devel openssl-devel lua-devel systemd-devel

Lua 버전을 확인합니다.

lua -v
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio

2. Lua 5.3 업그레이드

HAProxy Lua 기능 사용을 위해 Lua 5.3으로 업그레이드합니다.

 

1) EPEL 저장소 추가

wget http://www.nosuchhost.net/~cheese/fedora/packages/epel-7/x86_64/cheese-release-7-1.noarch.rpm
yum install -y cheese-release-7-1.noarch.rpm

2) Lua 업데이트

yum update -y lua-devel

3) Lua 버전 확인

lua -v
Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio

3. HAProxy 소스 다운로드

공식 사이트에서 HAProxy 소스를 다운로드합니다.

wget https://www.haproxy.org/download/2.6/src/haproxy-2.6.1.tar.gz \
  -O /usr/local/src/haproxy-2.6.1.tar.gz

압축 해제

tar xfz /usr/local/src/haproxy-2.6.1.tar.gz -C /usr/local/src

디렉터리 이동

cd /usr/local/src/haproxy-*

4. HAProxy 컴파일

컴파일 옵션 설명

  • USE_PCRE : 정규식 지원
  • USE_OPENSSL : SSL/TLS 지원
  • USE_ZLIB : HTTP 압축
  • USE_LUA : Lua 스크립트 지원
  • USE_SYSTEMD ; systemd 서비스 지원

컴파일

make -j$(nproc) \
  TARGET=linux-glibc \
  USE_PCRE=1 \
  USE_OPENSSL=1 \
  USE_ZLIB=1 \
  USE_LUA=1 \
  USE_SYSTEMD=1
  CC      src/hlua.o
  CC      src/hlua_fcn.o
  CC      src/namespace.o
  ...
  CC      dev/flags/flags.o
  LD      haproxy
  LD      dev/flags/flags

설치

make install -j$(nproc)
install: creating directory ‘/usr/local/doc’
install: creating directory ‘/usr/local/doc/haproxy’
‘doc/haproxy.1’ -> ‘/usr/local/share/man/man1/haproxy.1’
‘haproxy’ -> ‘/usr/local/sbin/haproxy’
‘doc/configuration.txt’ -> ‘/usr/local/doc/haproxy/configuration.txt’
...
‘doc/intro.txt’ -> ‘/usr/local/doc/haproxy/intro.txt’

설치 위치

/usr/local/sbin/haproxy

5. HAProxy 버전 확인

haproxy -version
HAProxy version 2.6.1-f6ca66d 2022/06/21 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2027.
Known bugs: http://www.haproxy.org/bugs/bugs-2.6.1.html
Running on: Linux 3.10.0-1160.66.1.el7.x86_64 #1 SMP Wed May 18 16:02:34 UTC 2022 x86_64

빌드 상세 정보 확인

haproxy -vv
HAProxy version 2.6.1-f6ca66d 2022/06/21 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2027.
Known bugs: http://www.haproxy.org/bugs/bugs-2.6.1.html
Running on: Linux 3.10.0-1160.66.1.el7.x86_64 #1 SMP Wed May 18 16:02:34 UTC 2022 x86_64
Build options :
  TARGET  = linux-glibc
  CPU     = generic
  CC      = cc
  CFLAGS  = -O2 -g -Wall -Wextra -Wundef -Wdeclaration-after-statement -Wfatal-errors -Wtype-limits -fwrapv -Wno-address-of-packed-member -Wno-unused-label -Wno-sign-compare -Wno-unused-parameter -Wno-clobbered -Wno-missing-field-initializers -Wno-cast-function-type -Wno-string-plus-int -Wno-atomic-alignment
  OPTIONS = USE_PCRE=1 USE_OPENSSL=1 USE_LUA=1 USE_ZLIB=1 USE_SYSTEMD=1
  DEBUG   = -DDEBUG_STRICT -DDEBUG_MEMORY_POOLS

Feature list : +EPOLL -KQUEUE +NETFILTER +PCRE -PCRE_JIT -PCRE2 -PCRE2_JIT +POLL +THREAD +BACKTRACE -STATIC_PCRE -STATIC_PCRE2 +TPROXY +LINUX_TPROXY +LINUX_SPLICE +LIBCRYPT +CRYPT_H -ENGINE +GETADDRINFO +OPENSSL +LUA +ACCEPT4 -CLOSEFROM +ZLIB -SLZ +CPU_AFFINITY +TFO +NS +DL +RT -DEVICEATLAS -51DEGREES -WURFL +SYSTEMD -OBSOLETE_LINKER +PRCTL -PROCCTL +THREAD_DUMP -EVPORTS -OT -QUIC -PROMEX -MEMORY_PROFILING

Default settings :
  bufsize = 16384, maxrewrite = 1024, maxpollevents = 200
728x90

6. HAProxy 계정 생성

보안을 위해 별도 계정을 생성합니다.

groupadd -g 99 haproxy
useradd -m -c "HAProxy Service" -d /var/lib/haproxy -s /sbin/nologin -u 99 -g 99 haproxy

7. HAProxy 디렉터리 생성

필수 디렉터리 생성

mkdir -p /etc/haproxy
mkdir -p /var/lib/haproxy

필수 파일 생성

touch /etc/haproxy/haproxy.cfg
touch /etc/haproxy/domain2backend.map
touch /var/lib/haproxy/stats

8. 실행 파일 링크 생성

ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy

9. 디렉터리 권한 설정

chown -R haproxy:haproxy /etc/haproxy
chown -R haproxy:haproxy /var/lib/haproxy

10. Systemd 서비스 등록

HAProxy 소스에는 systemd 서비스 파일이 포함되어 있습니다.

 

디렉터리 이동

## cd /usr/local/src/haproxy-2.6.1/admin/systemd
cd admin/systemd
$ ls
haproxy.service.in  Makefile

서비스 파일 생성

make
sed -e 's:@SBINDIR@:'/usr/local/sbin':' haproxy.service.in > haproxy.service

생성된 서비스 파일 복사

cp haproxy.service /lib/systemd/system/

11. systemd 서비스 주요 설정

vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=network-online.target
Wants=network-online.target

[Service]
EnvironmentFile=-/etc/default/haproxy
EnvironmentFile=-/etc/sysconfig/haproxy
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid" "EXTRAOPTS=-S /run/haproxy-master.sock"
ExecStart=/usr/local/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS
ExecReload=/usr/local/sbin/haproxy -Ws -f $CONFIG -c -q $EXTRAOPTS
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
SuccessExitStatus=143
Type=notify

# The following lines leverage SystemD's sandboxing options to provide
# defense in depth protection at the expense of restricting some flexibility
# in your setup (e.g. placement of your configuration files) or possibly
# reduced performance. See systemd.service(5) and systemd.exec(5) for further
# information.

# NoNewPrivileges=true
# ProtectHome=true
# If you want to use 'ProtectSystem=strict' you should whitelist the PIDFILE,
# any state files and any other files written using 'ReadWritePaths' or
# 'RuntimeDirectory'.
# ProtectSystem=true
# ProtectKernelTunables=true
# ProtectKernelModules=true
# ProtectControlGroups=true
# If your SystemD version supports them, you can add: @reboot, @swap, @sync
# SystemCallFilter=~@cpu-emulation @keyring @module @obsolete @raw-io

[Install]
WantedBy=multi-user.target

옵션 설명

  • -W : Master Worker 모드
  • -s : systemd notify
  • -f : 설정 파일
  • -p : PID 파일
  • -S : Runtime Socket

12. 서비스 등록 및 실행

systemd 리로드

systemctl daemon-reload

서비스 등록 및 시작

systemctl --now enable haproxy
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.

13. 설정 파일 검증

HAProxy 설정 파일 문법 검증

haproxy -f /etc/haproxy/haproxy.cfg -c
$ haproxy -f /etc/haproxy/haproxy.cfg -c
Configuration file is valid

14. 서비스 상태 확인

systemctl status haproxy

또는

ps -ef | grep haproxy

15. 설치 완료

HAProxy가 정상적으로 설치되고 systemd 서비스로 관리됩니다.

 

실행 파일 위치

/usr/local/sbin/haproxy

설정 파일 위치

/etc/haproxy/haproxy.cfg

 

참고URL

- HAProxy 공식 사이트 : https://haproxy.org

 

728x90
반응형