haproxy在线管理与维护分享

news/2024/5/19 18:13:26

相比于nginx负载均衡,haproxy有一个很好用的功能,就是可以动态的维护后端的server,而不必重启整个服务。完成这项功能需要使用到haproxy socket和socat。

1. haproxy sock

开启haproxy unix socket

  1. 在配置文件的global选项里添加:
#vim /etc/haproxy/haproxy.cfg
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
stats timeout 2m

#整个配置文件内容如下
global
    maxconn 10000
    chroot /var/lib/haproxy
    uid haproxy
    gid haproxy
    daemon
    nbproc 1 
    pidfile /var/lib/haproxy/haproxy.pid 
    log 127.0.0.1 local3 info
    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
    stats timeout 2m

defaults
    mode http
    log global
    option http-keep-alive
    maxconn 10000
    timeout connect 5000ms
    timeout client  50000ms
    timeout server 50000ms

listen stats
    mode http
    bind 0.0.0.0:8888
    stats refresh 30s
    stats enable
    stats uri     /stats 
    stats auth    haproxy:123456

frontend frontend_www_example_com
    bind 10.0.0.43:80
    mode http
    option httplog
    log global
    default_backend backend_www_example_com

backend backend_www_example_com
    option forwardfor header X-REAL-IP
    option httpchk HEAD / HTTP/1.0
    balance roundrobin
    server web-node1  10.0.0.41:8080 check inter 2000 rise 30 fall 15
    server web-node2  10.0.0.42:8080 check inter 2000 rise 30 fall 15

#修改完成后重启haroxy
systemctl restart haproxy.service
  1. 验证配置是否生效

    查看haproxy.sock文件,如果存在则说明配置成功!

[root@haproxy01 ~]# ls -l /var/lib/haproxy/haproxy.sock 
srw------- 1 root root 0 Feb 25 21:13 /var/lib/haproxy/haproxy.sock

2. Socat工具

Socat是一个多功能的网络工具,名字来由是”Socket CAT”,可以看作是netcat的N倍加强版,socat的官方网站:http://www.dest-unreach.org/socat/。 Socat是一个两个独立数据通道之间的双向数据传输继电器,这些数据通道包含文件、管道、设备、插座(Unix,IP4,IP6-raw,UPD,TCP)、SSL、SOCKS4客户端或代理CONNECT。Socat支持广播和多播、抽象Unix sockets、Linux tun/tap、GUN readline和PTY。它提供了分叉、记录和进程通信的不同模式。多个选项可用于调整socket和其渠道,Socket可以作为TCP中继(一次性或守护进程),做为一个守护进程基于socksifier,作为一个shell Unix套接字接口,作为IP6的继电器,或面向TCP的程序重定向到一个串行线。 chcket的主要特点就是在两个数据流之间建立通道,且支持众多协议和链接方式:ip、tcp、udp、ipv6、pipe、exec、system、open、proxy、openssl、socket等。

2.1 安装socat

#直接yum安装(推荐)
yum -y install socat

#编译安装
yum -y install readline-devel openssl-devel tcp_wrappers
cd /usr/local/src
wget http://www.dest-unreach.org/socat/download/socat-1.7.2.4.tar.gz
tar xf socat-1.7.2.4.tar.gz
cd socat-1.7.2.4
./configure
make
make install

#验证是否安装成功
[root@haproxy02 socat-1.7.2.4]# socat -V
socat by Gerhard Rieger - see www.dest-unreach.org
socat version 1.7.2.4 on Feb 25 2019 21:09:25

2.2 查看socat帮助

查看socat管理haproxy的命令帮助

echo "help"  | socat --stdio /var/lib/haproxy/haproxy.sock

#输出结果如下,这里就不对内容详细解释了,感兴趣的同学可以自己看下
Unknown command. Please enter one of the following commands only :
  clear counters : clear max statistics counters (add 'all' for all counters)
  clear table    : remove an entry from a table
  help           : this message
  prompt         : toggle interactive mode with prompt
  quit           : disconnect
  show info      : report information about the running process
  show pools     : report information about the memory pools usage
  show stat      : report counters for each proxy and server
  show errors    : report last request and response errors for each proxy
  show sess [id] : report the list of current sessions or dump this session
  show table [id]: report table usage stats or dump this table's contents
  get weight     : report a server's current weight
  set weight     : change a server's weight
  set server     : change a server's state or weight
  set table [id] : update or create a table entry's data
  set timeout    : change a timeout setting
  set maxconn    : change a maxconn setting
  set rate-limit : change a rate limiting value
  disable        : put a server or frontend in maintenance mode
  enable         : re-enable a server or frontend which is in maintenance mode
  shutdown       : kill a session or a frontend (eg:to release listening ports)
  show acl [id]  : report available acls or dump an acl's contents
  get acl        : reports the patterns matching a sample for an ACL
  add acl        : add acl entry
  del acl        : delete acl entry
  clear acl <id> : clear the content of this acl
  show map [id]  : report available maps or dump a map's contents
  get map        : reports the keys and values matching a sample for a map
  set map        : modify map entry
  add map        : add map entry
  del map        : delete map entry
  clear map <id> : clear the content of this map
  set ssl <stmt> : set statement for ssl

