keepalive+nginx/haproxy+keepalive 高可用

keepalive:

调度器的高可用

vip地址在主备之间的切换,主在工作时,vip地址只在主;主停止工作,vip飘到备上。

在主备的优先级不变的情况下,主恢复工作,vip会飘会主服务器

1、配优先级

2、配vip和真实服务器

3、主备的id要一致

4、主备的id要区分

keepalive是专门为lvs打造的,但是不是为lvs专门服务

keepalive也可以使用nginx、haproxy

keepalive+nginx实现高可用

vrrp_script check—_nginx {
script "/opt/check nginx.sh"
#调用脚本内容,检测nginx的状态
interval 5
#检测的时间是5秒一次
}
[root@nginx1 keepalived]# vim keepalived.conf 
​
! Configuration File for keepalived
​
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_iptables
}
   vrrp_script check—_nginx {
     script "/opt/check nginx.sh"
      interval 5
}
​
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 120
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.65.100
    }
    track_script {
    check_nginx
}
​
[root@nginx1 opt]# vim check_nginx.sh
​
#!/bin/bash
/usr/bin/curl -I http://localhost &> /dev/null
if [ $? -ne 0 ]
then
   systemctl stop keepalived
fi
[root@nginx2 keepalived]# vim keepalived.conf 
​
! Configuration File for keepalived
​
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_02
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_iptables
}
   vrrp_script check—_nginx {
     script "/opt/check_nginx.sh"
      interval 5
}
​
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.65.100
    }
    track_script {
    check_nginx
}

面试题

脑裂是什么

HA 高可用架构中的一个特殊现象,只要使用vp地址代理的冗余模式的高可用。都有可能出现脑裂的问题。 主和备同时都有yip地址。 主和备无法确定各自的身份,同时出现了vp地址,两边起来了,但是两边都无法使用。

原因:

1、keepalive的配置文件问题

2、心跳线(网线)断了 老化

3、网卡处理问题(硬件问题),IP地址配置冲突

4、防火墙的策略,iptables的策略,屏蔽了组播的地址广播。屏蔽了vrrp协议的报文

5、两台服务器的时间不同步也可能导致

6、其他的服务配置对心跳线的检测造成了干扰

怎么解决

1、检查keepalive的配置文件①名称不能相同 ②优先级不能相同 ③接口要相同 ④调用的文件权限不足 ⑤调整连接超时的时间(太长太短都不行)⑥查看日志

2、换根新网线

3、换台服务器,重新分配ip地址

4、关闭防火墙和安全机制,iptables -F;或者在配置文件中加入"vrrp_iptables"来清楚安全机制带来的阻碍

5、将两台服务器的时间调整到同步

6、更改其他服务配置

nginx Ivs Ivs+keepalivekeepalive单独配置

web集群

Haporxy负载均衡:

nginx 四层转发(ip+端口) 七层代理

lvs四层转发 内核态 七层用户态

Haporxy 四层转发 七层转发

Haporxy的作用和使用场景

场景

1、用于高并发的web场景,可以支持一万个以上的并发请求,高性能的tcp和http的负载均衡

工作原理:

提高一个代理地址,访问集群

作用

1、进行四层和七层转发

2、支持https

3、haporxy本身不自带缓存功能。请求当中添加cookie,使用缓存

4、支持主备切换(keepalive)

特点

1、可靠性高,稳定性好

2、可以同时维护40000-50000个并发,单位时间内可以处理最大请求数20000个(3秒)

3、支持负载均衡算法,虽然不带缓存,但是可以支持会话保持

算法

1、rr

2、wrr

3、leastconn

安装haproxy 解压

[root@test-41 opt]# tar -xf haproxy-1.5.19.tar.gz 
[root@test-41 opt]# uname -r
[root@test-41 haproxy-1.5.19]# make TARGET=linux2628 ARCH=x86_64
#target使用的版本要大于linux.2.60以上的版本
[root@test-41 haproxy]# make install
[root@test-41 haproxy]# mkdir /etc/haproxy
[root@test-41 opt]# cd examples/
[root@test-41 examples]# cp haproxy.cfg /etc/haproxy/

