最近刚学习了负载均衡的知识,昨天实战了一下,但是遇到了一些奇怪的问题,查找无方之后,自己探索摸出了原因,所以今天就带大家实战一遍,避免走坑。
方案规划
VIP | IP | Nginx port | 主从 |
---|---|---|---|
192.168.100.241 | 192.168.100.70 | 80 | master |
192.168.100.241 | 192.168.100.3 | 80 | backup |
一、虚拟机的网络适配器修改为桥接模式
二、更新yum
$ yum upgrade
三、安装依赖
$ yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
四、安装wget
$ yum install -y wget
五、下载Nginx、Keepalived
$ cd /home
$ wget http://nginx.org/download/nginx-1.13.0.tar.gz
$ wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz
六、解压
$ tar -zxvf nginx-1.13.0.tar.gz
$ tar -zxvf keepalived-2.0.20.tar.gz
七、安装Nginx
① 创建Nginx目录
$ mkdir /usr/local/nginx
② 进入Nginx解压文件目录,配置安装路径
$ cd /home/nginx-1.13.0
$ ./configure --prefix=/usr/local/nginx
③ 编译安装
$ make && make install
④ 设置Nginx开机自启
$ vi /etc/rc.local
#末尾加入
/usr/local/nginx/sbin/nginx
⑤ 修改页面标识
$ cd /usr/local/nginx/html
$ vi /index.html
如图,在h1标签中的nginx后加上-1,另一台的加上-2
⑥ 启动nignx
$ ./usr/local/nginx/sbin/nginx
八、安装Keepalived
① 创建keepalived目录
$ mkdir /usr/local/keepalived
② 进入keepalived解压目录,配置安装路径
$ cd /home/keepalived-2.0.20
$ ./configure --prefix=/usr/local/keepalived
③ 编译安装
$ make && make install
④ 创建目录,复制keepalived的配置到该目录
$ mkdir /etc/keepalived
$ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
⑤ 将keepalived安装service服务
$ cp /home/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/
$ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# 以下两步如果提示已存在则忽略
$ ln -s /usr/local/sbin/keepalived /usr/sbin/
$ ln -s /usr/local/keepalived/sbin/keepalived /sbin/
⑥ 设置keepalived服务开机启动
$ chkconfig keepalived on
⑦ 编辑keepalived配置(192.168.100.70)
$ vi /etc/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx_master
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2 ## 检测时间间隔
weight -20 ## 如果条件成立,权重 -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 58
mcast_src_ip 192.168.100.70
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.100.241
}
}
⑧ 编辑keepalived配置(192.168.100.3)
$ vi /etc/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx_backup_01
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 58
mcast_src_ip 192.168.100.3
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.100.241
}
}
特别注意interface的值要跟你的网卡(外网)名称一样, ip addr 可查看到名称
⑨ 编写Nginx状态检测脚本
$ touch /etc/keepalived/nginx_check.sh
$ vi /etc/keepalived/nginx_check.sh
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
⑩ 给nginx_check.sh加上执行权限
$ chmod +x /etc/keepalived/nginx_check.sh
⑪ 启动keepalived
$ service keepalived start
Starting keepalived (via systemctl): [ 确定 ]
⑫ 查看日志
$ tail -fn 200 /var/log/messages
⑬ 查看网卡信息
$ ip addr
⑭ 在浏览器上输入VIP地址
⑮ 关闭master(192.168.100.70)的Keepalived和Nginx
$ service keepalived stop
$ /usr/local/nginx/sbin/nginx -s stop
⑯ 查看backup(192.168.100.3)的网卡信息
$ ip addr
⑰ 刷新浏览器