【Nginx】如何实现Nginx的高可用负载均衡?看完我也会了!! (2)

测试Nginx是否安装成功

# /usr/local/nginx-1.19.1/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.19.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.19.1/conf/nginx.conf test is successful

启动Nginx

# /usr/local/nginx-1.19.1/sbin/nginx

重启 Nginx

# /usr/local/nginx-1.19.1/sbin/nginx -s reload

设置Nginx开机自启动

# vim /etc/rc.local

加入如下一行配置。

/usr/local/nginx-1.19.1/sbin/nginx

接下来,分别访问两台服务器上Nginx,如下所示。

【Nginx】如何实现Nginx的高可用负载均衡?看完我也会了!!

【Nginx】如何实现Nginx的高可用负载均衡?看完我也会了!!

安装 Keepalived

官方下载链接为: 。

上传或下载 keepalived

上传或下载 keepalived(keepalived-1.2.18.tar.gz) 到 /usr/local/src 目录

解压安装 # cd /usr/local/src # tar -zxvf keepalived-1.2.18.tar.gz # cd keepalived-1.2.18 # ./configure --prefix=http://www.likecs.com/usr/local/keepalived # make && make install 将 keepalived 安装成 Linux 系统服务

因为没有使用 keepalived 的默认路径安装(默认是/usr/local) ,安装完成之后,需要做一些工作复制默认配置文件到默认路径

# mkdir /etc/keepalived # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

复制 keepalived 服务脚本到默认的地址

# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ # ln -s /usr/local/sbin/keepalived /usr/sbin/ # ln -s /usr/local/keepalived/sbin/keepalived /sbin/

设置 keepalived 服务开机启动。

# chkconfig keepalived on 修改 Keepalived 配置文件 MASTER 节点配置文件(192.168.50.133) # vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { ## keepalived 自带的邮件提醒需要开启 sendmail 服务。 建议用独立的监控或第三方 SMTP router_id liuyazhuang133 ## 标识本节点的字条串,通常为 hostname } ## keepalived 会定时执行脚本并对脚本执行的结果进行分析,动态调整 vrrp_instance 的优先级。如果脚本执行结果为 0,并且 weight 配置的值大于 0,则优先级相应的增加。如果脚本执行结果非 0,并且 weight配置的值小于 0,则优先级相应的减少。其他情况,维持原本配置的优先级,即配置文件中 priority 对应的值。 vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径 interval 2 ## 检测时间间隔 weight -20 ## 如果条件成立,权重-20 } ## 定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称 vrrp_instance VI_1 { state MASTER ## 主节点为 MASTER, 对应的备份节点为 BACKUP interface eth0 ## 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同, 我的是 eth0 virtual_router_id 33 ## 虚拟路由的 ID 号, 两个节点设置必须一样, 可选 IP 最后一段使用, 相同的 VRID 为一个组,他将决定多播的 MAC 地址 mcast_src_ip 192.168.50.133 ## 本机 IP 地址 priority 100 ## 节点优先级, 值范围 0-254, MASTER 要比 BACKUP 高 nopreempt ## 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题 advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样, 默认 1s ## 设置验证信息,两个节点必须一致 authentication { auth_type PASS auth_pass 1111 ## 真实生产,按需求对应该过来 } ## 将 track_script 块加入 instance 配置块 track_script { chk_nginx ## 执行 Nginx 监控的服务 } # # 虚拟 IP 池, 两个节点设置必须一样 virtual_ipaddress { 192.168.50.130 ## 虚拟 ip,可以定义多个 } } BACKUP 节点配置文件(192.168.50.134) # vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id liuyazhuang134 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 33 mcast_src_ip 192.168.50.134 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_nginx } virtual_ipaddress { 192.168.50.130 } } 编写 Nginx 状态检测脚本

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspswz.html