Centos7中安装和配置nginx和keepalived(主从模式)系统版本 Centos7
IP:192.168.0.3 Master
IP:192.168.0.4 Slave
⼀.Linux安装nginx (主从都安装NGINX)
1.安装依赖
[root@Master ~]# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
[root@Slave ~]# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2.创建⼀个⽂件夹
[root@Master ~]# cd /usr/local
[root@Master local]# mkdir nginx
[root@Master local]# cd nginx/
[root@Master nginx]#
[root@Slave ~]# cd /usr/local
You have new mail in /var/spool/mail/root
[root@Slave local]# mkdir nginx
[root@Slave local]# cd nginx/
[root@Slave nginx]#
4.编译并make
[root@Master nginx]# cd nginx-1.16.1
[root@Master nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
[root@Master nginx-1.16.1]# make
[root@Master nginx-1.16.1]# make install
修改nginx访问界⾯html
[root@Master nginx-1.16.1]# cd /usr/local/nginx/html/
You have new mail in /var/spool/mail/root
[root@Master html]# ll
total 8
-rw-r--r-- 1 root root 494 Oct 12 03:23 50x.html
linux离线安装nginx详细教程-rw-r--r-- 1 root root 612 Oct 12 03:23 index.html
[root@Master html]# vi index.html
<p><em>Master ip:192.168.0.3</em></p>
[root@Slave nginx]# cd nginx-1.16.1
[root@Slave nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
[root@Slave nginx-1.16.1]# make
[root@Slave nginx-1.16.1]# make install
修改nginx访问界⾯html
[root@Slave nginx-1.16.1]# cd /usr/local/nginx/html/
You have new mail in /var/spool/mail/root
[root@Slave html]# ll
total 8
-rw-r--r-- 1 root root 494 Oct 12 03:23 50x.html
-rw-r--r-- 1 root root 612 Oct 12 03:23 index.html
[root@Slave html]# vi index.html
<p><em>Slave ip:192.168.0.4</em></p>
常⽤命令
#启动
/usr/local/nginx/sbin/nginx
#修改配置⽂件重新加载
/usr/local/nginx/sbin/nginx -s reload
#关闭
/usr/local/nginx/sbin/nginx
2.安装相关依赖,有不⽤下载了
[root@Master local]# yum install -y gcc openssl-devel popt-devel
[root@Slave local]# yum install -y gcc openssl-devel popt-devel
3.编译安装
[root@Master local]# cd keepalived-1.4.2
[root@Master keepalived-1.4.2]# ./configure --prefix=/usr/local/keepalived
[root@Master keepalived-1.4.2]# make
[root@Master keepalived-1.4.2]# make install
[root@Slave local]# cd keepalived-1.4.2
[root@Slave keepalived-1.4.2]# ./configure --prefix=/usr/local/keepalived
[root@Slave keepalived-1.4.2]# make
[root@Slave keepalived-1.4.2]# make install
4.相关配置,复制就完了
[root@Master keepalived-1.4.2]# pwd
/usr/local/keepalived-1.4.2
You have new mail in /var/spool/mail/root
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
[root@Master keepalived-1.4.2]# mkdir /etc/keepalived
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived/etc/f /etc/keepalived/
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@Master keepalived-1.4.2]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@Slave keepalived-1.4.2]# pwd
/usr/local/keepalived-1.4.2
You have new mail in /var/spool/mail/root
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
[root@Slave keepalived-1.4.2]# mkdir /etc/keepalived
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived/etc/f /etc/keepalived/
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@Slave keepalived-1.4.2]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@Slave keepalived-1.4.2]#
使⽤keepalived保证nginx的⾼可⽤(⼀主⼀从),我的主服务器ip为(192.168.0.3),从机(192.168.0.4)
在两条服务器分别安装nginx和keepalived,修改f⽂件
vim /etc/f
主机配置⽂件
! Configuration File for keepalived
global_defs {
#⼀个没重复的名字即可
router_id LVS_DEVEL
}
# 检测nginx是否运⾏
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
#权重减少20,减少完要⽐从机的值⼩
weight -20
}
vrrp_instance VI_1 {
# MASTER代表主机
state MASTER
# ⽹卡名字 ip addr 可以查看
interface eno16777736
# 同⼀个keepalived集的virtual_router_id相同
virtual_router_id 51
# 权重,master要⼤于slave
priority 100
# 主备通讯时间间隔
advert_int 1
# 如果两节点的上联交换机禁⽤了组播,则采⽤vrrp单播通告的⽅式# 本机ip
unicast_src_ip 192.168.118.3
unicast_peer {
# 其他机器ip
192.168.118.5
}
# 设置nopreempt防⽌抢占资源
nopreempt
# 主备保持⼀致
authentication {
auth_type PASS
auth_pass 1111
}
# 与上⽅nginx运⾏状况检测呼应
track_script {
chk_nginx
}
virtual_ipaddress {
# 虚拟ip地址(VIP,⼀个尚未占⽤的内⽹ip即可)
192.168.118.118
}
}
[root@Master keepalived-1.4.2]# cd /etc/keepalived/
[root@Master keepalived]# ll
total 4
-rw-r--r-- 1 root root 3550 Oct 12 03:f
[root@Master keepalived]# f.bak [root@Master keepalived]# vi /etc/f global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface enp0s3
virtual_router_id 51
priority 100
advert_int 1
unicast_src_ip 192.168.0.3
unicast_peer {
192.168.0.4
}
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.0.10
}
}
从机配置⽂! Configuration File for keepalived
global_defs {
#⼀个没重复的名字即可
router_id LVS_DEVEL
}
# 检测nginx是否运⾏
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
# 代表是从机
state BACKUP
# ⽹卡名字,ip addr获取
interface eno16777736
# 同⼀个keepalived集的virtual_router_id相同
virtual_router_id 51
# 权重,master要⼤于slave
priority 90
# 主备通讯时间间隔
advert_int 1
# 如果两节点的上联交换机禁⽤了组播,则采⽤vrrp单播通告的⽅式# 本机ip
unicast_src_ip 192.168.118.5
unicast_peer {
# 其他机器ip
192.168.118.3
}
# 设置nopreempt防⽌抢占资源
nopreempt
# 主备保持⼀致
authentication {
auth_type PASS
auth_pass 1111
}
# 与上⽅nginx运⾏状况检测呼应
track_script {
chk_nginx
}
virtual_ipaddress {
# 虚拟ip地址(VIP,⼀个尚未占⽤的内⽹ip即可)
192.168.118.118
}
}
-
------------------------------------------------------------
从机配置⽂!
[root@Slave keepalived-1.4.2]# cd /etc/keepalived/
You have new mail in /var/spool/mail/root
[root@Slave keepalived]# ll
total 4
-rw-r--r-- 1 root root 3550 Oct 12 03:f
[root@Slave keepalived]# f.bak
[root@Slave keepalived]# vi /etc/f
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state Slave
interface enp0s3
virtual_router_id 51
priority 90
advert_int 1
unicast_src_ip 192.168.0.4
unicast_peer {
192.168.0.3
}
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.0.10
}
}
主从机都需配置检测nginx是否在运⾏,不在允许就直接启动nginx的脚本,和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
[root@Master keepalived]# pwd
/etc/keepalived
[root@Master keepalived]# vi 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
[root@Slave keepalived]# pwd
/etc/keepalived
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论