• Nginx+Keepalived


    1.  规划环境:

    主机名称
    服务器名称
    IP
    用途
    centosnode1
    Nginx_Master
    192.168.188.223
    提供负载均衡
    centosnode2
    Nginx_Backup
    192.168.188.226
    提供负载均衡
    VIP
    192.168.188.100
    网站的 VIP 地址
    centosnode3
    Web1 服务器
    192.168.188.227
    提供 Web 服务
    centosnode4
    Web2 服务器
    192.168.188.228
    提供 Web 服务

    2.  配置Nginx负载均衡,这里通过子配置文件进行配置:

            1>.  Nginx_Master服务器Nginx是通过源码安装,配置文件在/usr/local/nginx/conf/nginx.conf

            2>.  Nginx_Backup服务器Nginx使用过yum源安装,配置文件在/etc/nginx/nginx.conf

    1. ##Nginx_Master服务器Nginx配置
    2. [root@centosnode1 ~]# vim /usr/local/nginx/conf.d/lb2.conf
    3. upstream websrvs {
    4. server 192.168.188.227:80 weight=1;
    5. server 192.168.188.228:80 weight=1;
    6. }
    7. server {
    8. location / {
    9. proxy_pass http://websrvs;
    10. index index.html;
    11. }
    12. }
    1. ##Nginx_Backup服务器Nginx配置
    2. [root@centosnode2 ~]# vim /etc/nginx/conf.d/lb2.conf
    3. upstream websrvs {
    4. server 192.168.188.227:80 weight=1;
    5. server 192.168.188.228:80 weight=1;
    6. }
    7. server {
    8. location / {
    9. proxy_pass http://websrvs;
    10. index index.html;
    11. }
    12. }

     3.  启动Nginx服务,测试负载均衡是否成功:

    1. ##启动主,从Nginx服务
    2. [root@centosnode1 ~]# systemctl start nginx.service
    3. [root@centosnode2 ~]# systemctl start nginx.service
    4. ##设置Web1服务器的简单web页面
    5. [root@centosnode3 ~]# echo "this is page ip : 192.168.188.227" > /var/www/html/index.html
    6. ##启动Web1服务器的httpd服务
    7. [root@centosnode3 ~]# systemctl start httpd
    8. ##设置Web2服务器的简单web页面
    9. [root@centosnode4 ~]# echo "this is page ip : 192.168.188.228" > /var/www/html/index.html
    10. ##启动Web2服务器的httpd服务
    11. [root@centosnode4 ~]# systemctl start httpd
    12. ##测试Nginx服务负载均衡
    13. [root@centosnode5 ~]# curl 192.168.188.223
    14. this is page ip : 192.168.188.227
    15. [root@centosnode5 ~]# curl 192.168.188.223
    16. this is page ip : 192.168.188.228

    4.  配置Nginx_Master服务器Keepalived服务:

    1. [root@centosnode1 ~]# vim /etc/keepalived/keepalived.conf
    2. ! Configuration File for keepalived
    3. global_defs {
    4. notification_email {
    5. acassen@firewall.loc
    6. failover@firewall.loc
    7. sysadmin@firewall.loc
    8. }
    9. notification_email_from Alexandre.Cassen@firewall.loc
    10. smtp_server 192.168.200.1
    11. smtp_connect_timeout 30
    12. router_id NODE1_MASTER
    13. }
    14. vrrp_script chk_nginx {
    15. script "killall -0 nginx" ##判断nginx服务是否运行,若不运行,则返回状态码非0
    16. interval 2
    17. }
    18. vrrp_instance VI_1 {
    19. state MASTER
    20. interface ens33
    21. virtual_router_id 51
    22. priority 100
    23. advert_int 1
    24. authentication {
    25. auth_type PASS
    26. auth_pass 1111
    27. }
    28. track_script { ##调用vrrp_script中定义的脚本
    29. chk_nginx
    30. }
    31. virtual_ipaddress {
    32. 192.168.188.100
    33. }
    34. }

    5.  配置Nginx_Backup服务器Keepalived服务:

    1. [root@centosnode2 ~]# vim /etc/keepalived/keepalived.conf
    2. ! Configuration File for keepalived
    3. global_defs {
    4. notification_email {
    5. acassen@firewall.loc
    6. failover@firewall.loc
    7. sysadmin@firewall.loc
    8. }
    9. notification_email_from Alexandre.Cassen@firewall.loc
    10. smtp_server 192.168.200.1
    11. smtp_connect_timeout 30
    12. router_id NODE2_BACKUP
    13. }
    14. vrrp_script chk_nginx {
    15. script "killall -0 nginx" ##判断nginx服务是否运行,若不运行,则返回状态码非0
    16. interval 2
    17. }
    18. vrrp_instance VI_1 {
    19. state BACKUP
    20. interface ens33
    21. virtual_router_id 51
    22. priority 80
    23. advert_int 1
    24. authentication {
    25. auth_type PASS
    26. auth_pass 1111
    27. }
    28. track_script { ##调用vrrp_script中定义的脚本
    29. chk_nginx
    30. }
    31. virtual_ipaddress {
    32. 192.168.188.100
    33. }
    34. }

    6.  启动Keepalived服务:

    1. [root@centosnode1 ~]# systemctl start keepalived.service
    2. [root@centosnode2 ~]# systemctl start keepalived.service

    7.  测试Nginx负载均衡是否正常,访问VIP:

    1. ##测试Nginx服务负载均衡
    2. [root@centosnode5 ~]# curl 192.168.188.100
    3. this is page ip : 192.168.188.227
    4. [root@centosnode5 ~]# curl 192.168.188.100
    5. this is page ip : 192.168.188.228

    8.  测试故障检查是否正常,停掉一台web后端的web服务,访问VIP是否可以正常访问:

    1. ##停掉centosnode3的httpd服务
    2. [root@centosnode3 ~]# systemctl stop httpd
    3. ##访问vip
    4. [root@centosnode5 ~]# for ((i=1;i<=5;i++))
    5. > do
    6. > curl 192.168.188.100
    7. > done
    8. this is page ip : 192.168.188.228
    9. this is page ip : 192.168.188.228
    10. this is page ip : 192.168.188.228
    11. this is page ip : 192.168.188.228
    12. this is page ip : 192.168.188.228

    由此可见,当centosnode3节点的httpd故障时,会自动将其从集群中去除;而且访问vip可以正常访问。当centosnode3节点的httpd恢复时,会自动将其加入到集群中。

    9.  测试Keepalived故障转换是否正常,是否会发生ip飘移:

    由此可见当前vip地址在centosnode1主机上,然后模拟服务器故障,停掉Nginx服务。

    [root@centosnode1 ~]# systemctl stop nginx.service

     

    再次查看vip,发现vip飘移到了centosnode2主机上。可以将centosnode1主机的Nginx服务再次开启,会发现vip又会飘移到了centosnode1主机上,因为keepalived默认配置抢占主服务器,优先级高的服务器,将抢占到vip。

    [root@centosnode1 ~]# systemctl start nginx.service

    1. ##监控日志文件
    2. tail -f 日志文件目录

  • 相关阅读:
    SpringBootWeb请求响应
    java 问题解决
    java学习part05
    计算机等级考试信息安全三级填空题-二
    大学生静态HTML网页源码 我的校园网页设计成品 学校班级网页制作模板 web课程设计 dreamweaver网页作业
    基于SpringBoot+thymeleaf的物资管理系统
    【蓝桥杯物联网赛项学习日志】Day2 中断矩阵按键
    压力测试总共需要几个步骤?思路总结篇
    【智能优化算法】基于Jaya算法求解单目标优化问题附matlab代码MOJAYA
    王思葱、京西购物网-《软件方法》自测题解析017
  • 原文地址:https://blog.csdn.net/NancyLCL/article/details/126751082