Day 59 应用工具_nginx高可用
nginx高可用
高可用介绍
分发器宕机怎么办
数据服务器宕机怎么办
Keepalived
Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代
替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
一个监控+自愈的软件
Keepalived获得
http://www.keepalived.org/download.html
wget http://www.keepalived.org/software/keepalived-2.0.8.tar.gz
Keepalived 安装
#cat keepalived_install.sh #!/bin/bash pkg=keepalived-2.0.8.tar.gz tar xf $pkg yum -y install kernel-devel ln -s /usr/src/kernels/3.10.0-862.14.4.el7.x86_64/ /usr/src/linux cd keepalived-2.0.8/ yum install openssl-* -y ./configure --prefix=/usr/local/keepalived make make install mkdir -pv /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ ln -s /usr/local/keepalived/sbin/keepalived /sbin/
构建高可用Nginx集群
声明?
• 实验机器: Vmware 虚拟机 2核4G
• ⽹网卡:桥接
• 系统:centos7.5
• 防火墙:关闭
• Selinux:关闭
• 网段:192.168.10.0/24
主机名 IP 角色 软件
Master.ayitula.com 192.168.10.40 主分发器器 Nginx+keepalived
Backup.ayitula.com 192.168.10.41 备分发器器 Nginx+keepalived
Web01.ayitula.com 192.168.10.42 数据服务器器1 nginx
Web02.ayitula.com 192.168.10.43 数据服务器器2 nginx
部署步骤
软件环境部署
1)分发器 nginx+keepalived
2)数据服务器 Nginx
3) 配置Nginx分发器
4)配置数据服务器页面(web01 web02)
5)配置keepalived.conf
6)测试
配置Nginx集群
upstream web { server 192.168.10.42 max_fails=2 fail_timeout=3; server 192.168.10.43 max_fails=2 fail_timeout=3; server { listen 80; server_name localhost; location / { proxy_pass http://web; } }
配置keepalived
#cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id NGINX_DEVEL } vrrp_script check_nginx { script "/etc/keepalived/nginx_pid.sh" interval 2 fall 1 } vrrp_instance nginx { state MASTER interface ens33 mcast_src_ip 192.168.10.40 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx } virtual_ipaddress { 192.168.10.213/24 } }
关联脚本 nginx_pid.sh
#chmod 755 /etc/keepalived/nginx_pid.sh #cat /etc/keepalived/nginx_pid.sh #!/bin/bash nginx_kp_check () { nginxpid=`ps -C nginx --no-header |wc -l` if [ $nginxpid -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 1 nginxpid=`ps -C nginx --no-header |wc -l` if [ $nginxpid -eq 0 ];then systemctl stop keepalived fi fi }
启动keepalived
systemctl start keepalived
配置备份Nginx分发器
backuo.ayitula.com #cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id NGINX_DEVEL } vrrp_script check_nginx { script "/etc/keepalived/nginx_pid.sh" interval 2 fall 1 } vrrp_instance nginx { state BACKUP interface ens33 mcast_src_ip 192.168.10.41 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx } virtual_ipaddress { 192.168.10.213/24 } }
测试高可用
模拟故障
数据服务器器宕机测试
分发器器宕机测试