当前位置:   article > 正文

部署haproxy_haproxy.service

haproxy.service

使用3台主机。lvs安装haproxy,RS1 .RS2安装apache

      LVs192.168.80.40haproxy
RS1192.168.80.20apache
RS2192.168.80.30apache
  1. [root@lvs ~]# systemctl disable --now firewalld
  2. Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
  3. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  4. [root@lvs ~]# vim /etc/selinux/config
  5. [root@lvs ~]# setenforce o
  6. //RS1 RS2也需要关闭

 

  1. [root@RS1 ~]# yum -y install httpd
  2. [root@RS2 ~]# yum -y install httpd
  3. //安装apache
  4. [root@RS1 ~]# systemctl enable --now httpd
  5. Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
  6. [root@RS1 ~]# ss -antl
  7. State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
  8. LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
  9. LISTEN 0 128 *:80 *:*
  10. LISTEN 0 128 [::]:22 [::]:*
  11. [root@RS2 ~]# systemctl enable --now httpd
  12. Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
  13. [root@RS2 ~]# ss -antl
  14. State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
  15. LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
  16. LISTEN 0 128 *:80 *:*
  17. LISTEN 0 128 [::]:22 [::]:*
  18. [root@RS1 ~]#echo 'RS1' > /var/www/html/index.html
  19. [root@RS2 ~]#echo 'RS2' > /var/www/html/index.html

访问

 接下来在lvs主机安装haproxy

  1. [root@lvs ~]# ls
  2. anaconda-ks.cfg haproxy-2.6.0.tar.gz
  3. //提前把包下下来
  4. https://github.com/haproxy/haproxy/tags
  5. //下载链接
  6. [root@lvs ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
  7. //安装依赖包
  8. [root@node2 ~]# useradd -r -M -s /sbin/nologin haproxy

 

  1. [root@lvs ~]# tar xf haproxy-2.6.0.tar.gz
  2. [root@lvs ~]# ls
  3. anaconda-ks.cfg haproxy-2.6.0
  4. [root@lvs ~]# cd haproxy-2.6.0
  5. [root@lvs haproxy-2.6.0]# make clean
  6. [root@lvs haproxy-2.6.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
  7. > TARGET=linux-glibc \
  8. > USE_OPENSSL=1 \
  9. > USE_ZLIB=1 \
  10. > USE_PCRE=1 \
  11. > USE_SYSTEMD=1
  12. [root@lvs haproxy-2.6.0]# make install PREFIX=/usr/local/haproxy
  13. //编译安装
  14. [root@lvs haproxy]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
  15. [root@lvs haproxy]# which haproxy
  16. /usr/sbin/haproxy

配置lvs

  1. [root@lvs ~]# vi /etc/sysctl.conf
  2. # sysctl settings are defined through files in
  3. # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
  4. #
  5. # Vendors settings live in /usr/lib/sysctl.d/.
  6. # To override a whole file, create a new file with the same in
  7. # /etc/sysctl.d/ and put new settings there. To override
  8. # only specific settings, add a file with a lexically later
  9. # name in /etc/sysctl.d/ and put new settings there.
  10. #
  11. # For more information, see sysctl.conf(5) and sysctl.d(5).
  12. net.ipv4.ip_nonlocal_bind = 1
  13. net.ipv4.ip_forward = 1

提供配置文件

  1. [root@lvs ~]# vim /etc/rsyslog.conf
  2. # Save boot messages also to boot.log
  3. local0.* /var/log/haproxy.log
  4. local7.* /var/log/boot.log
  5. [root@lvs ~]# systemctl enable --now rsyslog
  1. mkdir /etc/haproxy
  2. cat > /etc/haproxy/haproxy.cfg <<EOF
  3. #--------------全局配置----------------
  4. global
  5. log 127.0.0.1 local0 info
  6. #log loghost local0 info
  7. maxconn 20480
  8. #chroot /usr/local/haproxy
  9. pidfile /var/run/haproxy.pid
  10. #maxconn 4000
  11. user haproxy
  12. group haproxy
  13. daemon
  14. #---------------------------------------------------------------------
  15. #common defaults that all the 'listen' and 'backend' sections will
  16. #use if not designated in their block
  17. #---------------------------------------------------------------------
  18. defaults
  19. mode http
  20. log global
  21. option dontlognull
  22. option httpclose
  23. option httplog
  24. #option forwardfor
  25. option redispatch
  26. balance roundrobin
  27. timeout connect 10s
  28. timeout client 10s
  29. timeout server 10s
  30. timeout check 10s
  31. maxconn 60000
  32. retries 3
  33. #--------------统计页面配置------------------
  34. listen admin_stats
  35. bind 0.0.0.0:8189
  36. stats enable
  37. mode http
  38. log global
  39. stats uri /haproxy_stats
  40. stats realm Haproxy\ Statistics
  41. stats auth admin:admin
  42. #stats hide-version
  43. stats admin if TRUE
  44. stats refresh 30s
  45. #---------------web设置-----------------------
  46. listen webcluster
  47. bind 0.0.0.0:80
  48. mode http
  49. #option httpchk GET /index.html
  50. log global
  51. maxconn 3000
  52. balance roundrobin
  53. cookie SESSION_COOKIE insert indirect nocache
  54. server web01 192.168.80.20:80 check inter 2000 fall 5
  55. server web01 192.168.80.30:80 check inter 2000 fall 5
  56. #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
  57. EOF

开机自启

  1. cat > /usr/lib/systemd/system/haproxy.service <<EOF
  2. [Unit]
  3. Description=HAProxy Load Balancer
  4. After=syslog.target network.target
  5. [Service]
  6. ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
  7. ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
  8. ExecReload=/bin/kill -USR2
  9. [Install]
  10. WantedBy=multi-user.target
  11. EOF
  12. [root@node2 ~]# ss -antl
  13. State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
  14. LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
  15. LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
  16. LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
  17. LISTEN 0 128 [::]:22 [::]:*

接下来用lvs192.168.80.40/访问其他2个主机的apache

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/987923
推荐阅读
相关标签
  

闽ICP备14008679号