实验步骤:
1.网络拓扑图
2.环境描述
服务器名称 |
IP地址 |
DNS服务器 | 192.168.1.107 |
web1 | 10.0.0.21(VIP:192.168.1.201) |
web2 | 10.0.0.22(VIP:192.168.1.202) |
web3 | 10.0.0.23(VIP:192.168.1.203) |
3.部署三台web服务器
web1:
[root@web1 ~]# cat /var/www/html/index.html
web1 site
web2:
[root@web2 ~]# cat /var/www/html/index.html
web2 site
[root@web3 ~]# cat /var/www/html/index.html
web3 site
4.配置DNS服务实现DNS轮询功能
[root@dns-M named]# pwd
/var/named/chroot/var/named
[root@dns-M named]# cat pp.org.zone
$TTL 86400
@ IN SOA dns.pp.org. root.pp.org. (
201 ; serial (d. adams)
3H ; refresh
15M ; retry
1W ; expiry
1D ) ; minimum
IN NS dns.pp.org.
IN MX 10 mail.pp.org.
dns.pp.org IN A 192.168.1.107
www IN A 192.168.1.201
www IN A 192.168.1.202
www IN A 192.168.1.203
5、结果测试
A.nslookup测试
[root@dns-M named]# nslookup
> www.pp.org
Server: 192.168.1.107
Address: 192.168.1.107#53
Name: www.pp.org
Address: 192.168.1.201
Name: www.pp.org
Address: 192.168.1.202
Name: www.pp.org
Address: 192.168.1.203
B.访问域名测试
[root@dns-M named]# curl http://www.pp.org
web2 site
[root@dns-M named]# curl http://www.pp.org
web1 site
[root@dns-M named]# curl http://www.pp.org
web3 site
6.应用服务器之间高可用
DNS轮询方式虽然可以实现负载均衡,但是并不能判断某台应用服务器在某一时刻是否可用,它只是将请求解析到应用服务器上。为了解决这个问题,可以用keepalived来实现应用服务器之间的高可用性。
1)网络拓扑图:
此时环境描述如下:
DNS:192.168.1.107 配置不变!
web服务器的IP修改为如下,且每个web服务器有一个VIP:
web1:10.0.0.21(VIP:192.168.1.201);
web2:10.0.0.22(VIP:192.168.1.202);
web3:10.0.0.23(VIP:192.168.1.203)
2)应用服务器的keepalived配置
A、配置规划
编辑配置文件:
web1服务器的keepalived配置文件如下:
[root@web1 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id web_1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.201/24 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.202/24 } } vrrp_instance VI_3 { state BACKUP interface eth0 virtual_router_id 53 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.203/24 } } |
web2服务器的keepalived配置文件如下:
[root@web2 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id web_1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.201/24 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.202/24 } } vrrp_instance VI_3 { state BACKUP interface eth0 virtual_router_id 53 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.203/24 } } |
web3服务器的keepalived配置文件如下:
[root@web3 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id web_1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.201/24 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.202/24 } } vrrp_instance VI_3 { state MASTER interface eth0 virtual_router_id 53 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.203/24 } } |
3)配置DNS轮询
[root@dns-M ~]# cat /var/named/chroot/var/named/pp.org.zone $TTL 86400 @ IN SOA dns.pp.org. root.pp.org. ( 201 ; serial (d. adams) 3H ; refresh 15M ; retry 1W ; expiry 1D ) ; minimum IN NS dns.pp.org. IN MX 10 mail.pp.org. dns.pp.org. IN A 192.168.1.107 www IN A 192.168.1.201 www IN A 192.168.1.202 www IN A 192.168.1.203 |
4)测试
依次启动web服务的keepalived服务
查看启动后的结果:
web1:
[root@web1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:b7:f2:d0 brd ff:ff:ff:ff:ff:ff inet 10.0.0.21/24 brd 10.0.0.255 scope global eth0 inet 192.168.1.201/24 scope global eth0 inet6 fe80::20c:29ff:feb7:f2d0/64 scope link valid_lft forever preferred_lft forever 3: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 |
web2:
[root@web2 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:db:ef:b6 brd ff:ff:ff:ff:ff:ff inet 10.0.0.22/24 brd 10.0.0.255 scope global eth0 inet 192.168.1.202/24 scope global eth0 inet6 fe80::20c:29ff:fedb:efb6/64 scope link valid_lft forever preferred_lft forever 3: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 |
web3:
[root@web3 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:12:0a:ea brd ff:ff:ff:ff:ff:ff inet 10.0.0.23/24 brd 10.0.0.255 scope global eth0 inet 192.168.1.203/24 scope global eth0 inet6 fe80::20c:29ff:fe12:aea/64 scope link valid_lft forever preferred_lft forever 3: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 |
用curl命令查看结果:
[root@dns-M ~]# curl http://www.pp.org web1 site [root@dns-M ~]# curl http://www.pp.org web3 site [root@dns-M ~]# curl http://www.pp.org web2 site |
注意:在实际工作中,三台web服务器的内容是一致的!!!