18.1 集群介绍nginx
1. 根据功能划分为两大类:高可用和负载均衡vim
2. 高可用集群一般为两台服务器,一台工做,另一台做为冗余,当提供服务的机器宕机,冗余将接替继续提供服务后端
3. 实现高可用的开源软件有:heartbeat、keepalivedcentos
负载均衡集群,须要有一台服务器做为分发器,它负责把用户的请求分发给后端的服务器处理,在这个集群里,除了分发器外,就是给用户提供服务的服务器了,这些服务器数量至少为2bash
4. 实现负载均衡的开源软件有LVS、keepalived、haproxy、nginx,商业的有F5、Netscaler 服务器
18.2 keepalived介绍负载均衡
1. 在这里咱们使用keepalived来实现高可用集群,由于heartbeat在centos6上有一些问题,影响实验效果tcp
2. keepalived经过VRRP(Virtual Router Redundancy Protocl)来实现高可用。ide
3. 在这个协议里会将多台功能相同的路由器组成一个小组,这个小组里会有1个master角色和N(N>=1)个backup角色。测试
4. master会经过组播的形式向各个backup发送VRRP协议的数据包,当backup收不到master发来的VRRP数据包时,就会认为master宕机了。此时就须要根据各个backup的优先级来决定谁成为新的mater。
5. Keepalived要有三个模块,分别是core、check和vrrp。其中core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析,check模块负责健康检查,vrrp模块是来实现VRRP协议的。
18.3-18.5用keepalived配置高可用集群(上中下)
1. 准备两台机器hao1和hao2 :
hao1做为master,hao2做为backup
2. 两台机器都安装 :yum install -y keepalived
[root@hao-01 ~]# yum install -y keepalived
[root@hao-02 ~]# yum install -y keepalived
3. 两台机器都安装nginx,能够编译安装nginx :
也能够用yum安装nginx : yum install -y nginx
安装epel仓库(源码包,默认centos不带nginx包) :
yum install -y epel-release
yum install -y nginx
yum安装nginx启动方式:service nginx start
编译安装nginx启动方式:/etc/init.d/nginx start
4. 搜索nginx是否启动 ?
[root@hao-01 ~]# ps aux |grep nginx
[root@hao-02 ~]# ps aux |grep nginx
hao1机器上操做
(这里hao1是编译安装的nginx)
5. 清空keepalived.conf文件中的内容(hao1机器) :
[root@hao-01 ~]# > /etc/keepalived/keepalived.conf
6. 从新配置keepalived.conf文件(hao1机器) :
[root@hao-01 ~]# vim /etc/keepalived/keepalived.conf
插入内容:
global_defs {
bal_defs {
notification_email {
1071599947@qq.com
}
notification_email_from root@hao.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 haomima>com
}
virtual_ipaddress {
192.168.130.100
}
track_script {
chk_nginx
}
}
7. 建立check_ng.sh脚本,并添加内容 :
[root@hao-01 ~]# 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
#yum安装的nginx用:systemctl start nginx 编译安装的用:/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
8. 设定check_ng.sh脚本755权限 :
[root@hao-01 ~]# chmod 755 /usr/local/sbin/check_ng.sh
9. 启动keepalived :
[root@hao-01 ~]# systemctl start keepalived
10. 搜索keepalived是否启动 ?
[root@hao-02 ~]# ps aux |grep keepalived
11. 关闭Nginx,再搜索nginx是否启动 ?
[root@hao-01 ~]# /etc/init.d/nginx stop
[root@hao-01 ~]# ps aux |grep nginx
11. 临时关闭getenforce防火墙 :
[root@hao-01 ~]# setenforce 0
关闭firewalld防火墙:
[root@hao-01 ~]# systemctl stop firewalld
hao2机器上操做
(这里hao2是yum安装的nginx)
12. 临时关闭getenforce防火墙 :
[root@hao-02 ~]# setenforce 0
关闭firewalld防火墙:
[root@hao-02 ~]# systemctl stop firewalld
13. 清空keepalived.conf文件中的内容(hao2机器) :
[root@hao-02 ~]# > /etc/keepalived/keepalived.conf
14. 从新配置keepalived.conf文件(hao2机器) :
[root@hao-02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
1071599947@qq.com
}
notification_email_from root@hao.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 haomima>com
}
virtual_ipaddress {
192.168.130.100
}
track_script {
chk_nginx
}
}
15. 建立check_ng.sh脚本,并添加内容 :
[root@hao-02 ~]# 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
systemctl start nginx
#yum安装的nginx用:systemctl start nginx 编译安装的用:/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
16. 设定check_ng.sh脚本755权限 :
[root@hao-02 ~]# chmod 755 /usr/local/sbin/check_ng.sh
17. 启动keepalived :
[root@hao-02 ~]# systemctl start keepalived
18. 搜索keepalived是否启动 ?
[root@hao-02 ~]# ps aux |grep keepalived
hoa1 hao2机器都打开80端口:
打开80端口 :
[root@hao-01 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@hao-02 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
游览器访问hao1 hao2 ip :
测试高可用
1. 关闭Nginx,再搜索nginx是否启动 ?
[root@hao-01 ~]# systemctl stop nginx
[root@hao-01 ~]# ps aux |grep nginx
2. ip add查看.100是否是在hao1机器上(master) :
[root@hao-01 ~]# ip add
模仿hao1机器挂了,关闭了 keepalived
[root@hao-01 ~]# systemctl stop keepalived
[root@hao-01 ~]# ip add
3. hao1机器(master)挂了,hao2机器(backup)上,ip add查看.100自动挂载到了hao2上!
4. hao1机器(master)再启动keepalived .100又挂在hao1机器 :
[root@hao-01 ~]# systemctl start keepalived