trrtrtrt 发表于 2018-4-10 15:12:04

keepalived配置高可用集群

准备工作

分别在主从上安装keepalived和nginx
yum install -y keepalived
yum install -y nginx
关闭主从上的防火墙和SELinux
systemctl stop firewalld
setenforce 0
配置主机

查看主机ip

# ifconfig
ens33: flags=4163mtu 1500
      inet 192.168.130.116netmask 255.255.255.0broadcast 192.168.130.255
      inet6 fe80::181c:4847:3f62:dc4cprefixlen 64scopeid 0x20
      ether 00:0c:29:60:ed:4atxqueuelen 1000(Ethernet)
      RX packets 11701bytes 10394449 (9.9 MiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 13559bytes 972155 (949.3 KiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0

lo: flags=73mtu 65536
      inet 127.0.0.1netmask 255.0.0.0
      inet6 ::1prefixlen 128scopeid 0x10
      looptxqueuelen 1(Local Loopback)
      RX packets 86bytes 6720 (6.5 KiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 86bytes 6720 (6.5 KiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0

清空主上keepalived配置文件/etc/keepalived/keepalived.conf

# > /etc/keepalived/keepalived.conf

编辑主上配置文件/etc/keepalived/keepalived.conf

# vim /etc/keepalived/keepalived.conf

加入以下内容:

global_defs {
   notification_email {
   aming@aminglinux.com
   }
   notification_email_from root@aminglinux.com
   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.130.100
    }
    track_script {
      chk_nginx
    }
}

定义主上监控脚本/usr/local/sbin/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

更改主上/usr/local/sbin/check_ng.sh权限

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

启动主上keepalived

# systemctl start keepalived
# ps aux |grep keepalived
root       17410.00.1 1207401404 ?      Ss   10:46   0:00 /usr/sbin/keepalived -D
root       17420.00.3 1274763260 ?      S    10:46   0:00 /usr/sbin/keepalived -D
root       17450.00.3 1317803116 ?      S    10:46   0:00 /usr/sbin/keepalived -D
root       17990.00.0 112676   988 pts/0    S+   10:47   0:00 grep --color=auto keepalived

为了区分主从的nginx需要把nginx的默认页做个标记

# echo master > /data/wwwroot/default/index.html

在另一台机器使用从ip访问
得到下面结果

# curl 192.168.130.116
master

配置从机

查看从机ip

# ifconfig
ens33: flags=4163mtu 1500
      inet 192.168.130.128netmask 255.255.255.0broadcast 192.168.130.255
      inet6 fe80::fef6:fd37:2682:fabcprefixlen 64scopeid 0x20
      ether 00:0c:29:af:03:e8txqueuelen 1000(Ethernet)
      RX packets 45666bytes 48434560 (46.1 MiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 14753bytes 1287938 (1.2 MiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0

lo: flags=73mtu 65536
      inet 127.0.0.1netmask 255.0.0.0
      inet6 ::1prefixlen 128scopeid 0x10
      looptxqueuelen 1(Local Loopback)
      RX packets 80bytes 6256 (6.1 KiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 80bytes 6256 (6.1 KiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0

清空从上配置文件/etc/keepalived/keepalived.conf

# > /etc/keepalived/keepalived.conf

编辑从上配置文件/etc/keepalived/keepalived.conf

# vim /etc/keepalived/keepalived.conf

加入下面内容

global_defs {
   notification_email {
   aming@aminglinux.com
   }
   notification_email_from root@aminglinux.com
   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.130.100
    }
    track_script {
      chk_nginx
    }
}

定义从上监控脚本/usr/local/sbin/check_ng.sh

# 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
      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

更改从上/usr/local/sbin/check_ng.sh权限

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

启动从上keepalived

# systemctl start keepalived
# ps aux |grep keepalived
root       35890.00.1 1207401400 ?      Ss   11:03   0:00 /usr/sbin/keepalived -D
root       35900.00.3 1274763260 ?      S    11:03   0:00 /usr/sbin/keepalived -D
root       35910.00.3 1317803124 ?      S    11:03   0:00 /usr/sbin/keepalived -D
root       36800.00.0 112676   988 pts/0    S+   11:03   0:00 grep --color=auto keepalived

为了区分主从的nginx需要把nginx的默认页做个标记

# echo backup > /usr/share/nginx/html/index.html

在另一台机器使用从ip访问
得到下面结果

# curl 192.168.130.128
backup

测试高可用

首先在主从keepalived都开启的情况
下查看得到vip在主机上 使用另一台机器通过vip访问得到master

然后在主机keepalived关闭的情况下查看得到vip在从机上 使用另一台机器通过vip访问得到backup

再次开启主机的keepalived情况下查看得到vip又回到主机上 使用另一台机器通过vip访问得到master

gdhsdhs45643 发表于 2018-6-12 17:23:05

不是吧不能看

iyunv.com.cn 发表于 2018-7-12 09:08:47

111

zhy1022wo 发表于 2018-10-17 13:40:02

很详细,谢谢分享!
页: [1]
查看完整版本: keepalived配置高可用集群