配置haproxy.cfg

[root@test-41 haproxy]# vim haproxy.cfg 
# this config needs haproxy-1.1.28 or haproxy-1.2.1
​
global
        log /dev/log    local0 info
        log /dev/log    local1 notice
        #log loghost    local0 info
        maxconn 4096
        #最大连接数,推荐使用1024
        #chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        nbproc 4
        #haproxy的并发的线程数,设置的数量最好是cpu的两倍或者和cpu保持一致
        #debug
        #quiet
​
defaults
#默认参数配置,包含连接配置监听配置,代理配置
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        #检查节点服务器3次,连续3次失败就是节点服务器不可用
        redispatch
        #服务器负载很高时,自动结束当前队列中处理比较久的连接
        maxconn 2000
        #最大连接数这个数可以和global中保持一致可以比他小不能大于他,一般一致
        #contimeout     5000
        #clitimeout     50000
        #srvtimeout     50000
        timeout http-request 10s
        #http请求的默认超时时间
        timeout queue 1m
        #在队列当中请求超时的时间
        timeout connect 10s
        #连接超时时间
        timeout client 1m
        #客户端的超时时间
        timeout server 1m
        #服务端的超时时间
        timeout http-keep-alive 10s
        #默认长连接的超时时间
        timeout check 10s
        #检查后端服务器的超时时间
七层配置
#转发请求的设置,即可以是四层也可以是七层
listen  xy102 0.0.0.0:80
        option httpchk GET /index.html
        balance static-r
#轮询算法
        server  rs01 192.168.65.10 check inter 2000 fall 3 weight 2
        server  rs02 192.168.65.20 check inter 2000 fall 3 weight 3
#server指定真实服务器rs01 自定后台服务器名称 check inter 2000 启动对后端服务器进行检查,检查的间隔时间2000毫秒
​
四层配置
frontend test
bind *:80
mode tcp
default_backend test
​
backend test
mode tcp
balance roundrobin
server server1 192.168.65.10:80 check inter 2000 fall 3 weight 2
server server2 192.168.65.20:80 check inter 2000 fall 3 weight 2
​

对配置文件进行复制、赋权、添加服务、创建软连接

[root@test-41 examples]# cp haproxy.init /etc/init.d/haproxy    #复制
[root@test-41 examples]# chmod 777 /etc/init.d/haproxy          #赋权
[root@test-41 examples]# chkconfig --add /etc/init.d/haproxy    #添加到管理服务
[root@test-41 examples]# ln -s /usr/local/sbin/haproxy /usr/sbin/  #创建软连接
​

结果

[root@test-41 haproxy]# curl 192.168.65.41
This is nginx2
[root@test-41 haproxy]# curl 192.168.65.41
This is nginx1
​

作业   keepalive+haproxy实现高可用

keepalive+haproxy高可用

haproxy地址192.168.65.41  虚拟地址vip 192.168.65.100
​
rs1   keepalive  192.169.65.10
​
rs2   keepalive  192.168.65.20
​

调度器

[root@test-41 haproxy]# vim haproxy.cfg 
​
# this config needs haproxy-1.1.28 or haproxy-1.2.1
​
global
        log /dev/log    local0 info
        log /dev/log    local1 notice
        #log loghost    local0 info
        maxconn 4096
        #最大连接数,推荐使用1024
        #chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        nbproc 4
        #haproxy的并发的线程数,设置的数量最好是cpu的两倍或者和cpu保持一致
        #debug
        #quiet
