Keepalived+Nginx负载均衡+web实验
Posted 林炜玮_51CTO
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived+Nginx负载均衡+web实验相关的知识,希望对你有一定的参考价值。
实验拓扑
实验目的为了实现nginx实现负载均衡和keepalived高可用,从而保障web1和web2提供的服务
nginx172.16.10.11
安装nginx keepalived
dnf -y install nginx keepalived;systemctl enable --now nginx keepalived #172.16.10.12设备同时安装,方便远程传文件
systemctl status nginx keepalived #检查状态,有问题的请查看错误日志
配置nginx
vim /etc/nginx/nginx.conf #具体配置看下面图一
scp /etc/nginx/nginx.conf root@172.16.10.12:/etc/nginx/nginx.conf
图一:nginx.conf配置
配置keepalived
步骤梳理:
- vim /etc/keepalived/keepalived.conf
- mkdir /etc/keepalived/conf.d/ -pv
- vim /etc/keepalived/conf.d/keepalived.conf
vim /etc/keepalived/keepalived.conf
#文本内容
vrrp_gna_interval 0
! Configuration File for keepalived
global_defs
notification_email
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id ka1.linweiwei.com
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
include /etc/keepalived/conf.d/*conf
#保存退出
mkdir /etc/keepalived/conf.d/ -pv
vim /etc/keepalived/conf.d/keepalived.conf
#文本内容
vrrp_instance keepalived
state MASTER
interface eth0
virtual_router_id 61
priority 100
advert_int 1
authentication
auth_type PASS
auth_pass 123456
virtual_ipaddress
172.16.10.200/24 dev eth0 label eth0:1
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
track_script
check_down #利用VRRP-Script脚本判断应用程序的健康性
#保存退出
scp /etc/keepalived/conf.d/keepalived.conf root@172.16.10.12:/etc/keepalived/conf.d/keepalived.conf
scp /etc/keepalived/keepalived.conf root@172.16.10.11:/etc/keepalived/keepalived.conf
systemctl restart keepalived.service
[root@Centos8 keepalived]# hostname -I
172.16.10.12 172.16.10.200 #172.16.10.200的VIP出现
nginx172.16.10.12
dnf -y install nginx keepalived;systemctl enable --now nginx keepalived
systemctl status nginx keepalived #检查状态,有问题的请查看错误日志
vim /etc/keepalived/keepalived.conf
router_id ka1.linweiwei.com 改成router_id ka2.linweiwei.com
vim /etc/keepalived/conf.d/keepalived.conf
state MASTER 改成state BACKUP
virtual_router_id 61 改成virtual_router_id 62
priority 80
#保存退出
systemctl restart keepalived.service
配置web1 172.16.10.8
yum -y install httpd;systemctl enable --now httpd
echo "This is Keepalived+Nginx+web test by web1_172.16.10.8 " > /var/www/html/index.html
配置web2 172.16.10.9
yum -y install httpd;systemctl enable --now httpd
echo "This is Keepalived+Nginx+web test by web2_172.16.10.9 " > /var/www/html/index.html
配置客户机
测试结果
负载均衡web1和web2正常
高可用部分,我们模拟172.16.10.11故障关机
关机前VIP的情况
[root@Centos8 keepalived]# ip a
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:1b:67:3f brd ff:ff:ff:ff:ff:ff
inet 172.16.10.11/24 brd 172.16.10.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 172.16.10.200/24 scope global secondary eth0:1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe1b:673f/64 scope link
valid_lft forever preferred_lft forever
172.16.10.11故障关机后,172.16.10.12获得VIP,我们再看看网页情况,收官~
以上是关于Keepalived+Nginx负载均衡+web实验的主要内容,如果未能解决你的问题,请参考以下文章