十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本文索引:
十多年的兰坪网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整兰坪建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“兰坪网站设计”,“兰坪网站推广”以来,每个客户项目都认真落实执行。集群介绍 keepalived介绍 keepalived配置高可用集群根据功能划分为2类:高可用和负载均衡
高可用集群:通常为两台服务器,一台工作,另外一台作为冗余。当提供服务的机器宕机,冗余将接替继续提供服务。按可用的效率衡量高可用,例如“4个9”即99.99%的可用性,在99.99%的时间上不允许服务器宕机,当服务器宕机后作为冗余的服务器将立即接替主服务器提供服务,切换的时间间隔也较短,给用户造成的影响较小。实现高可用的开源软件有:heartbeat、keepalived。
负载均衡集群:通常需要有一台服务器作为分发器,它负责把用户的请求分发给后端的服务器集群进行处理,在这个集群里,除了分发器外,就是给用户提供服务的服务器,并且这些服务器的数量至少为2台。实现负载均衡的开源软件有LVS、keepalived、haproxy、nginx,商业的负载均衡器有F5、Netscaler,优点为高并发量、高稳定性。
keepalived通过VRRP(Virtual Router Redundancy Protocol,虚拟路由冗余协议)来实现高可用。
在这个协议里会将多台功能相同的路由器组成一个小组,这个小组里会有1个master角色和N个backup角色。master会通过组播的形式向各个backup发送VRRP协议的数据包,当backup收不到master发来的VRRP数据包时,就会认为master已宕机。此时就需要根据各个backup的优先级来决定哪个backup成为新的master。
keepalived有三个模块,分别是core、check和vrrp。其中core模块是keepalived的核心,负责主进程的启动、维护及全局配置文件的加载和解析;check模块负责健康检查;vrrp模块用来实现VRRP协议。
test1:192.168.65.133 test2:192.168.65.134 test1作为master,test2作为backup
test1/test2都安装keepalived和nginx
# 如果主机内已经源码安装过nginx就不需要再安装了 [root@test1 ~]# yum install -y keepalived [root@test1 ~]# yum install -y nginx [root@test2 ~]# yum install -y keepalived [root@test2 ~]# yum install -y nginx
更改配置文件
# 默认安装keepalived后会有一个keepalived.conf,这里清空内容后输入下列的代码 [root@test1 ~]# > /etc/keepalived/keepalived.conf [root@test1 ~]# vim /etc/keepalived/keepalived.conf # 全局定义参数 global_defs { # 出现问题时发邮件,邮件地址自定义 notification_email { 1245626656@qq.com } notification_email_from 1245626656@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } # 用来检测服务是否正常 vrrp_script chk_nginx { # 自定义脚本:检测服务是否正常 script "/usr/local/sbin/check_ng.sh" # 检测间隔3s interval 3 } # 定义相关master信息 vrrp_instance VI_1 { # 角色定义:主为MASTER,从为BACKUP state MASTER # 定义发送数据包的网卡 interface ens33 # 定义路由器id virtual_router_id 51 # 权重 priority 100 advert_int 1 # 定义认证信息 authentication { auth_type PASS auth_pass test1 } # 定义公有ip,正常时master绑定,master宕机后backup绑定 virtual_ipaddress { 192.168.188.100 } # 加载检测脚本,对于上面 track_script { chk_nginx } }
编辑检测脚本
# 检测服务是否正常,脚本所在路径在/etc/keepalived/keepalived.conf内定义 [root@test1 ~]# vim /usr/local/sbin/check_ng.sh #!/bin/bash #时间变量,用于记录日志 d=`date --date today +%Y%m%d_%H:%M:%S` #计算nginx进程数量 n=`ps -C nginx --no-heading | wc -l` #如果进程为0,则启动nginx,并且再次检测nginx进程数量, #如果还为0,说明nginx无法启动,此时需要关闭keepalived if [ $n -eq "0" ]; then /etc/init.d/nginx start n2=`ps -C nginx --no-heading|wc -l` # 执行启动nginx后发现nginx启动不成功,就关闭keepalived服务,并记录日志 if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi [root@test1 ~]# chmod 755 /usr/local/sbin/check_ng.sh [root@test1 ~]# systemctl stop firewalld [root@test1 ~]# setenforce 0
如果你master上的nginx是源码编译的,那么对应脚本上启动nginx的命令要做相应修改,此外master上的防火墙和selinux必须关闭,否则keepalived无法拉起nginx。
keepalived的日志文件记录在/var/log/messages文件内
[root@test1 data]# less /var/log/messages ... Jan 16 19:58:06 test1 Keepalived_vrrp[5547]: Registering gratuitous ARP shared channel Jan 16 19:58:06 test1 Keepalived_vrrp[5547]: Opening file \'/etc/keepalived/keepalived.conf\'. Jan 16 19:58:06 test1 kernel: show_signal_msg: 53 callbacks suppressed Jan 16 19:58:06 test1 kernel: keepalived[5547]: segfault at 0 ip (null) sp 00007fff063621b8 error 14 in libgcc_s-4.8.5-20150702.so.1[7fd2356f4000+15000] Jan 16 19:58:06 test1 Keepalived_vrrp[5547]: VRRP_Instance(VI_ 1) removing protocol VIPs. Jan 16 19:58:06 test1 Keepalived[4983]: Keepalived_vrrp exited due to segmentation fault (SIGSEGV). Jan 16 19:58:06 test1 Keepalived[4983]: Please report a bug at https://github.com/acassen/keepalived/issues Jan 16 19:58:06 test1 Keepalived[4983]: and include this log from when keepalived started, what happened Jan 16 19:58:06 test1 Keepalived[4983]: immediately before the crash, and your configuration file. Jan 16 19:58:06 test1 Keepalived[4983]: VRRP child process(5547) died: Respawning Jan 16 19:58:06 test1 Keepalived[4983]: Starting VRRP child process, pid=5548 Jan 16 19:58:06 test1 Keepalived_vrrp[5548]: Registering Kernel netlink reflector Jan 16 19:58:06 test1 Keepalived_vrrp[5548]: Registering Kernel netlink command channel Jan 16 19:58:06 test1 Keepalived_vrrp[5548]: Registering gratuitous ARP shared channel Jan 16 19:58:06 test1 Keepalived_vrrp[5548]: Opening file \'/etc/keepalived/keepalived.conf\'. ...
公有ip的查看
[root@test1 data]# ip addr ... 2: ens33:
修改配置文件
# 配置前关闭防火墙及selinux [root@test1 ~]# systemctl stop firewalld [root@test1 ~]# setenforce 0 [root@test2 ~]# cat /etc/keepalived/keepalived.conf global_defs { notification_email { 1245626656@qq.com } notification_email_from 1245626656@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/usr/local/sbin/check_ng.sh" interval 3 } vrrp_instance VI_1 { # 配置为backup机 state BACKUP interface ens33 # router_id与master一致 virtual_router_id 51 # 权重较master要小 priority 90 advert_int 1 authentication { auth_type PASS auth_pass test2 } # 这样设置公有ip,master宕机后,backup机绑定 virtual_ipaddress { 192.168.65.100 } track_script { chk_nginx } }
配置检测脚本
[root@test2 ~]# vim /usr/local/sbin/check_ng.sh #!/bin/bash #时间变量,用于记录日志 d=`date --date today +%Y%m%d_%H:%M:%S` #计算nginx进程数量 n=`ps -C nginx --no-heading | wc -l` if [ $n -eq "0" ]; then # backup机上的nginx使用yum安装的,启动目录不同 systemctl start nginx n2=`ps -C nginx --no-heading|wc -l` # 执行启动nginx后发现nginx启动不成功,就关闭keepalived服务,并记录日志 if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi # 配置权限 [root@test1 ~]# chmod 755 /usr/local/sbin/check_ng.sh 区分主从内的nginx
修改master上nginx默认主机的默认网页内容为“master web”;backup上nginx默认主页的内容为“backup web”;访问公有ip看显示的是哪个服务器的默认网页。
# 修改默认虚拟主机上的默认网页内容,这个要根据自己的实际情况修改 [root@test1 ~]# echo "master web server" > /data/wwwroot/default/index.html # yum 安装的nginx的默认网页路径是/usr/share/nginx/html/index.html [root@test2 ~]# echo "backup web server" > /usr/share/nginx/html/index.html 测试高可用
master信息
[root@test1 data]# curl -x127.0.0.1:80 test1.com master web server [root@test1 data]# curl -x127.0.0.1:80 test1.com -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 16 Jan 2018 13:36:35 GMT Content-Type: text/html Content-Length: 18 Last-Modified: Tue, 16 Jan 2018 13:33:27 GMT Connection: keep-alive ETag: "5a5dff27-12" Accept-Ranges: bytes # 当前公用ip绑定在test1上 [root@test1 data]# ip addr 1: lo:
backup信息
[root@test2 ~]# curl -x127.0.0.1:80 test2.com backup web server [root@test2 ~]# curl -x127.0.0.1:80 test2.com -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 16 Jan 2018 13:35:49 GMT Content-Type: text/html Content-Length: 18 Last-Modified: Tue, 16 Jan 2018 13:35:33 GMT Connection: keep-alive ETag: "5a5dffa5-12" Accept-Ranges: bytes 模拟主宕机:关闭master上的keepalived 模拟主恢复服务:重启master上的keepalived