haproxy keepalive实践

news2025/1/17 6:57:54

1 安装haproxy

wget http://download.openpkg.org/components/cache/haproxy/haproxy-2.6.6.tar.gz
tar -zxvf haproxy-2.6.6.tar.gz
cd haproxy-2.6.6
mkdir -p /app/haproxy
# 安装依赖,解决haproxy.c:80:31的问题
sudo yum -y install gcc openssl-devel pcre-devel systemd-devel
src/haproxy.c:80:31: fatal error: systemd/sd-daemon.h: No such file or directory
 #include <systemd/sd-daemon.h>

sudo make ARCH=x86_64 TARGET=linux3100 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/app/haproxy

sudo make install PREFIX=/app/haproxy
sudo su - 
echo 'export PATH=$PATH:/app/haproxy/sbin' >> /etc/profile
tail -1 /etc/profile
source /etc/profile

 
cd /app/haproxy
mkdir  -p bin conf logs  var/chroot

配置转发

[root@server-10-160 haproxy]# cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).

fs.file-max = 101365
vm.max_map_count=655360

net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
#net.bridge.bridge-nf-call-iptables = 1
#net.bridge.bridge-nf-call-ip6tables = 1

用户

useradd haproxy -s  /sbin/nologin 

配置

vi /usr/lib/systemd/system/haproxy.service

[Unit]
Description=Haproxy
[Service]
Type=forking
ExecStart=/app/haproxy/sbin/haproxy -f /app/haproxy/conf/haproxy.cfg
ExecReload=/app/haproxy/sbin/haproxy -f /app/haproxy/conf/haproxy.cfg -sf 'cat /run/haproxy.pid'
[Install]
WantedBy=multi-user.target

# 加载生效
systemctl daemon-reload

在设置开机自启动的时候systemctl enable haproxy,提示Failed to execute operation: Invalid argument,执行systemctl list-unit-files,那是因为haproxy.service中配置错误
1

haproxy.cfg配置

global
    chroot /app/haproxy/var/chroot
    group  haproxy
    user haproxy
    daemon
    log 127.0.0.1:514 local0  notice
    #warning info
    pidfile /var/run/haproxy.pid
    #pidfile /drbd_data/haproxy/var/run/haproxy.pid
    maxconn 20000
    spread-checks 3
    # haproxy 2 不支持nbproc
    # nbproc 4

defaults 
    log global
    mode http 
    retries 3 
    option redispatch
    timeout connect 10000
    timeout client 50000
    timeout server 50000
    timeout client 50s
    timeout server 50s
    timeout connect  5s

listen admin
       bind *:8888
       mode  http
       stats enable
       stats hide-version
       stats uri /admin?status
       stats auth ha:ha
       stats   refresh 5s
       bind-process    1    #此行为上面加入到配置文件当中的
       #监控页面的刷新时间


#---------------------------------------------------------------------
#http协议转发 ACL规则 定义转发规则
#acl web-client path_beg -i /vsphere-client
#acl bbs hdr_reg(host) -i ^(bbs.test.com|shequ.test.com|forum) 
#acl monitor hdr_beg(host) -i monitor.test.com    #定义ACL名称,对应的请求的主机头是monitor.test.com  
#acl www hdr_beg(host) -i www.test.com 
#use_backend  cache.test.com if static    
#use_backend  monitor.test.com if bbs or monitor 
#use_backend  www.test.com if www 
#use_backend  vsphere-client if web-client 
#---------------------------------------------------------------------

frontend www
    bind *:80
    mode http
    option forwardfor
    option httpclose
    option httplog      #启用提前将HTTP请求记入日志,不能用于backend区段。
    option dontlognull  #保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包。   空连接
    option logasap
    balance roundrobin
    log global
    #cookie SERVERID insert indirect #haproxy基于cookie实现会话绑定
    timeout client 15s
    timeout server 15s
    option  allbackups


    #定义ACL名称,对应的请求的主机头是txy.580sc.net
	acl web1 hdr_reg(host) -i ^(bookinfo.580sc.net|cstom.580sc.net|txy2.580sc.net|metersphere-server.580sc.net) 
	acl web2 hdr_beg(host) -i bookinfo.580sc.net
	
	use_backend webporter if web1 or web2 

	default_backend openresty

