18.1-18.5 集群介绍,用keepalived配置高可用集群

18.1 集群介绍


  • 根据功能划分为两大类:高可用和负载均衡

  • 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务大概意思是:高可用一般使用两台机器,功能,角色是一样的。当一台服务器宕机不能服务了,利用另外的服务器顶替。

  • 实现高可用的开源软件有:heartbeat(不建议使用,切换通信速度慢,2010年停止更新,)、keepalived(建议使用,有高可用和负载均衡的功能)

  • 负载均衡集群,需要有一台服务器作为分发器,它负责把用户的请求分发给后端的服务器处理,在这个集群里,除了分发器外,就是给用户提供服务的服务器了,这些服务器数量至少为2

  • 原理:一个请求,分发到后端的多台服务器去处理。假如一台服务器满足不到用户的服务需求,需要扩容到多台服务器去处理服务请求。

  • 实现负载均衡的开源软件有LVS、keepalived、haproxy、nginx,商业的有F5、Netscaler 


18.2 keepalived介绍

  • 在这里我们使用keepalived来实现高可用集群,因为heartbeat在centos6上有一些问题,影响实验效果

  • keepalived通过VRRP(Virtual Router Redundancy Protocl,虚拟路由冗余协议)来实现高可用。

  • 在这个协议里会将多台功能相同的路由器组成一个小组,这个小组里会有1个master(主)角色和N(N>=1)个backup角色。

  • master会通过组播的形式向各个backup发送VRRP协议的数据包,当backup收不到master发来的VRRP数据包时,就会认为master宕机了。此时就需要根据各个backup的优先级来决定谁成为新的mater。

  • Keepalived要有三个模块,分别是core、check和vrrp。其中core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析,check模块负责健康检查,vrrp模块是来实现VRRP协议的。


18.3 用keepalived配置高可用集群(上)


用keepalived配置高可用

准备工作 2台linux系统

准备两台机器128和129,128作为master,129作为backup

1 两台机器都执行yum install -y keepalived

[root@centos7-01 src]# yum install -y keepalived

[root@centos7-02 conf]# yum install -y keepalived

2 两台机器都安装nginx(负载均衡也可以用上)

在生产环境中,许多企业把Nginx作为负载均衡器来用,它的重要性很高,一旦宕机会导致整个站点不能访问,所以有必要再准备一台备用Nginx,keepalived用在这种场景非常合适。

3 编辑128上keepalived配置文件,内容从https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/master_keepalived.conf获取

先清空原有的配置信息

[root@centos7-01 src]# > /etc/keepalived/keepalived.conf

然后再添加如下配置信息

[root@centos7-01 src]# vim /etc/keepalived/keepalived.conf


