【Linux运维】Centos7上借助ansible搭建LVS+Keepalived

安装ansible

安装ansible:
[root@localhost ~]# tail -5 /etc/hosts
192.168.19.129 web129.yanglt.com web129
192.168.19.130 web130.yanglt.com web130
192.168.19.131 web131.yanglt.com web131
192.168.19.132 web132.yanglt.com web132

[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install ansible
[root@localhost ~]# ssh-keygen
[root@localhost ~]# for i in {
     129..132}
> do
> ssh-copy-id web$i
> done

安装LVS+Keepalived

主lvs_master:192.168.19.129
备lvs_backup:192.168.19.130

http(RS1):192.168.19.131
http(RS2):192.168.19.132

虚IP:192.168.19.150


virtual_ipaddress {
         192.168.19.150
    }
}

virtual_server 192.168.19.150 80 {

如上安装好ansible之后便于安装配置:
[root@localhost ~]# cat /etc/ansible/hosts
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
[webservers]
web129.yanglt.com
web130.yanglt.com
web131.yanglt.com
web132.yanglt.com
[master]
web129.yanglt.com
web130.yanglt.com
[web]
web131.yanglt.com
web132.yanglt.com

Master 安装keepalived+latest:
[root@localhost ~]# ansible master -m yum -a 'name=keepalived state=latest'
[root@localhost ~]# ansible master -m yum -a 'name=ipvsadm state=latest'
[root@web129 ~]#ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost.localdomain:http rr
-> web131.yanglt.com:http Route 1 0 7
-> web132.yanglt.com:http Route 1 0 0
[root@web129 ~]# lsmod|grep ip_vs #检查当前加载的内核模块,看是否存在 ip_vs 模块,只有执行 ipvsadm 以后,才会在内核加载 ip_vs 模块

LVS的ip负载均衡技术就是通过IPVS模块来实现的
ip_vs_rr 12600 1
ip_vs 141092 3 ip_vs_rr
nf_conntrack 111302 1 ip_vs
libcrc32c 12644 2 xfs,ip_vs
[root@web129 ~]#

 
    

Slaver:


[root@localhost ~]# ansible master -m yum -a 'name=keepalived state=latest'
[root@localhost ~]# ansible master -m yum -a 'name=ipvsadm state=latest'
[root@web129 ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost.localdomain:http rr
-> web131.yanglt.com:http Route 1 0 7
-> web132.yanglt.com:http Route 1 0 0
[root@web129 ~]# lsmod|grep ip_vs
ip_vs_rr 12600 1
ip_vs 141092 3 ip_vs_rr
nf_conntrack 111302 1 ip_vs
libcrc32c 12644 2 xfs,ip_vs
[root@web129 ~]#

MASTER:配置如下
[root@web129 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   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 ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.19.200
    }
}
virtual_server 192.168.19.200 80 {
    delay_loop 6
    lb_algo rr 
    lb_kind DR
    nat_mask 255.255.255.0 
    persistence_timeout 0
    protocol TCP

    real_server 192.168.19.131 80 {
        weight 1
        HTTP_GET {
            url { 
              path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.19.132 80 {
        weight 1
        HTTP_GET {
            url { 
              path /
        status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@web129 ~]# 


slave配置如下:
[root@web130 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   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
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.19.200
    }
}
virtual_server 192.168.19.200 80 {
    delay_loop 6
    lb_algo rr 
    lb_kind DR
    nat_mask 255.255.0.0 
    persistence_timeout 50
    protocol TCP

    real_server 192.168.19.131 80 {
        weight 1
        HTTP_GET {
            url { 
              path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.19.132 80 {
        weight 1
        HTTP_GET {
            url { 
              path /
        status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@web130 ~]# 

两台RS服务器上执行如下脚本,
两台RS上为lo:0绑定VIP地址、抑制ARP广播:
[root@web131 ~]# cat lvs_client.sh 
#!/bin/bash  
#description : start realserver
VIP=192.168.19.200
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of 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)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
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@web131 ~]# 

重启keepalived:
[root@web129 ~]# systemctl restart keepalived.service
看到有如下进程即可:
[root@web129 ~]# ps aux | grep keepalived 
root       2391  0.0  0.1 118080  1368 ?        Ss   23:03   0:00 /usr/sbin/keepalived -D
root       2392  0.0  0.3 120324  3044 ?        S    23:03   0:00 /usr/sbin/keepalived -D
root       2393  0.0  0.2 120204  2352 ?        S    23:03   0:00 /usr/sbin/keepalived -D
root       2411  0.0  0.0 112664   976 pts/0    S+   23:17   0:00 grep --color=auto keepalived
[root@web129 ~]# 
[root@web129 ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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: ens33:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:82:86:6c brd ff:ff:ff:ff:ff:ff
    inet 192.168.19.129/24 brd 192.168.19.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.19.200/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe82:866c/64 scope link 
       valid_lft forever preferred_lft forever
[root@web129 ~]#

做端口转发:未测试
[root@web129 ~]# echo "1">/proc/sys/net/ipv4/ip_forward
[root@web130 ~]# echo "1">/proc/sys/net/ipv4/ip_forward

 
    



参考资料:
$ vi /etc/keepalived/keepalived.conf

配置信息如下

! Configuration File for keepalived

global_defs {
    notification_email {
        [email protected]     #设置报警邮件地址,可以设置多个,每行一个。
        [email protected]    #需开启本机的sendmail服务
        [email protected]
    }
    notification_email_from [email protected]  #设置邮件的发送地址
    smtp_server 127.0.0.1      #设置smtp server地址
    smtp_connect_timeout 30    #设置连接smtp server的超时时间
    router_id LVS_DEVEL        #表示运行keepalived服务器的一个标识。发邮件时显示在邮件主题的信息
}

vrrp_instance VI_1 {
    state MASTER              #指定keepalived的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器
    interface enp0s3     #指定HA监测网络的接口
    virtual_router_id 51      #虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。即同一vrrp_instance下,MASTER和BACKUP必须是一致的
    priority 100              #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级
    advert_int 1              #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
    authentication {          #设置验证类型和密码
        auth_type PASS        #设置验证类型,主要有PASS和AH两种
        auth_pass 1111        #设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
    }
    virtual_ipaddress {       #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个
        192.168.137.100 
    }
}

virtual_server 192.168.137.100 80 {  #设置虚拟服务器,需要指定虚拟IP地址和服务端口,IP与端口之间用空格隔开
    delay_loop 6              #设置运行情况检查时间,单位是秒
    lb_algo rr                #设置负载调度算法,这里设置为rr,即轮询算法
    lb_kind DR                #设置LVS实现负载均衡的机制,有NAT、TUN、DR三个模式可选
    nat_mask 255.255.255.0 
    persistence_timeout 0    #会话保持时间,单位是秒。这个选项对动态网页是非常有用的,为集群系统中的session共享提供了一个很好的解决方案。
                              #有了这个会话保持功能,用户的请求会被一直分发到某个服务节点,直到超过这个会话的保持时间。
                              #需要注意的是,这个会话保持时间是最大无响应超时时间,也就是说,用户在操作动态页面时,如果50秒内没有执行任何操作
                              #那么接下来的操作会被分发到另外的节点,但是如果用户一直在操作动态页面,则不受50秒的时间限制
    protocol TCP              #指定转发协议类型,有TCP和UDP两种

    real_server 192.168.137.5 80 { #配置服务节点1,需要指定real server的真实IP地址和端口,IP与端口之间用空格隔开
        weight 1              #配置服务节点的权值,权值大小用数字表示,数字越大,权值越高,设置权值大小可以为不同性能的服务器
                              #分配不同的负载,可以为性能高的服务器设置较高的权值,而为性能较低的服务器设置相对较低的权值,这样才能合理地利用和分配系统资源
        TCP_CHECK {           #realserver的状态检测设置部分,单位是秒
            connect_timeout 3    #表示3秒无响应超时
            nb_get_retry 3       #表示重试次数
            delay_before_retry 3 #表示重试间隔
            connect_port 80
        } 
    }
    real_server 192.168.137.6 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

测试Lvs+Keepalived

测试:
[root@localhost ~]# curl 192.168.19.200
this is server1
[root@localhost ~]# curl 192.168.19.200
this is server2
[root@localhost ~]# curl 192.168.19.200
this is server1
[root@localhost ~]# curl 192.168.19.200
this is server2
[root@localhost ~]# 

停掉RS2的httpd服务:
[root@localhost ~]# curl 192.168.19.200
this is server1
[root@localhost ~]# curl 192.168.19.200
curl: (7) Failed connect to 192.168.19.200:80; 拒绝连接
[root@localhost ~]# curl 192.168.19.200
this is server1
[root@localhost ~]# curl 192.168.19.200
this is server1
[root@localhost ~]# curl 192.168.19.200

 

转载于:https://www.cnblogs.com/yangleitao/p/9434263.html

你可能感兴趣的:(【Linux运维】Centos7上借助ansible搭建LVS+Keepalived)