本文主要是介绍Linux:keepalived实现nginx高可用(三台nginx),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
keepalived实现nginx高可用
之前发过一篇使用keepalived实现nginx负载均衡的高可用,当时使用的是两台nginx,这次使用三台nginx来做个实验。
环境
zabbix-server 192.168.186.10
zabbix-agent-1 192.168.186.11
zabbix-agent-2 192.168.186.12
VIP 192.168.186.100
(刚好在做zabbix,所以直接在zabbix的机器上安装几个nginx进行测试)
安装nginx(所有机器)
[root@zabbix-server ~]# systemctl -y install nginx
[root@zabbix-agent-1 ~]# systemctl -y install nginx
[root@zabbix-agent-2 ~]# systemctl -y install nginx
修改配置文件(所有nginx)(非必须)
【因为我的机器上的80端口已经被使用了,所以我改一下端口】
...
listen 8880 default_server;listen [::]:8880 default_server;server_name _;root /usr/share/nginx/html;
...
修改nginx默认网页内容
[root@zabbix-server ~]# echo `hostname` > /usr/share/nginx/html/index.html
[root@zabbix-agent-1 ~]# echo `hostname` > /usr/share/nginx/html/index.html
[root@zabbix-agent-2 ~]# echo `hostname` > /usr/share/nginx/html/index.html
启动nginx服务并测试
[root@zabbix-server ~]# systemctl start nginx
[root@zabbix-agent-1 ~]# systemctl start nginx
[root@zabbix-agent-2 ~]# systemctl start nginx[root@zabbix-server ~]# curl 127.0.0.1:8880
zabbix-server
[root@zabbix-agent-1 ~]# curl 127.0.0.1:8880
zabbix-agent-1
[root@zabbix-agent-2 ~]# curl 127.0.0.1:8880
zabbix-agent-2
安装keepalived(所有机器)
[root@zabbix-server ~]# systemctl -y install keepalived
[root@zabbix-agent-1 ~]# systemctl -y install keepalived
[root@zabbix-agent-2 ~]# systemctl -y install keepalived
配置keepalived(所有机器都一样)
! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addr
# vrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0
}
vrrp_script nginx {script "/keepalived/curl_nginx.sh"interval 3weight -30fall 2rise 1
}
vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 100advert_int 1nopreemptauthentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.186.100}track_script {nginx}
}
#之前配置两台keepalived的时候,只需要在某一台配置检测脚本,现在配置三台,直接把所有配置文件都设置成一样的,这样可以实现三台ngnix之间的服务可以自由切换。
检测脚本(所有机器都要有)
[root@zabbix-server ~]# mkdir /keepalived/
[root@zabbix-server ~]# vim /keepalived/curl_nginx.sh
#!/bin/bash
curl 127.0.0.1:8880||systemctl stop keepalived
[root@zabbix-server ~]# chmod +x /keepalived/curl_nginx.sh -R
#脚本比较简陋,实际生产中不要使用这种脚本,测试可以使用
启动所有keepalived服务
[root@zabbix-server ~]# systemctl start keepalived.service
[root@zabbix-agent-1 ~]# systemctl start keepalived.service
[root@zabbix-agent-2 ~]# systemctl start keepalived.service
测试
[root@zabbix-server ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope hostvalid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:0c:29:fe:92:0c brd ff:ff:ff:ff:ff:ffinet 192.168.186.10/24 brd 192.168.186.255 scope global ens33valid_lft forever preferred_lft foreverinet 192.168.186.100/32 scope global ens33valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fefe:920c/64 scope linkvalid_lft forever preferred_lft forever
#VIP现在在master上
[root@zabbix-server ~]# curl 192.168.186.100:8880
zabbix-server
#访问VIP,可以看出,确实是访问到了server的nginx上。
#关掉server上的nginx,再次访问。
[root@zabbix-server ~]# systemctl stop nginx
[root@zabbix-server ~]# curl 192.168.186.100:8880
curl: (7) Failed connect to 192.168.186.100:8880; Connection refused
[root@zabbix-server ~]# curl 192.168.186.100:8880
zabbix-agent-2
#可以看到,在VIP飘移的过程中,有一小段时间服务不可用
#后面VIP成功飘移到agent-2上
#反复对所有机器上的nginx进行启动关闭,可以看到服务一直都是可用的(除了切换VIP的时间)
#注意:重启nginx后,也要手动启动keepalived。
实验完毕!
实际上不会这么使用,上述场景只是做个实验。
这篇关于Linux:keepalived实现nginx高可用(三台nginx)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!