backend openresty
	mode http
	balance roundrobin
	cookie SERVERID insert indirect nocache
    server server-10-162 10.101.10.162:80    maxconn 5000  check inter 4000  rise 3 fall 5  
    server server-10-163 10.101.10.163:80    maxconn 5000  check inter 4000  rise 3 fall 5
	
#---------------------------------------------------------------------
# round robin balancing between the kubesphere porterLB backends
#---------------------------------------------------------------------

backend webporter              #定义后端服务器群(web server/apache/nginx/iis..) 
    mode http 
    option  forwardfor    #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP 
    balance leastconn     #负载均衡的方式,最小连接 
    cookie SERVERID       #插入serverid到cookie中,serverid后面可以定义 
    server eip199 10.101.10.199:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3 


frontend https_frontend
	bind *:443
    mode tcp
    log global
    option tcplog
    timeout client 3600s
    backlog 4096
    maxconn 1000000
	default_backend https_back

backend https_back
	mode tcp
    option log-health-checks
    option redispatch
    option tcplog
    balance roundrobin
    timeout connect 1s
    timeout queue 5s
    timeout server 3600s
    balance roundrobin
	server server-10-162 10.101.10.162:443    maxconn 50000  check inter 4000  rise 3 fall 5
    server server-10-163 10.101.10.163:443    maxconn 50000  check inter 4000   rise 3 fall 5

查看端口统计信息

[root@server-10-161 ~]# ss -tnl
[root@server-10-161 conf]# ss -tnl
State      Recv-Q Send-Q                                          Local Address:Port                                                         Peer Address:Port              
LISTEN     0      128                                                         *:80                                                                      *:*                  
LISTEN     0      128                                                         *:22                                                                      *:*                  
LISTEN     0      20                                                          *:2007                                                                    *:*                  
LISTEN     0      128                                                         *:8888                                                                    *:*                  
LISTEN     0      100                                                 127.0.0.1:25                                                                      *:*                  
LISTEN     0      128                                                         *:443                                                                     *:*                  
LISTEN     0      128                                                      [::]:22                                                                   [::]:*                  
LISTEN     0      100                                                     [::1]:25                                                                   [::]:* 

查看端口对应的进程

[root@server-10-161 conf]# netstat -tunlp | grep 2007
tcp        0      0 0.0.0.0:2007            0.0.0.0:*               LISTEN      914/fileserver      
[root@server-10-161 conf]# netstat -tunlp | grep 8888
tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      31602/haproxy 

2 keepalived
Keepalived详解
keepalived官网

yum -y install gcc curl openssl-devel libnl3-devel net-snmp-devel
tar -zxvf keepalived-2.2.7.tar.gz
sudo cp -r keepalived-2.2.7 /app
sudo su -
cd /app/keepalived-2.2.7
./configure --prefix=/app/keepalived 
make && make install
# 设置快捷
ln -s /app/keepalived/sbin/keepalived /usr/sbin/

再centos7.9中,执行下面的命令后,重新make && make install


yum install automake -y
autoreconf -ivf

# 用于解决下面的问题
 cd . && /bin/sh /app/keepalived-2.2.7/build-aux/missing automake-1.16 --foreign
/app/keepalived-2.2.7/build-aux/missing: line 81: automake-1.16: command not found
WARNING: 'automake-1.16' is missing on your system.

设置启动

vi /usr/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=-/app/keepalived/etc/sysconfig/keepalived
ExecStart=/app/keepalived/sbin/keepalived  $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

# 使之生效
systemctl daemon-reload
cd /app/keepalived/etc/keepalived
cp keepalived.conf.sample keepalived.conf
mkdir -p /etc/keepalived
cp /app/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# 设置开机启动
systemctl enable keepalived

haproxy+keepalived(主从模式)实现高可用环境的简单配置
参考haproxy+keepalived高可用搭建 实现vip漂移,照抄出现下面的问题

