nginx+keepalive
[root@www ~]# yum -y install pcre-devel zlib-devel gcc [root@www ~]# useradd -M -s /sbin/nologin nginx [root@www ~]# tar zxvf nginx-1.16.1.tar.gz -C /usr/src/ [root@www ~]# cd /usr/src/nginx-1.16.1/ [root@www nginx-1.16.1]# ./configure --prefix=/app/nginx --user=nginx --group=nginx --with-http_stub_status_module [root@www nginx-1.16.1]# make&&make install
[root@localhost ~]# yum -y install kernel-devel openssl-devel popt-deve [root@localhost ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/ [root@localhost ~]# cd /usr/src/keepalived-1.2.13/ [root@localhost keepalived-1.2.13]# ./configure --prefix=/app/keepalived [root@localhost keepalived-1.2.13]# make &&make install [root@localhost ~]# mkdir /etc/keepalived [root@localhost ~]# cp /app/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ ##复制主配置文件 [root@localhost ~]# cp /app/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ## 复 制启动时需要加载的配置文件 [root@localhost ~]# cp /app/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ ## 复 制服务的控制脚本 [root@localhost ~]# cp /app/keepalived/sbin/keepalived /usr/sbin/ ## 复 制keepalived 的命令 [root@localhost ~]# chmod 755 /etc/init.d/keepalived ##为控制脚本指定权限
编辑主服务器配置文件
[root@localhost ~]# vim /etc/keepalived/keepalived.conf global_defs { router_id HA_TEST_R1 ##本服务器的名称,若环境中有多个 keepalived 时,此名称不能一致 } vrrp_instance VI_1 { ##定义 VRRP 热备实例,每一个 keep 组都不同 state MASTER ##MASTER 表示主服务器 interface ens33 ##承载 VIP 地址的物理接口 virtual_router_id 1 ##虚拟路由器的 ID 号,每一个 keep 组都不同 priority 100 ##优先级,数值越大优先级越高 advert_int 1 ##通告检查间隔秒数(心跳频率) authentication { ##认证信息 auth_type PASS ##认证类型 auth_pass 123456 ##密码字串 } virtual_ipaddress { 192.168.100.95 ##指定漂移地址(VIP) } } virtual_server 192.168.100.95 80 { #vip 配置 delay_loop 2 #每隔 2 秒检查一次 real_server 状态 lb_algo wrr ##指定 lvs 的调度算法 lb_kind DR ##lvs 集群模式 persistence_timeout 60 ##会话保持时间 protocol TCP ##选择协议 real_server 192.168.100.21 80 { ##本机地址 weight 3 ##服务器的权重 notify_down /etc/keepalived/check.sh ## 指 定 节 点 失 效 后 , 采 用 的 脚 本 ,notify_up 表示节点正常后,采用的脚本 ##健康检查方式一共有 HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK这些 TCP_CHECK { connect_timeout 10 ##连接超时时间 nb_get_retry 3 ##重连次数 delay_before_retry 3 ##重连间隔时间 connect_port 80 ##健康检查端口 } } } [root@localhost ~]# vi /etc/keepalived/check.sh #编辑节点检测失败后执行脚本 #!/bin/bash /etc/init.d/keepalived stop echo -e "$(ip a |grep ens33 |grep inet |awk '{print $2}'|awk -F'/' '{print $1}') (nginx) is down on $(date +%F-%T)" >> /etc/keepalived/check_httpd.log sleep 5 kill -9 `ps -ef|grep nginx|grep master|awk '{print $2}'`
kill -9 `ps -ef|grep nginx|grep worker|awk '{print $2}'` /app/nginx/sbin/nginx /etc/init.d/keepalived start [root@localhost ~]# /etc/init.d/keepalived start #启动服务
编辑副服务器配置文件
[root@localhost html]# vi /etc/keepalived/keepalived.conf global_defs { router_id HA_TEST_R2 ##本服务器的名称 } vrrp_instance VI_1 { state BACKUP ##BACKUP 表示从服务器 interface ens33 virtual_router_id 1 priority 99 ##优先级,低于主服务器 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.100.95 } } virtual_server 192.168.100.95 80{ ##vip 配置 delay_loop 2 ##每隔 2 秒检查一次 real_server 状态 lb_algo wrr lb_kind DR persistence_timeout 60 ##会话保持时间 protocol TCP real_server 192.168.100.22 80 { ##本机地址 weight 3 notify_down /etc/keepalived/check.sh TCP_CHECK { connect_timeout 10 ##连接超时时间 nb_get_retry 3 ##重连次数 delay_before_retry 3 ##重连间隔时间 connect_port 80 ##健康检查端口 } } } [root@localhost html]# vi /etc/keepalived/check.sh #编辑节点检测失败后执行脚本 #!/bin/bash /etc/init.d/keepalived stop echo -e "$(ip a |grep ens33 |grep inet |awk '{print $2}'|awk -F'/' '{print $1}') (nginx) is down on $(date +%F-%T)" >> /etc/keepalived/check_httpd.log sleep 5 kill -9 `ps -ef|grep nginx|grep master|awk '{print $2}'`
kill -9 `ps -ef|grep nginx|grep worker|awk '{print $2}'` /app/nginx/sbin/nginx /etc/init.d/keepalived start [root@localhost html]# /etc/init.d/keepalived start #启动keepalived