[k8s实战]haproxy 离线安装

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曼岛_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值