리눅스
HAProxy 구성을 동기화하고 HAProxy를 다시 시작하는 스크립트
변군이글루
2024. 2. 17. 17:26
반응형
HAProxy 구성을 동기화하고 HAProxy를 다시 시작하는 스크립트
사전 작업
- SSH(/etc/ssh/sshd_config) 설정
sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
sudo systemctl restart sshd
- SSH 키 교환
ssh-keygen -t rsa -b 4096
ssh-copy-id username@remote_host
scp ~/.ssh/id_rsa username@remote_host:~/.ssh/id_rsa
ssh username@remote_host
스크립트 작성
vim /usr/local/bin/haproxy_sync_restart.sh
#!/bin/bash
haproxy_conf="/etc/haproxy/haproxy.cfg"
target_server="root@knode1"
# haproxy.cfg 파일의 구문이 올바른지 확인합니다.
if haproxy -c -f "$haproxy_conf" -V; then
echo -e "haproxy configuration syntax is valid. Proceeding...\n"
# localhost 서버의 haproxy를 다시 시작합니다.
echo "Restarting HAProxy on target server..."
sudo systemctl restart haproxy
# 구문이 올바르다면 haproxy.cfg 파일을 Target 서버로 복사합니다.
scp -q "$haproxy_conf" "$target_server":"$haproxy_conf" &&
# Target 서버에서 haproxy를 다시 시작합니다.
echo -e "\nRestarting HAProxy on target server..."
ssh "$target_server" "sudo systemctl restart haproxy" &&
echo "HAProxy configuration synchronized and HAProxy restarted successfully."
else
echo "ERROR: Configuration file is not valid. Please check haproxy.cfg."
fi
더보기
---
#!/bin/bash
haproxy_conf="/etc/haproxy/haproxy.cfg"
target_server="root@knode1"
# haproxy.cfg 파일의 구문이 올바른지 확인합니다.
if haproxy -c -f "$haproxy_conf" -V; then
# 구문이 올바르다면 haproxy.cfg 파일을 knode1 서버로 복사합니다.
scp "$haproxy_conf" "$target_server":"$haproxy_conf" && \
# knode1 서버에서 haproxy를 다시 시작합니다.
ssh "$target_server" "sudo systemctl restart haproxy" && \
echo "HAProxy configuration synchronized and HAProxy restarted successfully."
else
echo "ERROR: Configuration file is not valid. Please check haproxy.cfg."
fi
---
- 스크립트 실행 권한 여부
chmod +x /usr/local/bin/haproxy_sync_restart.sh
- 스크립트 링크 생성
ln -s /usr/local/bin/haproxy_sync_restart.sh /usr/local/bin/haproxy_sync_restart
사용 예시
- haproxy.cfg 편집
vim /etc/haproxy/haproxy.cfg
- 실패
$ haproxy_sync_restart
[NOTICE] (61492) : haproxy version is 2.4.24-0ubuntu0.22.04.1
[NOTICE] (61492) : path to executable is /usr/sbin/haproxy
[ALERT] (61492) : parsing [/etc/haproxy/haproxy.cfg:56] : unknown keyword 'd' in 'listen' section
[ALERT] (61492) : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg
[ALERT] (61492) : Fatal errors found in configuration.
ERROR: Configuration file is not valid. Please check haproxy.cfg.
- 성공
$ haproxy_sync_restart
Configuration file is valid
haproxy configuration syntax is valid. Proceeding...
Restarting HAProxy on target server...
Restarting HAProxy on target server...
HAProxy configuration synchronized and HAProxy restarted successfully.
728x90
반응형