系统版本 Centos7
IP:192.168.8.145 Master
IP:192.168.8.150 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]#
3.将nginx安装包放在nginx文件夹下并解压或者使用wget联网下载
[root@Master nginx]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@Master nginx]# tar -zxvf nginx-1.16.1.tar.gz
[root@Slave nginx]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@Slave nginx]# tar -zxvf nginx-1.16.1.tar.gz
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
-rw-r–r-- 1 root root 612 Oct 12 03:23 index.html
[root@Master html]# vi index.html
Master ip:192.168.8.145
[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
Slave ip:192.168.8.150
常用命令
#启动
/usr/local/nginx/sbin/nginx
访问验证:
http://192.168.8.145/ http://192.168.8.150/
#修改配置文件重新加载
/usr/local/nginx/sbin/nginx -s reload
#关闭
/usr/local/nginx/sbin/nginx
//—二:安装Keepalived—
1.联网下载到/usr/local目录下并解压有可能会提示连接不成功,加上它提示的命令再下载就好了
[root@Master html]# cd /usr/local
[root@Master local]# wget https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
[root@Master local]# tar -zxvf keepalived-1.4.2.tar.gz
[root@Slave html]# cd /usr/local
You have new mail in /var/spool/mail/root
[root@Slave local]# wget https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
[root@Slave local]# tar -zxvf keepalived-1.4.2.tar.gz
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/keepalived/keepalived.conf /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/keepalived/keepalived.conf /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.8.145),从机(192.168.8.150)
在两条服务器分别安装nginx和keepalived,修改keeplived.conf文件
vim /etc/keepalived/keepalived.conf
主机配置文件:
! 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.8.145
unicast_peer {
其他机器ip
192.168.8.150
}
设置nopreempt防止抢占资源
nopreempt
主备保持一致
authentication {
auth_type PASS
auth_pass 1111
}
与上方nginx运行状况检测呼应
track_script {
chk_nginx
}
virtual_ipaddress {
虚拟ip地址(VIP,一个尚未占用的同一网段内网ip即可,方便跨域访问)
192.168.8.166
}
}
[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:41 keepalived.conf
[root@Master keepalived]# cp keepalived.conf keepalived.conf.bak
[root@Master keepalived]# vi /etc/keepalived/keepalived.conf
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.8.145
unicast_peer {
192.168.8.150
}
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.8.166
}
}
从机配置文!
[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:42 keepalived.conf
[root@Slave keepalived]# cp keepalived.conf keepalived.conf.bak
[root@Slave keepalived]# vi /etc/keepalived/keepalived.conf
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.8.150
unicast_peer {
192.168.8.145
}
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.8.166
}
}
主从机都需配置检测nginx是否在运行,不在允许就直接启动nginx的脚本,和keepalived放在一起
脚本名称 nginx_check.sh
如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程,keepalived将虚拟 ip 绑定到 BACKUP 机器上。内容如下:
#!/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
[root@Slave 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
只要配置好了,以后直接启动keepalived就好了,keepalived运行之后就会检测nginx是否在运行,不在运行就通过脚本去启动
自动重启不了解决方案
查看脚本是否有运行的权限
如果你是root登陆的话(不是的话,切换到root用户,对*.sh 赋可执行的权限)
chmod 777*.sh
或者
chmod +x *.sh
主:
[root@Master keepalived]# chmod +x nginx_check.sh
[root@Master keepalived]# ll
total 12
-rw-r–r-- 1 root root 572 Oct 12 03:55 keepalived.conf
-rw-r–r-- 1 root root 3550 Oct 12 03:48 keepalived.conf.bak
-rwxr-xr-x 1 root root 205 Oct 12 04:04 nginx_check.sh
[root@Master keepalived]#
备:
[root@Slave keepalived]# chmod +x nginx_check.sh
[root@Slave keepalived]# ll
total 12
-rw-r–r-- 1 root root 575 Oct 12 04:02 keepalived.conf
-rw-r–r-- 1 root root 3550 Oct 12 03:57 keepalived.conf.bak
-rwxr-xr-x 1 root root 205 Oct 12 04:04 nginx_check.sh
[root@Slave keepalived]#
keepalived常用命令
#启动
service keepalived start
#停止
service keepalived stop
#查看状态
service keepalived status
重新加载nginx
/usr/local/nginx/sbin/nginx -s reload
开始测试
直接访问主机的nginx服务
http://192.168.8.145/
直接访问从机的nginx服务
http://192.168.8.150/
通过keepalived 设置的虚拟ip进行访问
http://192.168.8.166/
检查启动:
ps -ef | grep keepalived
keepalived运行时,会启动3个进程,分别为:core(核心进程),check和vrrp