Nov 28 17:07:01 server-10-160 Keepalived[16872]: Starting Keepalived v2.2.7 (01/16,2022)
Nov 28 17:07:01 server-10-160 Keepalived[16872]: Running on Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 (built for Linux 3.10.0)
Nov 28 17:07:01 server-10-160 Keepalived[16872]: Command line: '/app/keepalived/sbin/keepalived' '--dont-fork' '-D'
Nov 28 17:07:01 server-10-160 Keepalived[16872]: WARNING - using deprecated default config file '/etc/keepalived/keepalived.conf' - please move to '/usr/local/etc/keepalived/ke
Nov 28 17:07:01 server-10-160 Keepalived[16872]: Opening file '/etc/keepalived/keepalived.conf'.
Nov 28 17:07:01 server-10-160 Keepalived[16872]: Configuration file /etc/keepalived/keepalived.conf
Nov 28 17:07:01 server-10-160 Keepalived[16872]: NOTICE: setting config option max_auto_priority should result in better keepalived performance
Nov 28 17:07:01 server-10-160 Keepalived[16872]: Starting VRRP child process, pid=16873
Nov 28 17:07:01 server-10-160 Keepalived_vrrp[16873]: Registering Kernel netlink reflector
Nov 28 17:07:01 server-10-160 Keepalived_vrrp[16873]: Registering Kernel netlink command channel
Nov 28 17:07:01 server-10-160 Keepalived_vrrp[16873]: Script user 'keepalived_script' does not exist
Nov 28 17:07:01 server-10-160 Keepalived_vrrp[16873]: (/etc/keepalived/keepalived.conf: Line 20) WARNING - interface bond0 for vrrp_instance http1 doesn't exist
Nov 28 17:07:01 server-10-160 Keepalived_vrrp[16873]: Non-existent interface specified in configuration
cd /etc/keepalived
vi check_haproxy.sh
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then    ###判断haproxy是否已经启动
    systemctl start haproxy                                ###如果没有启动,则启动haproxy程序
fi
sleep 2                                                    ###睡眠两秒钟,等待haproxy完全启动
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then    ###判断haproxy是否已经启动
    systemctl stop keepalived                              ###如果haproxy没有启动起来,则将keepalived停掉,则VIP自动漂移到另外一台haproxy机器,实现了对haproxy的高可用
fi
#
chmod +x check_haproxy.sh

主节点配置

! Copnfiguration File for keepalived
global_defs {
   notification_email {
    xxxx@qq.com
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   # 开启SNMP陷阱
   enable_traps
   # 配置于host那么相同
   router_id server-10-160
}

vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 2
}

vrrp_instance http1 {
    state MASTER
    interface eth0
    virtual_router_id 69
    priority 100
    advert_int 1
    nopreempt   
    authentication {
    auth_type PASS
    auth_pass 111111
    }
    virtual_ipaddress {
                #配置vip
        10.101.10.40
    }
    track_script {
        chk_haproxy
    }
}

从节点配置

! Configuration File for keepalived
global_defs {
   notification_email {
    xxx@qq.com
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   enable_traps
   router_id server-10-161
}

vrrp_script chk_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 2
}


vrrp_instance http1 {
    state BACKUP
    interface eth0
    virtual_router_id 69
    priority 70
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 111111
    }
    virtual_ipaddress {
        10.101.10.40
    }
    track_script {
        chk_haproxy
    }
}

使用下列指令查看IP是否绑定成功(会在eth0上显示出来),执行ip addr
下面是在主节点

[root@server-10-160 keepalived]# ip add show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 42:33:a3:8a:71:43 brd ff:ff:ff:ff:ff:ff
    inet 10.101.10.160/24 brd 10.101.10.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.101.10.12/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::4033:a3ff:fe8a:7143/64 scope link 
       valid_lft forever preferred_lft forever

下面是在从节点执行

