负载均衡---haproxy

港控/mmm° 2023-03-01 12:50 13阅读 0赞

haproxy简介

HAProxy特别适用于那些负载特大的web站点(中型企业),这些站点通常又需要会话保持或七层处理

HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

   

haproxy配置

  1. 准备环境
  2. [root@zyy180 haproxy-2.1.3]# yum -y install openssl-devel pcre-devel gcc gcc-c++ systemd-devel
  3. 压缩软件包
  4. [root@zyy180 ~]# tar xf haproxy-2.1.3.tar.gz
  5. 1.创建用户
  6. useradd -r -M -s /sbin/nologin haproxy
  7. 2.进入软件包编译安装
  8. [root@zyy180 ~]# cd haproxy-2.1.3
  9. [root@zyy180 haproxy-2.1.3]#make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1
  10. [root@zyy180 haproxy-2.1.3]# make install
  11. 3.配置各个负载的内核参数
  12. [root@zyy180 haproxy-2.1.3]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
  13. [root@zyy180 haproxy-2.1.3]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
  14. [root@zyy180 haproxy-2.1.3]# sysctl -p
  15. net.ipv4.ip_nonlocal_bind = 1
  16. net.ipv4.ip_forward = 1
  17. 4.创建haproxy的配置文件
  18. [root@zyy180 haproxy-2.1.3]# mkdir /etc/haproxy
  19. cat > /etc/haproxy/haproxy.cfg <<EOF
  20. #--------------全局配置----------------
  21. global
  22. log 127.0.0.1 local0 info
  23. #log loghost local0 info
  24. maxconn 20480
  25. #chroot /usr/local/haproxy
  26. pidfile /var/run/haproxy.pid
  27. #maxconn 4000
  28. user haproxy ##用户名
  29. group haproxy
  30. daemon
  31. #---------------------------------------------------------------------
  32. #common defaults that all the 'listen' and 'backend' sections will
  33. #use if not designated in their block
  34. #---------------------------------------------------------------------
  35. defaults
  36. mode http
  37. log global
  38. option dontlognull
  39. option httpclose
  40. option httplog
  41. #option forwardfor
  42. option redispatch
  43. balance roundrobin
  44. timeout connect 10s
  45. timeout client 10s
  46. timeout server 10s
  47. timeout check 10s
  48. maxconn 60000
  49. retries 3
  50. #--------------统计页面配置------------------
  51. listen admin_stats
  52. bind 0.0.0.0:8189
  53. stats enable
  54. mode http
  55. log global
  56. stats uri /haproxy_stats ##网页界面
  57. stats realm Haproxy\ Statistics
  58. stats auth admin:admin
  59. #stats hide-version
  60. stats admin if TRUE
  61. stats refresh 30s
  62. #---------------web设置-----------------------
  63. listen webcluster
  64. bind 0.0.0.0:80 ##想添加其他服务直接这一段,端口改下就好
  65. mode http
  66. #option httpchk GET /index.html
  67. log global
  68. maxconn 3000
  69. balance roundrobin
  70. cookie SESSION_COOKIE insert indirect nocache
  71. server web01 172.16.103.253:80 check inter 2000 fall 5 ##添加主机
  72. server web02 172.16.103.254:80 check inter 2000 fall 5
  73. #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
  74. EOF
  75. 5.创建启动服务脚本
  76. [root@zyy180 haproxy-2.1.3]# vim /usr/lib/systemd/system/haproxy.service
  77. [Unit]
  78. Description=HAProxy Load Balancer
  79. After=syslog.target network.target
  80. [Service]
  81. ExecStartPre=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
  82. ExecStart=/usr/local/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
  83. ExecReload=/bin/kill -USR2
  84. [Install]
  85. WantedBy=multi-user.target
  86. 6.启动服务
  87. [root@zyy180 haproxy-2.1.3]# systemctl daemon-reload
  88. [root@zyy180 haproxy-2.1.3]# systemctl enable --now haproxy
  89. [root@zyy180 haproxy-2.1.3]# systemctl start --now haproxy
  90. [root@zyy180 haproxy-2.1.3]# ss -anlt
  91. State Recv-Q Send-Q Local Address:Port Peer Address:Port
  92. LISTEN 0 128 *:80 *:*
  93. LISTEN 0 128 *:22 *:*
  94. LISTEN 0 100 127.0.0.1:25 *:*
  95. LISTEN 0 128 *:8189 *:*
  96. LISTEN 0 128 :::22 :::*
  97. LISTEN 0 100 ::1:25 :::*

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,13人围观)

还没有评论,来说两句吧...

相关阅读