haproxy

haproxy安装

haproxy——192.168.2.128
RS1——192.168.2.129
RS2——192.168.2.131

#前期准备
//haproxy
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   9275      0 --:--:-- --:--:-- --:--:--  9240
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache
[root@localhost yum.repos.d]# cd
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# vi /etc/selinux/config
[root@localhost ~]# yum -y install vim wget
Complete!
[root@localhost ~]# hostnamectl set-hostname haproxy
[root@localhost ~]# bash
[root@haproxy ~]# 

//RS1
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   4586      0 --:--:-- --:--:-- --:--:--  4586
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache
[root@localhost yum.repos.d]# cd
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# vi /etc/selinux/config 
[root@localhost ~]# hostnamectl set-hostname RS1
[root@localhost ~]# bash
[root@RS1 ~]# yum -y install httpd
Complete!
[root@RS1 ~]# echo 'RS1' > /var/www/html/index.html
[root@RS1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS1 ~]# ss -antl
State   Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:22            0.0.0.0:*              
LISTEN  0        128                 [::]:22               [::]:*              
LISTEN  0        128                    *:80                  *:*              
[root@RS1 ~]# 

//RS2
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
[root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   7920      0 --:--:-- --:--:-- --:--:--  7945
[root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache
[root@localhost yum.repos.d]# cd
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# vi /etc/selinux/config 
[root@localhost ~]# hostnamectl set-hostname RS2
[root@localhost ~]# bash
[root@RS2 ~]# yum -y install httpd
Complete!
[root@RS2 ~]# echo 'RS2' > /var/www/html/index.html
[root@RS2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@RS2 ~]# ss -antl
State   Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process  
LISTEN  0        128              0.0.0.0:22            0.0.0.0:*              
LISTEN  0        128                    *:80                  *:*              
LISTEN  0        128                 [::]:22               [::]:*              
[root@RS2 ~]# 

 

 下载地址

 

#安装
[root@haproxy ~]# wget https://github.com/haproxy/haproxy/archive/refs/tags/v2.6.0.tar.gz
[root@haproxy ~]# ls
anaconda-ks.cfg  Downloads             Pictures   v2.6.0.tar.gz
Desktop          initial-setup-ks.cfg  Public     Videos
Documents        Music                 Templates
[root@haproxy ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
[root@haproxy ~]# useradd -r -M -s /sbin/nologin haproxy
[root@haproxy ~]# tar xf v2.6.0.tar.gz 
[root@haproxy ~]# ls
anaconda-ks.cfg  Downloads             Music     Templates
Desktop          haproxy-2.6.0         Pictures  v2.6.0.tar.gz
Documents        initial-setup-ks.cfg  Public    Videos
[root@haproxy ~]# cd haproxy-2.6.0/
[root@haproxy haproxy-2.6.0]# ls      //在INSTALL和README里查看如何部署安装
addons    CHANGELOG     doc       INSTALL      Makefile   scripts  tests
admin     CONTRIBUTING  examples  LICENSE      README     src      VERDATE
BRANCHES  dev           include   MAINTAINERS  reg-tests  SUBVERS  VERSION


[root@haproxy haproxy-2.6.0]# make clean
[root@haproxy haproxy-2.6.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
> TARGET=linux-glibc  \
> USE_OPENSSL=1  \
> USE_ZLIB=1  \
> USE_PCRE=1  \
> USE_SYSTEMD=1
[root@haproxy haproxy-2.6.0]# make install PREFIX=/usr/local/haproxy
[root@haproxy haproxy-2.6.0]# ls /usr/local
bin  etc  games  haproxy  include  lib  lib64  libexec  sbin  share  src
[root@haproxy haproxy-2.6.0]# ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@haproxy haproxy-2.6.0]# which haproxy
/usr/sbin/haproxy

 

配置各个负载的内核参数

[root@haproxy ~]# vim /etc/sysctl.conf 
net.ipv4.ip_nonlocal_bind = 1      //可以用没有绑定的虚拟IP
net.ipv4.ip_forward = 1       //在最后加这两行

//生效
[root@haproxy ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

 

提供配置文件

[root@haproxy ~]# mkdir -p /etc/haproxy
[root@haproxy ~]# cd /etc/haproxy/
[root@haproxy haproxy]# ls
[root@haproxy haproxy]# cat > /etc/haproxy/haproxy.cfg <<EOF
> #--------------全局配置----------------
> global
>     log 127.0.0.1 local0  info
>     #log loghost local0 info
>     maxconn 20480
> #chroot /usr/local/haproxy
>     pidfile /var/run/haproxy.pid
>     #maxconn 4000
>     user haproxy
>     group haproxy
>     daemon
> #---------------------------------------------------------------------
> #common defaults that all the 'listen' and 'backend' sections will
> #use if not designated in their block
> #---------------------------------------------------------------------
> defaults
>     mode http
>     log global
>     option dontlognull
>     option httpclose
>     option httplog
>     #option forwardfor
>     option redispatch
>     balance roundrobin
>     timeout connect 10s
>     timeout client 10s
>     timeout server 10s
>     timeout check 10s
>     maxconn 60000
>     retries 3
> #--------------统计页面配置------------------
> listen admin_stats
>     bind 0.0.0.0:8189
>     stats enable
>     mode http
>     log global
>     stats uri /haproxy_stats
>     stats realm Haproxy\ Statistics
>     stats auth admin:admin
>     #stats hide-version
>     stats admin if TRUE
>     stats refresh 30s
> #---------------web设置-----------------------
> listen webcluster
>     bind 0.0.0.0:80
>     mode http
>     #option httpchk GET /index.html
>     log global
>     maxconn 3000
>     balance roundrobin
>     cookie SESSION_COOKIE insert indirect nocache
>     server web01 192.168.2.129:80 check inter 2000 fall 5   //RS1
>     server web02 192.168.2.131:80 check inter 2000 fall 5   //RS2
>     #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
> EOF
[root@haproxy haproxy]# ls
haproxy.cfg

//上面的haproxy.cfg文件里的内容可以在这个文件夹里的cfg内容查找怎么写,用less查看
[root@haproxy ~]# ls haproxy-2.6.0/examples     
basic-config-edge.cfg  haproxy.init           socks4.cfg
content-sw-sample.cfg  option-http_proxy.cfg  transparent_proxy.cfg
errorfiles             quick-test.cfg         wurfl-example.cfg

haproxy.service文件编写

[root@haproxy ~]# cd /etc/haproxy/
[root@haproxy haproxy]# ls
haproxy.cfg
[root@haproxy haproxy]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
> [Unit]
> Description=HAProxy Load Balancer
> After=syslog.target network.target
> 
> [Service]
> ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg   -c -q
> ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid
> ExecReload=/bin/kill -USR2 $MAINPID
> 
> [Install]
> WantedBy=multi-user.target
> EOF

启用日志

[root@haproxy ~]# cd haproxy-2.6.0/
[root@haproxy haproxy-2.6.0]# vim /etc/rsyslog.conf 
local0.*       /var/log/haproxy.log      //添加这个内容
local7.*                                                /var/log/boot.log
[root@haproxy haproxy-2.6.0]# systemctl enable --now rsyslog
[root@haproxy haproxy-2.6.0]# systemctl status rsyslog
● rsyslog.service - System Logging Service
   Loaded: loaded (/usr/lib/systemd/system/rsyslog.service; enabled; vendor pr>
   Active: active (running) since Mon 2022-08-15 18:17:06 CST; 1h 14min ago
     Docs: man:rsyslogd(8)
           https://www.rsyslog.com/doc/
 Main PID: 1429 (rsyslogd)
    Tasks: 3 (limit: 11088)
   Memory: 3.0M
   CGroup: /system.slice/rsyslog.service
           └─1429 /usr/sbin/rsyslogd -n

Aug 15 18:17:06 localhost.localdomain systemd[1]: Starting System Logging Serv>
Aug 15 18:17:06 localhost.localdomain rsyslogd[1429]: [origin software="rsyslo>
Aug 15 18:17:06 localhost.localdomain systemd[1]: Started System Logging Servi>
Aug 15 18:17:06 localhost.localdomain rsyslogd[1429]: imjournal: journal files>
[root@haproxy haproxy-2.6.0]# systemctl restart rsyslog

启动服务

[root@haproxy haproxy]# systemctl daemon-reload
[root@haproxy ~]# systemctl enable --now haproxy
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
[root@haproxy ~]# ss -antl
State   Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process           
LISTEN  0        128              0.0.0.0:22            0.0.0.0:*                         
LISTEN  0        128              0.0.0.0:8189          0.0.0.0:*                           
LISTEN  0        128              0.0.0.0:80            0.0.0.0:*              
LISTEN  0        128                 [::]:22               [::]:*              
 
[root@haproxy ~]# ip a     //192.168.2.128此IP为VIP
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:44:3d:96 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.128/24 brd 192.168.2.255 scope global dynamic noprefixroute ens160
       valid_lft 1282sec preferred_lft 1282sec
    inet6 fe80::20c:29ff:fe44:3d96/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:a1:e1:1c brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:a1:e1:1c brd ff:ff:ff:ff:ff:ff

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值