# node3 # nmcli con mod ens224 ipv4.address 10.10.20.3/16 ipv4.gateway 10.10.20.2 # nmcli con down ens224 && nmcli con up ens224 # node4 # nmcli con mod ens224 ipv4.address 10.10.20.4/16 ipv4.gateway 10.10.20.2 # nmcli con down ens224 && nmcli con up ens224
# for i in {1..10}; do curl 10.80.20.2; done nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3
# for i in {1..10}; do curl 10.80.20.2; done nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.3 nginx web 10.10.20.4
lvs/dr模式配置
软件
版本
nginx
1.21.5
ipvsadm
1.31
节点
IP1
IP2
VIP
系统
功能
CPU
内存
硬盘
node1
10.80.20.1
openeuler20.03
client
2核心
4GB
20GB
node2
10.80.20.2
10.10.20.2
10.10.20.100
openeuler20.03
lvs
2核心
4GB
20GB
node3
10.80.20.3
10.10.20.3
10.10.20.100
openeuler20.03
nginx
2核心
4GB
20GB
node4
10.80.20.4
10.10.20.4
10.10.20.100
openeuler20.03
nginx
2核心
4GB
20GB
node3、node4
添加vip:
1
# nmcli con add type dummy ifname dummy2 ipv4.method manual ipv4.address 10.10.20.100/32
# for i in {1..10}; do curl 10.10.20.100; done nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3
# vim /etc/nginx/conf.d/lb.conf upstream 10.10.20.2 { server 10.10.20.3:80; server 10.10.20.4:80; } server { listen 80; server_name localhost; location / { proxy_pass http://10.10.20.2; } }
1
# nginx -s reload
node1
客户端访问:
1 2 3 4 5 6 7 8 9 10 11
# for i in {1..10}; do curl 10.10.20.2; done nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4 nginx web 10.10.20.3 nginx web 10.10.20.4
node3、node4
配置虚拟主机:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# vim /etc/nginx/conf.d/vhost.conf server { listen 0.0.0.0:80; root /data/Nginx/; server_name localhost; index index80.html; } server { listen 0.0.0.0:81; root /data/Nginx/; server_name localhost; index index81.html; } server { listen 0.0.0.0:82; root /data/Nginx/; server_name localhost; index index82.html; }
# vim /etc/nginx/conf.d/lb.conf upstream 10.10.20.2 { server 10.10.20.3:80; server 10.10.20.3:81; server 10.10.20.3:82; server 10.10.20.4:80; server 10.10.20.4:81; server 10.10.20.4:82; } server { listen 80; server_name localhost; location / { proxy_pass http://10.10.20.2; } }
1
# nginx -s reload
node1
客户端访问测试:
1 2 3 4 5 6 7 8 9 10 11
# for i in {1..10}; do curl 10.10.20.2; done hello 10.10.20.3:80 hello 10.10.20.3:81 hello 10.10.20.3:82 hello 10.10.20.4:80 hello 10.10.20.4:81 hello 10.10.20.4:82 hello 10.10.20.3:80 hello 10.10.20.3:81 hello 10.10.20.3:82 hello 10.10.20.4:80
node2
负载均衡配置ip hash算法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# vim /etc/nginx/conf.d/lb.conf upstream 10.10.20.2 { ip_hash; server 10.10.20.3:80; server 10.10.20.3:81; server 10.10.20.3:82; server 10.10.20.4:80; server 10.10.20.4:81; server 10.10.20.4:82; } server { listen 80; server_name localhost; location / { proxy_pass http://10.10.20.2; } }
1
# nginx -s reload
node1
客户端访问测试:
1 2 3 4 5 6 7 8 9 10 11
# for i in {1..10}; do curl 10.10.20.2; done hello 10.10.20.4:82 hello 10.10.20.4:82 hello 10.10.20.4:82 hello 10.10.20.4:82 hello 10.10.20.4:82 hello 10.10.20.4:82 hello 10.10.20.4:82 hello 10.10.20.4:82 hello 10.10.20.4:82 hello 10.10.20.4:82
# vim /etc/nginx/conf.d/lb.conf upstream 10.10.20.2 { server 10.10.20.3:80; server 10.10.20.3:81; server 10.10.20.3:82; server 10.10.20.4:80; server 10.10.20.4:81; server 10.10.20.4:82; } server { listen 80; server_name localhost; location / { proxy_pass http://10.10.20.2; } }
1
# nginx -s reload
node1
客户端访问测试:
1 2 3 4 5 6 7 8 9 10 11
# for i in {1..10}; do curl 10.10.20.2/test.txt; done 10.10.20.3 generic hash 10.10.20.3 generic hash 10.10.20.3 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.3 generic hash 10.10.20.3 generic hash 10.10.20.3 generic hash 10.10.20.4 generic hash
node2
负载均衡配置generic hash算法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# vim /etc/nginx/conf.d/lb.conf upstream 10.10.20.2 { hash $request_uri; server 10.10.20.3:80; server 10.10.20.3:81; server 10.10.20.3:82; server 10.10.20.4:80; server 10.10.20.4:81; server 10.10.20.4:82; } server { listen 80; server_name localhost; location / { proxy_pass http://10.10.20.2; } }
1
# nginx -s reload
node1
客户端访问测试:
1 2 3 4 5 6 7 8 9 10 11
# for i in {1..10}; do curl 10.10.20.2/test.txt; done 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash
node2
负载均衡配置random算法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# vim /etc/nginx/conf.d/lb.conf upstream 10.10.20.2 { random two; server 10.10.20.3:80; server 10.10.20.3:81; server 10.10.20.3:82; server 10.10.20.4:80; server 10.10.20.4:81; server 10.10.20.4:82; } server { listen 80; server_name localhost; location / { proxy_pass http://10.10.20.2; } }
1
# nginx -s reload
node1
客户端访问测试:
1 2 3 4 5 6 7 8 9 10 11
# for i in {1..10}; do curl 10.10.20.2; done hello 10.10.20.4:81 hello 10.10.20.4:80 hello 10.10.20.3:82 hello 10.10.20.3:82 hello 10.10.20.4:81 hello 10.10.20.4:81 hello 10.10.20.3:81 hello 10.10.20.3:82 hello 10.10.20.4:81 hello 10.10.20.4:80
node2
负载均衡配置权重和备份:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# vim /etc/nginx/conf.d/lb.conf upstream 10.10.20.2 { server 10.10.20.3:80 weight=2; server 10.10.20.3:81; server 10.10.20.3:82; server 10.10.20.4:80 backup; server 10.10.20.4:81 backup; server 10.10.20.4:82 backup; } server { listen 80; server_name localhost; location / { proxy_pass http://10.10.20.2; } }
1
# nginx -s reload
node1
客户端访问测试:
1 2 3 4 5 6 7 8 9 10 11
# for i in {1..10}; do curl 10.10.20.2; done hello 10.10.20.3:82 hello 10.10.20.3:80 hello 10.10.20.3:80 hello 10.10.20.3:81 hello 10.10.20.3:82 hello 10.10.20.3:80 hello 10.10.20.3:80 hello 10.10.20.3:81 hello 10.10.20.3:82 hello 10.10.20.3:80
node3
停止nginx:
1
# systemctl stop nginx
node1
客户端访问测试:
1 2 3 4 5 6 7 8 9 10 11
# for i in {1..10}; do curl 10.10.20.2; done hello 10.10.20.4:80 hello 10.10.20.4:81 hello 10.10.20.4:82 hello 10.10.20.4:80 hello 10.10.20.4:81 hello 10.10.20.4:82 hello 10.10.20.4:80 hello 10.10.20.4:81 hello 10.10.20.4:82 hello 10.10.20.4:80
# vim /etc/haproxy/haproxy.cfg # 33~35行,修改配置 frontend main bind *:80 acl test url_reg -i \.txt$ use_backend test if test default_backend http_back # 尾行,添加配置 backend test balance roundrobin server test1 10.10.20.3:80/test.txt check server test2 10.10.20.4:80/test.txt check
1
# systemctl restart rsyslog
node1
客户端访问测试:
1 2 3 4 5 6 7 8 9 10 11
# for i in {1..10}; do curl 10.80.20.2/test.txt; done 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.3 generic hash 10.10.20.3 generic hash 10.10.20.3 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.4 generic hash 10.10.20.3 generic hash 10.10.20.3 generic hash