[root@server-10-161 keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 3e:44:17:6b:cc:2d brd ff:ff:ff:ff:ff:ff
    inet 10.101.10.161/24 brd 10.101.10.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::3c44:17ff:fe6b:cc2d/64 scope link 
       valid_lft forever preferred_lft forever

将原主节点关机,很快切换在从节点看到vip切换过来了,配置成功。
当原主节点启动后,vip又漂移回去了
看这个脚本是否生效,将两个haproxy都停掉,haproxy没有自动起来
3 防火墙

# 开启防火墙
systemctl start firewalld

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/44595.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Python】一、Python程序运行方式

文章目录实验目的一、熟悉Python的安装二、熟悉PyCharm安装三、启动IDLE&#xff0c;进入Python Shell&#xff0c;熟悉环境&#xff0c;运行教材案例&#xff1a;输出“hello world&#xff01;”四、打开“命令提示符”窗口&#xff0c;使用pip安装管理pygame、numpy等第三方…

算法训练Day30 回溯算法专题 | LeetCode332. 重新安排行程;51.N皇后(棋盘问题);37.解数独(二维的递归)

前言&#xff1a; 算法训练系列是做《代码随想录》一刷&#xff0c;个人的学习笔记和详细的解题思路&#xff0c;总共会有60篇博客来记录&#xff0c;计划用60天的时间刷完。 内容包括了面试常见的10类题目&#xff0c;分别是&#xff1a;数组&#xff0c;链表&#xff0c;哈…

android 自定义view: 跑马灯-光圈

本系列自定义View全部采用kt **系统: **mac android studio: 4.1.3 **kotlin version:**1.5.0 gradle: gradle-6.5-bin.zip 本篇效果: 前沿 最近在bilibili看到一个跑马灯光圈效果挺好, 参考着思路写了一下. bilibili地址,美中不足的是这是html代码 QaQ 实现思路 将效果…

Spark系列之Spark启动与基础使用

title: Spark系列 第三章 Spark启动与基础使用 3.1 Spark Shell 3.1.1 Spark Shell启动 安装目录的bin目录下面&#xff0c;启动命令&#xff1a; spark-shell$SPARK_HOME/bin/spark-shell \ --master spark://hadoop10:7077 \ --executor-memory 512M \ --total-executor-…

长短期记忆网络(LSTM)重点!(自己汇集了很多资料在这篇博客)

文章目录参考资料推荐基础知识评论区精髓代码实现底层实现简洁实现参考资料推荐 心心念念 学了这么久 &#xff0c;终于学到第57集了。 参考一篇掘金的图文LSTM 李宏毅老师的手撕视频配套课件 27:39 开始手撕 看完了李沐老师的LSTM又去找了李宏毅老师的课程然后发现又多了个导…

西门子机床联网

一、设备信息确认 1、确认型号 数控面板拍照确认&#xff1a; 此系统为&#xff1a;西门子828D 还有一种情况是面板无任何版本信息&#xff0c;这时就需要进入系统里面再确认。 2、确认通讯接口 1、数控面板的后面 X130为网络标号 2、其他位置 其他位置一般是前面位置用…

H3C mstp+vrrp实验 新华三杯拆解

H3C mstpvrrp实验一、实验拓扑二、实验要求局域网规划&#xff1a;可靠性&#xff1a;三、实验配置&#xff08;一&#xff09;链路聚合1.创建链路聚合组2.检查&#xff08;二&#xff09;VLAN1.创建vlan2.放行vlan3.检查&#xff08;三&#xff09;MSTP1.配置MSTP域2.配置主备…

通俗易懂的java设计模式(1)-单例模式

什么是单例模式&#xff1f; 单例模式是java中最简单的一种设计模式 需要注意的问题&#xff1a; 1.单例类有且只能有一个实例 2.单例类必须自己创建出这个实例&#xff0c;并提供给外界 那么如何自己创建实例而不让外界创建呢&#xff1f;很简单&#xff0c;我们将无参的构造函…

麦芽糖-刀豆球蛋白A,maltose-ConcanavalinA,刀豆球蛋白A-PEG-麦芽糖

麦芽糖-刀豆球蛋白A,maltose-ConcanavalinA,刀豆球蛋白A-PEG-麦芽糖 中文名称&#xff1a;麦芽糖-刀豆球蛋白A 英文名称&#xff1a;maltose-ConcanavalinA 别称&#xff1a;刀豆球蛋白A修饰麦芽糖&#xff0c;ConA-麦芽糖 还可以提供PEG接枝修饰麦芽糖&#xff0c;麦芽糖…

SpringCloud-alibaba-Nacos 从理论到落地使用

Nacos: Dynamic Naming and Configuration Service 就是&#xff1a; 一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。、 下面是生态图&#xff1a; Nacos EurekaConfig Bus 即 Nacos就是注册中心 配置中心的组合 他能干什么呢&#xff1f; 1、替代Eu…

防火墙firewalld

RHEL7中有几种防火墙共存&#xff1a;firewalld、iptables、ebtables等。基于iptables的防火墙默认不启动&#xff0c;但仍然可以继续使用。RHEL7默认使用firewalld作为防火墙&#xff0c;管理工具是firewall-cmd。RHEL7的内核版本是3.10&#xff0c;在此版本的内核里防火墙的包…

论文阅读2 Learning Hierarchy-Aware Knowledge Graph Embeddings for Link Prediction

目录 问题 创新 1、Introduction 2、相关工作 3、HAKE模型 原文&#xff1a;[1911.09419] Learning Hierarchy-Aware Knowledge Graph Embeddings for Link Prediction (arxiv.org) 问题 现有的知识图嵌入模型主要关注对称/反对称、反转和复合symmetry/antisymmetry, i…

EquiVSet

又搬来一个于最优子集的神经集合函数学习方法 集合函数被广泛应用于各种场景之中&#xff0c;例如商品推荐、异常检测和分子筛选等。在这些场景中&#xff0c;集合函数可以被视为一个评分函数&#xff1a;其将一个集合作为输入并输出该集合的分数。我们希望从给定的集合中选取…

【新知实验室-TRTC开发】实时音视频之欢度世界杯

目录 一、什么是TRTC 二、用5分钟跑通一个demo 1、开通腾讯云-TRTC 2、获取demo必须的两把钥匙 2.1输入应用名称 2.2下载对应的源码包&#xff08;手机、web、小程序等&#xff09; 2.3拿到钥匙 2.4完成 三、搭建一起看世界杯应用 1、解压源码&#xff08;耗时30S&#x…

Linux下top命令详解

Linux下top命令用法详解 作为一名Linux软件攻城狮&#xff0c;top命令大家应该并不陌生。top命令是Linux下常用的性能分析工具&#xff0c;能够实时显示系统中各个进程的资源占用状况。top可以动态显示过程,不断刷新当前状态。top命令提供了实时的对系统处理器的状态监视。它将…

Kotlin高仿微信-第5篇-主页-通讯录

Kotlin高仿微信-项目实践58篇详细讲解了各个功能点&#xff0c;包括&#xff1a;注册、登录、主页、单聊(文本、表情、语音、图片、小视频、视频通话、语音通话、红包、转账)、群聊、个人信息、朋友圈、支付服务、扫一扫、搜索好友、添加好友、开通VIP等众多功能。 Kotlin高仿…

【博客547】keepalived实现vip的原理剖析

keepalived实现vip的原理剖析 keepalived实现vip的原理&#xff1a;vrrp gratuitous arp 1、vrrp vrrp更多细节参考&#xff1a;vrrp技术白皮书 相关术语&#xff1a; VRRP工作过程 VRRP的工作过程为&#xff1a; 1、虚拟路由器中的路由器根据优先级选举出Master。Master…

【Python】五、程序循环结构

文章目录实验目的一、掌握while语句二、掌握for循环和range()内建函数三、掌握循环语句嵌套四、掌握break语句和continue语句五、编写程序实现猜数字的游戏1.设计思路2.设计算法3.参考代码4.实验截图实验目的 掌握循环结构&#xff1b;培养学生动手查阅资料能力和解决实际问题的…

matlab实现线性参数的最小二乘法处理

一、实验目的 最小二乘法原理是一种在多学科领域中获得广泛应用的数据处理方法。通过实验要求掌握最小二乘法基本原理、正规方程以及组合测量的最小二乘法处理办法。 二、实验原理 &#xff08;1&#xff09;测量结果的最可信赖值应在残余误差平方和为最小的条件下求出&#…

破局模块总结 -- 宁向东的清华管理学课总结

1. 管理学就是要破局而出 为什么需要管理学&#xff1f;德鲁克说&#xff0c;我们需要选择正确的事情去做&#xff0c;并且把事情作对。 管理学是一个分析、权衡和决策的学问&#xff0c;分析、权衡和选择的目的是什么&#xff1f;就是要破局而出。 管理學是研究人類管理活動…