原文链接:https://www.cnblogs.com/mmdln/p/8988951.html
高可用有2中方式。
1、Nginx+keepalived 主从配置
这种方案,使用一个vip地址,前端使用2台机器,一台做主,一台做备,但同时只有一台机器工作,另一台备份机器在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案不经济实惠。
2、Nginx+keepalived 双主配置
这种方案,使用两个vip地址,前端使用2台机器,互为主备,同时有两台机器工作,当其中一台机器出现故障,两台机器的请求转移到一台机器负担,非常适合于当前架构环境。
1、Nginx+keepalived 主从配置
1.环境如下
lb-01:192.168.96.130 nginx+keepalived-master lb-02:192.168.96.131 nginx+keepalived-backup VIP:192.168.75.135
nginx+tomcat负载均衡集群参照 nginx+tomcat负载均衡集群参照
两台机器都要安装nginx 配置文件相同
- <span style="font-size:18px;">upstream myServer{
- server 192.168.96.130:8080;
- server 192.168.96.131:8080;
- }
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- proxy_pass http://myServer;
- }
- span>
nginx结合keepalived高可用
为什么使用keepalived呢?使用keepalived就用来做高可用的,提供虚拟VIP
分别在2台机子上安装keepalived
# yum install keepalived -y
查看keepalived版本
# keepalived -v Keepalived v1.2.13 (11/20,2015)
关于2台keepalived配置文件/etc/keepalived
在130master keepalived配置文件内容如下
- <span style="font-size:18px;">global_defs {
- notification_email {
- [email protected]
- [email protected]
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_script chk_http_port {
- script "/usr/local/src/check_nginx_pid.sh"
- interval 2 #(检测脚本执行的间隔)
- weight 2
- }
- vrrp_instance VI_1 {
- state MASTER # 备份服务器上将 MASTER 改为 BACKUP
- interface eno16777736 //网卡
- virtual_router_id 51 # 主、备机的virtual_router_id必须相同
- priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.96.138 // VRRP H虚拟地址
- }
- }
- span>
在131-backup keepalived配置文件内容如下
- <span style="font-size:18px;">global_defs {
- notification_email {
- [email protected]
- [email protected]
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_script chk_http_port {
- script "/usr/local/src/check_nginx_pid.sh"
- interval 2 #(检测脚本执行的间隔)
- weight 2
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eno16777736
- virtual_router_id 51
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.96.138
- }
- }span>
然后分别启动2台keepalived服务
[root@lb-01 ~]# systemctl start keepalived.service [root@lb-02 ~]# systemctl start keepalived.service
查看虚拟VIP
130-master机器查看
从上面可以看到虚拟VIP
地址192.168.96.138
131-backup机器查看
从上面可以看到没有虚拟VIP
地址
测试访问虚拟VIP
此时虚拟VIP可以轮询访问了
模拟故障
把130-master nginx和keepalived停止查看是否还能正常提供服务
[root@lb-01 sbin]# ./nginx -s stop [root@lb-01 sbin]# systemctl stop keepalived.service
此时虚拟VIP已经不再master上了
客户端打开浏览器访问是否正常访问
130nginx和keepalived挂了也不影响服务
查看131backup机器VIP情况
此时虚拟VIP 已经在131机器上
那么如何实现nginx+keepalived双主模式呢?
1.其实只是需要更改下keepalived配置文件即可,配置文件实例如下
增加新的VIP192.168.96.139
,192.168.75.138
是130机器上主虚拟VIP,192.168.96.139
是131机器上主虚拟VIP
130的keepalived配置文件内容如下
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- [email protected]
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_script chk_http_port {
- script "/usr/local/src/check_nginx_pid.sh"
- interval 2 #(检测脚本执行的间隔)
- weight 2
- }
- vrrp_instance VI_1 {
- state MASTER
- interface eno16777736
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.96.138
- }
- }
- vrrp_instance VI_2 {
- state BACKUP
- interface eno16777736
- virtual_router_id 52
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.96.139
- }
- }
131的keepalived配置文件内容如下
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- [email protected]
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 192.168.200.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_script chk_http_port {
- script "/usr/local/src/check_nginx_pid.sh"
- interval 2 #(检测脚本执行的间隔)
- weight 2
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eno16777736
- virtual_router_id 51
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.96.138
- }
- }
- vrrp_instance VI_2 {
- state MASTER
- interface eno16777736
- virtual_router_id 52
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.96.139
- }
- }
分别启动2台lb上nginx和keepalived服务
[root@lb-01 sbin]# ./nginx [root@lb-01 sbin]# systemctl start keepalived.service
130查看虚拟ip
131查看虚拟ip
客户端测试访问虚拟VIP
访问虚拟VIP:192.168.96.138结果如下
访问虚拟VIP:192.168.96.139结果如下
模拟故障
把130服务停止
查看130虚拟VIP是否存在结果如下
没了
测试访问虚拟VIP**
从上面结果可以看到,即使130机器发生了故障也不影响使用,这样也利用131资源了
此时查看一下131虚拟ip结果情况如下
注:此时虚拟VIP地址都已经在lb-02机器上了