lamp部署请参照lamp(此为部署在一台服务器上)
zabbix部署请参照zabbix部署
环境说明:
系统信息 | 主机ip | 应用 |
---|---|---|
rhel7 | 192.168.100.100 | lamp zabbix_server zabbix_agentd |
rhel7 | 192.168.100.99 | keepalived nginx_master |
rhel7 | 192.168.100.96 | keepalived nginx_slave zabbix_ag0entd |
配置主keepalived
# 关闭防火墙与SELINUX
[root@swk ~]# systemctl stop firewalld
[root@swk ~]# systemctl disable firewalld
[root@swk ~]# setenforce 0
# 配置网络源
[root@swk ~]# curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@swk ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@swk ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@swk ~]# yum -y install epel-release vim wget gcc gcc-c++
安装过程略.....
# 安装keepalived
[root@swk ~]# yum -y install keepalived
# 查看安装生成的文件
[root@swk ~]# rpm -ql keepalived
/etc/keepalived //配置目录
/etc/keepalived/keepalived.conf //此为主配置文件
/etc/sysconfig/keepalived
/usr/bin/genhash
/usr/lib/systemd/system/keepalived.service //此为服务控制文件
/usr/libexec/keepalived
/usr/sbin/keepalived
.....此处省略N行
用同样的方法在备服务器上安装keepalived
# 关闭防火墙与SELINUX
[root@swk2 ~]# systemctl stop firewalld
[root@swk2 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@swk2 ~]# setenforce 0
[root@swk2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
# 配置网络源
[root@swk2 ~]# curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@swk2 ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@swk2 ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@swk2 ~]# yum -y install epel-release vim wget gcc gcc-c++
安装过程略.....
# 安装keepalived
[root@swk2 ~]# yum -y install keepalived
在主上安装nginx
[root@swk ~]# yum -y install nginx
[root@swk ~]# cd /usr/share/nginx/html/
[root@ html]# ls
404.html 50x.html index.html nginx-logo.png poweredby.png
[root@swk html]# mv index.html{,.bak}
[root@swk html]# echo 'slave' > index.html
[root@swk html]# ls
404.html 50x.html index.html index.html.bak nginx-logo.png poweredby.png
[root@swk html]# systemctl start nginx
[root@swk html]# systemctl enable nginx
在备上安装nginx
[root@swk2 ~]# yum -y install nginx
[root@swk2 ~]# cd /usr/share/nginx/html/
[root@swk2 html]# ls
404.html 50x.html index.html nginx-logo.png poweredby.png
[root@swk2 html]# mv index.html{,.bak}
[root@swk2 html]# echo 'slave' > index.html
[root@swk2 html]# ls
404.html 50x.html index.html index.html.bak nginx-logo.png poweredby.png
[root@swk2 html]# systemctl start nginx
[root@swk2 html]# systemctl disable nginx ## 备上面不能开机自动启动
在浏览器上访问试试,确保master上的nginx服务能够正常访问
配置主keepalived
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass shicailun
}
virtual_ipaddress {
192.168.100.250
}
}
virtual_server 192.168.100.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.100.99 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.96 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@swk html]# systemctl start keepalived
[root@swk html]# systemctl enable keepalived
配置备keepalived
[root@swk2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass shicailun
}
virtual_ipaddress {
192.168.100.250
}
}
virtual_server 192.168.100.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.100.99 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.96 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@swk2 ~]# systemctl start keepalived
[root@swk2 ~]# systemctl enable keepalived
在主上查看
[root@swk ~]# ip a show ens33
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2f:89:15 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.99/24 brd 192.168.100.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.100.250/32 scope global ens33 ## 现在在主这边说明没问题
valid_lft forever preferred_lft forever
inet6 fe80::92d4:d1c1:856a:e885/64 scope link
valid_lft forever preferred_lft forever
在备上查看
[root@swk2 ~]# ip a show ens33
2: ens33: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:ad:5e:54 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.96/24 brd 192.168.100.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
停掉主上面的keepalived看备上面会不会抢占过去
[root@swk ~]# systemctl stop keepalived
[root@swk ~]# ip a show ens33
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2f:89:15 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.99/24 brd 192.168.100.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::92d4:d1c1:856a:e885/64 scope link
valid_lft forever preferred_lft forever
[root@swk2 ~]# ip a show ens33
2: ens33: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:ad:5e:54 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.96/24 brd 192.168.100.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.100.250/32 scope global ens33 ## 备上面已经抢占过来了
valid_lft forever preferred_lft forever
在主上编写脚本
[root@swk ~]# mkdir /scripts
[root@swk ~]# cd /scripts/
[root@swk scripts]# vim check_n.sh
#!/bin/bash
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl stop keepalived
fi
[root@swk scripts]# chmod +x check_n.sh
[root@swk scripts]# ll
总用量 4
-rwxr-xr-x 1 root root 142 9月 4 13:50 check_n.sh
[root@swk scripts]# vim notify.sh
#!/bin/bash
VIP=$2
sendmail (){
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" [email protected]
}
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@swk scripts]# chmod +x notify.sh
[root@swk scripts]# ll
总用量 8
-rwxr-xr-x 1 root root 142 9月 4 13:50 check_n.sh
-rwxr-xr-x 1 root root 662 9月 4 13:52 notify.sh
在备上编写脚本
root@swk2 ~]# mkdir /scripts
[root@swk2 ~]# cd /scripts/
[root@swk2 scripts]# vim notify.sh
#!/bin/bash
VIP=$2
sendmail (){
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" [email protected]
}
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@swk2 scripts]# chmod +x notify.sh
[root@swk2 scripts]# ll
-rwxr-xr-x 1 root root 594 Oct 20 03:24 notify.sh
配置主keepalived
[root@swk ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script nginx_check {
script "/scripts/check_n.sh"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass shicailun
}
virtual_ipaddress {
192.168.100.250
}
track_script {
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.100.250"
notify_backup "/scripts/notify.sh backup 192.168.100.250"
}
virtual_server 192.168.100.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.100.99 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.96 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@swk ~]# systemctl restart keepalived
配置备keepalived
[root@swk2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass shicailun
}
virtual_ipaddress {
192.168.100.250
}
notify_master "/scripts/notify.sh master 192.168.100.250"
notify_backup "/scripts/notify.sh backup 192.168.100.250"
}
virtual_server 192.168.100.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.100.99 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.100.96 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@swk2 ~]# systemctl restart keepalived
停掉主的nginx服务备为自动成为主
[root@swk ~]# systemctl stop nginx && ip a show ens33
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2f:89:15 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.99/24 brd 192.168.100.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::92d4:d1c1:856a:e885/64 scope link
valid_lft forever preferred_lft forever
[root@swk2 scripts]# ip a show ens33
2: ens33: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:ad:5e:54 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.96/24 brd 192.168.100.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.100.250/32 scope global ens33
valid_lft forever preferred_lft forever
```
启动主上的nginx和keepalived又会抢占回来
```
[root@swk ~]# systemctl start nginx keepalived
[root@swk ~]# ip a show ens33
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2f:89:15 brd ff:ff:ff:ff:ff:ff
inet 192.168.100.99/24 brd 192.168.100.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.100.250/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::92d4:d1c1:856a:e885/64 scope link
valid_lft forever preferred_lft forever
```
写脚本,给执行权限,改脚本存放目录的属主为zabbix
[root@96 ~]# cd /scripts/
[root@96 scripts]# vim check_keepalived.sh
#!/bin/bash
if [ `ip a show ens33 |grep 192.168.100.250|wc -l` -ne 0 ]
then
echo "1"
else
echo "0"
fi
## 当备上面出现vip的时候要么是出现了脑裂要么是主服务器挂了
[root@swk2 scripts]# bash check_keepalived.sh
0
[root@swk2 scripts]# chmod +x check_keepalived.sh
[root@swk2 scripts]# chown -R zabbix.zabbix .
[root@swk2 scripts]# ll -d
drwxr-xr-x 2 zabbix zabbix 50 9月 4 14:48 .
改客户端配置文件agentd.conf
[root@swk2 ~]# vim /usr/local/etc/zabbix_agentd.conf ## 末尾处添加
UnsafeUserParameters=1
UserParameter=check_keepalived,/usr/bin/bash /scripts/check_keepalived.sh
# 重启zabbix_agent
[root@swk2 ~]# pkill zabbix
[root@swk2 ~]# zabbix_agentd
在zabbix_server服务端手动测试
[root@zabbix ~]# zabbix_get -s 192.168.100.96 -k check_keepalived
0
配置网页界面,添加监控项以及触发器
# 停掉主上面的nginx服务
[root@swk ~]# systemctl stop nginx