3 常见在线维护操作

3.1 查看haproxy状态

echo "show info;show stat" | socat stdio /var/lib/haproxy/haproxy.sock

haproxy在线管理与维护分享

3.2 关闭节点

echo "disable server backend_www_example_com/web-node1" | socat stdio /var/lib/haproxy/haproxy.sock

#注意,在操作后端节点时,需要使用backend模块名/节点实例的方式。

执行完disable命令后,在前端可以看到web01节点下线了,如下图:

haproxy在线管理与维护分享

3.3 启动节点

echo "enable server backend_www_example_com/web-node1" | socat stdio /var/lib/haproxy/haproxy.sock

haproxy在线管理与维护分享

根据socat功能的特性,我们可以从两方面来管理服务:

1、通过查看status,可以用zabbix对haproxy进行状态的监控;

2、通过enable和disable,可以在线调整节点,而不用去重启整个服务,在代码上线的时候非常有帮助。

分享到此结束,谢谢~

转载于:https://blog.51cto.com/13178102/2354739


http://www.niftyadmin.cn/n/4594201.html

相关文章

windows7下消除快捷键箭头的方法

&#xff08;1&#xff09;启动注册表编辑器&#xff1a;点击 开始—运行里面输入“regedit”&#xff08;2&#xff09;然后依次展开如下分支&#xff1a;“HKEY_CLASSES_ROOT\lnkfile”&#xff1b;&#xff08;3&#xff09;删除“lnkfile”子项中的“IsShortcut”字符串值项…

minigui中的Windows

最近在学习MiniGui&#xff0c;深入研究发现minigui的窗口虽然跟window差不多&#xff0c;但是还是有些不一样的东西&#xff0c;下面这篇文章写的不错。特别转过来。图形编程中&#xff0c;窗口是一个重要的概念&#xff0c;窗口其实是一个矩形框&#xff0c;应用程序可以使用…

冲动是魔鬼

天呐&#xff0c;我竟然在自学没有完成的情况下&#xff0c;一拍脑门就接下了数据库的课&#xff0c;是勇敢还是 不负责任&#xff1f;我本就是想自己学一下&#xff0c;可是正好赶上青黄不接&#xff0c;于是我就被赶鸭子上架了&#xff0c;我还以为没啥呢&#xff0c;可是上了…

NB-IoT 的“前世今生”

作者&#xff1a;个推B2D研发工程师 海晏 根据《爱立信2018移动报告》&#xff08;Ericsson Mobility Report,June 2018&#xff09;的预测&#xff0c;蜂窝物联网设备连接数将在2023年达到35亿&#xff0c;年增长率达到30%。 图片来源&#xff1a;《爱立信2018移动报告》&…

jQuery 选择器 筛选器 样式操作 文本操作 属性操作 文档处理 事件 动画效果 插件 each、data、Ajax...

jQuery jQuery介绍 1.jQuery是一个轻量级的、兼容多浏览器的JavaScript库。 2.jQuery使用户能够更方便地处理HTML Document、Events、实现动画效果、方便地进行Ajax交互&#xff0c;能够极大地简化JavaScript编程。它的宗旨就是&#xff1a;“Write less, do more.“ jQuery的优…

个人管理 - 第四代时间管理

在《个人管理 &#xff0d; 目标管理之前&#xff0c;你会时间管理吗》中讲了时间的四象限图&#xff0c;在《高效能人士的七个习惯》中的要事第一中也着重讲解了时间管理&#xff0c;本篇对第四代时间管理方法进行介绍&#xff0c;让大家对第四代时间管理方法更加清楚。 四代时…

顶嵌校园大使2010年新春获奖公告

经过一个月的激烈角逐&#xff0c;校园大使的努力也获得了相应的成效&#xff0c;通过浏览量、回复量等各项综合评审&#xff0c;获得年前奖品的同学是陈博强、周利宾和徐周昶&#xff0c;各赠送《C/C商用工程之道》一本&#xff0c;期望三位同学再接再厉继续保持你们的战线&am…

算法笔记--次小生成树 次短路 k 短路

1.次小生成树 非严格次小生成树&#xff1a;边权和小于等于最小生成树的边权和 严格次小生成树&#xff1a; 边权和小于最小生成树的边权和 算法&#xff1a;先建好最小生成树&#xff0c;然后对于每条不在最小生成树上的边&#xff08;u&#xff0c;v&#xff0c;w&#xf…