CentOS7.9 Haproxy 2.2版本 L7安装及使用
IUS is a yum repository that provides newer versions of select software for RHEL and CentOS.
它为CentOS按RHEL提供最新软件版本的yum源。
1.配置IUS yum源
vim /etc/yum.repos.d/ius-7.repo
[ius]
name = IUS for Enterprise Linux 7 - $basearch
baseurl = https://repo.ius.io/7/$basearch/
enabled = 1
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
[ius-debuginfo]
name = IUS for Enterprise Linux 7 - $basearch - Debug
baseurl = https://repo.ius.io/7/$basearch/debug/
enabled = 0
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
[ius-source]
name = IUS for Enterprise Linux 7 - Source
baseurl = https://repo.ius.io/7/src/
enabled = 0
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = https://repo.ius.io/RPM-GPG-KEY-IUS-7
3. 更新缓存:yum makecache fast
4. 查看版本:yum list|grep haproxy
51 yum install httpd -y
54 echo web1/2 > /var/www/html/index.html
57 systemctl start httpd
stats enable
stats refresh 30s
stats uri /haproxystats #url后缀
stats hide-version
stats realm Haproxy\ stats
stats auth up:123 #帐号:密码
stats admin if TRUE
#此处是负载均衡后端服务器的配置
listen web
mode http
bind *:80
balance roundrobin
server http1 192.168.31.129:80 maxconn 2000 weight 1 check
server http2 192.168.31.90:80 maxconn 2000 weight 1 check
Keepalived + Haproxy
router_id haproxy1 #HA1
}
vrrp_instance VI_1 {
state MASTER #主备,主
interface enp0s3 #网卡
virtual_router_id 100 #主备保持一致
priority 100 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.31.230 #虚拟IP
}
}
router_id haproxy2
}
vrrp_instance VI_1 {
state BACKUP
interface enp0s3
virtual_router_id 100
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.31.230
}
}
[root@master ~]# ip addr
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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:85:13:94 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.6/24 brd 192.168.31.255 scope global noprefixroute dynamic enp0s3
valid_lft 42589sec preferred_lft 42589sec
inet 192.168.31.230/32 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::f9e8:5d71:e6e4:8965/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::aa2:b386:daae:3d7e/64 scope link noprefixroute
valid_lft forever preferred_lft forever
验证:
当HA master服务器故障时,客户端访问vip不会出现异常,因为keepalived会自动切换到HA backup服务器,vip会出现在backup服务器上:
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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:0a:a4:c2 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.35/24 brd 192.168.31.255 scope global noprefixroute dynamic enp0s3
valid_lft 42483sec preferred_lft 42483sec
inet 192.168.31.230/32 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::f9e8:5d71:e6e4:8965/64 scope link noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::aa2:b386:daae:3d7e/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever