更新 scripts/haproxy.sh

12
This commit is contained in:
joy 2025-10-29 18:55:11 +08:00
parent 1998a9470c
commit 26d98850ca
1 changed files with 5 additions and 11 deletions

View File

@ -4,7 +4,6 @@ set -euo pipefail
# 初始化变量
ACTION=""
BACKENDS=""
VIP=""
HAPROXY_PORT=6443
HAPROXY_CONF="/etc/haproxy/haproxy.cfg"
HAPROXY_LOG_CONF="/etc/rsyslog.d/haproxy.conf"
@ -18,10 +17,9 @@ usage() {
echo " --uninstall 卸载haproxy并清理配置"
echo "安装选项:"
echo " --backend <节点列表> 必选,后端节点(格式:IP:端口,IP:端口..."
echo " --vip <VIP> 必选虚拟IP格式:192.168.1.100"
echo " --port <端口> 可选监听端口默认6443"
echo "示例:"
echo " 安装: $0 --install --backend 192.168.1.10:6443,192.168.1.11:6443 --vip 192.168.1.100"
echo " 安装: $0 --install --backend 192.168.1.10:6443,192.168.1.11:6443 --port 8443"
echo " 卸载: $0 --uninstall"
exit 1
}
@ -37,10 +35,6 @@ while [[ $# -gt 0 ]]; do
BACKENDS="$2"
shift 2
;;
--vip)
VIP="$2"
shift 2
;;
--port)
HAPROXY_PORT="$2"
shift 2
@ -67,8 +61,8 @@ fi
# 安装haproxy
install_haproxy() {
# 校验安装参数
if [[ -z "$BACKENDS" || -z "$VIP" ]]; then
echo "错误:安装必须指定 --backend 和 --vip"
if [[ -z "$BACKENDS" || -z "$HAPROXY_PORT" ]]; then
echo "错误:安装必须指定 --backend 和 --port"
usage
fi
@ -117,7 +111,7 @@ defaults
maxconn 8000
frontend k8s-api-frontend
bind $VIP:$HAPROXY_PORT
bind *:$HAPROXY_PORT
mode tcp
default_backend k8s-api-backend
@ -141,7 +135,7 @@ EOF
systemctl enable --now haproxy
if systemctl is-active --quiet haproxy; then
echo "===== haproxy安装完成 ====="
echo "监听:$VIP:$HAPROXY_PORT"
echo "监听:*:$HAPROXY_PORT"
echo "后端:$BACKENDS"
else
echo "错误haproxy启动失败请查看日志 $HAPROXY_LOG"