global_defs {

   notification_email {

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_nginx {

    script "/usr/local/sbin/check_ng.sh"

    interval 3

}

vrrp_instance VI_1 {

    state MASTER

    interface ens33

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass aminglinux>com

    }

    virtual_ipaddress {

        192.168.188.100

    }

    track_script {

        chk_nginx

    }

}

参数解释:


global_defs {

   notification_email {

     [email protected]//定义接收告警的人员信息

   }

   notification_email_from [email protected] //定义发邮件地址(实际上没用)

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

以上是自定义参数,


检测服务是否正常

vrrp_script chk_nginx {//chk_nginx为自定义名字,后面还会用到它

    script "/usr/local/sbin/check_ng.sh"//自定义脚本,该脚本为监控nginx服务的脚本,务必记住,稍后会用上

    interval 3//每隔3s执行一次该脚本

}


vrrp_instance VI_1 {

    state MASTER//角色为master,从的话是backup,这里参数要大写

    interface ens33//针对哪个网卡监听VIP

    virtual_router_id 51//定义路由器的ID

    priority 100//权重为100,master要比backup大

    advert_int 1

    authentication {//认证相关信息

        auth_type PASS//定义认证方式,PASS表示password 

        auth_pass aminglinux>com//定义密码,这个密码自定义

    }

    virtual_ipaddress {//定义VIP参数

        192.168.189.100//定义VIP(当master宕机的时候,访问backup的ip地址,这个IP是公共的)

    }

    track_script {

        chk_nginx//定义监控脚本,这里和上面vrr_script后面的字符串要保持一致

    }

}


关于VIP:它的英文名字是Virtual IP,即虚拟IP,也有浮动IP的叫法。因为这个IP是由keepalived给服务器配置上的,

服务器靠这个VIP对外提供服务,当master机器宕机,VIP被分配到backup上,这样用户看来是无感知的。


4 128编辑监控脚本,内容从https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/master_check_ng.sh获取

#vim /usr/local/sbin/check_ng.sh

#!/bin/bash

#时间变量,用于记录日志

d=`date --date today +%Y%m%d_%H:%M:%S`

#计算nginx进程数量

n=`ps -C nginx --no-heading|wc -l`

#如果进程为0,则启动nginx,并且再次检测nginx进程数量,

#如果还为0,说明nginx无法启动,此时需要关闭keepalived

if [ $n -eq "0" ]; then

        /etc/init.d/nginx start

        n2=`ps -C nginx --no-heading|wc -l`

        if [ $n2 -eq "0"  ]; then

                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log

                systemctl stop keepalived

        fi

fi

5 给予脚本755权限

[root@centos7-01 src]#chmod 755 /usr/local/sbin/check_ng.sh


6 在130启动keepalived服务,然后检查keepalived和nginx的服务进程

[root@centos7-01 src]# systemctl start keepalived

[root@centos7-01 src]# ps aux |grep keepalived

root      2370  0.0  0.1 118608  1384 ?        Ss   16:12   0:00 /usr/sbin/keepalived -D

root      2371  0.0  0.3 127468  3292 ?        S    16:12   0:00 /usr/sbin/keepalived -D

root      2372  0.1  0.2 127408  2828 ?        S    16:12   0:00 /usr/sbin/keepalived -D

root      2414  0.0  0.0 112676   984 pts/0    R+   16:12   0:00 grep --color=auto keepalived

[root@centos7-01 src]# ps aux |grep nginx

root      1153  0.0  0.0  24944   864 ?        Ss   14:52   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nobody    1156  0.0  0.3  27388  3696 ?        S    14:52   0:00 nginx: worker process

nobody    1157  0.0  0.3  27388  3444 ?        S    14:52   0:00 nginx: worker process

root      2434  0.0  0.0 112676   984 pts/0    R+   16:12   0:00 grep --color=auto nginx

7 先停止nginx服务,随后需要用到nginx再启动起来

[root@centos7-01 src]# /etc/init.d/nginx stop

Stopping nginx (via systemctl):                            [  确定  ]

可以看到停止了nginx,它还会被加载,因为keepalived检测脚本把nginx加载起来了

[root@centos7-01 src]# ps aux |grep nginx

root      2799  0.0  0.0  24944   872 ?        Ss   16:15   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nobody    2803  0.0  0.3  27388  3452 ?        S    16:15   0:00 nginx: worker process

nobody    2804  0.0  0.3  27388  3452 ?        S    16:15   0:00 nginx: worker process

root      2812  0.0  0.0 112676   980 pts/0    R+   16:15   0:00 grep --color=auto nginx




18.4 用keepalived配置高可用集群(中)

keepalived日志文件在/var/log/messages

1 查看VIP地址#ip add (#ifconfig是查不到VIP的)

[root@centos7-01 src]# ip add

1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:15:53:53 brd ff:ff:ff:ff:ff:ff

    inet 192.168.189.128/24 brd 192.168.189.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.189.100/32 scope global ens33//此处是VIP地址

       valid_lft forever preferred_lft forever

    inet 192.168.189.150/24 brd 192.168.189.255 scope global secondary ens33:0

       valid_lft forever preferred_lft forever

    inet6 fe80::243c:86d7:d85e:224d/64 scope link 

       valid_lft forever preferred_lft forever

3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:15:53:5d brd ff:ff:ff:ff:ff:ff


2 配置从之前,需要确保主和从角色有无开启防火墙,iptables,firewall和selinux等等有无处于关闭状态。

[root@centos7-01 src]# iptables -nvL如果没有或者没能被清空,此时需要停止firewall

[root@centos7-01 src]# getenforce 

Disabled

[root@centos7-01 src]# systemctl stop firewall.service

[root@centos7-02 conf]# systemctl stop firewall.service

[root@centos7-02 conf]# setenforce 0

[root@centos7-02 conf]# getenforce 

Permissive

[root@centos7-02 conf]# iptables -nvL


3 129上编辑配置文件,内容从https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/backup_keepalived.conf获取

[root@centos7-02 conf]# > /etc/keepalived/keepalived.conf 

[root@centos7-02 conf]# vim !$

vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_nginx {

    script "/usr/local/sbin/check_ng.sh"

    interval 3

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass aminglinux>com

    }

    virtual_ipaddress {

        192.168.189.100

    }

    track_script {

        chk_nginx

    }

}

4  129上编辑监控脚本,内容从https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D21Z/backup_check_ng.sh获取

[root@centos7-02 conf]# vim /usr/local/sbin/check_ng.sh

[root@centos7-02 conf]# !vim

vim /usr/local/sbin/check_ng.sh


#时间变量,用于记录日志

d=`date --date today +%Y%m%d_%H:%M:%S`

#计算nginx进程数量

n=`ps -C nginx --no-heading|wc -l`

#如果进程为0,则启动nginx,并且再次检测nginx进程数量,

#如果还为0,说明nginx无法启动,此时需要关闭keepalived

if [ $n -eq "0" ]; then

        /etc/init.d/nginx start

        #如果yum安装的nginx,用这个启动命令systemctl start nginx

        n2=`ps -C nginx --no-heading|wc -l`

        if [ $n2 -eq "0"  ]; then

                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log

                systemctl stop keepalived

        fi

fi


5 给脚本755权限

[root@centos7-02 conf]# chmod 755 !$

chmod 755 /usr/local/sbin/check_ng.sh


6 129上也启动服务 systemctl start keepalived

[root@centos7-02 conf]# systemctl start keepalived

[root@centos7-02 conf]# ps aux |grep keepalived

root      8027  0.0  0.1 118652  1400 ?        Ss   16:51   0:00 /usr/sbin/keepalived -D

root      8028  0.0  0.3 127516  3296 ?        S    16:51   0:00 /usr/sbin/keepalived -D

root      8029  0.0  0.2 127456  2848 ?        S    16:51   0:00 /usr/sbin/keepalived -D

root      8070  0.0  0.0 112720   972 pts/0    S+   16:51   0:00 grep --color=auto keepalived


7  区分主从的nginx

在浏览器访问128的页面192.168.189.128

在浏览器访问129的页面192.168.189.129

blob.png

blob.png

访问VIP 会自动跳转到master(192.168.189.128)

blob.png

访问VIP

blob.png



18.5 用keepalived配置高可用集群(下)

测试高可用

1 测试1:关闭master上的nginx服务

nginx服务关闭不到3秒,又被重新启动,这是因为keepalived的检测脚本起作用了。

2 测试2:在master上增加iptabls规则,

把vvrp出去的包给封掉 

[root@centos7-01 111.com]#iptables -I OUTPUT -p vrrp -j DROP

检查iptables的行为表

[root@centos7-01 111.com]# iptables -nvL

Chain INPUT (policy ACCEPT 43 packets, 3932 bytes)

 pkts bytes target     prot opt in     out     source               destination         


Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination         


Chain OUTPUT (policy ACCEPT 27 packets, 3268 bytes)

 pkts bytes target     prot opt in     out     source               destination         

   42  1680 DROP       112  --  *      *       0.0.0.0/0            0.0.0.0/0  

在主山查看日志记录,频繁出现vrrp类似错误的信息

[root@centos7-01 111.com]# tail /var/log/messages

May 21 17:33:09 centos7-01 NetworkManager[642]: <info>  [1526895189.1381] device (ens37): Activation: starting connection '有线连接 1' (d44e77b3-03bc-3209-8d77-782475a5a763)

May 21 17:33:09 centos7-01 NetworkManager[642]: <info>  [1526895189.1383] device (ens37): state change: disconnected -> prepare (reason 'none') [30 40 0]

May 21 17:33:09 centos7-01 NetworkManager[642]: <info>  [1526895189.1386] device (ens37): state change: prepare -> config (reason 'none') [40 50 0]

May 21 17:33:09 centos7-01 NetworkManager[642]: <info>  [1526895189.1431] device (ens37): state change: config -> ip-config (reason 'none') [50 70 0]

May 21 17:33:09 centos7-01 NetworkManager[642]: <info>  [1526895189.1435] dhcp4 (ens37): activation: beginning transaction (timeout in 45 seconds)

May 21 17:33:09 centos7-01 NetworkManager[642]: <info>  [1526895189.1559] dhcp4 (ens37): dhclient started with pid 12636

May 21 17:33:09 centos7-01 dhclient[12636]: DHCPDISCOVER on ens37 to 255.255.255.255 port 67 interval 8 (xid=0x5f29b3dd)

May 21 17:33:17 centos7-01 dhclient[12636]: DHCPDISCOVER on ens37 to 255.255.255.255 port 67 interval 8 (xid=0x5f29b3dd)

May 21 17:33:25 centos7-01 dhclient[12636]: DHCPDISCOVER on ens37 to 255.255.255.255 port 67 interval 14 (xid=0x5f29b3dd)

May 21 17:33:39 centos7-01 dhclient[12636]: DHCPDISCOVER on ens37 to 255.255.255.255 port 67 interval 16 (xid=0x5f29b3dd)

在从上查看日志,可以看到与其他机器(master)进行交互数据

[root@centos7-02 conf]# tail -10 /var/log/messages

May 21 17:34:12 centos7-02 dhclient[11315]: DHCPDISCOVER on ens37 to 255.255.255.255 port 67 interval 15 (xid=0x20d5f4a4)

May 21 17:34:27 centos7-02 dhclient[11315]: DHCPDISCOVER on ens37 to 255.255.255.255 port 67 interval 14 (xid=0x20d5f4a4)

May 21 17:34:36 centos7-02 NetworkManager[537]: <warn>  [1526895276.4276] dhcp4 (ens37): request timed out

May 21 17:34:36 centos7-02 NetworkManager[537]: <info>  [1526895276.4277] dhcp4 (ens37): state changed unknown -> timeout

May 21 17:34:36 centos7-02 NetworkManager[537]: <info>  [1526895276.4295] dhcp4 (ens37): canceled DHCP transaction, DHCP client pid 11315

May 21 17:34:36 centos7-02 NetworkManager[537]: <info>  [1526895276.4296] dhcp4 (ens37): state changed timeout -> done

May 21 17:34:36 centos7-02 NetworkManager[537]: <info>  [1526895276.4297] device (ens37): state change: ip-config -> failed (reason 'ip-config-unavailable') [70 120 5]

May 21 17:34:36 centos7-02 NetworkManager[537]: <info>  [1526895276.4299] policy: disabling autoconnect for connection '有线连接 1'.

May 21 17:34:36 centos7-02 NetworkManager[537]: <warn>  [1526895276.4300] device (ens37): Activation: failed for connection '有线连接 1'

May 21 17:34:36 centos7-02 NetworkManager[537]: <info>  [1526895276.4302] device (ens37): state change: failed -> disconnected (reason 'none') [120 30 0]

在浏览上访问VIP,还是没有问题的。

blob.png

实验证明,master虽然禁掉了VRRP协议,还是不能达到切换资源的目的的。

把VRRP恢复,规则清空即可

[root@centos7-01 111.com]#  iptables -F

[root@centos7-01 111.com]# iptables -nvL

Chain INPUT (policy ACCEPT 19 packets, 1332 bytes)

 pkts bytes target     prot opt in     out     source               destination         


Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination         


Chain OUTPUT (policy ACCEPT 17 packets, 1400 bytes)

 pkts bytes target     prot opt in     out     source               destination     


3 测试3:关闭master上的keepalived服务

最暴力的测试方法就是,在master上把keepalived服务停止

[root@centos7-01 111.com]# systemctl stop keepalived

在从上ip add看看vip有没有被释放到从上

[root@centos7-02 conf]# ip add

1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:73:7c:4c brd ff:ff:ff:ff:ff:ff

    inet 192.168.189.129/24 brd 192.168.189.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.189.100/32 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::b485:96d0:c537:251e/64 scope link 

       valid_lft forever preferred_lft forever

3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:73:7c:56 brd ff:ff:ff:ff:ff:ff

VIP 192.168.189.100 被释放到了在从上 

查看从的日志,发现VIP被添加上

4 在浏览器访问VIP,会跳转到backup的默认主页上

blob.png

由此证明,VIP已经到了从机器上了。

5 恢复master主上启动VIP服务,测试即可。



猜你喜欢

转载自blog.51cto.com/13578154/2118791