• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    使用Keepalived如何实现SFTP服务的高可用

    发布者: 皮3591 | 发布时间: 2025-8-16 06:19| 查看数: 25| 评论数: 0|帖子模式

    背景

    这个事情的背景是生产环境的数据采集流程时不时会出问题(这个也是不可避免的),目前的处理手段是:所有的数据接口服务器(也就是存放原始数据等待采集的服务器)都部署一模一样的2台,数据也传的一模一样,然后当采集程序采集当前节点的数据异常的时候,由运维人员去改配置手动的切换。
    这样操作面临的问题不用多说,首先就是时效性的问题,就算数据断了能够及时发出告警,等到运维人员处理完成那也是至少几十分钟后了,所以高可用的实现还是很有必要的。
    简单调研下来还是只能用Keepalived来做这个软负载,毕竟客户不愿意出钱(铁 公 鸡)去购买F5设备这些东西做硬负载,所以就基于这个目标开干。
    1. 由于是做高可用,且我们的使用场景不是主备,应该是角色相同的两个服务器,所以不使用Keepalived的抢占式机制,改为非抢占。
    复制代码
    准备工作


    服务器和VIP

    准备2台服务器和一个VIP:

    • 服务器A:172.18.0.26,sftp等服务提前装好
    • 服务器B:172.18.0.27,sftp等服务提前装好
    • VIP:172.18.0.78,虚拟IP,用于对外访问,在AB之间漂移

    Keepalived软件

    因为官网提供的是源码包的下载,为了方便后续实施人员在环境上做安装操作,做成RPM包更稳妥点,Centos7的官方仓库RPM包也是很老的版本,好像是1.3.X的,最新版已经2.2.8了,所以这里要自己打一下包,先写SPEC文件,保存为
    1. keepalived.spec
    复制代码
    1. %bcond_without snmp
    2. %bcond_without vrrp
    3. %bcond_without sha1
    4. %bcond_with profile
    5. %bcond_with debug
    6. %if 0%{?rhel} && 0%{?rhel} <= 6
    7. %bcond_with nftables
    8. %bcond_with track_process
    9. %bcond_with libiptc
    10. %else
    11. %bcond_without nftables
    12. %bcond_without track_process
    13. %bcond_without libiptc
    14. %endif

    15. %global _hardened_build 1

    16. Name: keepalived
    17. Summary: High Availability monitor built upon LVS, VRRP and service pollers
    18. Version: 2.2.8
    19. Release: 1%{?dist}
    20. License: GPLv2+
    21. URL: http://www.keepalived.org/
    22. Group: System Environment/Daemons

    23. Source0: http://www.keepalived.org/software/keepalived-%{version}.tar.gz
    24. Source1: keepalived.service
    25. Source2: keepalived.init

    26. # distribution specific definitions
    27. %define use_systemd (0%{?fedora} && 0%{?fedora} >= 18) || (0%{?rhel} && 0%{?rhel} >= 7) || (0%{?suse_version} == 1315)

    28. %if %{use_systemd}
    29. Requires(post): systemd
    30. Requires(preun): systemd
    31. Requires(postun): systemd
    32. %else
    33. Requires(post): /sbin/chkconfig
    34. Requires(preun): /sbin/chkconfig
    35. Requires(preun): /sbin/service
    36. Requires(postun): /sbin/service
    37. %endif

    38. BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
    39. %if %{with snmp}
    40. BuildRequires: net-snmp-devel
    41. %endif
    42. %if %{use_systemd}
    43. BuildRequires: systemd-units
    44. %endif
    45. BuildRequires: openssl-devel
    46. BuildRequires: libnl3-devel
    47. BuildRequires: ipset-devel
    48. BuildRequires: iptables-devel
    49. BuildRequires: libnfnetlink-devel

    50. %if (0%{?rhel} && 0%{?rhel} >= 7)
    51. Requires: ipset-libs
    52. %endif

    53. %description
    54. Keepalived provides simple and robust facilities for load balancing
    55. and high availability to Linux system and Linux based infrastructures.
    56. The load balancing framework relies on well-known and widely used
    57. Linux Virtual Server (IPVS) kernel module providing Layer4 load
    58. balancing. Keepalived implements a set of checkers to dynamically and
    59. adaptively maintain and manage load-balanced server pool according
    60. their health. High availability is achieved by VRRP protocol. VRRP is
    61. a fundamental brick for router failover. In addition, keepalived
    62. implements a set of hooks to the VRRP finite state machine providing
    63. low-level and high-speed protocol interactions. Keepalived frameworks
    64. can be used independently or all together to provide resilient
    65. infrastructures.

    66. %prep
    67. %setup -q

    68. %build
    69. %configure \
    70.     %{?with_debug:--enable-debug} \
    71.     %{?with_profile:--enable-profile} \
    72.     %{!?with_vrrp:--disable-vrrp} \
    73.     %{?with_snmp:--enable-snmp --enable-snmp-rfc} \
    74.     %{?with_sha1:--enable-sha1} \
    75.     %{!?with_nftables:--disable-nftables} \
    76.     %{!?with_track_process:--disable-track-process} \
    77.     %{!?with_libiptc:--disable-libiptc}
    78. %{__make} %{?_smp_mflags} STRIP=/bin/true

    79. %install
    80. rm -rf %{buildroot}
    81. make install DESTDIR=%{buildroot}
    82. rm -rf %{buildroot}%{_sysconfdir}/keepalived/samples/
    83. rm -rf %{buildroot}%{_defaultdocdir}/keepalived/
    84. %if %{use_systemd}
    85. rm -rf %{buildroot}%{_initrddir}/
    86. %{__install} -p -D -m 0644 %{SOURCE1} %{buildroot}%{_unitdir}/keepalived.service
    87. %else
    88. rm %{buildroot}%{_sysconfdir}/init/keepalived.conf
    89. %{__install} -p -D -m 0755 %{SOURCE2} %{buildroot}%{_initrddir}/keepalived
    90. %endif
    91. mkdir -p %{buildroot}%{_libexecdir}/keepalived

    92. %clean
    93. rm -rf %{buildroot}

    94. %post
    95. %if %{use_systemd}
    96. %systemd_post keepalived.service
    97. %else
    98. /sbin/chkconfig --add keepalived
    99. %endif

    100. %preun
    101. %if %{use_systemd}
    102. %systemd_preun keepalived.service
    103. %else
    104. if [ "$1" -eq 0 ]; then
    105.     /sbin/service keepalived stop >/dev/null 2>&1
    106.     /sbin/chkconfig --del keepalived
    107. fi
    108. %endif

    109. %postun
    110. %if %{use_systemd}
    111. %systemd_postun_with_restart keepalived.service
    112. %else
    113. if [ "$1" -eq 1 ]; then
    114.     /sbin/service keepalived condrestart >/dev/null 2>&1 || :
    115. fi
    116. %endif

    117. %files
    118. %defattr(-,root,root,-)
    119. %attr(0755,root,root) %{_sbindir}/keepalived
    120. %config(noreplace) %attr(0644,root,root) %{_sysconfdir}/sysconfig/keepalived
    121. %config(noreplace) %attr(0644,root,root) %{_sysconfdir}/keepalived/keepalived.conf.sample
    122. %doc AUTHOR ChangeLog CONTRIBUTORS COPYING README README.md TODO
    123. %doc doc/keepalived.conf.SYNOPSIS doc/samples/keepalived.conf.*
    124. %dir %{_sysconfdir}/keepalived/
    125. %dir %{_libexecdir}/keepalived/
    126. %if %{with snmp}
    127. %{_datadir}/snmp/mibs/KEEPALIVED-MIB.txt
    128. %{_datadir}/snmp/mibs/VRRP-MIB.txt
    129. %{_datadir}/snmp/mibs/VRRPv3-MIB.txt
    130. %endif
    131. %{_bindir}/genhash
    132. %if %{use_systemd}
    133. %{_unitdir}/keepalived.service
    134. %else
    135. %{_initrddir}/keepalived
    136. %endif
    137. %{_mandir}/man1/genhash.1*
    138. %{_mandir}/man5/keepalived.conf.5*
    139. %{_mandir}/man8/keepalived.8*
    复制代码
    把这个spec文件放在
    1. rpmbuild/SPECS
    复制代码
    下,把官网下载的源码包放在
    1. rpmbuild/SOURCES
    复制代码
    下然后执行编译命令:
    1. rpmbuild -bb ~/rpmbuild/SPECS/keepalived.spec
    复制代码
    该命令成功后会在
    1. rpmbuild/RPMS/x86_64/
    复制代码
    目录下生成这两个rpm包:

    我们只需要用
    1. keepalived-2.2.8-1.el7.x86_64.rpm
    复制代码
    包就行了。

    实施


    安装Keepalived软件

    rpm包拷贝到服务器A和服务器B上做安装,或者自己会做yum就做成yum装,要方便些,不用到处scp:
    1. rpm -ivh keepalived-2.2.8-1.el7.x86_64.rpm
    复制代码

    如果报了缺少
    1. libnetsnmp
    复制代码
    之类的依赖,需要安装几个依赖软件:
    1. yum install -y net-snmp-libs net-snmp-agent-libs
    复制代码


    准备健康检查脚本

    准备以下健康脚本用来检查服务的状态,这个脚本可以根据实际情况来改动:
    1. #!/bin/bash

    2. # 检查SSH服务是否正在运行
    3. ssh_status=$(systemctl is-active sshd)

    4. # 判断SSH服务状态
    5. if [ "$ssh_status" = "active" ]; then
    6.     exit 0
    7. else
    8.     systemctl stop keepalived
    9.     exit 1
    10. fi
    复制代码
    当sshd服务异常的时候,sftp自然不能用了,keepalived也就没有必要启动了,于是执行stop逻辑

    配置服务器

    上面说到,我们使用的是非抢占式的模式,所以配置文件这样写,只要注意改动几个特别说明的字段就可以:
    1. ! Configuration File for keepalived

    2. global_defs {
    3.    router_id LVS_DEVEL
    4.    vrrp_skip_check_adv_addr
    5.    vrrp_garp_interval 0
    6.    vrrp_gna_interval 0
    7. }

    8. vrrp_script check_sftp {
    9.     script "/etc/keepalived/scripts/check_sftp.sh"
    10.     interval 2
    11.     timeout 5
    12.     fall 2
    13.     rise 1
    14. }


    15. # 节点配置内容
    16. vrrp_instance VI_1 {
    17.     state BACKUP
    18.     interface p1p2 # 绑定VIP的网卡
    19.     nopreempt # 配置为非抢占式
    20.     virtual_router_id 53
    21.     mcast_src_ip 172.18.0.26
    22.     priority 100
    23.     advert_int 1
    24.     authentication {
    25.         auth_type PASS
    26.         auth_pass 1111
    27.     }
    28.     virtual_ipaddress {
    29.         172.18.0.78
    30.     }

    31.     track_script {
    32.         check_sftp
    33.     }
    34. }

    35. # 注意此处
    36. virtual_server 172.18.0.78 22 { # 虚拟服务
    37.     delay_loop 6
    38.     lb_algo rr
    39.     lb_kind DR
    40.     nat_mask 255.255.255.0   
    41.     persistence_timeout 0
    42.     protocol TCP
    43.    
    44.     real_server 172.18.0.26 22 { # 实际对应的服务,这是A服务器的
    45.         weight 1
    46.         TCP_CHECK {
    47.             connect_timeout 8
    48.             nb_get_retry 3
    49.             delay_before_retry 3
    50.             connect_port 22 # 服务端口
    51.         }
    52.     }

    53.     real_server 172.18.0.27 22 { # 实际对应的服务,这是B服务器的
    54.         weight 1
    55.         TCP_CHECK {
    56.             connect_timeout 8
    57.             nb_get_retry 3
    58.             delay_before_retry 3
    59.             connect_port 22 # 服务端口
    60.         }
    61.     }
    62. }
    复制代码
    按照上述配置配置好2台服务器,然后分别启动keepalived服务:
    1. systemctl start keepalived
    2. systemctl status keepalived
    复制代码

    我们可以通过ip addr查看当前vip绑定的机器是服务器B


    测试验证

    接下来测试验证一下高可用的能力,为了方便区分,首先在两个服务器的root目录下放不同的文件,如果使用别的用户测试就放在对应用户的默认目录下就行,编写以下的测试脚本:
    1. import time
    2. import paramiko

    3. host = "172.18.0.78"
    4. username = "root"
    5. password = "xxxxx"

    6. print("开始运行测试脚本")
    7. ssh_client = paramiko.SSHClient()
    8. print("首次建立ssh和sftp连接")
    9. ssh_client.set_missing_host_key_policy(paramiko.WarningPolicy)
    10. ssh_client.connect(hostname=host, username=username, password=password)
    11. sftp = ssh_client.open_sftp()
    12. while True:
    13.     try:
    14.         tran = ssh_client.get_transport()
    15.         if tran.is_active():
    16.             print("检测到ssh连接已经建立,直接执行测试逻辑")
    17.             # 如果连接已经建立
    18.             print(sftp.listdir())
    19.         else:

    20.             ssh_client.connect(
    21.                 hostname=host, username=username, password=password)
    22.             sftp = ssh_client.open_sftp()
    23.     except Exception as e:
    24.         print("检测到ssh发生主备切换,重新建立sftp连接")
    25.         ssh_client.connect(hostname=host, username=username, password=password)
    26.         sftp = ssh_client.open_sftp()
    27.     time.sleep(10)
    复制代码
    脚本会每隔十秒就在sftp上面列出以下当前目录,运行起来:

    然后我们后台去停止主节点(当前是服务器B)的keepalived服务:
    1. systemctl stop keepalived
    复制代码


    总结

    ok,大功告成
    以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表