​
defaults
#默认参数配置,包含连接配置监听配置,代理配置
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        #检查节点服务器3次,连续3次失败就是节点服务器不可用
        redispatch
        #服务器负载很高时,自动结束当前队列中处理比较久的连接
        maxconn 2000
        #最大连接数这个数可以和global中保持一致可以比他小不能大于他,一般一致
        #contimeout     5000
        #clitimeout     50000
        #srvtimeout     50000
        timeout http-request 10s
        #http请求的默认超时时间
        timeout queue 1m
        #在队列当中请求超时的时间
        timeout connect 10s
        #连接超时时间
        timeout client 1m
        #客户端的超时时间
        timeout server 1m
        #服务端的超时时间
        timeout http-keep-alive 10s
        #默认长连接的超时时间
        timeout check 10s
        #检查后端服务器的超时时间
​
#转发请求的设置,即可以是四层也可以是七层
listen  xy102 0.0.0.0:80
        option httpchk GET /index.html
        balance static-rr
#轮询算法
        server  rs01 192.168.65.10 check inter 2000 fall 3 weight 2
        server  rs02 192.168.65.20 check inter 2000 fall 3 weight 3
#server指定真实服务器rs01 自定后台服务器名称 check inter 2000 启动对后端服务器进行检查,检查的间隔时间2000毫秒
​
四层
#frontend test
#bind *:80
#mode tcp
#default_backend test
​
#backend test
#mode tcp
#balance roundrobin
#server server1 192.168.65.10:80 check inter 2000 fall 3 weight 2
#server server2 192.168.65.20:80 check inter 2000 fall 3 weight 2
​

rs 1

[root@nginx1 keepalived]# vim keepalived.conf 
​
! Configuration File for keepalived
​
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_01
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_iptables
}
   vrrp_script check_nginx {
     script "/opt/check_nginx.sh"
      interval 5
}
​
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 120
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.65.100
    }
    track_script {
    check_nginx
}
}
​

rs 2

[root@nginx2 keepalived]# vim keepalived.conf 
​
! Configuration File for keepalived
​
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_02
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_iptables
}
   vrrp_script check_nginx {
     script "/opt/check_nginx.sh"
      interval 5
}
​
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.65.100
    }
    track_script {
    check_nginx
}
}
​

结果

访问虚拟地址就是访问哪个优先级高访问哪个
[root@test-42 ~]# curl 192.168.65.100
This is nginx1
[root@test-42 ~]# curl 192.168.65.100
This is nginx1
[root@test-42 ~]# curl 192.168.65.100
This is nginx1
​
访问haproxy就是keepalive的nginx来回轮询
[root@test-42 ~]# curl 192.168.65.41
This is nginx2
[root@test-42 ~]# curl 192.168.65.41
This is nginx1
[root@test-42 ~]# curl 192.168.65.41
This is nginx2
[root@test-42 ~]# curl 192.168.65.41
This is nginx1
​

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
keepalivenginx都与可用相关。keepalive是一个用于维持TCP连接的工具,它可以确保在网络中的连接保持活跃,从而提服务的可用性和可靠性。而nginx是一个性能的Web服务器和反向代理服务器,它可以处理大量的并发连接并分发请求到不同的后端服务器,从而提供可用性和负载均衡。在可用的部署中,我们可以将nginxkeepalive结合使用,通过配置keepalive来监控后端服务器的健康状态,并使用nginx来实现负载均衡和故障转移,以保证系统的可用性。引用中的命令systemctl enable nginx keepalived用于启用nginxkeepalived服务,使其在系统启动时自动运行。引用中的命令systemctl enable kubelet用于启用kubelet服务,kubelet是Kubernetes集群中的一个核心组件,它负责管理节点上的容器和Pod。而引用中的命令vim /etc/nginx/nginx.conf用于编辑nginx的配置文件,通过配置nginx的参数和指令可以实现可用性的设置,例如配置反向代理、负载均衡和健康检查等。因此,通过结合keepalivenginx的使用,我们可以实现可用的部署和管理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [keepalive+nginx可用K8S集群部署](https://blog.csdn.net/fzqdyyd/article/details/127340860)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值