keepalived+lvs简单实现
一,部署实战环节
01,服务架构图:
服务器镜像为centos6.9
02,服务安装:
10.0.0.10:
1 下载keeplived官方包--> 2 3 http://www.keepalived.org/software/keepalived-2.0.12.tar.gz 4 5 安装基础应用环境 6 7 yum -y install openssl-devel kernel-devel make gcc openssl-devel libnl* popt* 8 9 下载popt包主要用于lvs 10 11 http://mirror.centos.org/centos/6/os/x86_64/Packages/popt-static-1.13-7.el6.x86_64.rpm 12 13 14 15 安装:LVS两台服务都需要执行 16 17 设置软连接: 18 19 ln -s /usr/src/kernels/2.6.32-754.10.1.el6.x86_64/ /usr/src/linux ##可能有的kernels不一样,根据实际而定 20 21 安装lvs 22 yum -y install ipvsadm 23 24 安装keeplived 25 26 tar zxvf keepalived-2.0.12.tar.gz -->解压 27 28 cd keepalived-2.0.12 29 30 ./configure --with-kernel-dir=/usr/src/kernels/2.6.32-754.10.1.el6.x86_64/ 31 32 make && make install
##上面的源码安装报错 configure: error: nftables header files missing,所以选择用yum安装
yum -y install keepalived ipvsadm
更改配置文件/etc/keepalived/keepalived.conf:--->配置文件大致意思,上一篇有讲解,这里不再重复
[root@lvs01 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { test@admin.com } notification_email_from admin@test.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_MASTER } vrrp_instance VI_1 { state MASTER interface eth1 virtual_router_id 60 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.9 } } virtual_server 10.0.0.9 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 10.0.0.12 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 10.0.0.13 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
启动keeplived:
/etc/init.d/keepalived start
ps -ef|grep keepalived 可以查看到进程相关
IP a 命令可以查看到IP
10.0.0.11服务器:
yum -y install keepalived ipvsadm
更改配置文件/etc/keepalived/keepalived.conf:--->配置文件大致意思,上一篇有讲解,这里不再重复
1 [root@lvs02 ~]# cat /etc/keepalived/keepalived.conf 2 ! Configuration File for keepalived 3 global_defs { 4 notification_email { 5 test@sina.com 6 } 7 notification_email_from admin@test.com 8 smtp_server 127.0.0.1 9 smtp_connect_timeout 30 10 router_id LVS_BACKUP 11 } 12 13 vrrp_instance VI_1 { 14 state MASTER 15 interface eth1 16 virtual_router_id 60 17 priority 60 18 advert_int 1 19 authentication { 20 auth_type PASS 21 auth_pass 1111 22 } 23 virtual_ipaddress { 24 10.0.0.9 25 } 26 } 27 28 virtual_server 10.0.0.9 80 { 29 delay_loop 6 30 lb_algo rr 31 lb_kind DR 32 nat_mask 255.255.255.0 33 persistence_timeout 50 34 protocol TCP 35 36 real_server 10.0.0.12 80 { 37 weight 1 38 TCP_CHECK { 39 connect_timeout 3 40 nb_get_retry 3 41 delay_before_retry 3 42 } 43 } 44 45 real_server 10.0.0.13 80 { 46 weight 1 47 TCP_CHECK { 48 connect_timeout 3 49 nb_get_retry 3 50 delay_before_retry 3 51 } 52 } 53 }
启动从库
/etc/init.d/keepalived start
10.0.0.12 , 10.0.0.13web:
下载nginx web包
wget http://nginx.org/packages/rhel/6/x86_64/RPMS/nginx-1.14.0-1.el6.ngx.x86_64.rpm
yum install nginx-1.14.0-1.el6.ngx.x86_64.rpm -y
就能完成安装
更改配置:
vim /usr/share/nginx/html/index.html
10.0.0.12的服务器添加随机字符或者:
echo "web01" >/usr/share/nginx/html/index.html
10.0.0.13:
echo "web01" >/usr/share/nginx/html/index.html
10.0.0.12 10.0.0.13配置脚本并运行脚本(配置虚拟IP的)
1 [root@ng01 ~]# cat ser.sh 2 #!/bin/bash 3 SNS_VIP=10.0.0.9 4 . /etc/rc.d/init.d/functions 5 case "$1" in 6 start) 7 ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP 8 /sbin/route add -host $SNS_VIP dev lo:0 9 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore 10 echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce 11 echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore 12 echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 13 sysctl -p >/dev/null 2>&1 14 echo "RealServer Start OK" 15 ;; 16 top) 17 ifconfig lo:0 down 18 route del $SNS_VIP >/dev/null 2>&1 19 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore 20 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce 21 echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore 22 echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce 23 echo "RealServer Stoped" 24 ;; 25 *) 26 echo "Usage: $0 {start|stop}" 27 exit 1 28 esac 29 exit 0
完成后启动:
service start nginx
三,LVS查看
LVS监控的服务器:
ipvsadm -ln
ipvsadm -lcn
再服务器宕机的过程中可以看到这个TCP数据的转发情况
四, 验证效果
访问VIP出现的页面:
01,nginx服务异常宕机
web01服务
迅速刷新页面
刷新速度还是很快的