前言:
互联网行业的三高
修改yum 镜像源
vim /etc/yum.repos.d/nginx.repo
# nginx.repo(删除原理的镜像源,把下面的复制进去)
[nginx]
name=nginx
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
更新镜像源
yum update
install
yum install nginx -y
启动
systemctl start nginx
install
yum install nginx -y
启动
systemctl start nginx
默认配置文件位置:(主,备配置文件一致,都是在下面这个位置)
install
yum -y install keepalived
启动
systemctl start keepalived
config(修改配置)
keepalived.conf(默认抢占式)
1 查询网卡名称
ip addr
2 vim /etc/keepalived/keepalived.conf
3 添加nginx 状态检查脚本
vrrp_script chk_http_ port {
script "/usr/local/src/nginx_check.sh" #检测脚本,这个脚本在下面第二步进行创建
interval 1 # (检测脚本执行的间隔)1s
weight -21 #权重,如果这个脚本检测为真,服务器权重+2
}
4 修改 varrp_instance_VI_1的内容,为下面内容
vrrp_instance VI_1 {
state MASTER # 主节点MASTER
interface ens33 # ip addr 查询到的网卡信息
virtual_router_id 51
priority 100 # 权重信息默认
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.250 # 设置虚拟ip
}
# 监听nginx 脚本
track_script {
chk_http_ port
}
}
检查nginx状态配置
1 cd /usr/local/src //切换到src目录下,执行下面vim 创建脚本
vim nginx_check.sh
# 复制如下内容到 nginx_check.sh 里面
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
# 当nginx 关闭,就立刻关闭 keepalived,用于转移虚拟ip到备用机上。
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
systemctl stop keepalived
fi
fi
2 更改 nginx_check.sh 执行权限
chmod +x nginx_check.sh
重启keepalived
systemctl restart keepalived
install
yum -y install keepalived
启动
systemctl start keepalived
config(修改配置)
keepalived.conf(默认抢占式)
1 询网卡名称
ip addr
2vim /etc/keepalived/keepalived.conf
3 添加nginx 状态检查脚本
vrrp_script chk_http_ port {
script "/usr/local/src/nginx_check.sh" #检测脚本,这个脚本和master 一样。参考下面步骤 5 把文件复制到当前机器
interval 1 # (检测脚本执行的间隔)1s
weight -21 #权重,如果这个脚本检测为真,服务器权重+2
}
4 修改 varrp_instance_VI_1的内容,为下面内容
vrrp_instance VI_1 {
state BACKUP # 备节点 BACKUP
interface ens33 # ip addr 查询到的网卡信息
virtual_router_id 51
priority 90 # 降低权重
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.96.250 # 设置虚拟ip
}
# 监听nginx 脚本
track_script {
chk_http_ port
}
}
5 从新登录mast机器上,切到如下目录
cd /usr/local/src/
执行如下命令,进行把当前目录下的检查nginx 状态脚本复制到备用机器上
scp ./nginx_check.sh root@192.168.96.124:/usr/local/src/
6 改 nginx_check.sh 执行权限
chmod +x nginx_check.sh
重启keepalived
systemctl restart keepalived
关闭maser nginx
systemctl stop nginx
自动切换到备用机上
systemctl start nginx
启动keepalived
systemctl start keepalived
再次访问nginx-已经切回master节点
感谢阅读~本次关于nginx+keepalived(抢占式)实现服务高可用,总结到此结束。