003.Keepalived搭建LVS高可用集群
程序员文章站
2022-03-29 11:27:25
一 基础环境 1.1 IP规划 OS:CentOS 6.8 64位 节点类型 IP规划 主机名 类型 主 Director Server eth0:172.24.8.10 DR1 公共IP eth1:192.168.56.100 心跳 私有IP eth0:0:172.24.8.100 无 虚拟IP ......
一 基础环境
1.1 ip规划
os:centos 6.8 64位
1.2 架构规划
二 高可用lvs负载均衡集群部署
2.1 ntp部署
操作略,具体可参考n01.1.1-常见服务《ntp》。
注意:为了保证集群的稳定性,强烈建议在所有节点均部署ntp同步服务,保证所有时钟一致。
2.2 部署httpd集群
1 [root@rserver01 ~]# yum -y install httpd 2 [root@rserver01 ~]# service iptables stop 3 [root@rserver01 ~]# chkconfig iptables off 4 [root@rserver01 ~]# vi /etc/selinux/config 5 selinux=disabled 6 [root@master ~]# setenforce 0 #关闭selinux及防火墙
注意:后端所有real服务器节点都需要安装,用于模拟测试。
建议:为了测试方便,建议所有节点关闭防火墙和selinux,若未关闭防火墙也可通过下列方式放通:
1 firewall-cmd --permanent–-add-service=keepalived 2 firewall-cmd --reload
2.3 安装keepalived
1 [root@lvsmaster ~]# yum -y install gcc gcc-c++ make kernel-devel kernel-tools kernel-tools-libs kernel libnl libnl-devel libnfnetlink-devel openssl-devel wget openssh-clients #安装基础环境及依赖 2 [root@lvsmaster ~]# ln -s /usr/src/kernels/`uname -r` /usr/src/linux 3 [root@lvsmaster ~]# wget http://www.keepalived.org/software/keepalived-1.3.6.tar.gz 4 [root@lvsmaster ~]# tar -zxvf keepalived-1.3.6.tar.gz #编译安装keepalived 5 [root@lvsmaster ~]# cd keepalived-1.3.6/ 6 [root@lvsmaster keepalived-1.3.6]# ./configure --prefix=/usr/local/keepalived 7 [root@master keepalived-1.3.9]# make && make install
注意:centos6.8安装高于1.3.6版本会出现未知错误。
2.4 添加启动相关服务
1 [root@lvsmaster ~]# mkdir /etc/keepalived 2 [root@lvsmaster ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ 3 [root@lvsmaster ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 4 [root@lvsmaster ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ 5 [root@lvsmaster ~]# vi /etc/init.d/keepalived #创建keepalived启动脚本,如附件 6 #!/bin/sh 7 # 8 # keepalived high availability monitor built upon lvs and vrrp 9 # 10 # chkconfig: - 86 14 11 # description: robust keepalive facility to the linux virtual server project \ 12 # with multilayer tcp/ip stack checks. 13 14 ### begin init info 15 # provides: keepalived 16 # required-start: $local_fs $network $named $syslog 17 # required-stop: $local_fs $network $named $syslog 18 # should-start: smtpdaemon httpd 19 # should-stop: smtpdaemon httpd 20 # default-start: 21 # default-stop: 0 1 2 3 4 5 6 22 # short-description: high availability monitor built upon lvs and vrrp 23 # description: robust keepalive facility to the linux virtual server 24 # project with multilayer tcp/ip stack checks. 25 ### end init info 26 27 # source function library. 28 . /etc/rc.d/init.d/functions 29 30 exec="/usr/sbin/keepalived" 31 prog="keepalived" 32 config="/etc/keepalived/keepalived.conf" 33 34 [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog 35 36 lockfile=/var/lock/subsys/keepalived 37 38 start() { 39 [ -x $exec ] || exit 5 40 [ -e $config ] || exit 6 41 echo -n $"starting $prog: " 42 daemon $exec $keepalived_options 43 retval=$? 44 echo 45 [ $retval -eq 0 ] && touch $lockfile 46 return $retval 47 } 48 49 stop() { 50 echo -n $"stopping $prog: " 51 killproc $prog 52 retval=$? 53 echo 54 [ $retval -eq 0 ] && rm -f $lockfile 55 return $retval 56 } 57 58 restart() { 59 stop 60 start 61 } 62 63 reload() { 64 echo -n $"reloading $prog: " 65 killproc $prog -1 66 retval=$? 67 echo 68 return $retval 69 } 70 71 force_reload() { 72 restart 73 } 74 75 rh_status() { 76 status $prog 77 } 78 79 rh_status_q() { 80 rh_status &>/dev/null 81 } 82 83 84 case "$1" in 85 start) 86 rh_status_q && exit 0 87 $1 88 ;; 89 stop) 90 rh_status_q || exit 0 91 $1 92 ;; 93 restart) 94 $1 95 ;; 96 reload) 97 rh_status_q || exit 7 98 $1 99 ;; 100 force-reload) 101 force_reload 102 ;; 103 status) 104 rh_status 105 ;; 106 condrestart|try-restart) 107 rh_status_q || exit 0 108 restart 109 ;; 110 *) 111 echo $"usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" 112 exit 2 113 esac 114 exit $ 115 [root@lvsmaster ~]# chmod u+x /etc/rc.d/init.d/keepalived 116 [root@lvsmaster ~]# vi /etc/keepalived/keepalived.conf 117 ! configuration file for keepalived 118 …… 119 smtp_connect_timeout 30 120 router_id lvs_master #表示运行keepalived服务器的一个标识 121 } 122 123 vrrp_instance vi_1 { 124 state master #指定keepalived的角色 125 interface eth0 #指定ha监测网络的接口 126 virtual_router_id 51 #同一个vrrp实例使用唯一的标识,即同一个vrrp_instance下,master和backup必须是一致的 128 priority 100 #定义优先级,数值越大,优先级越高 129 advert_int 1 #设定mater和backup负载均衡器之间同步检查时间间隔 130 authentication { 131 auth_type pass 132 auth_pass 1111 133 } 134 virtual_ipaddress { 135 172.24.8.100 #设置虚拟ip地址 136 } 137 } 138 139 virtual_server 172.24.8.100 80 { 140 delay_loop 6 #运行情况检查时间 141 lb_algo rr #设置负载均衡算法 142 lb_kind dr #设置lvs实现负载均衡的机制,有nat/dr/tun 143 persistence_timeout 50 #会话保持时间 144 protocol tcp #指定转发类型 145 146 real_server 172.24.8.12 80 { 147 weight 1 #服务节点的权值,数值越大,权值越高 148 tcp_check { 149 connect_timeout 5 #表示无响应超时时间,单位是秒 150 nb_get_retry 3 #表示重试次数 151 delay_before_retry 3 #表示重试间隔 152 } 153 } 154 real_server 172.24.8.13 80 { 155 weight 1 156 tcp_check { 157 connect_timeout 5 158 nb_get_retry 3 159 delay_before_retry 3 160 } 161 } 162 } 163 [root@lvsmaster ~]# scp /etc/keepalived/keepalived.conf 172.24.8.11:/etc/keepalived/keepalived.conf 164 [root@lvsbackup ~]# vi /etc/keepalived/keepalived.conf 165 state backup 166 priority 80
注意;备用director server上需要修改状态为backup和priority优先级。
2.5 安装ipvs管理工具
1 [root@lvsmaster ~]# yum -y install ipvsadm
2.6 配置real server节点
在lvs的dr和tun模式下,用户访问请求到大real server后,real server的响应报文直接返回给用户,而不需经过director server。因此,需要在每个real server上配置虚拟vip地址。
注意:回环接口绑定vip,且禁止arp请求等操作,可通过以下脚本实现:脚本可留言索要。
1 [root@rserver01 ~]# vi /etc/init.d/lvsrs 2 [root@rserver01 ~]# chmod u+x /etc/init.d/lvsrs
2.7 启动集群
1 [root@rserver01 ~]# service httpd start 2 [root@rserver01 ~]# chkconfig httpd on 3 [root@rserver02 ~]# service httpd start 4 [root@rserver02 ~]# chkconfig httpd on 5 6 [root@lvsmaster ~]# service keepalived start 7 [root@lvsmaster ~]# chkconfig keepalived on 8 [root@lvsbackup ~]# service keepalived start 9 [root@lvsbackup ~]# chkconfig keepalived on 10 11 [root@rserver01 ~]# service lvsrs start 12 [root@rserver02 ~]# service lvsrs start
三 测试集群
3.1 高可用功能测试
停止主director server服务器的keepalived,观察/var/log/messages日志,可知备机会立刻变为master,并且接管主机的虚拟ip资源。重启主director server服务器的keepalived,备机会重新恢复为backup角色。
具体测试略。
3.2 负载均衡测试
1 [root@rserver01 ~]# echo 'this is real server01!' >>/var/www/html/index.html 2 [root@rserver01 ~]# echo 'this is real server02!' >>/var/www/html/index.html
然后浏览器访问:http://172.24.8.100,并不断的刷新,能分别看到server01和server02即可。
3.3 故障切换测试
1 [root@rserver01 ~]# service httpd stop
当关掉其中一个real server时,访问vip,只会显示还处于集群中的web节点。
推荐阅读
-
Spring-Cloud Eureka注册中心实现高可用搭建
-
Linux系统 CentOS 7怎么搭建集群? 搭建高可用集群的步骤
-
CentOS下RabbitMq高可用集群环境搭建教程
-
MongoDB高可用集群搭建
-
[图文][提供可行性脚本] CentOS 7 Fencing+Pacemaker三节点搭建高可用集群
-
LVS+Keepalived构建高可用负载均衡(测试篇)
-
Linux利用keepalived实现lvs的高可用性的操作方法
-
一张图讲解最少机器搭建FastDFS高可用分布式集群安装说明
-
Spark快速入门系列(6) | Spark环境搭建—standalone(3) 配置HA高可用模式
-
高可用集群