Nginx-keepalived dual-machine hot standby cluster

Detailed steps of Nginx server installation and deployment (multiple installations, detailed process)

Install keepalived offline

Centos7 offline installation and configuration Keepalived
link: https://pan.baidu.com/s/1x7ucXNyOt9sB-M6r8x7_bw
Extraction code: ok3o

Install keepalived online

# 安装依赖包
yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
# 安装keepalived
yum install -y keepalived

Uninstall keepalived

keepalived installation and uninstallation
uninstall keepalived under Linux

# 进入自己的安装目录
cd /usr/local/src/keepalived

# 执行卸载命令
make uninstall

# 找到所有的keepalived文件
whereis keepalived

# 执行 rm -rf 命令挨个删除

keepalived basic command

# 开机⾃启动
systemctl enable keepalived 
# 启动
systemctl start keepalived 
# 暂停
systemctl stop keepalived 
# 重启
systemctl restart keepalived 
# 查看状态
systemctl status keepalived
# 查看keepalived日志
tail -f /var/log/messages

insert image description here

keepalived configuration virtual ip

Nginx configuration - build Nginx high-availability cluster (dual machine hot standby)
after the installation is complete, there is a keepalived configuration file in the /etc/keepalived directory

cat /etc/keepalived/keepalived.conf 

Under the global_defs configuration, delete the vrrp_strict configuration
state: the master node is MASTER, the backup node is BACKUP
interface: the network card that needs to be bound (you can check it through ifconfig, mine is ens33)
priority: the master node is configured as 100, and the backup node is configured as 99
virtual_ipaddress : Virtual IP, two nodes need to be the same

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  # 通过它,可以访问到主机,在hosts文件中,要做映射关系,类似于 127.0.0.1 LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict 	# 删除这句
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    
    
    state MASTER 	# 现在MASTER是主机, 如果是从机要换成 BACKUP
    interface eth0 	# 把eth0 换成自己服务器的网卡
    virtual_router_id 51 	# 主、备机的 virtual_router_id 必须相同
    priority 100 	# 主节点配置成100,备节点配置成99 ,  主、备机取不同的优先级,主机值较大,备份机值较小
    advert_int 1 	# #每隔一秒发送一次心跳,确保从服务器是否还活着
    authentication {
    
    
        auth_type PASS
        auth_pass 1111	# 心跳检测需要的密码
    }
    virtual_ipaddress {
    
     # 要虚拟的地址
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
}

To get the interface value, (the network card of your own server) you can use the ip a command

ip a

insert image description here

Bind Nginx process and Keepalived service

Create a new detection script nginx_check.sh under the path /usr/local/src/

#! /bin/bash
A=`ps -C nginx -no-header | wc - 1`
if [ $A -eq 0];then
	/usr/local/nginx/sbin/nginx  #自己nginx的安装目录
	sleep 2
	if [`ps -C nginx --no-header| wc -1` -eq 0 ];then
		killall keepalived
	fi
fi

Modify the keepalived configuration file, then restart the keepalived service

vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
    
    
   notification_email {
    
    
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.20.61
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

# 添加这一段检测脚本配置
vrrp_script chk_http_ port {
    
    
	script "/usr/local/src/nginx_check.sh"
	interval 2   # (检测脚本执行的间隔)2s
	weight 2  #权重,如果这个脚本检测为真,服务器权重+2
}

vrrp_instance VI_1 {
    
    
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
    
    
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    
    
        192.168.200.16
    }
}

Guess you like

Origin blog.csdn.net/qq_44154912/article/details/126290550