负载均衡集群LVS实战篇
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了负载均衡集群LVS实战篇相关的知识,希望对你有一定的参考价值。
Keepalived+LVS的高可用集群系统
一.拓扑图结构:
二.Keepalived的安装
1.主服务器的安装
[[email protected] ~]# wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz
[[email protected] ~]# tar -zxvf keepalived-1.2.20.tar.gz
[[email protected] ~]# cd keepalived-1.2.20/
[[email protected] 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. !!! |
[[email protected] keepalived-1.2.20]# yum -y install openssl-devel libnl-devel libnfnetlink-devel ipvsadm
[[email protected] 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 |
[[email protected] keepalived-1.2.20]# make && make install
[[email protected] keepalived-1.2.20]# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived
[[email protected] keepalived-1.2.20]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
[[email protected] keepalived-1.2.20]# chmod 755 /etc/init.d/keepalived
[[email protected] keepalived-1.2.20]# chkconfig keepalived on
[[email protected] 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 |
[[email protected] keepalived-1.2.20]# mkdir /etc/keepalived
[[email protected] keepalived-1.2.20]# ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
[[email protected] keepalived-1.2.20]# service keepalived restart
2.主服务器配置文件
[[email protected] /]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { notification_email { } notification_email_from [email protected] 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 } } } |
3.配置 IPVS
3.1.开启IP转发功能
[[email protected] /]# echo 1 >/proc/sys/net/ipv4/ip_forward 默认是0,关闭ip转发;这里需要开启,所以设置值为1。
3.2.配置重定向
[[email protected] /]# echo "0" >/proc/sys/net/ipv4/ip_forward
[[email protected] /]# echo "1" >/proc/sys/net/ipv4/conf/all/send_redirects
[[email protected] /]# echo "1" >/proc/sys/net/ipv4/conf/default/send_redirects
[[email protected] /]# echo "1" >/proc/sys/net/ipv4/conf/eth0/send_redirects
3.3.清除ipvsadm表
[[email protected] /]# ipvsadm –C
3.4.使用ipvsadm安装web服务
[[email protected] /]# ipvsadm -A -t 192.168.1.100:80-s rr
3.5.增加二台web服务器
[[email protected] /]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.30:80 -m -w 1
[[email protected] /]# ipvsadm -a -t 192.168.1.100:80 -r 192.168.1.40:80 -m -w 1
3.6查看结果
[[email protected] ~]# 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 |
[[email protected] keepalived-1.2.20]# service keepalived restart
4.主服务器IP查看
[[email protected] /]# 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 |
5.备服务器的安装
[[email protected] ~]# wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz
[[email protected] ~]# tar -zxvf keepalived-1.2.20.tar.gz
[[email protected] ~]# cd keepalived-1.2.20/
[[email protected] 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. !!! |
[[email protected] keepalived-1.2.20]# yum -y install openssl-devel libnl-devel libnfnetlink-devel ipvsadm
[[email protected] 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 |
[[email protected] keepalived-1.2.20]# make && make install
[[email protected] keepalived-1.2.20]# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived
[[email protected] keepalived-1.2.20]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
[[email protected] keepalived-1.2.20]# chmod 755 /etc/init.d/keepalived
[[email protected] keepalived-1.2.20]# chkconfig keepalived on
[[email protected] keepalived-1.2.20]# vim /etc/init.d/keepalived
[[email protected] keepalived-1.2.20]# mkdir /etc/keepalived
[[email protected] keepalived-1.2.20]# ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
[[email protected] keepalived-1.2.20]# service keepalived restart
6.备服务器配置文件
[[email protected] /]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { notification_email { } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL }
vrrp_instance VI_1 { state BACKUP 以上是关于负载均衡集群LVS实战篇的主要内容,如果未能解决你的问题,请参考以下文章 企业级Nginx负载均衡与keepalived高可用实战keepalived篇 网站集群架构实战(LVS负载均衡Nginx代理缓存Nginx动静分离Rsync+Inotify全网备份Zabbix自动注册全网监控)--技术流ken |