安装 telnet
重启ng,并配置ip
地址hash
最大链接数
携带header
带主机名和ip
域名的拆分
include包含文件
proxy.conf代理设置
动静分离
通过扩展名实现动静分离,也阔以通过if判断
通过useragent进行判断
健康检查
高可用:两台业务系统启动着相同的服务,如果有一台故障,另一台自动接管,我们将将这个称之为高可用;
Keepalived 会生成一个虚拟IP,此虚拟IP与多台真实的服务器的IP绑定,当用户发来请求到虚拟IP时,请求先路由到主服务器,当主服务器宕机后,请求会路由到备用服务器,以达到高可用的目的。
Keepalived工作方式:抢占式、非抢占式
@1. yum安装:
yum install keepalived -y 日志存放位置:/var/log/messages
@2. 官网
可以到Keepalived官网(https://www.keepalived.org)下载
tar -zxvf keepalived-2.2.4.tar.gz @1. 安装依赖包 yum -y install libnl libnl-devel yum install -y openssl openssl-devel @2. 执行配置命令,进入目录执行 ./configure --prefix=/usr/local/keepalived --sysconf=/etc @3. 执行编译并安装命令 make && make install @4. 安装成功后,在 /etc/keepalived 目录下,会看到 keepalived.conf 文件。
A服务器配置如下: ! Configuration File for keepalived global_defs { # 全局唯一的主机标识 router_id server_a } vrrp_instance VI_1 { # 标识是主节点还是备用节点,值为 MASTER 或 BACKUP state MASTER # 绑定的网卡 interface ens33 # 虚拟路由id,保证主备节点是一致的 virtual_router_id 51 # 权重 priority 100 # 同步检查时间,间隔默认1秒 advert_int 1 # 认证授权的密码,所有主备需要一样 authentication { auth_type PASS auth_pass 1111 } # 虚拟IP virtual_ipaddress { 192.168.1.88 } }
B服务器配置如下: ! Configuration File for keepalived global_defs { router_id server_b } vrrp_instance VI_1 { # 设置为备机 state BACKUP interface ens33 virtual_router_id 51 # 权重设置的要比主机低 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } # 虚拟IP需要主备设置成一样 virtual_ipaddress { 192.168.1.88 } }
5、启动Keepalived
进入 /usr/local/keepalived/sbin 目录 ./keepalived 或者 用脚本启动nginx 以及keepalived: systemctl start keepalived.service
查看日志:
启动脚本, 已经帮我把nginx启动了。查看网卡生成的虚拟ip地址:192.168.116.15:
验证服务
6、停止A服务器,看虚拟IP是否路由到B服务器
经试验可知,A服务器关闭后,虚拟IP便路由到了B服务器。
最后修改nginx的代理指向这个虚拟IP即可
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_pools { server 10.0.0.7:80; server 10.0.0.8:80; } server { listen 192.168.1.15:80; server_name www.etiantian.org; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } }
非抢占式不再有主从之分,全部都为BACKUP,并且配置文件中添加nopreempt,用来标识为非抢占式;
第一台配置文件设置
第二台配置文件设置
实现思路:将keepalived 中的vip作为nginx负载均衡的监听地址,并且域名绑定的也是vip的地址。说明:Nginx 负载均衡实现高可用,需要借助Keepalived地址漂移功能。
两台负载均衡配置:
由于某些原因,导致两台keepalived高可用服务器在指定时间内,无法检测到对方存活心跳信息,从而导致互相抢占对方的资源和服务所有权,然而此时两台高可用服务器有都还存活。
可能出现的原因:
! 所以需要编写一个检测nginx的存活状态的脚本,如果nginx不存活,则kill掉宕掉的nginx主机上面的keepalived。(所有的keepalived都要配置)
脚本如下:
配置文件如下:
编写监控脚本,check_www.sh
vim check_www.sh #!/bin/bash while true do if [ `ps -ef |grep nginx |grep -v grep |wc -l` -lt 2 ] then /etc/init.d/keepalived stop exit fi done chomod +x check_www.sh
2.7.1 lb01的keepalived配置文件
[root@lb01 scripts]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id lb01 } vrrp_script check { script "/server/scripts/check_www.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3/24 dev eth0 label eth0:1 } track_script { check } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.4/24 dev eth0 label eth0:2 } }
2.7.2 修改lb02的keepalived配置文件
[root@lb02 conf]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id lb02 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 dev eth0 label eth0:1 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.4 dev eth0 label eth0:2 } }
修改nginx配置文件,让bbs 与www分别监听不同的ip地址
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream server_pools { server 10.0.0.7:80; server 10.0.0.8:80; server 10.0.0.9:80; } server { listen 10.0.0.3:80; server_name www.etiantian.org; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } server { listen 10.0.0.4:80; server_name bbs.etiantian.org; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } }