2.2 haproxy 离线安装
2.2.1 下载haproxy 安装
#主节点安装
192.167.14.119 haproxy+keepalived master119
192.167.14.223 haproxy+keepalived master223
192.167.14.226 haproxy+keepalived master226
# 下载haproxy
wget https://www.haproxy.org/download/3.1/src/haproxy-3.1.1.tar.gz
#解压
tar -zxvf haproxy-3.1.1.tar.gz -C /usr/local
#进入目录、进行编译、安装
cd /usr/local/haproxy-3.1.1
make TARGET=linux31 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
#创建目录
mkdir /usr/local/haproxy/logs
2.2.2 haproxy设置为系统服务
#编写脚本
vim /etc/rc.d/init.d/haproxy
#!/bin/bash
# chkconfig: 2345 10 90
# description: HAProxy
BASE_DIR="/usr/local/haproxy"
ARGV="$@"
start() {
echo "Starting HAProxy servers..."
if [ -f "$BASE_DIR/haproxy.cfg" ]; then
$BASE_DIR/sbin/haproxy -f $BASE_DIR/haproxy.cfg
echo "HAProxy started successfully."
else
echo "HAProxy configuration file not found."
exit 1
fi
}
stop() {
echo "Stopping HAProxy..."
if [ -f "$BASE_DIR/logs/haproxy.pid" ]; then
kill -USR1 $(cat $BASE_DIR/logs/haproxy.pid)
echo "HAProxy stopped successfully."
else
echo "HAProxy PID file not found."
exit 1
fi
}
restart() {
echo "Restarting HAProxy..."
stop
sleep 2 # Wait for a moment to ensure the process has stopped
start
}
case $ARGV in
start)
start
ERROR=$?
;;
stop)
stop
ERROR=$?
;;
restart)
restart
ERROR=$?
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $ERROR
#赋予脚本权限
chmod +x /etc/rc.d/init.d/haproxy
#脚本以及开机自启动
chkconfig haproxy on
#添加为系统服务
chkconfig --add haproxy
systemctl start haproxy
systemctl enable haproxy
systemctl status haproxy
#配置haproxy.cfg
操作节点:master119 master223 master226 所有节点
cat > /usr/local/haproxy/haproxy.cfg <<EOF
global
maxconn 2000
ulimit-n 16384
log 127.0.0.1 local0 err
stats timeout 30s
defaults
log global
mode http
option httplog
timeout connect 5000
timeout client 50000
timeout server 50000
timeout http-request 15s
timeout http-keep-alive 15s
frontend monitor-in
bind *:33305
mode http
option httplog
monitor-uri /monitor
frontend k8s-master
bind 0.0.0.0:16443
bind 127.0.0.1:16443
mode tcp
option tcplog
tcp-request inspect-delay 5s
default_backend k8s-apiserver
backend k8s-apiserver
mode tcp
option tcp-check
balance roundrobin
default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
server master119 192.167.14.119:6443 check
server master223 192.167.14.223:6443 check
server master226 192.167.14.226:6443 check
EOF
#启动
service haproxy start
#停止
service haproxy stop
#重启
service haproxy restart
#强制杀死
killall haproxy
pkill -9 haproxy
#查看状态
systemctl status haproxy
ss -untlp|grep 16443
netstat -lntup|grep haproxy