一、准备四台服务器
二、关闭防火墙
[root@localhost ~]# iptables -F
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
三、在其中两台安装Nginx,keepalived
1)Nginx安装步骤
[root@localhost ~]# ls
anaconda-ks.cfg nginx-1.16.0.tar.gz 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src/
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module && make && make install
修改nginx配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
keepalive_timeout 65;
upstream httpd_server {
server 192.168.200.127:80 weight=1;
server 192.168.200.128:80 weight=1;
}
#gzip on;
server {
listen 80;
server_name localhost;
charset utf8;
location / {
root html;
index index.html index.htm;
proxy_pass http://httpd_server;
proxy_set_header Host $http_host; #新版必须加这一条
}
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx
2)keepalived安装步骤
[root@localhost ~]# yum -y install keepalived
编写nginx与keepalived关联的脚本
#!/bin/bash
count="$(ps -C nginx --no-header|wc -l)" #判断nginx是否正常开启
if [ $count -eq 0 ];then
systemctl restart nginx
sleep 2
if [ ps -c nginx --no-header|wc -l -eq 0 ];then
systemctl stop keepalived
fi
fi
~
修改keepalived配置文件
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_nginx { #引用脚本文件
script "/shell/nginx_check.sh" #脚本文件的路径
interval 2 #重复检查的次数
weight -20 #失败后降低权重
fall 2 #失败两次才算失败
rice 1 #成功一次就算成功
}
vrrp_instance VI_1 {
state MASTER #主的 备的:BACKUP
interface eno16777728 #心跳接口
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.254 #VIP
}
}
track_script { #调用脚本
check_nginx
}
[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# ip a
2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:52:16:e1 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.126/24 brd 192.168.200.255 scope global eno16777728
valid_lft forever preferred_lft forever
inet 192.168.200.254/32 scope global eno16777728
valid_lft forever preferred_lft forever
另外两台安装apache
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
测试文件
[root@localhost ~]# echo "111111" > /var/www/html/index.html
四、 测试
[root@localhost ~]# while :
> do
> curl 192.168.200.254
> done
111111
222222
111111
222222
111111
222222
111111
222222
111111
如果第一个nginx服务器出错,则另一台继承VIP,并且能继续运行。