标签:linux 服务器 高可用 负载均衡 集群 keepalived+lvs
[root@wjb10000-master ~]# wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz
[root@wjb10000-master ~]# tar -zxvf keepalived-1.2.20.tar.gz
[root@wjb10000-master ~]# cd keepalived-1.2.20/
[root@wjb10000-master keepalived-1.2.20]# ./configure --prefix=/usr/local/keepalived
configure: error: !!! OpenSSL is not properly installed on your system. !!! !!! Can not include OpenSSL headers files. !!! |
[root@wjb10000-master keepalived-1.2.20]# yum -y install openssl-devel libnl-devel libnfnetlink-devel ipvsadm
[root@wjb10000-master keepalived-1.2.20]# ./configure --prefix=/usr/local/keepalived
Keepalived configuration ------------------------ Keepalived version : 1.2.20 Compiler : gcc Compiler flags : -g -O2 -DFALLBACK_LIBNL1 Extra Lib : -lssl -lcrypto -lcrypt -lnl Use IPVS Framework : Yes IPVS sync daemon support : Yes IPVS use libnl : Yes fwmark socket support : Yes Use VRRP Framework : Yes Use VRRP VMAC : Yes Use VRRP authentication : Yes SNMP keepalived support : No SNMP checker support : No SNMP RFCv2 support : No SNMP RFCv3 support : No SHA1 support : No Use Debug flags : No libnl version : 1 Use IPv4 devconf : No Use libiptc : No Use libipset : No |
[root@wjb10000-master keepalived-1.2.20]# make && make install
[root@wjb10000-master keepalived-1.2.20]# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived
[root@wjb10000-master keepalived-1.2.20]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
[root@wjb10000-master keepalived-1.2.20]# chmod 755 /etc/init.d/keepalived
[root@wjb10000-master keepalived-1.2.20]# chkconfig keepalived on
[root@wjb10000-master keepalived-1.2.20]# vim /etc/init.d/keepalived
# Source configuration file (we set KEEPALIVED_OPTIONS there) . /etc/sysconfig/keepalived 改为: # Source configuration file (we set KEEPALIVED_OPTIONS there) . /usr/local/keepalived/etc/sysconfig/keepalived |
[root@wjb10000-master keepalived-1.2.20]# mkdir /etc/keepalived
[root@wjb10000-master keepalived-1.2.20]# ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
[root@wjb10000-master keepalived-1.2.20]# service keepalived restart
[root@wjb10000-master /]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { notification_email { 65556505@qq.com } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL }
vrrp_instance VI_1 { state MASTER interface eno16777736 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } }
virtual_server 192.168.1.100 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP
real_server 192.168.1.30 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }
real_server 192.168.1.40 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } |
[root@wjb10000-master /]# echo 1 >/proc/sys/net/ipv4/ip_forward 默认是0,关闭ip转发;这里需要开启,所以设置值为1。
[root@wjb10000-master /]# echo "0" >/proc/sys/net/ipv4/ip_forward
[root@wjb10000-master /]# echo "1" >/proc/sys/net/ipv4/conf/all/send_redirects
[root@wjb10000-master /]# echo "1" >/proc/sys/net/ipv4/conf/default/send_redirects
[root@wjb10000-master /]# echo "1" >/proc/sys/net/ipv4/conf/eth0/send_redirects
[root@wjb10000-master /]# ipvsadm –C
[root@wjb10000-master /]# ipvsadm -A -t 192.168.1.100:80-s rr
[root@wjb10000-master /]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.30:80 -m -w 1
[root@wjb10000-master /]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.40:80 -m -w 1
[root@wjb10000-master ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.1.100:80 rr -> 192.168.1.30:80 Route 1 0 1 -> 192.168.1.40:80 Route 1 0 0 |
[root@wjb10000-master keepalived-1.2.20]# service keepalived restart
[root@wjb10000-master /]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:74:1f:4e brd ff:ff:ff:ff:ff:ff inet 192.168.1.10/24 brd 192.168.1.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.1.100/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe74:1f4e/64 scope link valid_lft forever preferred_lft forever |
[root@wjb10000-backup ~]# wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz
[root@wjb10000-backup ~]# tar -zxvf keepalived-1.2.20.tar.gz
[root@wjb10000-backup ~]# cd keepalived-1.2.20/
[root@wjb10000-backup keepalived-1.2.20]# ./configure --prefix=/usr/local/keepalived
configure: error: !!! OpenSSL is not properly installed on your system. !!! !!! Can not include OpenSSL headers files. !!! |
[root@wjb10000-backup keepalived-1.2.20]# yum -y install openssl-devel libnl-devel libnfnetlink-devel ipvsadm
[root@wjb10000-backup keepalived-1.2.20]# ./configure --prefix=/usr/local/keepalived
Keepalived configuration ------------------------ Keepalived version : 1.2.20 Compiler : gcc Compiler flags : -g -O2 -DFALLBACK_LIBNL1 Extra Lib : -lssl -lcrypto -lcrypt -lnl Use IPVS Framework : Yes IPVS sync daemon support : Yes IPVS use libnl : Yes fwmark socket support : Yes Use VRRP Framework : Yes Use VRRP VMAC : Yes Use VRRP authentication : Yes SNMP keepalived support : No SNMP checker support : No SNMP RFCv2 support : No SNMP RFCv3 support : No SHA1 support : No Use Debug flags : No libnl version : 1 Use IPv4 devconf : No Use libiptc : No Use libipset : No |
[root@wjb10000-backup keepalived-1.2.20]# make && make install
[root@wjb10000-backup keepalived-1.2.20]# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived
[root@wjb10000-backup keepalived-1.2.20]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
[root@wjb10000-backup keepalived-1.2.20]# chmod 755 /etc/init.d/keepalived
[root@wjb10000-backup keepalived-1.2.20]# chkconfig keepalived on
[root@wjb10000-backup keepalived-1.2.20]# vim /etc/init.d/keepalived
[root@wjb10000-backup keepalived-1.2.20]# mkdir /etc/keepalived
[root@wjb10000-backup keepalived-1.2.20]# ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
[root@wjb10000-backup keepalived-1.2.20]# service keepalived restart
[root@wjb10000-backup /]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { notification_email { 65556505@qq.com } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL }
vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } }
virtual_server 192.168.1.100 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP
real_server 192.168.1.30 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }
real_server 192.168.1.40 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } |
[root@wjb10000-backup /]# echo 1 >/proc/sys/net/ipv4/ip_forward
[root@wjb10000-backup /]# echo "0" >/proc/sys/net/ipv4/ip_forward
[root@wjb10000-backup /]# echo "1" >/proc/sys/net/ipv4/conf/all/send_redirects
[root@wjb10000-backup /]# echo "1" >/proc/sys/net/ipv4/conf/default/send_redirects
[root@wjb10000-backup /]# echo "1" >/proc/sys/net/ipv4/conf/eth0/send_redirects
[root@wjb10000-backup /]# ipvsadm –C
[root@wjb10000-backup /]# ipvsadm -A -t 192.168.1.100:80-s rr
[root@wjb10000-backup /]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.30:80 -m -w 1
[root@wjb10000-backup /]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.40:80 -m -w 1
[root@wjb10000-backup /]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.1.100:80 rr -> 192.168.1.30:80 Route 1 0 1 -> 192.168.1.40:80 Route 1 0 0 |
[root@wjb10000-backup /]# service keepalived restart
[root@wjb10000-backup /]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:85:b8:f8 brd ff:ff:ff:ff:ff:ff inet 192.168.1.20/24 brd 192.168.1.255 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe85:b8f8/64 scope link valid_lft forever preferred_lft forever |
备注:如果主备服务器都有VIP存在,请关闭selinux和防火墙
# setenforce 0
# systemctl stop firewalld.service
[root@wjb10000-web1 /]# yum -y install httpd
[root@wjb10000-web1 /]# vim /var/www/html/index.html
<h1>web1 server</h1>
[root@wjb10000-web1 /]# systemctl start httpd.service
[root@wjb10000-web2 /]# yum -y install httpd
[root@wjb10000-web2 /]# vim /var/www/html/index.html
<h1>web2 server</h1>
[root@wjb10000-web2 /]# systemctl start httpd.service
[root@wjb10000-web1 /]# vim /etc/init.d/lvsrs
#!/bin/bash #description:start realserver vip=192.168.1.100 source /etc/rc.d/init.d/functions case $1 in start) echo "Start Realserver" /sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce ;; stop) echo "Stop Realserver" /sbin/ifconfig lo:0 down echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce ;; *) echo "Usage: $0 (start | stop)" exit 1 esac |
[root@wjb10000-web1 /]# chmod 755 /etc/init.d/lvsrs
[root@wjb10000-web1 /]# service lvsrs start
[root@wjb10000-web2 ~]# vim /etc/init.d/lvsrs
#!/bin/bash #description:start realserver vip=192.168.1.100 source /etc/rc.d/init.d/functions case $1 in start) echo "Start Realserver" /sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce ;; stop) echo "Stop Realserver" /sbin/ifconfig lo:0 down echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce ;; *) echo "Usage: $0 (start | stop)" exit 1 esac |
[root@wjb10000-web2 ~]# chmod 755 /etc/init.d/lvsrs
[root@wjb10000-web2 ~]# service lvsrs start
到此为止,Keepalived+LVS的高可用集群系统搭建完成,下面进行测试。
[root@wjb10000-master ~]# service keepalived stop
[root@wjb10000-master ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:74:1f:4e brd ff:ff:ff:ff:ff:ff inet 192.168.1.10/24 brd 192.168.1.255 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe74:1f4e/64 scope link valid_lft forever preferred_lft forever |
[root@wjb10000-backup ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:85:b8:f8 brd ff:ff:ff:ff:ff:ff inet 192.168.1.20/24 brd 192.168.1.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.1.100/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe85:b8f8/64 scope link valid_lft forever preferred_lft forever |
上面信息可以看到备服务器已经接管vip地址。
[root@wjb10000-master ~]# service keepalived start
[root@wjb10000-master ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:74:1f:4e brd ff:ff:ff:ff:ff:ff inet 192.168.1.10/24 brd 192.168.1.255 scope global eno16777736 valid_lft forever preferred_lft forever inet 192.168.1.100/32 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe74:1f4e/64 scope link valid_lft forever preferred_lft forever |
上面信息可以看到主服务器已经重新接管vip地址。
[root@wjb10000-backup ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:85:b8:f8 brd ff:ff:ff:ff:ff:ff inet 192.168.1.20/24 brd 192.168.1.255 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe85:b8f8/64 scope link valid_lft forever preferred_lft forever |
上面信息可以看到主服务器已经释放了vip地址。
打开浏览器输入http://192.168.1.100.不断刷新页面分别看到web1 server和web2 server表示LVS已经负载均衡了。
[root@wjb10000-web1 ~]# systemctl stop httpd.service
[root@wjb10000-master ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP wjb10000-master:http rr -> 192.168.1.40:http Route 1 0 0 |
[root@wjb10000-backup ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.1.100:http rr -> 192.168.1.40:http Route 1 0 0 |
以上内容可以看出Keepalived检测web1服务器出现故障,将此服务器从集群中移除。
[root@wjb10000-web1 ~]# systemctl start httpd.service
[root@wjb10000-master ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP wjb10000-master:http rr -> 192.168.1.30:http Route 1 0 0 -> 192.168.1.40:http Route 1 0 0 |
[root@wjb10000-backup ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.1.100:http rr -> 192.168.1.30:http Route 1 0 0 -> 192.168.1.40:http Route 1 0 0 |
节点恢复重新加入集群当中,浏览器刷新页面,可以重新看到web1 server和web2 server内容交替显示。
结束语:到此Keepalived+LVS的高可用集群系统实验完全结束。大家哪里不明白的欢迎留言。
本文出自 “随风飘扬” 博客,请务必保留此出处http://wjb10000.blog.51cto.com/9702295/1789519
标签:linux 服务器 高可用 负载均衡 集群 keepalived+lvs
原文地址:http://wjb10000.blog.51cto.com/9702295/1789519