虚拟服务器配置实验:
两台centos6主机为web service系统为CentOS6.8 n1:172.16.253.87 n2:172.16.252.5都安装httpd yum install -y httpd[root@n1 ~]#vim /var/www/html/index.html<h1>Real Server 1</h1>[root@n1 ~]#service httpd start[root@n2 ~]#vim /var/www/html/index.html<h1>RealServer 2</h1>[root@n1 ~]#service httpd start#!/bin/bash##vip='172.16.0.99'netmask='255.255.255.255'iface='lo:0' case $1 instart) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $iface $vip netmask $netmask broadcast $vip up route add -host $vip dev $iface ;;stop) ifconfig $iface down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce ;;*) exit 1esac[root@n1 ~]#bash -x setrs.sh start[root@n1 ~]#route -n172.16.0.99 0.0.0.0 255.255.255.255 UH 0 0 0 lo[root@n1 ~]#scp setrs.sh 172.16.252.5:/root[root@n2 ~]#bash -x setrs.sh start[root@n2 ~]#route -n172.16.0.99 0.0.0.0 255.255.255.255 UH 0 0 0 lo [root@node1 ~]#yum -y install nginx[root@node1 ~]#systemctl start nginx.service[root@node2 ~]#yum -y install nginx[root@node2 ~]#systemctl start nginx.service[root@node1 ~]#yum install -y ipvsadm[root@node1 /etc/keepalived]#vim keepalived.confvirtual_server 172.16.0.99 80 { delay_ loop 1 服务轮询的时间间隔 lb_algo wrr 定义调度方法 lb_kind DR 集群的类型 protocol TCP 服务协议,仅支持TCP sorry_server 127.0.0.1 80 备用服务器地址 real_server 172.16.252.5 80 { weight 1 HTTP_GET { url { 定义当前主机的健康状态检测方法 path /index.html 定义要监控的URL status_code 200 判断上述检测机制为健康状态的响应码 } nb_get_retry 3 重试次数 delay_before_retry 2 重试之前的延迟时长 connect_timeout 3 连接请求的超时时长 } } real_server 172.16.252.5 80 { weight 1 HTTP_GET { url { path /index.html status_code 200 } nb_get_retry 3 delay_before_retry 2 connect_timeout 3 } } } [root@node2 /etc/keepalived]#vim keepalived.conf 做同上操作测试:[root@node1 /etc/keepalived]#systemctl stop keepalived.service[root@node2 /etc/keepalived]#curl http://172.16.0.99<h1>Real Server 1</h1>[root@node2 /etc/keepalived]#curl http://172.16.0.99<h1>RealServer 2</h1>[root@node2 /etc/keepalived]#curl http://172.16.0.99<h1>Real Server 1</h1>[root@node2 /etc/keepalived]#curl http://172.16.0.99<h1>RealServer 2</h1>[root@node1 /etc/keepalived]#systemctl start keepalived.service[root@node1 /etc/keepalived]#ipvsadm -ln -> 172.16.252.5:80 Route 1 0 2 -> 172.16.253.87:80 Route 1 0 2 [root@node2 /etc/keepalived]#ipvsadm -ln -> 172.16.252.5:80 Route 1 0 0 -> 172.16.253.87:80 Route 1 0 0 [root@n1 ~]#service httpd stop[root@node1 /etc/keepalived]#ipvsadm -ln -> 172.16.252.5:80 Route 1 0 0 [root@n1 ~]#service httpd start [root@n2 ~]#service httpd stop[root@node2 /etc/keepalived]#ipvsadm -ln -> 172.16.253.87:80 Route 1 0 0
TCP_CHECK使用示例:
[root@node1 /etc/keepalived]#vim keepalived.confvirtual_server 172.16.0.99 80 { delay_ loop 1 lb_algo wrr lb_kind DR protocol TCP sorry_server 127.0.0.1 80 real_server 172.16.253.87 80 { weight 1 HTTP_GET { url { path /index.html status_code 200 } nb_get_retry 3 delay_before_retry 2 connect_timeout 3 } } real_server 172.16.252.5 80 { weight 1 TCP_CHECK { nb_get_retry 3 delay_before_retry 2 connect_timeout 3 } }}
原创文章,作者:Linux.rookie,如若转载,请注明出处:http://www.178linux.com/78798