socat管理haproxy以及haproxy調優


Unix套接字命令(Unix Socket commands)
socat是一個多功能的網絡工具,名字來由是“Socket CAT”,可以看作是netcat的N倍加強版,socat的官方網站:http://www.dest-unreach.org/socat/ 。
socat是一個兩個獨立數據通道之間的雙向數據傳輸的繼電器。
這些數據通道包含文件、管道、設備(終端或調制解調器等)、插座(Unix,IP4,IP6 - raw,UDP,TCP)、SSL、SOCKS4客戶端或代理CONNECT。
socat支持廣播和多播、抽象Unix sockets、Linux tun/tap、GNU readline和PTY。
它提供了分叉、記錄和進程間通信的不同模式。多個選項可用於調整socat和其渠道,Socat可以作為TCP中繼(一次性或守護進程),作為一個守護進程基於socksifier,
作為一個shell Unix套接字接口,作為IP6的繼電器,或面向TCP的程序重定向到一個串行線。
socat的主要特點就是在兩個數據流之間建立通道;且支持眾多協議和鏈接方式:ip, tcp, udp, ipv6, pipe,exec,system,open,proxy,openssl,socket等。
使用socat可以查看和設置HAProxy狀態,首先得讓HAProxy產生出一個sock出來(hatop ,socat都是基於這個的,沒這個什么都做不了)。
設置配置文件開啟unix socket
在global 下面 加一行:
stats socket /usr/local/haproxy/stats #路徑和名字隨意
然后重啟服務就可以了。

 

 

配置文件加入socket這行
權限600,級別admin

[root@linux-node1 ~]# cat /etc/haproxy/haproxy.cfg 
global
    chroot  /var/lib/haproxy
    daemon
    group  haproxy
    user  haproxy
    log  127.0.0.1:514  local3  info
    stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
    stats timeout 2m
defaults
    log	global
    mode	http
   option	httplog
   option  dontlognull
   timeout  client	50000
   timeout  server	50000
   timeout	connect	5000

frontend http_front
   mode  http
   bind    *:80
   stats	uri	/haproxy?stats
   default_backend http_back
backend http_back
   option forwardfor header X-REAL-IP
   #option httpchk  GET /index.html 
   balance  roundrobin
   server	linux-node1	10.0.1.105:8080 #   check inter 2000 rise 3 fall 3 weight 1
   server	linux-node2	10.0.1.106:8080	#   check inter 2000 rise 3 fall 3 weight 1
[root@linux-node1 ~]# 

  

重啟服務

[root@linux-node1 ~]# /etc/init.d/haproxy restart
Restarting haproxy (via systemctl):                        [  確定  ]
[root@linux-node1 ~]# 
[root@linux-node1 ~]# 
[root@linux-node1 ~]# lsof -i:80
COMMAND   PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
haproxy 28420 haproxy    5u  IPv4 216721      0t0  TCP *:http (LISTEN)
[root@linux-node1 ~]# 

  

查看有沒有生成socket

[root@linux-node1 ~]# ls /var/lib/haproxy/
haproxy.sock
[root@linux-node1 ~]# 
 
通過socat和socket通信,它是cocket cat的縮寫,安裝
[root@linux-node1 ~]# yum list | grep socat
socat.x86_64                            1.7.2.2-5.el7                  base     
[root@linux-node1 ~]# yum install -y socat

  

利用管道查看幫助命令

[root@linux-node1 ~]# 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 backend   : list backends in the current running config
  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
  show servers state [id]: dump volatile server information (for backend <id>)
  get weight     : report a server's current weight
  set weight     : change a server's weight
  set server     : change a server's state, weight or address
  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 avalaible 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 avalaible 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

[root@linux-node1 ~]# 

上面把支持的命令都顯示出來了
下面就把頁面監控上的東西都列出來了

[root@linux-node1 ~]# echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock 
Name: HAProxy
Version: 1.6.3
Release_date: 2015/12/25
Nbproc: 1
Process_num: 1
Pid: 28420
Uptime: 0d 0h09m43s
Uptime_sec: 583
Memmax_MB: 0
Ulimit-n: 4031
Maxsock: 4031
Maxconn: 2000
Hard_maxconn: 2000
CurrConns: 0
CumConns: 2
CumReq: 2
Maxpipes: 0
PipesUsed: 0
PipesFree: 0
ConnRate: 0
ConnRateLimit: 0
MaxConnRate: 0
SessRate: 0
SessRateLimit: 0
MaxSessRate: 0
CompressBpsIn: 0
CompressBpsOut: 0
CompressBpsRateLim: 0
Tasks: 5
Run_queue: 1
Idle_pct: 100
node: linux-node1.example.com
description: 

[root@linux-node1 ~]# 

通過disable或者enable可以關閉或者啟動某台主機
准備把linux-node2關閉了

 

這里的主機名和頁面顯示的以及配置文件配置的一致

[root@linux-node1 ~]# echo "disable server linux-node2" | socat stdio /var/lib/haproxy/haproxy.sock 
Require 'backend/server'.

  

格式如下。 backend/server
[root@linux-node1 ~]# echo "disable server http_back/linux-node2" | socat stdio /var/lib/haproxy/haproxy.sock 

[root@linux-node1 ~]# 

  

再次刷新頁面就變成維護狀態了

啟動之后,恢復正常
[root@linux-node1 ~]# echo "enable server http_back/linux-node2" | socat stdio /var/lib/haproxy/haproxy.sock 

目前還無法通過命令新增節點

  

 

 haproxy調優的地方

1、不設置進程,默認就是1,單進程
2、網卡可能跑慢,換成萬兆網卡,或者拆業務,拆成不同集群
3、haproxy的端口可能被用光,因為linux提供端口最多65535。
(1)改local的端口范圍。
[root@linux-node1 ~]# cat /proc/sys/net/ipv4/ip_local_port_range 
32768	60999
[root@linux-node1 ~]# 
(2)改tcp的tw端口的復用,啟用reuse,禁用recycle,改成1 
[root@linux-node1 ~]# cat /proc/sys/net/ipv4/tcp_tw_reuse 
0
[root@linux-node1 ~]# 
(3)可以縮短tw的時間,默認是60秒。
[root@linux-node1 ~]# cat /proc/sys/net/ipv4/tcp_fin_timeout 
60
[root@linux-node1 ~]# 
(4)使用多個IP

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM