一、先设计网络结构
1、准备4台Linux服务器,在本文中选用的是 CentOS 6.6最小化安装;
2、选择2台做为LVS负载均衡(机器名分别为:LVS_Master 和 LVS_Backup);
3、选择2台做为Web服务器(机器名分别为:WebServer_01和 WebServer_02);
4、分别设置好4台服务器的物理IP地址如上图所示;
[root@localhost ~]# vi /etc/sysconfig/network — 修改机器名. HOSTNAME=lvs_master [root@lvs_master /]# rm /etc/udev/rules.d/70-persistent-net.rules — 如果是通过克隆虚拟机的,需要重新生成网卡信息。重新启动 reboot [root@lvs_master /]# vi /etc/udev/rules.d/70-persistent-net.rules — 修改NAME的值为: eth0 SUBSYSTEM==”net”, ACTION==”add”, DRIVERS==”?*”, ATTR{address}==”00:0c:29:0d:f3:72″, ATTR{type}==”1″, KERNEL==”eth*”, NAME=”eth0″ [root@lvs_master /]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 –-修改网络配置并重启 reboot . DEVICE=eth0 — 设置网卡名 IPADDR=192.168.0.30 — 设置IP地址 NETMASK=255.255.255.0 — 设置掩码 GATEWAY=192.168.0.1 — 设置网关 TYPE=Ethernet ONBOOT=yes — 是否随系统一并启动 BOOTPROTO=static — 使用静态IP地址,dhcp 为自动分配 IP DNS1=210.21.4.130 — 设置DSN地址 [root@lvs_master ~]# ifconfig — 查看网络配置 eth0 Link encap:Ethernet HWaddr 00:0C:29:C8:02:1B inet addr:192.168.0.50 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fec8:21b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2297 errors:0 dropped:0 overruns:0 frame:0 TX packets:78 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:180950 (176.7 KiB) TX bytes:10849 (10.5 KiB)
二、在2台LVS服务器上,安装ipvsadm 和 keepalived 软件。
1、这2款软件均可以通过 yum 进行安装。
在 CentOS 6.6 中的版本号为:
ipvsadm-1.26-4.el6.x86_64
keepalived-1.2.13-5.el6_6.x86_64
使用以下命令进行安装:
[root@localhost ~]# yum -y install openssl-devel lftplibnl* popt* libnl* libpopt* gcc* –先安装系统必须软件包 [root@localhost ~]# yum -y install ipvsadm [root@localhost ~]# yum -y install keepalived
2、开启路由转发功能:
[root@lvs_master ~]# vi /etc/sysctl.conf # Controls IP packet forwarding net.ipv4.ip_forward = 1 [root@lvs_master ~]# sysctl -p — 立即生效配置.
3、配置 keepalived
[root@lvs_master ~]# vi /etc/keepalived/keepalived.conf
在 vi 的命令模式下输入: :.,$d 用于清除内容,并输入以下内容.
lvs_master 主机使用如下配置:
!Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 51 priority 100 advert_int 5 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.20 } } virtual_server 192.168.0.20 8080 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.0.60 8080 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 8080 } } real_server 192.168.0.61 8080 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 8080 } } }
lvs_backup 使用如下配置:
!Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 51 priority 99 advert_int 5 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.20 } } virtual_server 192.168.0.20 8080 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.0.60 8080 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 8080 } } real_server 192.168.0.61 8080 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 8080 } } }